GOOGLE APPS SCRIPT
INTRODUCTION
HELLO!
I am Cage Chung
I am here because I like to share my experiences.
You can find me at https://kaichu.io / QNAP 雲端應用部資深工程師
https://www.facebook.com/groups/GCPUG.TW/
https://plus.google.com/u/0/communities/116100913832589966421
[您知道”GCPUG”要怎麼唸嗎?為什麼會有一隻狗在 Logo裡面呢?]
Google Cloud Platform User Group的縮寫是GCPUG
GCPUG直接唸成G.C.P.U.G?當然可以!
但它也可以分開來,唸成 G.C. PUG喔~
Pug,指的是巴哥犬,所以 GCPUG的Logo中間才會有一隻可愛的巴哥犬喲。
下次聽到別人說G.C. PUG 的時候,您就可以大聲 說:「我也是G.C. PUG社團成員!」
Outline
◉ What’s Google Apps Script?
◉ Study Cases
◉ CoffeMap
◉ i18n Helper
◉ Trips & Tips
◉ Study information
What’s Google App Scripts
Let’s start with the first set of slides 1
“Google Apps Script is a scripting
language based on JavaScript that
lets you do new and cool things with
Google Apps
One platform in the cloud
Sheet
Docs
Slide
Forms
Sites
Drive
Gmail Calendar
Contacts
Groups
Maps
Translate
Add-ons for Google Apps
Sheet Docs Forms
Sheet Add-ons
Slide Add-ons
Docs Add-ons
Add-ons for Google Apps - continue
G Suite Service
◉ Calendar
◉ Contacts
◉ Document
◉ Drive
◉ Forms
◉ Gmail
◉ Groups
◉ Language
◉ Maps
◉ Site
◉ Slides
◉ Spreadsheet
Advanced Google Service
◉ Admin SDK
◉ AdSence
◉ Analytics
◉ Apps Activity
◉ BigQuery
◉ Calendar
◉ Classroom
◉ Drive
◉ DoubleClick Campaigns
◉ Fusion Tables
◉ Gmail
◉ Google+
◉ Mirror
◉ Prediction
◉ Sheets
◉ Shopping Content
◉ Slides
◉ Tasks
◉ Tag Manager
◉ URL Shortener
◉ Youtube
Script Services
◉ Base
◉ Cache
◉ Card
◉ Charts
◉ Content
◉ HTML
◉ JDBC
◉ Lock
◉ Mail
◉ Optimization
◉ Properties
◉ Script
◉ URL Fetch
◉ Utilities
◉ XML
Type of Scripts - Standalone
// Log the name of every file in the user's Drive that modified after February 28,
// 2013 whose name contains "untitled".
function doAction(){
var files = DriveApp.searchFiles(
'modifiedDate > "2015-01-01" and title contains "untitled"');
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
}
}
Standalone
is any script that is not bound to a Google Sheets, Docs, Slides or Forms file or Google
Sites.
[iOS Taipei - Apps Script - Type of Scripts - Standalone](https://goo.gl/oj8WVO)
Type of Scripts - Bound to G Suite Documents
function doAction(range) {
// Get the active spreadsheet and the active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Get the range of cells that store employee data.
var libraryDataRange = sheet.getRange(range);
var libraryObjects = getRowsData(sheet, libraryDataRange, 1);
libraryObjects.forEach(function (row, index) {
var latlng = getLatitudeLongitude(row.address)
if (latlng) {
sheet.getRange(index + 2, 5, 1, 1).setValue(latlng.lat);
sheet.getRange(index + 2, 6, 1, 1).setValue(latlng.lng);
}
});
}
Bound to Google Apps
A script is bound to a Google Sheets, Docs, or Forms file.
Type of Scripts - Bound to G Suite Documents. continue
Trigger and Events
Triggers let Apps Script run a function automatically when a certain event,
like opening a document, occurs.
[coffeemap-testing-form - Google Sheets](https://goo.gl/ZAIOtT)
[coffeemap](https://goo.gl/Wb91tW) quick setup form
Type of Scripts - Web Apps and Site Gadgets
Web Apps and Site Gadgets
If you build a user interface for a
script, you can publish the script
as a web app
◉ It contains a doGet(e) and
doPost(e) function.
◉ The function returns an
HTML service HtmlOutput
object or a Content service
TextOutput object.
Type of Scripts - Web Apps and Site Gadgets. continue
function doGet() {
var ss = SpreadsheetApp.openById('1QgsaX4Vn_lIwLFRC_iHs6gmJYIP4y-35nVqQOpz4B0s');
var sheet = ss.getSheets()[0];
// Get the range of cells that store employee data.
var employeeDataRange = ss.getRangeByName("employeeData");
// For every row of employee data, generate an employee object.
var employeeObjects = getRowsData(sheet, employeeDataRange);
return ContentService.createTextOutput(JSON.stringify(employeeObjects)).setMimeType(ContentService.MimeType.JSON);
}
$ curl -L https://script.google.com/macros/s/AKfycbz4Z2dm-MUidB98H5XbekL0LZnvPVRM3ekpG-NSrScc9tvI87A/exec
[{"level":1,"id":"A00","parent":"root","type":"D","title":"資訊研發
處","email":"A00@aa.bb.cc"},{"level":2,"id":"A10","parent":"A00","type":"D","title":"資訊研發
部","email":"A10@aa.bb.cc"},{"level":3,"id":"SunnyHu","parent":"A10","type":"U","title":"胡適 ...
Study Cases
Let’s start with the second set of slides 2
Coffee map
function getLatitudeLongitude(address) {
var geocode = Maps.newGeocoder().geocode(address);
if (geocode.results.length)
return geocode.results[0].geometry.location;
else
return null;
}
i18n helper
function exportJson(data) {
google.script.run.withSuccessHandler(handleDownload)
.withFailureHandler(showError).exportJobs(data);
}
function handleDownload(data) {
var blob = new Blob([new Uint8Array(data.content)], { type: "octet/stream" });
var link = document.createElement('a');
link.download = data.fileName;
link.href = window.URL.createObjectURL(blob);
link.click();
$('#export').text('Export')
}
i18n helper. continue
function exportJobs(data) {
var blobs = [];
...
data.columns.forEach(function (column) {
blobs.push(getBlob(ss, data.sheet, column.name, parseInt(column.index, 10) + 1));
})
var now = new Date();
var datetime = Utilities.formatDate(now, 'Asia/Taipei', 'yyyyMMddHHmm');
var fileName = ass.getName() + "_" + ss.getName() + "_i18n_" + datetime + ".zip";
var zip = Utilities.zip(blobs, fileName);
return {
fileName: fileName,
content: zip.getBytes(),
}
}
function getBlob(ss, sheetName, columnName, columnIndex) {
content = getContent(ss, columnName, columnIndex)
return Utilities.newBlob(content, "text/javascript", columnName + ".js")
}
i18n files
Trips & Tips
Let’s start with the third set of slides 3
“gapps (Google Apps Script)
The easiest way to develop Google
Apps Script projects
$ gapps init 1k6eNig3veKXV_7_vQFcyQZDrDmB_qrJc5dlFRaQwDaM5ATaIMmclB-oP
$ gapps auth client_secret_566039949734-6b7kpll6rd4vil7896depbtt8t7fpc09.apps.googleusercontent.com.json
$ gapps push
gapps
STUDY INFORMATION
Let’s start with the fourth set of slides 4
Study info
◉ [Google apps script - simon
su](http://www.slideshare.net/peihsinsu/google-apps-script-24469585)
◉ [entaq/GoogleAppsScript](https://github.com/entaq/GoogleAppsScript)
◉ [Script It! with Android -
YouTube](https://www.youtube.com/watch?v=RSgMEtRl0sw&list=PL68F511F6
E3C122EB)
◉ [Apps Script Crash Course: ContentService
YouTube](https://www.youtube.com/watch?v=JRGzVdliQOQ&list=PL68F511F6
E3C122EB)
◉ [Apps Script | Google Developers](https://developers.google.com/apps-script/)
[Google Apps Script 入門與應用 - 資訊學科中
心](http://icerc.tnssh.tn.edu.tw/download/rs/1030429_3.pdf)
Study information - examples
◉ [Generate EPUB file with Google Apps Scripts |
blog.softapalvelin.com](http://blog.softapalvelin.com/2013/02/generate-epub-file-wi
th-google-app.html)
◉ [RSSToEPUB](https://goo.gl/YCDDBo)
[google/google-apps-script-samples](https://github.com/google/google-apps-script-s
amples)
◉ [Tutorials | Apps Script | Google
Developers](https://developers.google.com/apps-script/articles)
◉ [Parsing HTML - Google Apps Script
Examples](https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-
html)
Study information - Tools
◉ [danthareja/node-google-apps-script: The easiest way to develop Google Apps Script
projects](https://github.com/danthareja/node-google-apps-script)
THANKS!
Any questions?
You can find me at
https://kaichu.io / cage.chung@gmail.com

Google apps script introduction

  • 1.
  • 2.
    HELLO! I am CageChung I am here because I like to share my experiences. You can find me at https://kaichu.io / QNAP 雲端應用部資深工程師
  • 3.
    https://www.facebook.com/groups/GCPUG.TW/ https://plus.google.com/u/0/communities/116100913832589966421 [您知道”GCPUG”要怎麼唸嗎?為什麼會有一隻狗在 Logo裡面呢?] Google CloudPlatform User Group的縮寫是GCPUG GCPUG直接唸成G.C.P.U.G?當然可以! 但它也可以分開來,唸成 G.C. PUG喔~ Pug,指的是巴哥犬,所以 GCPUG的Logo中間才會有一隻可愛的巴哥犬喲。 下次聽到別人說G.C. PUG 的時候,您就可以大聲 說:「我也是G.C. PUG社團成員!」
  • 4.
    Outline ◉ What’s GoogleApps Script? ◉ Study Cases ◉ CoffeMap ◉ i18n Helper ◉ Trips & Tips ◉ Study information
  • 5.
    What’s Google AppScripts Let’s start with the first set of slides 1
  • 6.
    “Google Apps Scriptis a scripting language based on JavaScript that lets you do new and cool things with Google Apps
  • 7.
    One platform inthe cloud Sheet Docs Slide Forms Sites Drive Gmail Calendar Contacts Groups Maps Translate
  • 8.
    Add-ons for GoogleApps Sheet Docs Forms
  • 9.
  • 10.
  • 11.
  • 12.
    Add-ons for GoogleApps - continue
  • 13.
    G Suite Service ◉Calendar ◉ Contacts ◉ Document ◉ Drive ◉ Forms ◉ Gmail ◉ Groups ◉ Language ◉ Maps ◉ Site ◉ Slides ◉ Spreadsheet
  • 14.
    Advanced Google Service ◉Admin SDK ◉ AdSence ◉ Analytics ◉ Apps Activity ◉ BigQuery ◉ Calendar ◉ Classroom ◉ Drive ◉ DoubleClick Campaigns ◉ Fusion Tables ◉ Gmail ◉ Google+ ◉ Mirror ◉ Prediction ◉ Sheets ◉ Shopping Content ◉ Slides ◉ Tasks ◉ Tag Manager ◉ URL Shortener ◉ Youtube
  • 15.
    Script Services ◉ Base ◉Cache ◉ Card ◉ Charts ◉ Content ◉ HTML ◉ JDBC ◉ Lock ◉ Mail ◉ Optimization ◉ Properties ◉ Script ◉ URL Fetch ◉ Utilities ◉ XML
  • 16.
    Type of Scripts- Standalone // Log the name of every file in the user's Drive that modified after February 28, // 2013 whose name contains "untitled". function doAction(){ var files = DriveApp.searchFiles( 'modifiedDate > "2015-01-01" and title contains "untitled"'); while (files.hasNext()) { var file = files.next(); Logger.log(file.getName()); } } Standalone is any script that is not bound to a Google Sheets, Docs, Slides or Forms file or Google Sites. [iOS Taipei - Apps Script - Type of Scripts - Standalone](https://goo.gl/oj8WVO)
  • 17.
    Type of Scripts- Bound to G Suite Documents function doAction(range) { // Get the active spreadsheet and the active sheet var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); // Get the range of cells that store employee data. var libraryDataRange = sheet.getRange(range); var libraryObjects = getRowsData(sheet, libraryDataRange, 1); libraryObjects.forEach(function (row, index) { var latlng = getLatitudeLongitude(row.address) if (latlng) { sheet.getRange(index + 2, 5, 1, 1).setValue(latlng.lat); sheet.getRange(index + 2, 6, 1, 1).setValue(latlng.lng); } }); } Bound to Google Apps A script is bound to a Google Sheets, Docs, or Forms file.
  • 18.
    Type of Scripts- Bound to G Suite Documents. continue Trigger and Events Triggers let Apps Script run a function automatically when a certain event, like opening a document, occurs. [coffeemap-testing-form - Google Sheets](https://goo.gl/ZAIOtT) [coffeemap](https://goo.gl/Wb91tW) quick setup form
  • 19.
    Type of Scripts- Web Apps and Site Gadgets Web Apps and Site Gadgets If you build a user interface for a script, you can publish the script as a web app ◉ It contains a doGet(e) and doPost(e) function. ◉ The function returns an HTML service HtmlOutput object or a Content service TextOutput object.
  • 20.
    Type of Scripts- Web Apps and Site Gadgets. continue function doGet() { var ss = SpreadsheetApp.openById('1QgsaX4Vn_lIwLFRC_iHs6gmJYIP4y-35nVqQOpz4B0s'); var sheet = ss.getSheets()[0]; // Get the range of cells that store employee data. var employeeDataRange = ss.getRangeByName("employeeData"); // For every row of employee data, generate an employee object. var employeeObjects = getRowsData(sheet, employeeDataRange); return ContentService.createTextOutput(JSON.stringify(employeeObjects)).setMimeType(ContentService.MimeType.JSON); } $ curl -L https://script.google.com/macros/s/AKfycbz4Z2dm-MUidB98H5XbekL0LZnvPVRM3ekpG-NSrScc9tvI87A/exec [{"level":1,"id":"A00","parent":"root","type":"D","title":"資訊研發 處","email":"A00@aa.bb.cc"},{"level":2,"id":"A10","parent":"A00","type":"D","title":"資訊研發 部","email":"A10@aa.bb.cc"},{"level":3,"id":"SunnyHu","parent":"A10","type":"U","title":"胡適 ...
  • 21.
    Study Cases Let’s startwith the second set of slides 2
  • 22.
    Coffee map function getLatitudeLongitude(address){ var geocode = Maps.newGeocoder().geocode(address); if (geocode.results.length) return geocode.results[0].geometry.location; else return null; }
  • 23.
    i18n helper function exportJson(data){ google.script.run.withSuccessHandler(handleDownload) .withFailureHandler(showError).exportJobs(data); } function handleDownload(data) { var blob = new Blob([new Uint8Array(data.content)], { type: "octet/stream" }); var link = document.createElement('a'); link.download = data.fileName; link.href = window.URL.createObjectURL(blob); link.click(); $('#export').text('Export') }
  • 24.
    i18n helper. continue functionexportJobs(data) { var blobs = []; ... data.columns.forEach(function (column) { blobs.push(getBlob(ss, data.sheet, column.name, parseInt(column.index, 10) + 1)); }) var now = new Date(); var datetime = Utilities.formatDate(now, 'Asia/Taipei', 'yyyyMMddHHmm'); var fileName = ass.getName() + "_" + ss.getName() + "_i18n_" + datetime + ".zip"; var zip = Utilities.zip(blobs, fileName); return { fileName: fileName, content: zip.getBytes(), } } function getBlob(ss, sheetName, columnName, columnIndex) { content = getContent(ss, columnName, columnIndex) return Utilities.newBlob(content, "text/javascript", columnName + ".js") }
  • 25.
  • 26.
    Trips & Tips Let’sstart with the third set of slides 3
  • 27.
    “gapps (Google AppsScript) The easiest way to develop Google Apps Script projects
  • 28.
    $ gapps init1k6eNig3veKXV_7_vQFcyQZDrDmB_qrJc5dlFRaQwDaM5ATaIMmclB-oP $ gapps auth client_secret_566039949734-6b7kpll6rd4vil7896depbtt8t7fpc09.apps.googleusercontent.com.json $ gapps push gapps
  • 29.
    STUDY INFORMATION Let’s startwith the fourth set of slides 4
  • 30.
    Study info ◉ [Googleapps script - simon su](http://www.slideshare.net/peihsinsu/google-apps-script-24469585) ◉ [entaq/GoogleAppsScript](https://github.com/entaq/GoogleAppsScript) ◉ [Script It! with Android - YouTube](https://www.youtube.com/watch?v=RSgMEtRl0sw&list=PL68F511F6 E3C122EB) ◉ [Apps Script Crash Course: ContentService YouTube](https://www.youtube.com/watch?v=JRGzVdliQOQ&list=PL68F511F6 E3C122EB) ◉ [Apps Script | Google Developers](https://developers.google.com/apps-script/) [Google Apps Script 入門與應用 - 資訊學科中 心](http://icerc.tnssh.tn.edu.tw/download/rs/1030429_3.pdf)
  • 31.
    Study information -examples ◉ [Generate EPUB file with Google Apps Scripts | blog.softapalvelin.com](http://blog.softapalvelin.com/2013/02/generate-epub-file-wi th-google-app.html) ◉ [RSSToEPUB](https://goo.gl/YCDDBo) [google/google-apps-script-samples](https://github.com/google/google-apps-script-s amples) ◉ [Tutorials | Apps Script | Google Developers](https://developers.google.com/apps-script/articles) ◉ [Parsing HTML - Google Apps Script Examples](https://sites.google.com/site/scriptsexamples/learn-by-example/parsing- html)
  • 32.
    Study information -Tools ◉ [danthareja/node-google-apps-script: The easiest way to develop Google Apps Script projects](https://github.com/danthareja/node-google-apps-script)
  • 33.
    THANKS! Any questions? You canfind me at https://kaichu.io / cage.chung@gmail.com