Saturday, September 21, 2013

Google Apps Script for Google Sites

Google Apps Script started as functions for Google Spreadsheets. Today Apps Scripts have evolved into powerful development environment for extending Google Apps. Google Apps Scripts are written in JavaScript programming language.

Last week I attended the Google Apps For Education Summit in Hanoi. The event featured few tutorials about Google Apps Script. It seems that there is a need for simpler tutorials for teaching Apps Script programming.

Earlier this year I wrote these Google Apps Scripts Web Apps to fetch a RSS feed or web page, parse it and display its content. You can embed these Apps Script as gadgets in your Google Sites pages.

The more technically savvy readers will find the example code helpful in learning user interfaces(UI) development with Html Service and Ui Service. You might also want to read XML parsing with Google App Scripts tutorial as well. Please do refer to latest Google Developer documentation as Apps Script API change over time.

Daily Buzzword Google Apps Script

Google Apps Script to parse and display wordcentral.com's Daily Buzzword RSS feed.

function doGet() {
// Fetch XML
var response = UrlFetchApp.fetch("http://wordcentral.com/buzzword/rss.xml").getContentText();
// Parse XML
var parsedResponse = Xml.parse(response, false);
var word = parsedResponse.getElement().getElement('channel').getElement('item').getElement('title').getText();
var desc = parsedResponse.getElement().getElement('channel').getElement('item').getElement('description').getText();
var re = new RegExp('<div class="defset".*</div></div>', "g");
var myArray = desc.match(re);
var def = myArray[0];
return HtmlService.createHtmlOutput( '<div style="background-color:#e9eccf"><h2>' + word + "</h2>" + def + "</div>");
}
view raw gistfile1.txt hosted with ❤ by GitHub

Web App URI: https://script.google.com/macros/s/AKfycbwi3vmklN73Al0tXrtQIvfzwl6G3cGOBsTrY-0s1XQvN-zUhLw/exec | Source Code: https://github.com/arky/DailyBuzzword-AppScript

Daily Words Google Apps Script

Google Apps Script to parse and display wordsmith.org A Word A Day RSS Feed

//Parses Wordsmith XML Feed
function doGet(){
// Fetch XML
var response = UrlFetchApp.fetch("http://wordsmith.org/awad/rss1.xml").getContentText();
// Parse XML
var parsedResponse = Xml.parse(response, false);
var word = parsedResponse.getElement().getElement('channel').getElement('item').getElement('title').getText();
var desc = parsedResponse.getElement().getElement('channel').getElement('item').getElement('description').getText();
// Create UI
var app = UiApp.createApplication();
// Create labels
var wordLabel= app.createLabel(word).setStyleAttribute("fontSize","16px");
var descLabel= app.createLabel(desc).setStyleAttribute("fontSize","12px");
// add the label to the app container
app.add(wordLabel);
app.add(descLabel);
// Return App
return app;
}
view raw gistfile1.txt hosted with ❤ by GitHub

Web App URI: https://script.google.com/macros/s/AKfycbwbMrEhTkn-E3gqAMu7aS5PoMAOHYD62BnGpZFTGPxAY8aCsYW4/exec | Code: https://github.com/arky/awad-AppScript

How to add Apps Script Gadget in your web page?

First go to the page you want to add the Apps Script gadget. Select Apps Script item under Insert menu. In the next dialog box, copy and paste the Apps Script Web App URI into the provided textbox and choose select. The Apps Script Gadget will appear in your webpage.

No comments:

Post a Comment

You can leave a comment here using your Google account, OpenID or as an anonymous user.

Popular Posts