ScriptError 使用 Google Apps 脚本执行 API

ScriptError using Google Apps Script Execution API

按照这些指南 https://developers.google.com/apps-script/guides/rest/quickstart/target-script and https://developers.google.com/apps-script/guides/rest/quickstart/nodejs,我正在尝试使用节点中的执行 API 来 return Google 电子表格中的一些数据。

我已将脚本 ID 设置为 Apps 脚本文件的项目密钥。我还验证了 运行 脚本编辑器中的功能是否成功运行。

然而,当 运行 脚本在本地使用节点时,我得到这个错误:

The API returned an error: Error: ScriptError

我还确保该脚本与我用来通过 Google API 进行身份验证的项目相关联。

有人对我如何调试/修复此问题有任何建议吗?错误太普遍了,我不知道去哪里找。

更新:我在这个 JSBin 中包含了一份代码副本(year 函数是入口点) https://jsbin.com/zanefitasi/edit?js

更新 2:错误似乎是由包含这一行引起的 var spreadsheet = SpreadsheetApp.open(DriveApp.getFileById(docID));

编辑:随着信息的更新,我仔细查看了一下,发现您返回的是非基本类型。具体来说,您要返回一个 Sheet 对象。

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

在您的帐户中 "Class"

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

旧答案:

'data.business_exp' 在此上下文中将为空。您需要从某个地方加载数据。每次调用脚本时,都会创建一个新的脚本实例。在执行链的末尾,它将被销毁。任何存储为全局对象的数据都将丢失。您需要将该数据保存到永久位置,例如 script/user 属性,并在每次执行脚本时重新加载。

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

看来我没有要求正确的范围。 nodejs 示例包含 'https://www.googleapis.com/auth/drive',但我还需要在 SCOPES 数组中包含 'https://www.googleapis.com/auth/spreadsheets'。似乎错误消息 ScriptError 在这里提供的信息不多。

为了找到您需要的范围,请转到脚本编辑器 > 文件 > 项目属性 > 范围。请记住删除旧凭据 ~/.credentials/old-credential.json 以便脚本请求新凭据。