【问题标题】:how to get rows in google spreadsheets如何在谷歌电子表格中获取行
【发布时间】:2020-12-08 12:40:11
【问题描述】:

我想获取 Google 电子表格中的所有行和列,以便我可以水平迭代它们,但每次我尝试获取行时都会出现错误,请帮助

这是我的代码:

const GoogleSpreadSheet = require('google-spreadsheet');
const creds = require('./client_secret.json');
const { promisify } = require('util');
const app = require('express')()

async function accessSpreadSheet() {
    const doc = new GoogleSpreadSheet('1UkcGReVvw5Ea1IbamdFc45PWZBKXEUJI6paxO_AWwMk');
    await promisify(doc.useServiceAccountAuth)(creds);
    const info = await promisify(doc.getInfo)()
    console.log(`Title: ${info.title}`)
    const rows = await promisify(info.worksheets.getRows({
        offset: 1
    }))
    console.log(rows)
}

app.get('/', async (req, res) => {
    const info = await accessSpreadSheet()
    res.send(info)
})

app.listen(3000, () => {
    console.log('served')
})

【问题讨论】:

  • 您收到哪个错误?另外,您使用的是服务帐号吗?
  • 是的,我正在使用服务器帐户
  • 错误是 getRows 不是函数
  • 但是如果你知道一种更高级的方法来获取行甚至单元格或列,只需发布​​它,我会很高兴的????
  • worksheets 有多个元素吗?请记住,getRows 是一种可以从工作表中调用的方法。

标签: javascript node.js google-sheets-api


【解决方案1】:

正如您在文档here 中看到的,getRows 方法是从单个工作表调用的。

因此,为了解决此问题,您必须遍历电子表格中的所有工作表,然后调用 getRows 方法或仅使用单个工作表。

示例

const sheet = doc.worksheets[0]; // the first sheet
const rows = await sheet.getRows();
console.log(rows[0].PROPERTY1); 

您也可以先查看Sheets API documentation 中的示例。

参考

【讨论】:

    【解决方案2】:

    我注意到您正在使用 javascript API。我通常使用 Python,但 Google Sheet API 是一样的。

    我猜你试图遍历工作表中包含数据的所有行。在这种情况下,为什么不使用 getDataRange(),然后遍历结果范围?

    官方参考有一个例子说明这是如何工作的: https://developers.google.com/apps-script/reference/spreadsheet/sheet#getDataRange()

    【讨论】:

    • 但我使用的是google-sheets npm 包
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2013-02-11
    相关资源
    最近更新 更多