Search code examples
google-apps-scriptlibrariesjsdoc

How to type hint Google Types in Google Scripts Library?


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]


Solution

  • 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.