Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created August 1, 2017 08:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rheajt/34ec8ccc712f0382e6c48728b76fd18b to your computer and use it in GitHub Desktop.
Save rheajt/34ec8ccc712f0382e6c48728b76fd18b 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();
}
@akhilshuklaji
Copy link

can we add trigger for every new row added by response from a google form?

@dfglamit
Copy link

Hello, dont work e.user :( .. where is the "onEdit" documentation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment