Google Apps Script

Simon Su
About me...
● Job:MiCloud RD Leader (http:
//micloud.tw)
● Skills:
○ Service Architecture
○ Node.js
○ Google Service
● Open Sources:
○ opennodes.arecord.us
○ peihsinsu.blogspot.tw
○ blog.micloud.tw
○ slideshare.net/peihsinsu
We are...
Cloud is Already Exists!
Cloud Changes the Developer
Few years ago
● system and os
● network setup
● install what I need
● mail sdk
● sms bridge
● backup and archive
● socket protocol
● building versioning
system for develop
● ...

Now
● cloud machine
● vpc and permission
● software preload
● email service
● sms service
● object storage
● restful protocal
● git deploy & github
in the world
● ...
Our Topic
What does
Simple Business in the Cloud need?
Most Small Service Needs:
OA + Landing Page + REST Services
What is Apps Script?!
Apps Script Serves
By Type
● Standalone Scripts
● Container-Bound Scripts
By Function
● Spreadsheet Custom Functions
● Web Apps
● Google Sites Gadgets
● Container Extensions
API Supports
● Google Apps
Services
● Calendar
● Contacts
● DocsList
● Document
● Domain
● Drive

●
●
●
●
●
●
●
●

Finance
Forms
Gmail
Groups
Language
Maps
Sites
Spreadsheet
What you can do with Apps Script?

Monitor your Web

Google Form integrate

Build a RESTful endpoint
First Apps Script Service Monitor using Google Drive
● Idea
○ Using google sheet for data persistance
○ Using apps script UrlFetch to retrieve monitor target
resource

● Some Reference
○ http://gappsnews.blogspot.tw/2013/01/google-appsscript-http-monitor.html
○ http://gappsnews.blogspot.tw/2013/03/re-googlegoogle-apps-scriptgoogle-form.html
You will know...
● SpreadsheetApp.openById(...)
○ getSheetByName(...)
○ insertSheet(...)
○ appendRow(...)

● UrlFetchApp.fetch(...)
Piece of Code
Save data to sheet and setting chart
Web / Google Site Integrate
2nd Apps Script Google Form send notification
● Idea:
○ Send mail notify when form sent
○ Call remote rest when form sent
●

Some References:
○ http://gappsnews.blogspot.tw/2013/03/fwd-googleapps-script-event-object.html
○ http://gappsnews.blogspot.tw/2013/02/google-appsscriptgoogle-form.html
You will know...
● SpreadsheetApp.getActiveSheet()
● GmailApp.sendEmail(...)
● JSON
○ Object.keys(jsonObject)
○ JSON.stringify(jsonObject)
○ JSON.parse(string)
Create your form and result sheet

Live Form Editor
Create Script for Result Sheet
Piece of Code
function notify(){

function getLastRowTable(arr){

var sheet =

return jsonObjToTableWithTitle(arr[0],

SpreadsheetApp.getActiveSheet();

arr[arr.length-1]);

var rows = sheet.getDataRange();

}

var values = rows.getValues();

function jsonObjToTableWithTitle(title, row){

var content = getLastRowTable(values);

var TRs = '';

var htmlBody = "Hi Admin: <br/><br/>”+

var keys = Object.keys(row);

”有人填表拉,檢 查一下吧! <br/><br/>" +

for(var j = 0 ; j < keys.length ; j++) {

content +

var TR = '<tr>';

“<br/><br/>Send by Google Apps”;

var rowvalue = row[keys[j]];

GmailApp.sendEmail(

TR += ('<td>' + title[keys[j]] +

"your-mail@gmail.com",

'</td><td>' + rowvalue + '</td>' );

"Form Submit Confirm Notice",

TR+= '</tr>';

htmlBody,

TRs += TR;

{from: your-mail@gmail.com,

}

htmlBody:htmlBody}

var table = '<table border="1px">' +

);
}

TRs +'</table>';
return table;
Create the form submit event trigger
Test your form
3rd Apps Script Build a RESTful Service
● Idea
○ The advance interactive to all clients

● Restrictions
○ base url is fixed, only query and form as patameter
can be the input
○ get, post method only (2013.7.21)
○ need solve the HTTP CODE: 302 problem

● Some Reference
○ http://gappsnews.blogspot.tw/2013/07/apps-scriptwith-restful.html
○ http://gappsnews.blogspot.tw/2013/07/fwd-appsscript.html
○ http://gappsnews.blogspot.tw/2013/07/apps-script-
You will know...
● doGet()
● ContentService.createTextOutput(...)
Demo of Using RESTful
Piece of Code
Deploy & Publish
Testing in curl

URL Pattern:https://script.google.
com/macros/s/:id/exec

Script ID
Testing in Advance REST Client
Testing in
Ajax Code...
Thinking in different...
● Drive application integrate related,
scheduling, data present and interactive...
● Static page service (Github Page, Cloud
CMS) interactive...
● Mobile devices' remote thin app server, to be
a RDB, NDB, BigQuery... middleware
● The chrome extension server side...
Chrome Web Store Connect
Script Gallery
Quota of Apps Script
Other Features Connect to Google Services
● Google Apps
○ Document extension
○ Simple form services

● Google Cloud Platform
○ BigQuery integrate
○ ScriptDB

● External Services
○ Connect with JDBC
Reference
● Apps Script Example: https://developers.
google.com/apps-script/case-studies
● Sunset Scripts: https://developers.google.
com/apps-script/sunset
Q&A
Slideshare: http://www.slideshare.net/peihsinsu/google-apps-script-24469585

Google apps script

  • 1.
  • 2.
    About me... ● Job:MiCloudRD Leader (http: //micloud.tw) ● Skills: ○ Service Architecture ○ Node.js ○ Google Service ● Open Sources: ○ opennodes.arecord.us ○ peihsinsu.blogspot.tw ○ blog.micloud.tw ○ slideshare.net/peihsinsu
  • 3.
  • 4.
  • 5.
    Cloud Changes theDeveloper Few years ago ● system and os ● network setup ● install what I need ● mail sdk ● sms bridge ● backup and archive ● socket protocol ● building versioning system for develop ● ... Now ● cloud machine ● vpc and permission ● software preload ● email service ● sms service ● object storage ● restful protocal ● git deploy & github in the world ● ...
  • 6.
  • 7.
    What does Simple Businessin the Cloud need?
  • 8.
    Most Small ServiceNeeds: OA + Landing Page + REST Services
  • 9.
    What is AppsScript?!
  • 10.
    Apps Script Serves ByType ● Standalone Scripts ● Container-Bound Scripts By Function ● Spreadsheet Custom Functions ● Web Apps ● Google Sites Gadgets ● Container Extensions
  • 11.
    API Supports ● GoogleApps Services ● Calendar ● Contacts ● DocsList ● Document ● Domain ● Drive ● ● ● ● ● ● ● ● Finance Forms Gmail Groups Language Maps Sites Spreadsheet
  • 12.
    What you cando with Apps Script? Monitor your Web Google Form integrate Build a RESTful endpoint
  • 13.
    First Apps ScriptService Monitor using Google Drive ● Idea ○ Using google sheet for data persistance ○ Using apps script UrlFetch to retrieve monitor target resource ● Some Reference ○ http://gappsnews.blogspot.tw/2013/01/google-appsscript-http-monitor.html ○ http://gappsnews.blogspot.tw/2013/03/re-googlegoogle-apps-scriptgoogle-form.html
  • 14.
    You will know... ●SpreadsheetApp.openById(...) ○ getSheetByName(...) ○ insertSheet(...) ○ appendRow(...) ● UrlFetchApp.fetch(...)
  • 15.
  • 16.
    Save data tosheet and setting chart
  • 17.
    Web / GoogleSite Integrate
  • 18.
    2nd Apps ScriptGoogle Form send notification ● Idea: ○ Send mail notify when form sent ○ Call remote rest when form sent ● Some References: ○ http://gappsnews.blogspot.tw/2013/03/fwd-googleapps-script-event-object.html ○ http://gappsnews.blogspot.tw/2013/02/google-appsscriptgoogle-form.html
  • 19.
    You will know... ●SpreadsheetApp.getActiveSheet() ● GmailApp.sendEmail(...) ● JSON ○ Object.keys(jsonObject) ○ JSON.stringify(jsonObject) ○ JSON.parse(string)
  • 20.
    Create your formand result sheet Live Form Editor
  • 21.
    Create Script forResult Sheet
  • 22.
    Piece of Code functionnotify(){ function getLastRowTable(arr){ var sheet = return jsonObjToTableWithTitle(arr[0], SpreadsheetApp.getActiveSheet(); arr[arr.length-1]); var rows = sheet.getDataRange(); } var values = rows.getValues(); function jsonObjToTableWithTitle(title, row){ var content = getLastRowTable(values); var TRs = ''; var htmlBody = "Hi Admin: <br/><br/>”+ var keys = Object.keys(row); ”有人填表拉,檢 查一下吧! <br/><br/>" + for(var j = 0 ; j < keys.length ; j++) { content + var TR = '<tr>'; “<br/><br/>Send by Google Apps”; var rowvalue = row[keys[j]]; GmailApp.sendEmail( TR += ('<td>' + title[keys[j]] + "your-mail@gmail.com", '</td><td>' + rowvalue + '</td>' ); "Form Submit Confirm Notice", TR+= '</tr>'; htmlBody, TRs += TR; {from: your-mail@gmail.com, } htmlBody:htmlBody} var table = '<table border="1px">' + ); } TRs +'</table>'; return table;
  • 23.
    Create the formsubmit event trigger
  • 24.
  • 25.
    3rd Apps ScriptBuild a RESTful Service ● Idea ○ The advance interactive to all clients ● Restrictions ○ base url is fixed, only query and form as patameter can be the input ○ get, post method only (2013.7.21) ○ need solve the HTTP CODE: 302 problem ● Some Reference ○ http://gappsnews.blogspot.tw/2013/07/apps-scriptwith-restful.html ○ http://gappsnews.blogspot.tw/2013/07/fwd-appsscript.html ○ http://gappsnews.blogspot.tw/2013/07/apps-script-
  • 26.
    You will know... ●doGet() ● ContentService.createTextOutput(...)
  • 27.
    Demo of UsingRESTful Piece of Code
  • 28.
  • 29.
    Testing in curl URLPattern:https://script.google. com/macros/s/:id/exec Script ID
  • 30.
    Testing in AdvanceREST Client
  • 31.
  • 32.
    Thinking in different... ●Drive application integrate related, scheduling, data present and interactive... ● Static page service (Github Page, Cloud CMS) interactive... ● Mobile devices' remote thin app server, to be a RDB, NDB, BigQuery... middleware ● The chrome extension server side...
  • 33.
  • 34.
  • 35.
  • 36.
    Other Features Connectto Google Services ● Google Apps ○ Document extension ○ Simple form services ● Google Cloud Platform ○ BigQuery integrate ○ ScriptDB ● External Services ○ Connect with JDBC
  • 37.
    Reference ● Apps ScriptExample: https://developers. google.com/apps-script/case-studies ● Sunset Scripts: https://developers.google. com/apps-script/sunset
  • 38.