NodeJS 访问 Google 个电子表格
NodeJS accessing Google SpreadSheets
我制作了这段代码 (index.js):
const { GoogleSpreadsheet } = require('google-spreadsheet')
const credentials = require('./credentials.json')
const { promisify } = require('util')
const docId = '1lrGt8C9gjrabhVjGYoFh-q40rLuRHz6u9vRZqVeLG-c'
const accessSheet = async() => {
const doc = new GoogleSpreadsheet(docId)
await promisify(doc.useServiceAccountAuth)(credentials)
const info = await promisify(doc.getInfo)()
const worksheet = info.worksheets[0]
const rows = await promisify(worksheet.getRows)({
})
rows.forEach(row => {
console.log(row.Title)
})
}
accessSheet()
我正在尝试将我的代码与 google 电子表格连接起来。对于连接,我正在使用 node.js,但我收到此错误消息。
C:\Users\rodrigo.pasini\Desktop\robo_grpr\node_modules\google-spreadsheet\lib\GoogleSpreadsheet.js:101
this.jwtClient = new JWT({
^
TypeError: Cannot set properties of undefined (setting 'jwtClient')
at useServiceAccountAuth (C:\Users\rodrigo.pasini\Desktop\robo_grpr\node_modules\google-spreadsheet\lib\GoogleSpreadsheet.js:101:20)
at node:internal/util:360:7
at new Promise (<anonymous>)
at useServiceAccountAuth (node:internal/util:346:12)
at accessSheet (C:\Users\rodrigo.pasini\Desktop\robo_grpr\index.js:9:47)
at Object.<anonymous> (C:\Users\rodrigo.pasini\Desktop\robo_grpr\index.js:19:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
有谁知道我为什么会收到此错误?我做错了什么?
当我看到您的显示脚本和错误消息时,我担心您使用的 google-spreadsheet 当前版本可能与您的显示脚本不同。因此,在这个答案中,我想提出一个使用最新版本 google-spreadsheet.
从您的显示脚本转换而来的示例脚本
当我查看google-spreadsheet的最新版本时,似乎是3.2.0。所以,首先,请检查您使用的 google-spreadsheet 的版本。并且,请安装最新的。
当你的脚本被修改为使用最新版本(v3.2.0)时,它变成如下。
修改后的脚本:
const { GoogleSpreadsheet } = require("google-spreadsheet");
const creds = require('./credentials.json');
const doc = new GoogleSpreadsheet('###'); // Please set your Spreadsheet ID.
const accessSheet = async () => {
await doc.useServiceAccountAuth(creds);
await doc.loadInfo();
const worksheet = doc.sheetsByIndex[0];
const rows = await worksheet.getRows();
rows.forEach((row) => {
console.log(row.Title);
});
};
accessSheet();
参考:
我制作了这段代码 (index.js):
const { GoogleSpreadsheet } = require('google-spreadsheet')
const credentials = require('./credentials.json')
const { promisify } = require('util')
const docId = '1lrGt8C9gjrabhVjGYoFh-q40rLuRHz6u9vRZqVeLG-c'
const accessSheet = async() => {
const doc = new GoogleSpreadsheet(docId)
await promisify(doc.useServiceAccountAuth)(credentials)
const info = await promisify(doc.getInfo)()
const worksheet = info.worksheets[0]
const rows = await promisify(worksheet.getRows)({
})
rows.forEach(row => {
console.log(row.Title)
})
}
accessSheet()
我正在尝试将我的代码与 google 电子表格连接起来。对于连接,我正在使用 node.js,但我收到此错误消息。
C:\Users\rodrigo.pasini\Desktop\robo_grpr\node_modules\google-spreadsheet\lib\GoogleSpreadsheet.js:101
this.jwtClient = new JWT({
^
TypeError: Cannot set properties of undefined (setting 'jwtClient')
at useServiceAccountAuth (C:\Users\rodrigo.pasini\Desktop\robo_grpr\node_modules\google-spreadsheet\lib\GoogleSpreadsheet.js:101:20)
at node:internal/util:360:7
at new Promise (<anonymous>)
at useServiceAccountAuth (node:internal/util:346:12)
at accessSheet (C:\Users\rodrigo.pasini\Desktop\robo_grpr\index.js:9:47)
at Object.<anonymous> (C:\Users\rodrigo.pasini\Desktop\robo_grpr\index.js:19:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
有谁知道我为什么会收到此错误?我做错了什么?
当我看到您的显示脚本和错误消息时,我担心您使用的 google-spreadsheet 当前版本可能与您的显示脚本不同。因此,在这个答案中,我想提出一个使用最新版本 google-spreadsheet.
从您的显示脚本转换而来的示例脚本当我查看google-spreadsheet的最新版本时,似乎是3.2.0。所以,首先,请检查您使用的 google-spreadsheet 的版本。并且,请安装最新的。
当你的脚本被修改为使用最新版本(v3.2.0)时,它变成如下。
修改后的脚本:
const { GoogleSpreadsheet } = require("google-spreadsheet");
const creds = require('./credentials.json');
const doc = new GoogleSpreadsheet('###'); // Please set your Spreadsheet ID.
const accessSheet = async () => {
await doc.useServiceAccountAuth(creds);
await doc.loadInfo();
const worksheet = doc.sheetsByIndex[0];
const rows = await worksheet.getRows();
rows.forEach((row) => {
console.log(row.Title);
});
};
accessSheet();