google-apps-scriptautocompleteide

How to force auto complete in app script editor for ambiguous variables?


function onEdit(e)
  e... //I want auto completion
  var range = e.range();
  e.... //I want auto completion
}

How to force auto completion on a variable such as in onEdit, is there a way to explicitly specify what type e is for the purposes of auto completion?

  1. how to see what the datatype of e is.
  2. how to explicitly specify e is that datatype for auto completion in the google app script editor.

Solution

  • In the new Apps Script IDE you can use JSDoc for auto completion with built-in types.

    /**
     * Clears the content of the range, leaving the formatting intact.
     * @param {SpreadsheetApp.Range} range The range to clear the content
     * @return {SpreadsheetApp.Range} This range, for chaining.
     */
    function clearContent(range) {
      range.clearContent()
    }
    

    The Event object doesn't appear to have a built-in declaration, but you can always create your own.

    /**
     * @typedef {Object} onEditEvent 
     * @property {ScriptApp.AuthMode} authMode A value from the ScriptApp.AuthMode enum.
     * @property {string} oldValue Cell value prior to the edit, if any. Only available if the edited range is a single cell. 
     *                    Will be undefined if the cell had no previous content.
     * @property {SpreadsheetApp.Range} range A Range object, representing the cell or range of cells that were edited.
     * @property {SpreadsheetApp.Spreadsheet} source A Spreadsheet object, representing the Google Sheets file to which the script is bound.
     * @property {string} triggerUid ID of trigger that produced this event (installable triggers only).
     * @property {User} user A User object, representing the active user, if available (depending on a complex set of security restrictions).
     * @property {string} value New cell value after the edit. Only available if the edited range is a single cell.
     */
    
    
    /**
     * @param {onEditEvent} e The onEdit event.
     */
    function onEdit(e) {
      e... // type to show autocomplete 
    
    }
    

    The auto complete menu