2

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]

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Cryvate
  • 321
  • 3
  • 13
  • Is this about using only he Google Apps Script IDE ? Have considered using another IDE? – Rubén Oct 28 '20 at 13:47
  • Possible duplicate [Is it possible to get the autocomplete functionality in AppsScript's script editor to work on custom classes?](https://stackoverflow.com/q/28917654/1595451) – Rubén Oct 28 '20 at 15:46
  • @Rubén: yes, this is about the Google Apps Script IDE. I have considered it but this has to be (at least at a basic level) be easily updated/debugged as required by the next person as this code is used by an amateur sports club – Cryvate Oct 28 '20 at 15:46
  • @Ruben: I don't think it is a duplicate, because that's about getting *any* autocompletion on a particular class, but we know in this case that autocompletion does exist for this class because it works when you e.g. type `SpreadsheetApp.getActiveSheet().` things like `getRange` come up. – Cryvate Oct 28 '20 at 15:49
  • It's still a second level type – Rubén Oct 28 '20 at 15:53
  • 1
    Does this answer your question? [Is it possible to get the autocomplete functionality in AppsScript's script editor to work on custom classes?](https://stackoverflow.com/questions/28917654/is-it-possible-to-get-the-autocomplete-functionality-in-appsscripts-script-edit) – Alessandro Oct 28 '20 at 16:05
  • Aren't you a tenacious one? :) JSDoc does not magically "just work" - without a tag parser, it is simply a bunch of comments (for example, VS Code uses TypeScript + `@types/google-apps-script` npm package to provide autocompletion). It is not like you can *guess* the type correctly if it is not explicitly allowed to be "typed" by Google. The online IDE does provide the basic type inference and autocompletion, but nothing too complex, especially when it comes to Libraries – Oleg Valter is with Ukraine Oct 28 '20 at 17:29
  • 1
    You might want to create a Feature request on issuetracker to get Google employees to support jsdoc for Google types or provide the secret jsdoc strings you're looking for. See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for more details. – TheMaster Oct 28 '20 at 18:58
  • It does seem weird to me this is a problem, but as I said, I am not too familiar with JSDOC, so maybe I am just wrong... Anyway, I filled this: https://issuetracker.google.com/issues/171898327 – Cryvate Oct 28 '20 at 20:01
  • [Edit] your question with the link or add the link as a answer. In addition it'll help Google if you link this question in your feature request as well. – TheMaster Oct 28 '20 at 20:22
  • @Cryvate - without Google's explicit support for such things, JSDoc typing will not help you, unfortunately. That said, it is nice that you submitted a feature request (although since the new editor is on the horizon, I doubt this will be implemented (but who knows, let's see). – Oleg Valter is with Ukraine Oct 29 '20 at 02:34

2 Answers2

5

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.

Cryvate
  • 321
  • 3
  • 13
  • Can you also typehint variables? Like `/** @var {SpreadsheetApp.Range} */ this.range = SpreadsheetApp.getActiveRange();` ? – Simon East Oct 22 '21 at 08:45
  • @SimonEast Unclear to me whether you mean "adding an annotation to a variable to help the IDE know what type it is" which does not seem to work (with the syntax you suggested), if you mean, tab completion working for `this.range` knowing it is a `SpreadsheetApp.Range`, yes, that does work (and has worked in the last editor too, which is why this functionality of typing your own functions being missing being annoying). – Cryvate Oct 23 '21 at 13:13
  • Can you show example with custom module in library? In order to get completion after `library.MyModule.` where is function `run` witn object `{}` result. – Viewed Apr 10 '22 at 07:44
  • @Viewed can you give an example of exactly how to set it up? Googlescript is not my main programming environment and javascript not my main language, so I need a bit more context! – Cryvate Apr 14 '22 at 13:29
  • @Cryvate https://gist.github.com/Chimildic/07376823f562481884ad408de03b693e – Viewed Apr 14 '22 at 15:39
3

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.

Alessandro
  • 2,848
  • 1
  • 8
  • 16
  • I am not using classes and methods, I am using bare functions, so the first link does not apply insofar as I can see. – Cryvate Oct 28 '20 at 16:23
  • "This applies to custom classes and to Apps Script classes like Spreadsheet, Sheet, Range as the autocompletion here would require a second level iteration." What do you mean by second level iteration? As I said in the question, when I acquire a range (e.g. `SpreadsheetApp.getActiveSpreadsheet()`) and then type a dot, it will give me type completion, so how is that happening and how is that first iteration and not second iteration? – Cryvate Oct 28 '20 at 16:25
  • 1
    You are using bare functions, but the autocompletion software will have to access the Classes definitions to suggest the right methods. – Alessandro Oct 28 '20 at 16:31