-
-
Save bferronato/1d1029fe74efaac95efcf8aebb546557 to your computer and use it in GitHub Desktop.
examples of simple triggers with google apps script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* These simple triggers are available in Sheets, Docs, and Forms | |
* Most of this information can be found: | |
* https://developers.google.com/apps-script/guides/triggers/events | |
*/ | |
function onOpen(e) { | |
// { | |
// authMode: 'LIMITED', | |
// source: 'Spreadsheet' || 'Document' || 'Form', | |
// user: 'User' | |
// } | |
var user = e.user; | |
var authMode = e.authMode; | |
var source = e.source; | |
// SpreadsheetApp.getActiveSpreadsheet() | |
// DocumentApp.getActiveDocument() | |
// FormApp.getActiveForm() **This is triggered when the form is opened for editing | |
} | |
function onInstall(e) { | |
// This event is used when developing add-ons | |
// { authMode: 'LIMITED' or 'FULL' } | |
// You can run other simple triggers here | |
onOpen(); | |
} | |
/** | |
* onEdit is a simple trigger that is available in Sheets | |
*/ | |
function onEdit(e) { | |
// event object parameters: | |
// authMode | |
// source | |
// user | |
// range | |
// value | |
// oldValue | |
} | |
/** | |
* doGet and doPost are simple triggers that are used by web apps built with apps script | |
* They pass an event object that is similar | |
*/ | |
function doGet(e) { | |
e.queryString; | |
// The value of the query string portion of the URL, or null if no query string is specified | |
// ex. name=alice&n=1&n=2 | |
e.parameter; | |
// An object of key/value pairs that correspond to the request parameters. | |
// Only the first value is returned for parameters that have multiple values. | |
// ex. {"name": "alice", "n": "1"} | |
e.parameters; | |
// An object similar to e.parameter, but with an array of values for each key | |
// ex. {"name": ["alice"], "n": ["1", "2"]} | |
// You can read more about the rest in the documentation | |
// https://developers.google.com/apps-script/guides/web | |
e.contextPath; | |
e.contentLength; | |
e.postData.length; | |
e.postData.type; // the MIME type of the POST body | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doGet(e) { | |
// var user = e.parameter.user || 'Anonymous'; | |
var user = e.parameter.user || Session.getActiveUser().getEmail(); | |
var age = e.parameter.age || 'ageless'; | |
return HtmlService.createHtmlOutput(user + ' is aged ' + age); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onEdit(e) { | |
var row = e.range.getRow(); | |
var column = e.range.getColumn(); | |
// You don't always have to use the event object to target cells | |
var adjacentCell = SpreadsheetApp.getActiveSheet().getRange(row, column + 1); | |
var newValue = (typeof e.value !== 'object') ? e.value : 0; | |
var oldValue = e.oldValue; | |
if(newValue && newValue > oldValue) { | |
adjacentCell.setValue('New value increased'); | |
} else if(newValue && newValue < oldValue) { | |
adjacentCell.setValue('New value decreased'); | |
} else if(newValue && newValue == oldValue) { | |
adjacentCell.setValue('Values the same'); | |
} else { | |
adjacentCell.clear(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen(e) { | |
var today = new Date(); | |
// A really annoying thing is there is no autocomplete on event objects! | |
var body = e.source.getBody(); | |
body.appendParagraph(today.toDateString() + ' - ' + today.toTimeString()) | |
.appendHorizontalRule(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment