Download - Intro to Google Apps Script
Google Apps ScriptIntroduction
ModMonstR.com
01-17-2013
What is Google Apps Script?
● A Google Cloud-based scripting technology used in the Google Apps/Drive ecosystem
● Write code in JavaScript and HTML● Has native APIs to most Google services,
except Google+, Blogger, and YouTube● Has Web Service support● Has App Engine integration support
Script Execution
● Invocation Methods:○ Google Drive○ Google Spreadsheet○ Web (Sites or Web App)
● Scheduling Capabilities○ By Specific Times○ By Time Interval
● Quotas○ Goes against the executor's Google Account
Container-Bound vs Standalone
● Container-Bound scripts○ Resides within a Google Spreadsheet○ Goes away with the spreadsheet
● Standalone scripts○ Resides in Google Drive○ Can be scheduled to execute○ As a Web App, it can be deployed to the Chrome
App Store
Default and Experimental Google APIs
Google API Support
Default Google APIs
● Blob● Browser● Buttons● Cache● Calendar● Charts● Contacts● Content● DocList● Document● Domain
● Finance● Gmail● Groups● Html● Jdbc● Language● Lock● Logger● Mail● Maps● Properties
● Script● ScriptDb● Sites● Soap● Spreadsheet● Ui● UrlFetch● Utilities● Xml
Experimental Google APIs
These APIs need to be enabled by going to the Resources>Use Google APIs menu
● AdSense● Analytics● BigQuery● Prediction● Tasks● UrlShortener
Good Morning World
Sample
1. Gathers Calendar events for the current day2. Write them in a Google Document3. Shorten the URL of the Document via goo.gl4. Email the shortened URL5. Schedule the script
About Good Morning World
Create the Script
1. Go to Google Drive2. Click Create>More>Script3. Click Blank Project 4. Rename Untitled project to your
project's name 1
2
22
3
4
Initializefunction myFunction() { //Get current Date and format as a string var today = new Date(); var todayString = today.getFullYear().toString() + lPad(1+today.getMonth())+lPad(today.getDate());}function lPad(i){ return i<10?"0"+i:i;}
● The current date is obtained and saved as a string in yyyymmdd format for file naming
● The lPad function pads a zero to the left if the input is a single-digit number
Step 1. Gather Today's Calendar Events
var cal = CalendarApp.getDefaultCalendar().getEventsForDay(today);● CalendarApp is used to reference the end-
user's Calendar● getDefaultCalendar() obtains the end-
user's default calendar● getEventsForDay(Date) obtains all
events for the specified date● today is the current date from previous slide
Step 2. Write them in a Document//Create a Google Document,//filename starts with the current datevar doc = DocumentApp.create(todayString+'-Good Morning');
● Create the Google Document● The filename begins with the current date in
yyyymmdd format● Doing so enables the Document filenames to
sort naturally
Step 2. Write them in a Document//Header part of the documentdoc.appendParagraph("Good Morning World");doc.appendParagraph("Below are the activities for today, " + today);doc.appendHorizontalRule()
● The header is composed of three parts:○ A greeting○ Language to indicate today's events○ A horizontal rule to delineate the Calendar Events
from the header
Step 2. Write them in a Document//Iterate through the Calendar and write to the Documentvar i = 0;for(i = 0;i<cal.length;i++){ doc.appendParagraph(basicTime(cal[i].getStartTime()) + ' to ' + basicTime(cal[i].getEndTime()) + ' - ' + cal[i].getTitle() + ' in ' + cal[i].getLocation());}
● The Calendar events are stored as an array of CalendarEvent objects
● Iterate via a for loop● Inside, add a paragraph to the Document
with the time range, title, and location of the event
Step 2. Write them in a Documentfunction basicTime(t){ var output = lPad(t.getHours()) + ":" + lPad(t.getMinutes()); return output;}
● basicTime formats the input time in hh:mm format
● This function is called in the previous slide to format the Start and End time of the calendar event
Step 2. Write them in a Document
//Save and close the Documentdoc.saveAndClose();● We are done with the Google Document● Save changes and Close it
Step 3. Shorten Document URL // Get the URL of the documentvar url = doc.getUrl();// Shorten the URLvar toShorten = UrlShortener.newUrl().setLongUrl(url)var shortUrl = UrlShortener.Url.insert(toShorten);
● getUrl() gets the Document's URL● toShorten gets a url object by converting
from getUrl's output (string)● shortUrl contains the shortened Url
Step 4. Email the shortened URL// Get the email address of the active user - that's youvar emailAddress = Session.getActiveUser().getEmail();// Send yourself an email with a link to the documentGmailApp.sendEmail(emailAddress, 'Today\'s Calendar Events', 'Attached is a link to Document containing ' + 'today\'s activities ' + shortUrl.getId());
● emailAddress contains the end-user's email address
● sendEmail sends the email● the \' results in a ' character
Step 4. Email the shortened URL
● We need to enable the URL Shortener API● Click Resources● Click User Google APIs...
Step 4. Email the shortened URL
1. Set URL Shortener API to on2. Click OK
1
2
Step 5. Schedule
1. Go to Resources2. Click Current script's triggers...
1
2
Step 5. Schedule
1. Click No triggers set up. Click here to add one now.
1
Step 5. Schedule
● Run - Pick the main function in the script● Events - Configure the scheduling● Click Save
Step 5. Schedule
● Authorization is similar to Android apps● Each Google service accessed by the script
is listed in the authorization request
Step 5. Schedule
● Click Grant access
Step 5. Schedule
● Confirmation of authorization
To Run
1. Select the goodMorningWorld function2. Click on the play button to run
12
Output
● Email sent containing the shortened URL
Output
● Google Document generated by the script
Resources
● This Presentation:○ http://goo.gl/2dGhW
● Sample Source Code:○ http://www.modmonstr.com/search/label/Apps%20Script
● Google○ http://script.google.com ○ https://developers.google.com/apps-script/articles○ https://developers.google.com/apps-script/videos
● Stack Overflow○ http://stackoverflow.com/questions/tagged/google-apps-
script
Next Time
● Container-bound Apps Script● Web App● Chrome Web Store deployment