1
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.
CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • 1
    Generally this is only available for already-typed items e.g. the return values of natively defined methods. So the "solution" is to use a tool like `clasp` and work on your code from an editor that supports type deduction – tehhowch Nov 18 '19 at 04:43
  • @tehhowch that's a shame, I seem to be able to glitch it to work sometimes by forcibly declaring known objects with the same name but sometimes it backfires. – CodeCamper Nov 18 '19 at 04:58
  • 1
    Do yourself a favor and use [tag:clasp]. You can use jsdoc or typescript for auto completion with built-in type files. – TheMaster Nov 18 '19 at 05:55
  • 1
    Does this answer your question? [Enabling autocomplete for Google Apps Script in locally-installed IDE](https://stackoverflow.com/questions/49015874/enabling-autocomplete-for-google-apps-script-in-locally-installed-ide) – TheMaster Nov 18 '19 at 05:56

2 Answers2

5

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

  • There is an Events namespace defined, [in this file in the DefinitelyTyped repository](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/google-apps-script/google-apps-script-events.d.ts). These types in general seem to correspond to what is accessible in the online editor. Did you look here for the type? I think there is a good chance the type is accessible in the online editor somehow, but I can't seem to find it right now – Jacob Akkerboom Feb 03 '23 at 12:19
  • @JacobAkkerboom Yes, I use these definitions for clasp and VSCode, but on the Apps Scrip IDE I couldn't figure out how to integrate them. They are still a good starting point for deriving types for use with JSDOC – Andrea Leardini Feb 17 '23 at 15:42
1

I'm posting this as community wiki for documentation purposes.

As others already said in comments, you cannot use autocomplete for event objects using Apps Script editor.

The workaround, then, also as referenced by other users, would be to use clasp and work with an IDE with which you can enable autocomplete.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27