0

Following these guides https://developers.google.com/apps-script/guides/rest/quickstart/target-script and https://developers.google.com/apps-script/guides/rest/quickstart/nodejs, I am trying to use the Execution API in node to return some data that are in a Google Spreadsheet.

I have set the script ID to be the Project Key of the Apps Script file. I have also verified that running the function in the Script Editor works successfully.

However, when running the script locally with node, I get this error:

The API returned an error: Error: ScriptError

I have also made sure the script is associated with the project that I use to auth with Google APIs as well.

Does anyone have any suggestion on what I can do to debug/ fix this issue? The error is so generic that I am not sure where to look.

UPDATE: I've included a copy of the code in this JSBin (the year function is the entry point) https://jsbin.com/zanefitasi/edit?js

UPDATE 2: The error seems to be caused by the inclusion of this line var spreadsheet = SpreadsheetApp.open(DriveApp.getFileById(docID));

2
  • Even though the error is generic please provide your code. We are not clairvoyants. Commented Feb 29, 2016 at 16:08
  • @dasjanik I was thinking more about debugging it, but I've updated the question to include the JSBin to a copy of the code Commented Feb 29, 2016 at 17:24

2 Answers 2

4

It seems that I didn't request the right scopes. The nodejs example include 'https://www.googleapis.com/auth/drive', but I also needed to include 'https://www.googleapis.com/auth/spreadsheets' in the SCOPES array. It seems like the error message ScriptError is not very informative here.

In order to find what scopes you'd need, to go the Script Editor > File > Project Properties > Scopes. Remember to delete the old credentials ~/.credentials/old-credential.json so that the script will request a new one.

Sign up to request clarification or add additional context in comments.

2 Comments

As you said, even when updating the scopes, it still won't re-authorize itself without actually deleting the credentials.
Just came across this issue and your post solved my problem. Thank you!
0

EDIT: With the update in information I took a closer look and saw you are returning a non-basic type. Specifically you are returning a Sheet Object.

The basic types in Apps Script are similar to the basic types in JavaScript: strings, arrays, objects, numbers and booleans. The Execution API can only take and return values corresponding to these basic types -- more complex Apps Script objects (like a Document or Sheet) cannot be passed by the API.

https://developers.google.com/apps-script/guides/rest/api

In your Account "Class"

this.report = spreadsheet.getSheetByName(data.reportSheet);

old answer:

'data.business_exp' will be null in this context. You need to load the data from somewhere. Every time a script is called a new instance of the script is created. At the end of execution chain it will be destroyed. Any data stored as global objects will be lost. You need to save that data to a permanent location such as the script/user properties, and reloaded on each script execution.

https://developers.google.com/apps-script/reference/properties/

6 Comments

I define data in the same file before year. Does that not work that way?
Yes if 'data' is defined "statically" then 'year' will have access to the 'data' object. You didn't show that in the jsbin example. 'error: Error: ScriptError' means there is a bug in the script itself then.
I've updated the jsbin to include what data looks like. I am not sure what bug there would be in the script, as running the year function locally works.
Instead of returning business_exp in the year function, I instead return business_exp.months, but still get the ScriptError. I thought if I just return this.months, which is an Array, it would not be a non-basic type?
Take a look at the node.js example: developers.google.com/apps-script/guides/rest/quickstart/nodejs. It shows how they do a dump of the entire stack trace when an error is returned.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.