Top Banner
Claudio Cherubino Google Apps Developer Relations http://plus.claudiocherubino.it Enterprise Workflow with Apps Script
23

Enterprise workflow with Apps Script

Jun 13, 2015

Download

Technology

ccherubino
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Enterprise workflow with Apps Script

Claudio CherubinoGoogle Apps Developer Relations

http://plus.claudiocherubino.it

Enterprise Workflow withApps Script

Page 2: Enterprise workflow with Apps Script

What is Apps Script?

● JavaScript engine in the cloud○ JavaScript Runtime○ Write and execute code in browser

● Comes with○ JavaScript syntax and classes○ Built-in access to various Google APIs○ Ability to integrate 3rd party services

Page 3: Enterprise workflow with Apps Script

Why Apps Script?

Don't hate, automate

Page 4: Enterprise workflow with Apps Script

Where is Apps Script?

● Executed in a variety of different ways○ Spreadsheet, Sites, Standalone, Async

Page 5: Enterprise workflow with Apps Script

Who can use Apps Script?

Page 6: Enterprise workflow with Apps Script

Apps Script Services

Don't hate, automate

Page 7: Enterprise workflow with Apps Script

A basic example

// Function to convert from inches to centimeters function in2cm(inNum) { var outNum = 0; // this will hold the result var factor = 2.54; // multiply input by this factor if (typeof inNum != "number") { return("error: input must be a number"); } outNum = inNum * factor; return outNum; }

Page 8: Enterprise workflow with Apps Script

Let's write some code...

Page 9: Enterprise workflow with Apps Script

function main() { var documentId = '18ByAQxeL...'; var letter = DocumentApp.openById(documentId);

var subject = letter.getName(); var message = readDocumentBody(letter); }

function readDocumentBody(document) { var paragraphs = document.getParagraphs(); var txt = "";

for (var i = 0; i < paragraphs.length; i++) { if (paragraphs[i].getNumChildren() > 0) { txt += paragraphs[i].getChild(0).getText(); } txt += "\n"; } return txt; }

Read the content of a Google Doc

Page 10: Enterprise workflow with Apps Script

function main() { var documentId = '18ByAQxeL...'; var letter = DocumentApp.openById(documentId);

var subject = letter.getName(); var message = readDocumentBody(letter);

// retrieve GOOG stock price var stockInfo = FinanceApp.getStockInfo('GOOG'); var stockPrice = Math.round(stockInfo.price * 100) / 100; var stockValue = stockPrice + ' ' + stockInfo.currency;

// replace placeholder with current stock value message = message.replace('[STOCKVALUE]', stockValue); }

Retrieve stock information

Page 11: Enterprise workflow with Apps Script

var sheet = SpreadsheetApp.getActiveSheet(); var startRow = 2; // First row of data to process var numRows = sheet.getLastRow() - 1; // Number of rows to process

var dataRange = sheet.getRange(startRow, 1, numRows, 4); var data = dataRange.getValues();

for (i in data) { var row = data[i]; var name = row[0]; var emailAddress = row[1]; var language = row[2]; var location = row[3];

// personalize letter with shareholder's name var text = message.replace('[SHAREHOLDER]', name); }

Process users from spreadsheet

Page 12: Enterprise workflow with Apps Script

for (i in data) { var row = data[i]; var name = row[0]; var emailAddress = row[1]; var language = row[2]; var location = row[3];

// personalize letter with shareholder's name var text = message.replace('[SHAREHOLDER]', name);

if (language != 'en') { text = LanguageApp.translate(text, 'en', language); } }

Automatically translate messages

Page 13: Enterprise workflow with Apps Script

for (i in data) { var row = data[i]; var name = row[0]; var emailAddress = row[1]; var language = row[2]; var location = row[3];

// personalize letter with shareholder's name var text = message.replace('[SHAREHOLDER]', name);

if (language != 'en') { text = LanguageApp.translate(text, 'en', language); }

MailApp.sendEmail(emailAddress, subject, text); }

Send mail from a script

Page 14: Enterprise workflow with Apps Script

var map = Maps.newStaticMap().setSize(1024, 1024); for (i in data) { var row = data[i]; var name = row[0]; var emailAddress = row[1]; var language = row[2]; var location = row[3];

// personalize letter with shareholder's name var text = message.replace('[SHAREHOLDER]', name);

if (language != 'en') { text = LanguageApp.translate(text, 'en', language); }

MailApp.sendEmail(emailAddress, subject, text); map.addMarker(location); } var mapUrl = map.getMapUrl();

Add markers to Google Maps

Page 15: Enterprise workflow with Apps Script

More examples...

Page 16: Enterprise workflow with Apps Script

Calendar API

// get user's calendar var calendar = CalendarApp.getDefaultCalendar();

// schedule event var startDate = new Date(); var endDate = new Date(startDate); endDate.setHours(startDate.getHours()+2);

var event = calendar.createEvent( "Training:", startDate, endDate, {description:"Training event"});

Page 17: Enterprise workflow with Apps Script

3rd-party APIs

// function to access Netflix API function searchNetflixTitles(genre, startPosition, numberOfResults) { var ODATA_GENRE_URL = "http://odata.netflix.com/Catalog/Genres";

var requestURL = ODATA_GENRE_URL + "('" + genre + "')/Titles?" + "$select=Name,ShortSynopsis&$format=json&$skip=" + startPosition + "&$top=" + numberOfResults;

var content = UrlFetchApp.fetch(requestURL); var result = Utilities.jsonParse(content.getContentText()); return result; }

Page 18: Enterprise workflow with Apps Script

// create List Box Message Label var listBoxMessageLabel = app.createLabel() .setText("1. Choose the Training Category:"); decorateLabel_(listBoxMessageLabel);

// create List Box var categoryListBox = app.createListBox() .setName("categoryListBox") .setId("categoryListBox"); categoryListBox.addItem("Category List", "0"); categoryListBox.addItem("Language Instruction", "1"); categoryListBox.addItem("Computers & Electronics", "2"); decorateLabel_(categoryListBox);

// create change handler var categorySelectHandler = app.createServerChangeHandler("categorySelectionHandler_");

categorySelectHandler.addCallbackElement(mainPanel); categoryListBox.addChangeHandler(categorySelectHandler);

Ui Services

Page 19: Enterprise workflow with Apps Script

function getData() { // populate the DataTable. We'll have the data labels in // the first column, "Quarter", and then add two data columns, // for "Income" and "Expenses"List Box Message Label var dataTable = Charts.newDataTable() .addColumn(Charts.ColumnType.STRING, "Quarter") .addColumn(Charts.ColumnType.NUMBER, "Income") .addColumn(Charts.ColumnType.NUMBER, "Expenses") .addRow(["Q1", 50, 60]) .addRow(["Q2", 60, 55]) .addRow(["Q3", 70, 60]) .addRow(["Q4", 100, 50]) .build(); return dataTable; }

Charts Services - 1/2

Page 20: Enterprise workflow with Apps Script

function buildChart(dataTable) { // Build the chart. We'll make income green and expenses red var chart = Charts.newColumnChart() .setDataTable(dataTable) .setColors(["green", "red"]) .setDimensions(600, 400) .setXAxisTitle("Quarters") .setYAxisTitle("$") .setTitle("Income and Expenses per Quarter") .build();

return chart; }

function main() { var chart = buildChart(getData()); var ui = UiApp.createApplication(); ui.add(chart); SpreadsheetApp.getActiveSpreadsheet().show(ui); }

Charts Services - 2/2

Page 21: Enterprise workflow with Apps Script

Triggers

● Trigger ~= Event handler

● Triggers allow asynchronous execution of scripts

○ Helps in automation○ A user no longer has to manually execute the script

● Two Types of Triggers○ Event Driven - onEdit, onInstall, onOpen○ Time Driven

Page 22: Enterprise workflow with Apps Script

Resources

● Apps Script documentation○ http://code.google.com/googleapps/appsscript/

● Tutorials○ http://code.google.com/googleapps/appsscript/articles.html

● Online community○ http://www.google.com/support/forum/p/apps-script/

● Google Apps Script Blog○ http://googleappsscript.blogspot.com/

Page 23: Enterprise workflow with Apps Script

Thanks!

Questions?

@ccherubinohttp://plus.claudiocherubino.it