32
Google Apps Script Introduction ModMonstR.com 01-17-2013

Intro to Google Apps Script

Embed Size (px)

DESCRIPTION

An introduction to Google Apps Script. It is a technology that allows you to write in JavaScript and HTML, and execute the code within Google's Cloud. It revolves around the Google Account. In this presentation, we go through a demo app called Good Morning World. This app goes through the current day's Calendar events, puts a summary of those events to a Google Document, shortens the URL to the Document, and emails it to you. It also covers how to schedule it to run every morning. For more details, go to: http://www.modmonstr.com/search/label/code

Citation preview

Page 1: Intro to Google Apps Script

Google Apps ScriptIntroduction

ModMonstR.com

01-17-2013

Page 2: Intro to Google Apps Script

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

Page 3: Intro to Google Apps Script

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

Page 4: Intro to Google Apps Script

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

Page 5: Intro to Google Apps Script

Default and Experimental Google APIs

Google API Support

Page 6: Intro to Google Apps Script

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

Page 7: Intro to Google Apps Script

Experimental Google APIs

These APIs need to be enabled by going to the Resources>Use Google APIs menu

● AdSense● Analytics● BigQuery● Prediction● Tasks● UrlShortener

Page 8: Intro to Google Apps Script

Good Morning World

Sample

Page 9: Intro to Google Apps Script

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

Page 10: Intro to Google Apps Script

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

Page 11: Intro to Google Apps Script

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

Page 12: Intro to Google Apps Script

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

Page 13: Intro to Google Apps Script

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

Page 14: Intro to Google Apps Script

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

Page 15: Intro to Google Apps Script

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

Page 16: Intro to Google Apps Script

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

Page 17: Intro to Google Apps Script

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

Page 18: Intro to Google Apps Script

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

Page 19: Intro to Google Apps Script

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

Page 20: Intro to Google Apps Script

Step 4. Email the shortened URL

● We need to enable the URL Shortener API● Click Resources● Click User Google APIs...

Page 21: Intro to Google Apps Script

Step 4. Email the shortened URL

1. Set URL Shortener API to on2. Click OK

1

2

Page 22: Intro to Google Apps Script

Step 5. Schedule

1. Go to Resources2. Click Current script's triggers...

1

2

Page 23: Intro to Google Apps Script

Step 5. Schedule

1. Click No triggers set up. Click here to add one now.

1

Page 24: Intro to Google Apps Script

Step 5. Schedule

● Run - Pick the main function in the script● Events - Configure the scheduling● Click Save

Page 25: Intro to Google Apps Script

Step 5. Schedule

● Authorization is similar to Android apps● Each Google service accessed by the script

is listed in the authorization request

Page 26: Intro to Google Apps Script

Step 5. Schedule

● Click Grant access

Page 27: Intro to Google Apps Script

Step 5. Schedule

● Confirmation of authorization

Page 28: Intro to Google Apps Script

To Run

1. Select the goodMorningWorld function2. Click on the play button to run

12

Page 29: Intro to Google Apps Script

Output

● Email sent containing the shortened URL

Page 30: Intro to Google Apps Script

Output

● Google Document generated by the script

Page 31: Intro to Google Apps 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

Page 32: Intro to Google Apps Script

Next Time

● Container-bound Apps Script● Web App● Chrome Web Store deployment