Google 节点未定义工作表客户端
Google Sheets Client undefined with Node
我在获取节点中的 sheets 对象时遇到问题。我能够进行身份验证,但是当我尝试读取给定的 sheet、client.sheets
returns 未定义时。这很奇怪,因为 sheets 对象填充了数据,如下所示。即使我像 readSheets(gapi.client)
一样传入客户端并将变量记录在 readSheets 中,它仍然 returns 未定义。为什么会这样?
var CLIENT_ID = 'MYID.apps.googleusercontent.com';
var API_KEY = 'MYAPIKEY';
var DISCOVERY_DOCS = ["https://sheets.googleapis.com/$discovery/rest?version=v4"];
var SCOPES = "https://www.googleapis.com/auth/spreadsheets";
gapi.load('client', {
callback: function() {
initClient();
},
onerror: function() {
alert('gapi.client failed to load!');
}
});
const initClient = () => {
console.log(gapi.client)
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
});
readSheets();
}
function readSheets() {
console.log(gapi.client.sheets); // undefined
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: 'MySpreadsheetID',
majorDimension: "COLUMNS",
range: 'Sheet1!A1:B2',
}).then(function(response) {
console.log(response.result.values)
}, function(response) {
console.log('Error: ' + response.result.error.message);
});
}
gapi.client
然后在控制台中 gapi.client.sheets
:
您不应该在后端(在 NodeJS 环境中)使用浏览器又名客户端又名 GAPI。
对于 NodeJS,您应该使用 this library - googleapis。
对于浏览器,您应该使用 GAPI (https://apis.google.com/js/api.js). If you want to use TypeScript with GAPI - you should install @types/gapi.client.sheets or generate typing yourself using google-api-typings-generator。
有关这两个用例之间差异的更多详细信息,请参阅:JavaScript VS NodeJS Google API Clients
我在获取节点中的 sheets 对象时遇到问题。我能够进行身份验证,但是当我尝试读取给定的 sheet、client.sheets
returns 未定义时。这很奇怪,因为 sheets 对象填充了数据,如下所示。即使我像 readSheets(gapi.client)
一样传入客户端并将变量记录在 readSheets 中,它仍然 returns 未定义。为什么会这样?
var CLIENT_ID = 'MYID.apps.googleusercontent.com';
var API_KEY = 'MYAPIKEY';
var DISCOVERY_DOCS = ["https://sheets.googleapis.com/$discovery/rest?version=v4"];
var SCOPES = "https://www.googleapis.com/auth/spreadsheets";
gapi.load('client', {
callback: function() {
initClient();
},
onerror: function() {
alert('gapi.client failed to load!');
}
});
const initClient = () => {
console.log(gapi.client)
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
});
readSheets();
}
function readSheets() {
console.log(gapi.client.sheets); // undefined
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: 'MySpreadsheetID',
majorDimension: "COLUMNS",
range: 'Sheet1!A1:B2',
}).then(function(response) {
console.log(response.result.values)
}, function(response) {
console.log('Error: ' + response.result.error.message);
});
}
gapi.client
然后在控制台中 gapi.client.sheets
:
您不应该在后端(在 NodeJS 环境中)使用浏览器又名客户端又名 GAPI。
对于 NodeJS,您应该使用 this library - googleapis。
对于浏览器,您应该使用 GAPI (https://apis.google.com/js/api.js). If you want to use TypeScript with GAPI - you should install @types/gapi.client.sheets or generate typing yourself using google-api-typings-generator。
有关这两个用例之间差异的更多详细信息,请参阅:JavaScript VS NodeJS Google API Clients