Make an RSS Feed with Google Apps Script

ThinkAmI uses the XMLService of Google Apps Script to create a valid RSS feed that is served to the browser using ContentService with the MIME type set as RSS. Should be handy for creating RSS feeds for services like the Google Search Scraper that do not natively offer feeds.

/* Credit: https://gist.github.com/thinkAmi */

function doGet() {
  var rss = makeRss();

  rss.setTitle('RSS 2.0 Feed with Google Apps Script');
  rss.setLink('http://example.com');
  rss.setDescription('RSS 2.0 Feed');
  rss.setLanguage('en');
  rss.setAtomlink('http://example.com/rss');

  for (var i = 1; i < 3; i++) {
    rss.addItem({
      title: 'TITLE:' + i,
      link: 'http://example.com/#' + i,
      description: 'DESCRIPTION: ' + i,
      pubDate: new Date(),
    });
  }

  return ContentService.createTextOutput(rss.toString()).setMimeType(ContentService.MimeType.RSS);
}

var makeRss = function () {
  var channel = XmlService.createElement('channel');
  var root = XmlService.createElement('rss')
    .setAttribute('version', '2.0')
    .setAttribute('xmlnsatom', 'http://www.w3.org/2005/Atom')
    .addContent(channel);

  var title = '';
  var link = '';
  var description = '';
  var language = '';
  var atomlink = '';
  var items = {};

  var createElement = function (element, text) {
    return XmlService.createElement(element).setText(text);
  };

  return {
    setTitle: function (value) {
      title = value;
    },
    setLink: function (value) {
      link = value;
    },
    setDescription: function (value) {
      description = value;
    },
    setLanguage: function (value) {
      language = value;
    },
    setAtomlink: function (value) {
      atomlink = value;
    },

    addItem: function (args) {
      if (typeof args.title === 'undefined') {
        args.title = '';
      }
      if (typeof args.link === 'undefined') {
        args.link = '';
      }
      if (typeof args.description === 'undefined') {
        args.description = '';
      }
      if (!(args.pubDate instanceof Date)) {
        throw 'pubDate Missing';
      }
      if (typeof args.timezone === 'undefined') {
        args.timezone = 'GMT';
      }
      if (typeof args.guid === 'undefined' && typeof args.link === 'undefined') {
        throw 'GUID ERROR';
      }

      var item = {
        title: args.title,
        link: args.link,
        description: args.description,
        pubDate: Utilities.formatDate(args.pubDate, args.timezone, 'EEE, dd MMM yyyy HH:mm:ss Z'),
        guid: args.guid === 'undefined' ? args.link : args.link,
      };

      items[item.guid] = item;
    },

    toString: function () {
      channel.addContent(
        XmlService.createElement('atomlink')
          .setAttribute('href', atomlink)
          .setAttribute('rel', 'self')
          .setAttribute('type', 'application/rss+xml')
      );

      channel.addContent(createElement('title', title));
      channel.addContent(createElement('link', link));
      channel.addContent(createElement('description', description));
      channel.addContent(createElement('language', language));

      for (var i in items) {
        channel.addContent(
          XmlService.createElement('item')
            .addContent(createElement('title', items[i].title))
            .addContent(createElement('link', items[i].link))
            .addContent(createElement('description', items[i].description))
            .addContent(createElement('pubDate', items[i].pubDate))
            .addContent(createElement('guid', items[i].guid))
        );
      }

      var document = XmlService.createDocument(root);
      var xml = XmlService.getPrettyFormat().format(document);

      var result = xml.replace('xmlnsatom', 'xmlns:atom').replace('<atomlink href=', '<atom:link href=');

      return result;
    },
  };
};
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.