问题
I'm playing around with the new version of the Google Sheets API in my Aurelia application (I'm using TypeScript). I've managed to login the user and ask for the required scopes but when I'm trying to get the values of a spreadsheet the API throws the following error:
cb=gapi.loaded_0:269 Uncaught Error: arrayForEach was called with a non array value(…)
This is how I initialize the API and login the user:
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);});
});
});
})
});
});
The scopes
variable is an array of 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"]
The user logs in successfully, I can even get their name and email. After loading the Sheets API I've checked `gapi.client' and the 'sheets' object is there but when I try to get data from a spreadsheet it fails with the above error. The example I'm running is against Google's public spreadsheet that is used in this example. Also note that there is nothing wrong with my client id as I've run Google's sample code with it and it works fine.
回答1:
Is your Sheet named Class Data?, if so, it might be necessary to specify the range as follows:
range: "'Class Data'!A2:E"
This is certainly true within the Sheets UI and Apps Scripts, and I'm trying to remember if I experienced the same with the JS client library.
来源:https://stackoverflow.com/questions/40704962/sheets-javascript-api-uncaught-error