Introduction to Google Apps Script

4,436 views

Published on

Slides used in a presentation to introduce Google Apps Script and solve the problem of automatically creating new sheet for each user completing a form

  • Be the first to comment

Introduction to Google Apps Script

  1. 1. Introduction to Google Apps Script On form submit filter data to sheets Martin Hawksey @mhawksey
  2. 2. The Problem <ul><li>Hi there, I'm trying to get some student PT's to complete a training diary using a Google form. I would like the form to automatically create a new sheet for each user.  I should ultimately have 42 entries per user/sheet. </li></ul><ul><li>http://www.google.com/support/forum/p/Google+Docs/thread?tid=6fe8356595e9b859&hl=en </li></ul>http://mashe.hawksey.info
  3. 3. Solution 1 - Filter the data http://mashe.hawksey.info
  4. 4. Solution 2 – Google Apps Script <ul><li>Google Apps Script is a JavaScript cloud scripting language that provides easy ways to automate tasks across Google products and third party services. </li></ul><ul><li>http://code.google.com/googleapps/appsscript/ </li></ul>http://mashe.hawksey.info
  5. 5. How I see it http://mashe.hawksey.info
  6. 6. How you write it <ul><li>In Spreadsheets </li></ul><ul><ul><li>Tools > Script Editor </li></ul></ul><ul><li>In Sites </li></ul><ul><ul><li>Manage Site > Apps Script </li></ul></ul>http://mashe.hawksey.info
  7. 7. How does the user interact with it <ul><li>In Spreadsheets </li></ul><ul><li>In Sites </li></ul><ul><li>As a service </li></ul>http://mashe.hawksey.info
  8. 8. There’s good and bad news <ul><li>Bad news </li></ul><ul><ul><li>You need basic programming skills to write </li></ul></ul><ul><li>Good news </li></ul><ul><ul><li>Already a growing number of developers publishing scripts and tutorials for you to reuse e.g. http://www.flubaroo.com/ </li></ul></ul>http://mashe.hawksey.info
  9. 9. Lets do it <ul><li>https://spreadsheets.google.com/a/hawksey.info/spreadsheet/ccc?key=0Alq6_NgDnxLWdENvZHhwVm93TjRhTm5fcjU0X0t3Vnc </li></ul><ul><li>Logins: [email_address] </li></ul><ul><li>[email_address] </li></ul><ul><li>… </li></ul><ul><li>[email_address] </li></ul><ul><li>Password: ******* </li></ul>http://mashe.hawksey.info
  10. 10. Explanation of Javascript Objects got by getValues() <ul><li>data = [[“Dog”,”Cat”,”Cow”],[22, 31, 15],[6, 5, 5]] </li></ul><ul><li>data[0] = [“Dog”,”Cat”,”Cow”] </li></ul><ul><li>data[0][1] = “Cat” </li></ul>http://mashe.hawksey.info Dog Cat Cow 22 31 15 6 5 5
  11. 11. Useful links <ul><li>Workshop material from Open4Ed </li></ul><ul><ul><li>http://mashe.hawksey.info/2011/05/app-app-and-away-workshop-handout-open4ed-gas/ </li></ul></ul><ul><li>Simple Apps Solutions (free Apps Script consultancy for Ed and tutorials) </li></ul><ul><ul><li>http://simpleappssolutions.com </li></ul></ul><ul><li>Official Apps Script Site </li></ul><ul><ul><li>Docs </li></ul></ul><ul><ul><li>Tutorials </li></ul></ul><ul><ul><li>Help forum </li></ul></ul>http://mashe.hawksey.info
  12. 12. Not forgetting your friendly EdTech explorer <ul><li>Email: martin@hawksey.info </li></ul><ul><li>Blog: http://mashe.hawksey.info </li></ul><ul><li>Tweet: @mhawksey </li></ul>http://mashe.hawksey.info
  13. 13. Pseudo-code <ul><li>  // onFormSubmit </li></ul><ul><li>// get submitted data   // check if username has sheet   //    if not make   // copy submitted data to user's sheet </li></ul>http://mashe.hawksey.info
  14. 14. Final code <ul><li>function onFormSubmit() { </li></ul><ul><li>// onFormSubmit </li></ul><ul><li>// get submitted data </li></ul><ul><li>var ss = SpreadsheetApp.getActiveSpreadsheet(); </li></ul><ul><li>var sheet = ss.getSheetByName(&quot;Form_Sheet&quot;); </li></ul><ul><li>var headings = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); </li></ul><ul><li>var lastRow = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues(); </li></ul><ul><li>var studentUsername = lastRow[0][1]; </li></ul><ul><li>// check if username has sheet </li></ul><ul><li>if(ss.getSheetByName(studentUsername)){ </li></ul><ul><li>var userSheet = ss.getSheetByName(studentUsername); </li></ul><ul><li>// if not make </li></ul><ul><li>} else { </li></ul><ul><li>var userSheet = ss.insertSheet(studentUsername); </li></ul><ul><li>userSheet.getRange(1, 1 , 1, headings[0].length).setValues(headings); </li></ul><ul><li>} </li></ul><ul><li>// copy submitted data to user's sheet </li></ul><ul><li>userSheet.getRange(userSheet.getLastRow()+1, 1, 1, lastRow[0].length).setValues(lastRow); </li></ul><ul><li>} </li></ul>http://mashe.hawksey.info

×