How to type hint Google Types in Google Scripts Library?

Viewed 620

I asked a question and through the answer found I had not asked the right question!

Say LibraryFoo is a library and if I use that elsewhere as a library, I get type completion. For example, if I have

/**
 *  Lorem ipsum
 *
 *  @return {Object[]}
 *
 */
function bar() {}

in LibraryFoo and if in the other Google Script I type LibraryFoo.bar(). I get suggestions for an array like forEach and map. However, if instead I have

/**
 *  Lorem ipsum
 *
 *  @return {Sheet}
 *
 */
function barring() {}

and I type LibraryFoo.barring(). I do not get suggestions like getRange or getMaxColumns. I know that using clasp and local IDE might make more sense, but one of two seems to be the case:

  1. I am using the wrong name/reference (though when Google's own functions are typed, they simply say "Sheet". Is the source code for e.g. SpreadsheetApp available? That would maybe be a hint, or not be in proper GAS).
  2. This is not possible, because Google have done something I/we cannot do.

Regardless, despite searching and trying different ways to namespace the Sheet reference, I have not found out whether it is 1. or 2. (or something else!).

[EDIT: I have made a feature request to Google, still unsure which of 1./2./something else it is]

2 Answers

This is supported in the new editor, and an example of the syntax is as follows:

/**
 *  Returns price list data from the Stock tab/sheet
 *
 *  @param {number} index 
 *  @return {SpreadsheetApp.Range}
 *
 */
function getRange() {
  return SpreadsheetApp.getActiveRange();
}

function foo() {
  getRange().
}

and you will get completion after the . in getRange(). for a Range object.

This works as well if the function comes from a library.

Consideration

This is not currently possible to achieve in the Apps Script Editor.

There are a few open feature requests on Google Issuetracker about this:

Support Object Oriented Interfaces with documentation for Libraries

Full jsdoc for classes and methods

There is also a new feature request submitted as a result of the discussion - star it or the other ones mentioned above to increase the chances of the feature being implemented any time soon.

JSDoc is not fully supported in Google Apps Script, and autocompletion doesn't work further than the top-level definition at the current state. This applies to custom classes and to built-in classes like Spreadsheet, Sheet, Range as the autocompletion here would require a second level iteration.

Related