• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Enterprise workflow with Apps Script
 

Enterprise workflow with Apps Script

on

  • 1,430 views

 

Statistics

Views

Total Views
1,430
Views on SlideShare
1,430
Embed Views
0

Actions

Likes
1
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Enterprise workflow with Apps Script Enterprise workflow with Apps Script Presentation Transcript

    • Enterprise Workflow with Apps Script Claudio Cherubino Google Apps Developer Relations http://plus.claudiocherubino.it
    • 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
    • Why Apps Script? Dont hate, automate
    • Where is Apps Script? ● Executed in a variety of different ways ○ Spreadsheet, Sites, Standalone, Async
    • Who can use Apps Script?
    • Apps Script Services Dont hate, automate
    • 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; }
    • Lets write some code...
    • Read the content of a Google Doc 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; }
    • Retrieve stock information 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); }
    • Process users from spreadsheet 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 shareholders name var text = message.replace([SHAREHOLDER], name); }
    • Automatically translate messages 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 shareholders name var text = message.replace([SHAREHOLDER], name); if (language != en) { text = LanguageApp.translate(text, en, language); } }
    • Send mail from a 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 shareholders name var text = message.replace([SHAREHOLDER], name); if (language != en) { text = LanguageApp.translate(text, en, language); } MailApp.sendEmail(emailAddress, subject, text); }
    • Add markers to Google Maps 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 shareholders 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();
    • More examples...
    • Calendar API // get users 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"});
    • 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; }
    • Ui Services // 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);
    • Charts Services - 1/2 function getData() { // populate the DataTable. Well 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 - 2/2 function buildChart(dataTable) { // Build the chart. Well 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); }
    • 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
    • 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/
    • Thanks!Questions?@ccherubinohttp://plus.claudiocherubino.it