introduction to google apps script

14
Introduction to Google Apps Script On form submit filter data to sheets Martin Hawksey @mhawksey

Upload: martin-hawksey

Post on 06-May-2015

3.278 views

Category:

Education


10 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Introduction to Google Apps Script

Introduction to Google Apps ScriptOn form submit filter data to sheets

Martin Hawksey@mhawksey

Page 2: Introduction to Google Apps Script

The Problem

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.

http://www.google.com/support/forum/p/Google+Docs/thread?tid=6fe8356595e9b859&hl=en

http://mashe.hawksey.info

Page 3: Introduction to Google Apps Script

Solution 1 - Filter the data

http://mashe.hawksey.info

Page 4: Introduction to Google Apps Script

Solution 2 – Google Apps Script

• Google Apps Script is a JavaScript cloud scripting language that provides easy ways to automate tasks across Google products and third party services.

http://code.google.com/googleapps/appsscript/

http://mashe.hawksey.info

Page 5: Introduction to Google Apps Script

How I see it

http://mashe.hawksey.info

Page 6: Introduction to Google Apps Script

How you write it

http://mashe.hawksey.info

• In Spreadsheets– Tools > Script Editor

• In Sites– Manage Site > Apps Script

Page 7: Introduction to Google Apps Script

How does the user interact with it

http://mashe.hawksey.info

• In Spreadsheets

• In Sites

• As a service

Page 8: Introduction to Google Apps Script

There’s good and bad news

http://mashe.hawksey.info

• Bad news– You need basic programming skills to write

• Good news– Already a growing number of developers

publishing scripts and tutorials for you to reuse e.g. http://www.flubaroo.com/

Page 10: Introduction to Google Apps Script

Explanation of Javascript Objects got by getValues()

Dog Cat Cow

22 31 15

6 5 5

data = [[“Dog”,”Cat”,”Cow”],[22, 31, 15],[6, 5, 5]]

data[0] = [“Dog”,”Cat”,”Cow”]

data[0][1] = “Cat”

http://mashe.hawksey.info

Page 11: Introduction to Google Apps Script

Useful links

http://mashe.hawksey.info

• Workshop material from Open4Ed– http://mashe.hawksey.info/2011/05/app-app-and-

away-workshop-handout-open4ed-gas/• Simple Apps Solutions (free Apps Script

consultancy for Ed and tutorials)– http://simpleappssolutions.com

• Official Apps Script Site– Docs– Tutorials– Help forum

Page 12: Introduction to Google Apps Script

Not forgetting your friendly EdTech explorer

http://mashe.hawksey.info

• Email: [email protected]• Blog: http://mashe.hawksey.info• Tweet: @mhawksey

Page 13: Introduction to Google Apps Script

Pseudo-code

  // onFormSubmit // get submitted data  // check if username has sheet  //    if not make  // copy submitted data to user's sheet

http://mashe.hawksey.info

Page 14: Introduction to Google Apps Script

Final code

function onFormSubmit() { // onFormSubmit // get submitted data var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Form_Sheet"); var headings = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); var lastRow = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues(); var studentUsername = lastRow[0][1]; // check if username has sheet if(ss.getSheetByName(studentUsername)){ var userSheet = ss.getSheetByName(studentUsername); // if not make } else { var userSheet = ss.insertSheet(studentUsername); userSheet.getRange(1, 1 , 1, headings[0].length).setValues(headings); } // copy submitted data to user's sheet userSheet.getRange(userSheet.getLastRow()+1, 1, 1, lastRow[0].length).setValues(lastRow);}

http://mashe.hawksey.info