簡體   English   中英

如何在谷歌電子表格中獲取行

[英]how to get rows in google spreadsheets

我想獲取谷歌電子表格中的所有行和列,這樣我就可以水平迭代它們,但每次我試圖獲取行時,它都會給我一個錯誤,請幫忙

這是我的代碼:

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方法是從單個工作表中調用的。

因此,為了解決這個問題,您必須遍歷電子表格中的所有工作表,然后調用getRows方法或僅使用單個工作表。

例子

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

您還可以先查看Sheets API 文檔中的示例。

參考

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

我猜你試圖遍歷工作表中包含數據的所有行。 在這種情況下,為什么不使用 getDataRange(),然后遍歷結果范圍?

官方參考有一個關於這是如何工作的例子: https://developers.google.com/apps-script/reference/spreadsheet/sheet#getDataRange()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM