Google Sites Automation With Apps Script

Google Sites is becoming quite powerful and I am not sure if people are noticing. Last week, Google announced that Apps Script was available on a Google Site:

Starting today, you can create, edit, and launch Google Apps Scripts from any Google Site, which allows you to automate business processes that involve multiple applications. As in the example above, an Apps Script function can automate tasks such as sending emails, scheduling calendar events, creating and updating site pages using data from other systems, and more.

This may not sound like much, but think of this a little differently. If Google Apps Script is available within Google Sites, then your simple site now has a lot of capabilities behind it. Look at the API Documentation and see all of the available Google functions. You can now automate emails, create appointments, interact with Google Docs and even work with various web services.

Again, this may not sound like a big deal until you look at how much code it takes to create content. Creating a page is very easy. First, you need to call SitesApp.getSite(“mysite”), which will return a Site object. Then, you generate some html for your page, basically just some html that will appear within a body tag. Finally, call site.createWebPage(title, name, html), which returns your newly created Page. Congratulations, you just created a new page. Obviously, this is a highly simplistic example, and you would probably want to put the page in a specific location. There are API calls for that as well. Your entire site management could be manipulated through the API if you really wanted.

I know this still sounds a little simplistic, but I figured an example would show the real power. I have talked about auto-posting my Google Reader shares before. So, what would be required to auto-post Reader shares to a Google Site?

Create Basic Page Creation Function

function createDailyReaderPage() {
    var sharesHtml = getShares();
    var site = SitesApp.getSite("mysite");
    var date = Utilities.formatDate(new Date(), "EST", "MM-dd-yyyy");
    var title = "Daily Reader for " + date;
    var name = "daily-reader-for-" + date;
    var page = site.getChildByName(name);
    if (page == null) {
        page = site.createWebPage(title, name, sharesHtml);
    } else {

The function getShares() is a function that we will create to actually get the Reader shares and generate the HTML. Most of the code is actually just formatting the current date and setting up the page url and title. Site.getChildByName(name) is retrieving the page so that we can update the HTML, using Page.setHtmlContent(html), or create the page itself.

Create Function To Get And Parse Shares

There is a little more meat to this function, but again it is not difficult code to write.

function getShares() {
    var url = "" +
    var response = UrlFetchApp.fetch(url);
    var shareDoc = Xml.parse(response.getContentText(), true);

UrlFetchApp.fetch(url) is another Google API function that will retrieve any URL and return an HTTPResponse. The response text is then passed to another API function, Xml.parse(), which parses the text and returns an XmlDocument.

    var root = shareDoc.getElement(); // feed element
    var entryList = "<ul>n";

Get the root element of the document, feed in the case of ATOM, and start generating an unordered list.

    var entries = root.getElements("entry");
    for (var i=0; i<entries.length; i++) {
        var e = entries[i];
        var title = e.getElement("title").getText();
        var link = e.getElement("link").getAttribute("href").getValue();
        var ds = e.getElement("published").getText(); // format is yyyy-MM-dd'T'hh:mm:ss'Z'
        var pubDate = new Date(ds.substring(0,4), ds.substring(5,7)-1, ds.substring(8,10),
                ds.substring(11,13)-4, ds.substring(14,16), ds.substring(17,19));
        var dtStr = Utilities.formatDate(pubDate, "EST", "MMM dd yyyy 'at' HH:mm:ss");

This is just some basic code to get the XML values so we can generate the list item. In particular, there are a few lines of code to parse and format the publish date. Disappointingly, I could not find a date parsing API in the Apps Script documentation, otherwise this code would look fairly simple.

        var categories = e.getElements("category");
        var catList = "";
        for (var j=0; j<categories.length; j++) {
            var c = categories[j];
            catList += c.getAttribute("term").getValue();
            if (j+1 < categories.length) {
                catList += ", ";

As a nice addition, we process the category elements in the feed entry to include in the list item.

        entryList += "<li><a href='" + link + "'>" + title + "</a> was published on " + dtStr;
        entryList += "<br/>nTags: " + catList + "</li>n";
    entryList += "</ul>n";
    return entryList;

This last bit formats the HTML list items and returns the unordered list HTML to the calling function.

Reviewing The Created Page

The key here is that you need to have a good template for your pages in order for the output to really look good. However, even my simple example created a decent page in only 44 lines of formatted code. That may seem like a lot, but we are retrieving and parsing an Atom feed, verbosely generating list items for the entries, and creating or updating an HTML page for your site. I am using one of the standard themes for this test site, and here is what my quick page looks like:

Google Sites Daily Shares Page
Click the image to see it full size, so that you can see the actual list item text. By itself, the page may not look that interesting, but the script editor allows you to add a script trigger. In the case of a script created within Google Sites, you can have the script triggered every day or even every hour. Given these types of capabilities, you could quickly build an automated site that doesn’t suck.

What About Workflow?

When you look at the various APIs, you will see integration with your Mail, Contacts, Calendar, Google Docs, and JDBC. With all of this functionality, you could create a very robust application that embeds workflow, email notifications and a bunch of other features. There is even an interesting tutorial on creating a help desk workflow. Another interesting point is that all of this code is JavaScript. You just create a new script in the Google Sites interface and write some code, test the code, and setup a trigger to automatically run the script. So, no deployment of Java jars or other weird deployment magic.

What does all of this mean? I have talked about Google Sites being an enterprise play before. Microsoft Sharepoint better be looking over their shoulder, because Google Sites is coming with all of the power of assorted Google properties.

Enhanced by Zemanta

12 thoughts on “Google Sites Automation With Apps Script

  1. Rob,

    Thanks for putting this together. It is very helpful. However when I try to use a ATOM feed from Blogger as follows:

    var url = “”;
    //var url = “”;
    var response = UrlFetchApp.fetch(url);
    var rssFeed = Xml.parse(response.getContentText(), true);

    It gives me the following error msg:
    “Could on not parse”

    Any thoughts on what could be Xml.parse to fail?



    1. Saqib

      One thing I noticed is that the xml parser does not handle longer input streams. In my example, I limited the number of reader shares to the most recent 3. Otherwise, I was having issues with the parsing. You could be running into a similar problem.


      1. Got it to work. As it turns out the linient parsing flag has to be set to false. strange…….

        var url = “”;
        //var url = “”;
        var response = UrlFetchApp.fetch(url);
        var rssFeed = Xml.parse(response.getContentText(), false);


      2. Saqib

        Good to hear that you got it working. XML parsers are a notoriously picky bunch, so this does not surprise me. Also, in many cases, people may be generating feeds that are not standard RSS or ATOM though many services will still try to display things somewhat correctly. Feedburner even has a “check if your feed is valid” function because of this type of problem.


  2. Hi!
    I’m interested in auto-posting to Google Reader shares.
    Could you point me the link to your post on this matter?


    1. I do not auto-post my Google Reader shares yet, I just had a poll a few months ago about doing it. When I figure out what I am planning on doing, I will definitely write a post about it.


  3. What I’m really interested in is being able to sync my local files with Google sites, giving me an offsite online backup facility for only a couple of dollars a year .. any thoughts on how to create this?


    1. Garreth

      When you are dealing with your local PC, automation becomes a PC problem. Thankfully, there are several online backup services available that are about $5 per month. Mozy and Carbonite are two that I have used and I know there are a few others. Google Sites is not a great option for online backup, but there may be a service that does this for you as well.


Comments are closed.