javascript - Sheets JavaScript API 未捕获错误

标签 javascript typescript aurelia google-sheets-api google-api-js-client

我正在我的 Aurelia 应用程序中使用新版本的 Google Sheets API(我正在使用 TypeScript)。我已成功登录用户并询问所需的范围,但当我尝试获取电子表格的值时,API 抛出以下错误:

cb=gapi.loaded_0:269 Uncaught Error: arrayForEach was called with a non array value(…)

这是我初始化 API 并登录用户的方式:

gapi.load("auth2", () => {
    gapi.auth2.init({
       'client_id': this.clientId,
       'scope': this.scopes.join(' ')
    }).then(() => {
           this.auth = gapi.auth2.getAuthInstance();
           this.auth.signIn().then(response => {
               gapi.load("client", () => {
                     gapi.client.load("sheets", "v4", () => {
                         gapi.client.sheets.spreadsheets.values.get({
                             spreadsheetId: this.testSheet,
                             range: 'Class Data!A2:E'
                          }).then((response) => {console.log(response);});
                     });
               });
           })
    });
});

scopes 变量是一个范围数组:

scopes: string[] = ["https://www.googleapis.com/auth/spreadsheets.readonly", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive.readonly", "https://www.googleapis.com/auth/drive"]

用户登录成功,我什至可以得到他们的姓名和电子邮件。加载 Sheets API 后,我检查了“gapi.client”并且“sheets”对象在那里,但是当我尝试从电子表格获取数据时,它失败并出现上述错误。我运行的示例是针对 this 中使用的 Google 公共(public)电子表格。例子。另请注意,我的客户端 ID 没有任何问题,因为我已经用它运行了 Google 的示例代码,并且运行良好。

最佳答案

您的工作表是否名为Class Data?如果是,则可能需要指定范围,如下所示:

range: "'Class Data'!A2:E"

在 Sheets UI 和 Apps 脚本中确实如此,我试图记住我在 JS 客户端库中是否也经历过同样的情况。

关于javascript - Sheets JavaScript API 未捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704962/

相关文章:

javascript - .each() 搜索背景颜色

javascript - 在 chrome 的不同框架中访问 javascript 变量

javascript - 初始化 javascript 数字的最佳方法是什么?

typescript - Jest 和 TypeScript 的不完整 stub

javascript - 如何在 Angular 类中表示嵌套的 JSON?

typescript - 是否可以在 aurelia View 模型中指定项目根目录的路径?

javascript - 如何仅使用 CSS 修复网页中的第一行和第一列

typescript - angularjs2中的indexeddb实现

typescript - Aurelia/Typescript - 如何通过选择标签将对象绑定(bind)到变​​量

javascript - promise 回调中的单元测试逻辑