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



我使用谷歌电子表格npm包和express从我的电子表格中检索所有行。在我的客户端,我使用fetch并记录控制台中的所有行。

我使用的是一个简单的带有姓名的电子表格。每当我试图手动在电子表格中的行中添加一些新名称时,我都必须重新启动服务器,才能看到更改后的行再次获取数据。在更改工作表后使用间隔再次调用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>```

google-spreadsheet-npm包允许您对GoogleSheets进行API调用,以获取最新版本的电子表格数据。API调用是根据您的定义进行的,默认情况下,并不用于跟踪工作表本身的更改。这就是为什么只有当你重新启动服务器时,你的数据才会更新,因为你只调用API一次(启动时(

您正在寻找的是Google Sheets webhook,它可以在更改的工作表上向您指定的路由发送POST请求。你基本上可以使用这个webhook请求的收据作为"提示"来刷新你的谷歌表单数据。

相关内容

最新更新