38
Google Apps Script Simon Su

Google apps script

Embed Size (px)

Citation preview

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 ScriptsBy 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

● 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-apps-script-http-monitor.html

○ http://gappsnews.blogspot.tw/2013/03/re-google-google-apps-scriptgoogle-form.html

First Apps Script - Service Monitor using Google Drive

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-google-

apps-script-event-object.html○ http://gappsnews.blogspot.tw/2013/02/google-apps-

scriptgoogle-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

function notify(){ var sheet = SpreadsheetApp.getActiveSheet(); var rows = sheet.getDataRange(); var values = rows.getValues(); var content = getLastRowTable(values); var htmlBody = "Hi Admin: <br/><br/>”+ ”有人填表拉,檢查一下吧! <br/><br/>" + content + “<br/><br/>Send by Google Apps”; GmailApp.sendEmail( "[email protected]", "Form Submit Confirm Notice", htmlBody, {from: [email protected], htmlBody:htmlBody} ); }

Piece of Codefunction getLastRowTable(arr){ return jsonObjToTableWithTitle(arr[0], arr[arr.length-1]);}function jsonObjToTableWithTitle(title, row){ var TRs = ''; var keys = Object.keys(row); for(var j = 0 ; j < keys.length ; j++) { var TR = '<tr>'; var rowvalue = row[keys[j]]; TR += ('<td>' + title[keys[j]] + '</td><td>' + rowvalue + '</td>' ); TR+= '</tr>'; TRs += TR; } 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-script-

with-restful.html○ http://gappsnews.blogspot.tw/2013/07/fwd-apps-

script.html○ http://gappsnews.blogspot.tw/2013/07/apps-script-

rest-dopost-request.html

You will know...

● doGet()● ContentService.createTextOutput(...)

Demo of Using RESTfulPiece 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

Q&ASlideshare: http://www.slideshare.net/peihsinsu/google-apps-script-24469585