Google apps script introduction

Preview:

Citation preview

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的縮寫是GCPUGGCPUG直接唸成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 ScriptsLet’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 CalendarContacts

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

}

}

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)

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.

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

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.

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 CasesLet’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. 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")

}

i18n files

Trips & TipsLet’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 INFORMATIONLet’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=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)

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)

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 athttps://kaichu.io / cage.chung@gmail.com