SlideShare a Scribd company logo
Google Apps Script
    Introduction
      01-17-2013


                   ModMonstR.com
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
Google API Support
Default and Experimental Google APIs
Default Google APIs
●   Blob       ●   Finance      ●   Script
●   Browser    ●   Gmail        ●   ScriptDb
●   Buttons    ●   Groups       ●   Sites
●   Cache      ●   Html         ●   Soap
●   Calendar   ●   Jdbc         ●   Spreadsheet
●   Charts     ●   Language     ●   Ui
●   Contacts   ●   Lock         ●   UrlFetch
●   Content    ●   Logger       ●   Utilities
●   DocList    ●   Mail         ●   Xml
●   Document   ●   Maps
●   Domain     ●   Properties
Experimental Google APIs
These APIs need to be enabled by going to the
Resources>Use Google APIs menu
●   AdSense
●   Analytics
●   BigQuery
●   Prediction
●   Tasks
●   UrlShortener
Sample
Good Morning World
About Good Morning World
1.   Gathers Calendar events for the current day
2.   Write them in a Google Document
3.   Shorten the URL of the Document via goo.gl
4.   Email the shortened URL
5.   Schedule the script
Create the Script
1.      Go to Google Drive
2.      Click Create>More>Script
3.      Click Blank Project
4.      Rename Untitled project to your
        project's name    1


2
                                    3




    2
               2                   4
Initialize
function 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 date
var 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 document
doc.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 Document
var 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 Document
function 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 Document
doc.saveAndClose();
● We are done with the Google Document
● Save changes and Close it
Step 3. Shorten Document URL
// Get the URL of the document
var url = doc.getUrl();
// Shorten the URL
var 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 you
var emailAddress = Session.getActiveUser().getEmail();
// Send yourself an email with a link to the document
GmailApp.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 on
2. Click OK




                                 1




    2
Step 5. Schedule
1. Go to Resources
2. 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 function
2. Click on the play button to run


         2         1
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

More Related Content

PDF
Google apps script
PDF
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
PPTX
More Data, More Problems: Evolving big data machine learning pipelines with S...
PDF
Google Cloud Dataflow meets TensorFlow
PDF
JavaScript client API for Google Apps Script API primer
PDF
Interview with Developer Jose Luis Arenas regarding Google App Engine & Geosp...
PPTX
Using the Google Cloud -for Developers- part four
PPTX
Dbabstraction
Google apps script
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
More Data, More Problems: Evolving big data machine learning pipelines with S...
Google Cloud Dataflow meets TensorFlow
JavaScript client API for Google Apps Script API primer
Interview with Developer Jose Luis Arenas regarding Google App Engine & Geosp...
Using the Google Cloud -for Developers- part four
Dbabstraction

What's hot (16)

PPTX
Google cloud datastore driver for Google Apps Script DB abstraction
PDF
Code Management
PPTX
A Beginner's Guide to Building Data Pipelines with Luigi
PDF
How to make GAE adapt the Great Firewall
PPTX
Altitude San Francisco 2018: Logging at the Edge
PPTX
Understanding angular meteor
PDF
Why Grails?
PPTX
MongoDB Aggregation
PPTX
Learning Svelte
PPTX
MongoDB - Aggregation Pipeline
PPTX
GraphQL - an elegant weapon... for more civilized age
PDF
Grails 101
ODP
Aggregation Framework in MongoDB Overview Part-1
PDF
Git as NoSQL
PPT
Introduction to MongoDB
PDF
Aggregation Framework MongoDB Days Munich
Google cloud datastore driver for Google Apps Script DB abstraction
Code Management
A Beginner's Guide to Building Data Pipelines with Luigi
How to make GAE adapt the Great Firewall
Altitude San Francisco 2018: Logging at the Edge
Understanding angular meteor
Why Grails?
MongoDB Aggregation
Learning Svelte
MongoDB - Aggregation Pipeline
GraphQL - an elegant weapon... for more civilized age
Grails 101
Aggregation Framework in MongoDB Overview Part-1
Git as NoSQL
Introduction to MongoDB
Aggregation Framework MongoDB Days Munich
Ad

Similar to Intro to Google Apps Script (20)

KEY
Introduction to Google Apps Platform
PPT
GAS Session
PDF
Google Apps Script: Accessing G Suite & other Google services with JavaScript
PDF
Google Apps Script: Accessing G Suite & other Google services with JavaScript
PPTX
Google Apps Script the Authentic{ated} Mobile Playground
PDF
Extending Google Apps/Spreadsheet using Google Apps Script
PDF
G Suite & Google APIs coding workshop
PPTX
Google Apps Script: The authentic{ated} playground [2015 Ed.]
PDF
google apps powerpoint- overview.pdf
PDF
Getting Started with Google Apps & Google APIs
PPTX
Google Apps an Overview - Russell Masters
PDF
Basics : Google Apps for Work
PDF
Google apps for Education - Thiyagu
PPT
Google docs1 sweets gilrs
PDF
Integrate Google Drive with Google Apps Script
PPTX
Андрей Шульга "Google apps script"
PPTX
Google apps for work Mumbai
PPSX
Google Drive BasicsLinkedIn
PDF
Exploring Google APIs with Python & JavaScript
Introduction to Google Apps Platform
GAS Session
Google Apps Script: Accessing G Suite & other Google services with JavaScript
Google Apps Script: Accessing G Suite & other Google services with JavaScript
Google Apps Script the Authentic{ated} Mobile Playground
Extending Google Apps/Spreadsheet using Google Apps Script
G Suite & Google APIs coding workshop
Google Apps Script: The authentic{ated} playground [2015 Ed.]
google apps powerpoint- overview.pdf
Getting Started with Google Apps & Google APIs
Google Apps an Overview - Russell Masters
Basics : Google Apps for Work
Google apps for Education - Thiyagu
Google docs1 sweets gilrs
Integrate Google Drive with Google Apps Script
Андрей Шульга "Google apps script"
Google apps for work Mumbai
Google Drive BasicsLinkedIn
Exploring Google APIs with Python & JavaScript
Ad

Intro to Google Apps Script

  • 1. Google Apps Script Introduction 01-17-2013 ModMonstR.com
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. Google API Support Default and Experimental Google APIs
  • 6. Default Google APIs ● Blob ● Finance ● Script ● Browser ● Gmail ● ScriptDb ● Buttons ● Groups ● Sites ● Cache ● Html ● Soap ● Calendar ● Jdbc ● Spreadsheet ● Charts ● Language ● Ui ● Contacts ● Lock ● UrlFetch ● Content ● Logger ● Utilities ● DocList ● Mail ● Xml ● Document ● Maps ● Domain ● Properties
  • 7. Experimental Google APIs These APIs need to be enabled by going to the Resources>Use Google APIs menu ● AdSense ● Analytics ● BigQuery ● Prediction ● Tasks ● UrlShortener
  • 9. About Good Morning World 1. Gathers Calendar events for the current day 2. Write them in a Google Document 3. Shorten the URL of the Document via goo.gl 4. Email the shortened URL 5. Schedule the script
  • 10. Create the Script 1. Go to Google Drive 2. Click Create>More>Script 3. Click Blank Project 4. Rename Untitled project to your project's name 1 2 3 2 2 4
  • 11. Initialize function 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
  • 12. 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
  • 13. Step 2. Write them in a Document //Create a Google Document, //filename starts with the current date var 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
  • 14. Step 2. Write them in a Document //Header part of the document doc.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
  • 15. Step 2. Write them in a Document //Iterate through the Calendar and write to the Document var 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
  • 16. Step 2. Write them in a Document function 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
  • 17. Step 2. Write them in a Document //Save and close the Document doc.saveAndClose(); ● We are done with the Google Document ● Save changes and Close it
  • 18. Step 3. Shorten Document URL // Get the URL of the document var url = doc.getUrl(); // Shorten the URL var 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
  • 19. Step 4. Email the shortened URL // Get the email address of the active user - that's you var emailAddress = Session.getActiveUser().getEmail(); // Send yourself an email with a link to the document GmailApp.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
  • 20. Step 4. Email the shortened URL ● We need to enable the URL Shortener API ● Click Resources ● Click User Google APIs...
  • 21. Step 4. Email the shortened URL 1. Set URL Shortener API to on 2. Click OK 1 2
  • 22. Step 5. Schedule 1. Go to Resources 2. Click Current script's triggers... 1 2
  • 23. Step 5. Schedule 1. Click No triggers set up. Click here to add one now. 1
  • 24. Step 5. Schedule ● Run - Pick the main function in the script ● Events - Configure the scheduling ● Click Save
  • 25. Step 5. Schedule ● Authorization is similar to Android apps ● Each Google service accessed by the script is listed in the authorization request
  • 26. Step 5. Schedule ● Click Grant access
  • 27. Step 5. Schedule ● Confirmation of authorization
  • 28. To Run 1. Select the goodMorningWorld function 2. Click on the play button to run 2 1
  • 29. Output ● Email sent containing the shortened URL
  • 30. Output ● Google Document generated by the script
  • 31. 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
  • 32. Next Time ● Container-bound Apps Script ● Web App ● Chrome Web Store deployment