【发布时间】:2020-05-11 17:06:08
【问题描述】:
我正在使用 google-spreadsheet 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>```
【问题讨论】:
标签: node.js express google-sheets-api