Enterprise workflow with Apps Script

1,561 views
1,425 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,561
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Enterprise workflow with Apps Script

  1. 1. Enterprise Workflow with Apps Script Claudio Cherubino Google Apps Developer Relations http://plus.claudiocherubino.it
  2. 2. 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
  3. 3. Why Apps Script? Dont hate, automate
  4. 4. Where is Apps Script? ● Executed in a variety of different ways ○ Spreadsheet, Sites, Standalone, Async
  5. 5. Who can use Apps Script?
  6. 6. Apps Script Services Dont hate, automate
  7. 7. 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; }
  8. 8. Lets write some code...
  9. 9. 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; }
  10. 10. 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); }
  11. 11. 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); }
  12. 12. 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); } }
  13. 13. 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); }
  14. 14. 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();
  15. 15. More examples...
  16. 16. 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"});
  17. 17. 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; }
  18. 18. 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);
  19. 19. 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; }
  20. 20. 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); }
  21. 21. 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
  22. 22. 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/
  23. 23. Thanks!Questions?@ccherubinohttp://plus.claudiocherubino.it

×