- Created Apps Script via Google Sheets menu Extensions > Apps Script
- In Apps Script created a custom function like that:
function TEST() {
var test = 10;
return test;
}
- Used created function in Google Sheets cell like: TEST()
- Acquired an error "No such user (line 0)."
Any thoughts on this?
CodePudding user response:
The error says
No such user (line 0).
Notice that the error is on line 0. It means the function you're showing in your question is NOT the problem. The problem is because of other code in other files, which are invoked before running this function. Common culprits are
- Immediately invoked functions
- Global variables attempting to access privileged resources
CodePudding user response:
At runtime Google Apps Script, loads all the files before actually running the invoked function, automatically assigning an authorization mode. When running a custom function the authorization mode is "CUSTOM_FUNCTION" that impose several limitations. Because of this:
On projects having custom functions (and also when having simple triggers), avoid calling any method that requires authorization on the global scope. As a general rule of thumb for people starting with Google Apps Script, avoid making variable declarations using Google Apps Script methods. It's safe to assign literals primitives (i.e. numbers, strings, etc.) and object literals.
Avoid calling functions in the global scope, i.e. avoid writing
myFunction()
and(() => something )()
.When creating a "mcve" start from scratch, meaning create a new spreadsheet, create the bound project, then only add the custom function code. If necessary add the other code lines starting by the global variables.
NOTE: Bound projects are some sort of "light" editor add-on. If you will be working complex bound projects, checkout the Editor add-on documentation, like the article "Editor add-on authorization" (link included in the references section). The main functional difference with "full" editor add-ons is that the bound project simple triggers only works with the bounded document.
References