Skip to content

Instantly share code, notes, and snippets.

@bferronato
Forked from rheajt/Code.gs
Created October 8, 2020 09:40
Show Gist options
  • Save bferronato/1d1029fe74efaac95efcf8aebb546557 to your computer and use it in GitHub Desktop.
Save bferronato/1d1029fe74efaac95efcf8aebb546557 to your computer and use it in GitHub Desktop.
examples of simple triggers with google apps script
/**
* 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
}
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);
}
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();
}
}
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