如何解决访问Google电子表格Node.js中的数据
我正在尝试访问电子表格中的数据
const { GoogleSpreadsheet } = require('google-spreadsheet');
const {promisify} = require('util');
const creds = require('./client_secret.json');
async function accessSpreadsheet() {
const doc = new GoogleSpreadsheet('1cErelgrkTYCOiEaTiFviF3M7chqMesmLBSp2rbxnFD4');
await promisify(doc.useServiceAccountAuth)(creds);
const info = await promisify(doc.getInfo)();
const sheet = info.worksheets[0];
console.log(`Title: ${sheet.title}`);
}
accessSpreadsheet();
它给了我错误
TypeError: Cannot set property 'jwtClient' of undefined
有什么主意吗?
解决方法
如果您有:
- 客户ID
- 客户机密
- 刷新令牌
然后,您只需按照下面给出的所有步骤访问节点JS中的任何Google电子表格数据:
1。 包括以下库:
const { google } = require('googleapis')
const { OAuth2 } = google.auth
2。 设置用户
const oAuth2Client = new OAuth2('your client ID','your client Secret')
oAuth2Client.setCredentials({
refresh_token: 'your Refresh Token'
});
3。 输入要访问的范围和工作表ID:
const sheets = google.sheets({version: 'v4',auth:oAuth2Client});
let result = await sheets.spreadsheets.values.get({
spreadsheetId: 'SpreadSheet Id You Want To Access',range:'A1:D3' //range you can specify according to you
});
4。 现在访问以json格式存储数据的结果对象:
console.log(result.data.values);
您将获得上述电子表格中的数据
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。