GMath314
GMath314

Reputation: 73

Autocomplete Not Working - Google App Script

I'm having trouble with the autocomplete feature in Google App Script.

Built-in methods like SpreadsheetApp. will provide an autocomplete menu with methods to choose from.

However, if I create my own child object, autocomplete works for a little while, and then it just stops working.

for example:

var skywardRoster = SpreadsheetApp.getActiveSheet();

skywardRoster. will produce method options for a while, and then it stops.

However, the code still functions, and methods work if I type them out manually, so I know the declarations must be right. The menu simply won't appear, and it's just very inconvenient to have to look up each method individually as I go.

I have tried: breaking the variable and retyping that line; copy and pasting the code back into the editor; using different browsers; copying the gs file itself and working within the copy; and signing out completely and signing back in. Nothing seems to get it back to work.

I'm really new to coding, and I'm not sure what can be causing this.

Does anyone know how to fix this issue?

Upvotes: 4

Views: 14081

Answers (4)

dmitry1100
dmitry1100

Reputation: 1329

I was looking for a way how to improve Google Apps Script development experience. Sometimes autocomplete misses context. For example for Google Spreadsheet trigger event parameters. I solved the problem by using clasp and @ts-check.

clasp allows to edit sources in VS Code on local machine. It can pull and push Google Apps Script code. Here is an article how to try it.

When you move to VS Code and setup environment you can add //@ts-check in the beginning of the JavaScript file to help autocomplete with the special instructions. Here is the instructions set.

My trigger example looks like this (notice autocompletion works only in VS Code, Google Apps Script cloud editor doesn't understand @ts-check instruction):

//@ts-check

/**
 * @param {GoogleAppsScript.Events.SheetsOnEdit} e
 */
function onEditTrigger(e) {
    var spreadsheet = e.source;
    var activeSheet = spreadsheet.getActiveSheet();
    Logger.log(e.value);
}

Upvotes: 2

JTV
JTV

Reputation: 21

I agree, Google Script's autocomplete feature is pretty poor comparing with most of other implementations. However the lack is uderstandable in most cases and sometimes the function can be preserved.

Loosing context

The autocomplete is limited to Google objects (Spreasheets, Files, etc.). When working with them you get autocomplete hints, unless you pass such object instance to function as an argument. The context is lost then and the editor will not give you suggestions inside the called function. That is because js doesn't have type control.

You can pass an id into the function instead of the object (not File instance but fileId) and get the instance inside of the function but in most cases such operation will slow the script.

Better solution by Cameron Roberts

Cameron Roberts came with something what could be Goole's intence or a kind of hack, don't know. At the beginning of a function assign an proper object instance to parameter wariable and comment it to block:

function logFileChange(sheet, fileId){
  /*
    sheet = SpreadsheetApp.getActiveSheet();
  */
  sheet.appendRow([fileId]); // auto completion works here
}

Auto completion preserved

Upvotes: 0

Mitemmetim
Mitemmetim

Reputation: 742

Since this was the first result on google for a non-working google script autocompletion, I will post my solution here as it maybe helps someone in the future.

The autocompletion stopped working for me when I assigned a value to a variable for a second time.

Example:

var cell = tableRow.appendTableCell();
...
cell = tableRow.appendTableCell();

So maybe create a new variable for the second assignment just during the implementation so that autocompletion works correctly. When you are done with the implementation you can replace it with the original variable.

Example:

var cell = tableRow.appendTableCell();
...
var replaceMeCell = tableRow.appendTableCell(); // new variable during the implementation

And when the implementation is done:

var cell = tableRow.appendTableCell();
...
cell = tableRow.appendTableCell(); // replace the newly created variable with the original one when you are done

Hope this helps!

Upvotes: 2

Mr.Rebot
Mr.Rebot

Reputation: 6791

You might want to check Built-in Google Services:Using autocomplete:

The script editor provides a "content assist" feature, more commonly called "autocomplete," which reveals the global objects as well as methods and enums that are valid in the script's current context. To show autocomplete suggestions, select the menu item Edit > Content assist or press Ctrl+Space. Autocomplete suggestions also appear automatically whenever you type a period after a global object, enum, or method call that returns an Apps Script class. For example:

  • If you click on a blank line in the script editor and activate autocomplete, you will see a list of the global objects.
  • If you type the full name of a global object or select one from autocomplete, then type . (a period), you will see all methods and enums for that class.
  • If you type a few characters and activate autocomplete, you will see all valid suggestions that begin with those characters.

Upvotes: 3

Related Questions