[英]How would I sort information from google-spreadsheet into an array using node.js
我最近一直在嘗試將我的一些數據庫轉換為谷歌表格,因此用戶可以使用谷歌表單提交內容,它會自動放入表格中,然后我可以使用 npm package 谷歌電子表格提取數據。 但我希望能夠將這些數據放在一個數組中,以便在我的 node.js 應用程序中使用。
下面的代碼基本上只是簡單地從文檔中獲取信息並為我預先顯示所有信息(至少我想要的列/行)
function factdoc(fact) {
msg.channel.send(`Fact: ${fact.facts}`);
msg.channel.send(`------------------------`);
}
async function accessSpreadsheet() {
const doc = new GoogleSpreadsheet('1QdEn0w02w-qcxHl5dLRn5Ld0IolbgFM9IV2TY2TPTSQ');
await promisify(doc.useServiceAccountAuth)(creds);
const info = await promisify(doc.getInfo)();
const sheet = info.worksheets[0];
const rows = await promisify(sheet.getRows)({
offset: 1
});
rows.forEach(row => {
factdoc(row);
})
}
if (msg.content == prefix + 'test') {
accessSpreadsheet();
msg.delete();
}
output 是這樣出來的
谷歌表看起來像這樣
所以它可以很好地閱讀谷歌表。 但是,在閱讀了文檔並觀看了一些教程之后,我不確定如何進一步了解它。
我想獲取該信息並將其存儲為一個數組,這樣如果我要輸入msg.channel.send(fact[0]);
例如,它將顯示test0
。 或者如果我要做一個隨機的事實,我可以做這樣的事情(顯然不完全像這樣)
if (msg.content == prefix + "randomfact"){
randomfact = Math.floor(Math.random() * sheet.rowCount);
msg.channel.send(fact[randomfact]);
}
然后它會計算行數,從而允許我將其放入math.random
並允許我從列表中打印一個隨機事實。 我已經搞砸了幾個小時並不斷破壞我的代碼。 我不確定如何實現文檔中的功能,因為我以前從未使用過此功能。
我讓它工作了,你需要做的就是刪除 for each,創建一個新變量並將factdoc
function 解析為行內容
function factdoc(fact) {
msg.channel.send(`Fact: ${fact.facts}`);
msg.channel.send(`------------------------`);
}
async function accessSpreadsheet() {
const doc = new GoogleSpreadsheet('1QdEn0w02w-qcxHl5dLRn5Ld0IolbgFM9IV2TY2TPTSQ');
await promisify(doc.useServiceAccountAuth)(creds);
const info = await promisify(doc.getInfo)();
const sheet = info.worksheets[0];
const rows = await promisify(sheet.getRows)({
offset: 1
});
row = factdoc(rows[arrayno#]); //new code
}
if (msg.content == prefix + 'test') {
accessSpreadsheet();
msg.delete();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.