Make an RSS Feed with Google Apps Script

M
Published in: Google Apps Script - RSS

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;
        }
    };
};
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - RSS

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube