Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created August 1, 2017 08:16

Revisions

  1. rheajt created this gist Aug 1, 2017.
    66 changes: 66 additions & 0 deletions Code.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    /**
    * 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

    }
    8 changes: 8 additions & 0 deletions doGet-and-doPost.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    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);
    }
    20 changes: 20 additions & 0 deletions onEdit.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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();
    }
    }
    9 changes: 9 additions & 0 deletions onOpen.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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();
    }