当在电子表格中插入新值时,获取电子表格数据不会在客户端上更新

问题描述 投票:0回答:1

我正在使用google-spreadsheet npm软件包,并表示要从电子表格中检索所有行。在客户端,我正在使用访存并在控制台中记录所有行。

我正在使用带有名称/姓氏的简单电子表格。每当我尝试在电子表格的行中手动添加一些新名称时,我都必须重新启动服务器以查看更改后的行再次获取了数据。更改工作表后使用间隔再次调用api仍不会更新客户端上的数据。

是否有一种方法可以自动更新每次手动更改工作表中获取的数据,而无需每次都重新启动服务器?

index.js:

const GoogleSpreadsheet = require('google-spreadsheet')
const creds = require('./credentials.json')
const express = require('express')
app = express()

app.use(express.static('public'));


app.listen(3000, console.log('listening at http://localhost:3000/'))
var doc = new GoogleSpreadsheet('my spreadsheet ID');

// Authenticate with the Google Spreadsheets API.
doc.useServiceAccountAuth(creds, function (err) {

  // Get all of the rows from the spreadsheet.
  doc.getRows(1,  (err, rows) => {
    console.log(rows);
    app.get('/sheet', (req,res) =>{res.send(rows)})
  });
});

html:

<html>
<head>
<title>Title of the document</title>
</head>

<body>
The content of the document......
</body>

<script >
async function FetchFromSheet()
    {
        const response= await fetch('/sheet')
        const data = await response.json()
        console.log(data)
    }

    setInterval(() => {
        FetchFromSheet()
    }, 10000);
    </script>

</html>```



node.js express google-sheets-api
1个回答
0
投票

google-spreadsheet npm程序包可让您对Google表格进行API调用,以获取电子表格数据的最新版本。 API调用是根据您的定义进行的,默认情况下,并不意味着要跟踪工作表本身的更改。这就是为什么仅在重新启动服务器时才更新数据的原因,因为仅调用一次API(在启动时)]

您正在寻找的是Google Sheets webhook,它可以将POST请求发送到您要更改的工作表上的指定路由。实际上,您可以使用此Webhook请求的收据作为“提示”来刷新Google表格数据。

© www.soinside.com 2019 - 2024. All rights reserved.