我正在我的 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/