使用node.js查询google工作表并返回行



所以我正在构建一个discord bot,它像数据库一样使用谷歌表单。我希望用户能够输入一个具有唯一ID的命令,每一行都将具有该ID,然后返回所述行的所有值并将than存储为变量。随后将在其他地方使用。

用户将运行的命令是">读取(UNIQUEID(";,那么我希望机器人回复:";(UNIQUEID(已被找到";如果找到它;(UNIQUEID(找不到";。

然后用户将使用第二命令"0">上传";基本上将整行上传到一个新的电子表格中。

我有信心完成>我自己上传代码,但就我的生活而言,我不知道如何使用谷歌表单api进行查询。为了参考以下内容,我附上了一张我想阅读的纸张图像。

从中读取的谷歌表单

谷歌表单中也会有更多的信息,但出于测试目的,我只留下了一个条目。

编辑1:我一直在研究谷歌可视化API,我做了一些研究,但我不确定这是否真的适用于纸张?从一些文档来看,它似乎是针对图表的。。

我从另一个stackoverflow问题中找到了问题的答案,@Tanaike也回答了这个问题。下面是代码。

const request = require("request");
const csvParse = require("csv-parse");
const spreadsheetId = "DOCUMENT_ID";  // Document ID
const sheetId = "SHEET_ID";  // sheet ID
const searchId = `${QUERY}`;  // what im searching for

spread.getRequestHeaders().then((authorization) => {
const query = `select * where K='${searchId}'`; // K can be changed to the col the data is on
const url = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:csv&gid=${sheetId}&tq=${encodeURI(query)}`;
let options = {
url: url,
method: "GET",
headers: authorization,
};
request(options, (err, res, result) => {
if (err) {
console.log(err);
return;
}
if (result === ("")) {
return message.channel.send(``${QUERY}` Did not match to any records.`); //ID didnt match
} else {
message.channel.send(``${QUERY}` has been matched with; ${result}`) //confirms ID has matched
csvParse(result, {}, (err, ar) => console.log(ar)) // console logs results
````

最新更新