33
GOOGLE APPS SCRIPT INTRODUCTION

Google apps script introduction

Embed Size (px)

Citation preview

Page 1: Google apps script introduction

GOOGLE APPS SCRIPT INTRODUCTION

Page 2: 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 雲端應用部資深工程師

Page 3: Google apps script introduction

https://www.facebook.com/groups/GCPUG.TW/

https://plus.google.com/u/0/communities/116100913832589966421

[您知道”GCPUG”要怎麼唸嗎?為什麼會有一隻狗在 Logo裡面呢? ]

Google Cloud Platform User Group的縮寫是GCPUGGCPUG直接唸成G.C.P.U.G?當然可以!

但它也可以分開來,唸成 G.C. PUG喔~

Pug,指的是巴哥犬,所以 GCPUG的Logo中間才會有一隻可愛的巴哥犬喲。

下次聽到別人說G.C. PUG 的時候,您就可以大聲說:「我也是G.C. PUG社團成員!」

Page 4: Google apps script introduction

Outline

◉ What’s Google Apps Script?◉ Study Cases

◉ CoffeMap◉ i18n Helper

◉ Trips & Tips ◉ Study information

Page 5: Google apps script introduction

What’s Google App ScriptsLet’s start with the first set of slides 1

Page 6: Google apps script introduction

“Google Apps Script is a scripting

language based on JavaScript that lets you do new and cool things with

Google Apps

Page 7: Google apps script introduction

One platform in the cloud

Sheet

Docs

Slide

Forms

Sites

Drive

Gmail CalendarContacts

Groups

Maps

Translate

Page 8: Google apps script introduction

Add-ons for Google Apps

Sheet Docs Forms

Page 9: Google apps script introduction

Sheet Add-ons

Page 10: Google apps script introduction

Slide Add-ons

Page 11: Google apps script introduction

Docs Add-ons

Page 12: Google apps script introduction

Add-ons for Google Apps - continue

Page 13: Google apps script introduction

G Suite Service

◉ Calendar◉ Contacts◉ Document◉ Drive◉ Forms◉ Gmail

◉ Groups◉ Language◉ Maps◉ Site◉ Slides◉ Spreadsheet

Page 14: Google apps script introduction

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

Page 15: Google apps script introduction

Script Services

◉ Base◉ Cache◉ Card◉ Charts◉ Content◉ HTML◉ JDBC◉ Lock

◉ Mail◉ Optimization◉ Properties◉ Script◉ URL Fetch◉ Utilities◉ XML

Page 16: Google apps script introduction

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());

}

}

Standaloneis 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)

Page 17: Google apps script introduction

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 AppsA script is bound to a Google Sheets, Docs, or Forms file.

Page 18: Google apps script introduction

Type of Scripts - Bound to G Suite Documents. continueTrigger and EventsTriggers 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

Page 19: Google apps script introduction

Type of Scripts - Web Apps and Site Gadgets

Web Apps and Site GadgetsIf 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.

Page 20: Google apps script introduction

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":"[email protected]"},{"level":2,"id":"A10","parent":"A00","type":"D","title":"資訊研發

部","email":"[email protected]"},{"level":3,"id":"SunnyHu","parent":"A10","type":"U","title":"胡適 ...

Page 21: Google apps script introduction

Study CasesLet’s start with the second set of slides 2

Page 22: Google apps script introduction

Coffee map

function getLatitudeLongitude(address) {

var geocode = Maps.newGeocoder().geocode(address);

if (geocode.results.length)

return geocode.results[0].geometry.location;

else

return null;

}

Page 23: Google apps script introduction

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')

}

Page 24: Google apps script introduction

i18n helper. continuefunction 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")

}

Page 25: Google apps script introduction

i18n files

Page 26: Google apps script introduction

Trips & TipsLet’s start with the third set of slides 3

Page 27: Google apps script introduction

“gapps (Google Apps Script)

The easiest way to develop Google Apps Script projects

Page 28: Google apps script introduction

$ gapps init 1k6eNig3veKXV_7_vQFcyQZDrDmB_qrJc5dlFRaQwDaM5ATaIMmclB-oP

$ gapps auth client_secret_566039949734-6b7kpll6rd4vil7896depbtt8t7fpc09.apps.googleusercontent.com.json

$ gapps push

gapps

Page 29: Google apps script introduction

STUDY INFORMATIONLet’s start with the fourth set of slides 4

Page 30: Google apps script introduction

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=PL68F511F6E3C122EB)

◉ [Apps Script Crash Course: ContentService YouTube](https://www.youtube.com/watch?v=JRGzVdliQOQ&list=PL68F511F6E3C122EB)

◉ [Apps Script | Google Developers](https://developers.google.com/apps-script/)[Google Apps Script 入門與應用 - 資訊學科中

心](http://icerc.tnssh.tn.edu.tw/download/rs/1030429_3.pdf)

Page 31: Google apps script introduction

Study information - examples

◉ [Generate EPUB file with Google Apps Scripts | blog.softapalvelin.com](http://blog.softapalvelin.com/2013/02/generate-epub-file-with-google-app.html)

◉ [RSSToEPUB](https://goo.gl/YCDDBo)[google/google-apps-script-samples](https://github.com/google/google-apps-script-samples)

◉ [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)

Page 32: Google apps script introduction

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)

Page 33: Google apps script introduction

THANKS!Any questions?

You can find me athttps://kaichu.io / [email protected]