Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GooglesFeedsheet查询在节点js中不起作用_Javascript_Node.js_Google Sheets_Google Sheets Api - Fatal编程技术网

Javascript GooglesFeedsheet查询在节点js中不起作用

Javascript GooglesFeedsheet查询在节点js中不起作用,javascript,node.js,google-sheets,google-sheets-api,Javascript,Node.js,Google Sheets,Google Sheets Api,我目前正在使用google spreedsheet api v4。我希望使用查询参数仅选择单元格中具有特定Id的行。例如,在中,我希望选择一行,该行与传递的参数匹配,并且不会带来其余的数据。这就像php select*from db where id=“”;(让我简化一下,以便人们能够理解,我想做的就像我们使用select查询一样,我只希望选择SheetID值等于我传递的参数的行。我不希望像我们通常对其他数据库(如mysql等)所做的那样,返回整个json数组,但我的代码不起作用。这是我的密码

我目前正在使用google spreedsheet api v4。我希望使用查询参数仅选择单元格中具有特定Id的行。例如,在中,我希望选择一行,该行与传递的参数匹配,并且不会带来其余的数据。这就像php select*from db where id=“”;(让我简化一下,以便人们能够理解,我想做的就像我们使用select查询一样,我只希望选择SheetID值等于我传递的参数的行。我不希望像我们通常对其他数据库(如mysql等)所做的那样,返回整个json数组,但我的代码不起作用。这是我的密码

(async () => {
    await doc.useServiceAccountAuth(creds);
        await doc.loadInfo();
        const sheet = doc.sheetsByIndex[1];
        const rows = await sheet.getRows({
            query:'Clan = clan-12'
        });
        // rows = rows.map(a => a._rawData)
        rows.forEach(row =>{
             printStudent(row);
         });
      
     
    })()

function printStudent(data){
    console.log(data.Clan);
    console.log(data.SheetId);
    console.log(data.Enabled);
}

我已经搜索了整个文档,但不知道为什么它不起作用?GoogleSpeedSheet的api版本是4.0,GoogleSpeedSheet的NodeJS库是最新的3.1.15。任何建议都会有帮助。

我相信你的目标和现状如下

  • 您希望通过使用类似于
    select*from db where id=”“
    的查询来搜索值来检索行
  • 您正在使用“谷歌电子表格”库
  • 您已经能够使用带有“google电子表格”的Sheets API获取和输入值
修改点:
  • 在当前阶段,搜索查询不能直接与Sheets API一起使用。因此,在这种情况下,需要使用变通方法。在这个答复中,我想提出解决办法
  • 在此解决方案中,电子表格的查询语言与“google电子表格”一起使用。因此,访问令牌是从“google电子表格”的授权脚本中检索的
当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 在使用此脚本之前,请设置变量。在这个示例脚本中,从Google电子表格中第二个选项卡的列“A”中搜索ID。如果要从其他列中搜索ID,请修改
query

const { GoogleSpreadsheet } = require("google-spreadsheet");
const request = require("request");
// const csvParse = require("csv-parse"); // If you want to retrieve the values an array, please use this.

const creds = require("../credentialfile.json");  // Please set your credential file of the service account.

async function main() {
  const id = "sampleId"; // Please set the search text. In your case, it's ID.
  const query = `select * where A='${id}'`; // This is the query for searching.
  const spreadsheetId = "###"; // Please set the Spreadsheet ID.

  const doc = new GoogleSpreadsheet(spreadsheetId);
  await doc.useServiceAccountAuth(creds);
  await doc.loadInfo();
  const sheet = doc.sheetsByIndex[1]; // It seems that in your script, the ID is searched from the 2nd tab in Spreadsheet.
  const url = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:csv&gid=${sheet.sheetId}&tq=${encodeURI(query)}`;
  request(
    {
      url: url,
      method: "GET",
      headers: {authorization: `Bearer ${doc.jwtClient.credentials.access_token}`},
    },
    (err, res, result) => {
      if (err) {
        console.log(err);
        return;
      }
      console.log(result);
      // if (result != "") csvParse(result, {}, (err, ar) => console.log(ar));  // If you want to retrieve the values an array, please use this.
    }
  );
}

main();
参考:

    • 我相信你的目标和现状如下

      • 您希望通过使用类似于
        select*from db where id=”“
        的查询来搜索值来检索行
      • 您正在使用“谷歌电子表格”库
      • 您已经能够使用带有“google电子表格”的Sheets API获取和输入值
      修改点:
      • 在当前阶段,搜索查询不能直接与Sheets API一起使用。因此,在这种情况下,需要使用变通方法。在这个答复中,我想提出解决办法
      • 在此解决方案中,电子表格的查询语言与“google电子表格”一起使用。因此,访问令牌是从“google电子表格”的授权脚本中检索的
      当上述各点反映到脚本中时,它将变成如下所示

      修改脚本: 在使用此脚本之前,请设置变量。在这个示例脚本中,从Google电子表格中第二个选项卡的列“A”中搜索ID。如果要从其他列中搜索ID,请修改
      query

      const { GoogleSpreadsheet } = require("google-spreadsheet");
      const request = require("request");
      // const csvParse = require("csv-parse"); // If you want to retrieve the values an array, please use this.
      
      const creds = require("../credentialfile.json");  // Please set your credential file of the service account.
      
      async function main() {
        const id = "sampleId"; // Please set the search text. In your case, it's ID.
        const query = `select * where A='${id}'`; // This is the query for searching.
        const spreadsheetId = "###"; // Please set the Spreadsheet ID.
      
        const doc = new GoogleSpreadsheet(spreadsheetId);
        await doc.useServiceAccountAuth(creds);
        await doc.loadInfo();
        const sheet = doc.sheetsByIndex[1]; // It seems that in your script, the ID is searched from the 2nd tab in Spreadsheet.
        const url = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:csv&gid=${sheet.sheetId}&tq=${encodeURI(query)}`;
        request(
          {
            url: url,
            method: "GET",
            headers: {authorization: `Bearer ${doc.jwtClient.credentials.access_token}`},
          },
          (err, res, result) => {
            if (err) {
              console.log(err);
              return;
            }
            console.log(result);
            // if (result != "") csvParse(result, {}, (err, ar) => console.log(ar));  // If you want to retrieve the values an array, please use this.
          }
        );
      }
      
      main();
      
      参考:

      我可以问一下您的问题吗?1.我无法理解
      例如,在我的单元格D2中,我希望传递的参数将仅选择该id,而不会带来其余的数据。
      。我为此道歉。我能问一下你的目标的细节吗?2.我无法通过
      了解您当前的问题,但我的代码不起作用。
      我再次为此道歉。我能问一下你当前问题的细节吗?@Tanaike无问题我很抱歉我无法解释清楚。看,我想做的就像我们使用select查询一样,我只想选择SheetID值等于我传递的参数的行。我不希望返回整个json数组,只返回一条记录,就像我们通常使用其他数据库(如mysql等)时那样。我可以问一下您的问题吗?1.我无法理解
      例如,在我的单元格D2中,我希望传递的参数将仅选择该id,而不会带来其余的数据。
      。我为此道歉。我能问一下你的目标的细节吗?2.我无法通过
      了解您当前的问题,但我的代码不起作用。
      我再次为此道歉。我能问一下你当前问题的细节吗?@Tanaike无问题我很抱歉我无法解释清楚。看,我想做的就像我们使用select查询一样,我只想选择SheetID值等于我传递的参数的行。我不希望返回整个json数组,就像我们通常使用其他数据库(如mysql等)那样,只返回一条记录