简体   繁体   English

如何使用 node.js 将 google-spreadsheet 中的信息排序到数组中

[英]How would I sort information from google-spreadsheet into an array using node.js

I recently have been trying to convert some of my database to a google sheet, so users can submit things with a google form and it will automatically be put into a sheet, then I can pull the data using the npm package google-spreadsheet.我最近一直在尝试将我的一些数据库转换为谷歌表格,因此用户可以使用谷歌表单提交内容,它会自动放入表格中,然后我可以使用 npm package 谷歌电子表格提取数据。 But I want to be able to put that data in an array to use in my node.js application.但我希望能够将这些数据放在一个数组中,以便在我的 node.js 应用程序中使用。

The below code basically simply gets the info off the document and displays it all upfront for me (at least the columns/rows I want)下面的代码基本上只是简单地从文档中获取信息并为我预先显示所有信息(至少我想要的列/行)

    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();
    }

the output comes out like this output 是这样出来的

在此处输入图像描述

and the google sheet looks like this谷歌表看起来像这样

在此处输入图像描述

So it's reading off the google sheet fine.所以它可以很好地阅读谷歌表。 However, after reading the documentation and watching a couple tutorials, I'm not sure how to take it further.但是,在阅读了文档并观看了一些教程之后,我不确定如何进一步了解它。

I want to take that information and store it an array so that if I were to type msg.channel.send(fact[0]);我想获取该信息并将其存储为一个数组,这样如果我要输入msg.channel.send(fact[0]); for example, it would then display test0 .例如,它将显示test0 Or if I were to do a random fact, I could do something like this (obviously not exactly like this)或者如果我要做一个随机的事实,我可以做这样的事情(显然不完全像这样)

if (msg.content == prefix + "randomfact"){
randomfact = Math.floor(Math.random() * sheet.rowCount);
msg.channel.send(fact[randomfact]);
}

then it would count the rows, thus allowing me to put it into math.random and allowing me to print a random fact off the list.然后它会计算行数,从而允许我将其放入math.random并允许我从列表中打印一个随机事实。 I've been messing with this for a couple hours and keep breaking my code.我已经搞砸了几个小时并不断破坏我的代码。 I am not sure how to implement the functions from the documentation as I've never worked with this before.我不确定如何实现文档中的功能,因为我以前从未使用过此功能。

I got it working, alls you need to do is remove the for each, create a new variable and parse the factdoc function into the rows constent我让它工作了,你需要做的就是删除 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM