Introduction to Google Apps Script


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> </li></ul>
  3. 3. Solution 1 - Filter the data
  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> </li></ul>
  5. 5. How I see it
  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>
  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>
  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. </li></ul></ul>
  9. 9. Lets do it <ul><li> </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>
  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> Dog Cat Cow 22 31 15 6 5 5
  11. 11. Useful links <ul><li>Workshop material from Open4Ed </li></ul><ul><ul><li> </li></ul></ul><ul><li>Simple Apps Solutions (free Apps Script consultancy for Ed and tutorials) </li></ul><ul><ul><li> </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>
  12. 12. Not forgetting your friendly EdTech explorer <ul><li>Email: </li></ul><ul><li>Blog: </li></ul><ul><li>Tweet: @mhawksey </li></ul>
  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>
  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>