简体   繁体   English

按 Apps 脚本中的列名读取 Google Sheet Exported JSON

[英]Read Google Sheet Exported JSON by column name in Apps Script

I'm exploring the use of JSON exported Google Sheets as database for Apps Script.我正在探索使用 JSON 导出的 Google 表格作为 Apps 脚本的数据库。

在此处输入图像描述

The fetched url follows the structure: https://docs.google.com/spreadsheets/d/DOCUMENTID/gviz/tq?tqx=out:json&gid=SHEETID获取的 url 遵循以下结构: https://docs.google.com/spreadsheets/d/DOCUMENTID/gviz/tq?tqx=out:json&gid=SHEETID

JSON.json JSON.json

{
    "reqId": "0",
    "sig": "000",
    "status": "ok",
    "table": {
        "cols": [
            {
                "id": "A",
                "label": "",
                "type": "string"
            },
            {
                "id": "B",
                "label": "",
                "type": "string"
            },
            {
                "id": "C",
                "label": "",
                "type": "string"
            }
        ],
        "parsedNumHeaders": 0,
        "rows": [
            {
                "c": [
                    {
                        "v": "Data 1-1"
                    },
                    {
                        "v": "Data 1-2"
                    },
                    {
                        "v": "Data 1-3"
                    }
                ]
            },
            {
                "c": [
                    {
                        "v": "Data 2-1"
                    },
                    {
                        "v": "Data 2-2"
                    },
                    {
                        "v": "Data 2-3"
                    }
                ]
            },
            {
                "c": [
                    {
                        "v": "Emails"
                    },
                    {
                        "v": "Ids"
                    },
                    {
                        "v": "Names"
                    }
                ]
            }
        ]
    },
    "version": "0.6"
}

In this function I'm getting returned an array of values for the C column: dataArray = [Data 1-3, Data 2-3]在此 function 中,我返回了 C 列的值数组: dataArray = [Data 1-3, Data 2-3]

function getRolePermission(databaseUrl) {

  let databaseParsed = JSON.parse(UrlFetchApp.fetch(databaseUrl).getContentText().match(/(?<=.*\().*(?=\);)/s)[0]);
  let tableLength = Object.keys(databaseParsed.table.rows).length;

  let dataArray = [];
  for (let i = 0; i < tableLength; i++) {
    dataArray.push(databaseParsed.table.rows[i].c[2].v)
  }

  return dataArray;

}

It works well, but I don't know how to make this function more generic, so I call it with (url, headerName) arguments to get an array with the values of a column.它工作得很好,但我不知道如何使这个 function 更通用,所以我用 (url, headerName) arguments 调用它来获取一个包含列值的数组。 Something like:就像是:

function getRolePermission(databaseUrl, headerName) {
  // CODE ??
  return dataArray;
}

getRolePermission('https://docs.google.com/spreadsheets/d/1lc...', 'Emails')

to get dataArray = [Data 1-3, Data 2-3] , so if I change the order of columns I'm still getting the same results.得到dataArray = [Data 1-3, Data 2-3] ,所以如果我改变列的顺序,我仍然会得到相同的结果。

dataArray.push(databaseParsed.table.rows[i].c[2].v)

You just need to find the index 2 here to make the function more generic.您只需要在此处找到index 2即可使 function 更通用。 The table.cols should give you the column headers, but it is not doing that because parsedNumHeaders is 0 in your case. table.cols应该为您提供列标题,但它没有这样做,因为parsedNumHeaders在您的情况下为 0。 To manually set number of headers, use &headers=1 .要手动设置标题数量,请使用&headers=1 The url should look like: url 应如下所示:

https://docs.google.com/spreadsheets/d/DOCUMENTID/gviz/tq?tqx=out:json&gid=SHEETID&headers=1 https://docs.google.com/spreadsheets/d/DOCUMENTID/gviz/tq?tqx=out:json&gid=SHEETID&headers=1

The response then would look like:然后响应将如下所示:

{
  "reqId": "0",
  "sig": "000",
  "status": "ok",
  "table": {
    "cols": [
      {
        "id": "A",
        "label": "Names",
        "type": "string"
      },
      {
        "id": "B",
        "label": "Ids",
        "type": "string"
      },
      {
        "id": "C",
        "label": "Emails",
        "type": "string"
      }
    ],
    "parsedNumHeaders": 1,
    "rows": [
      {
        "c": [
          {
            "v": "Data 1-1"
          },
          {
            "v": "Data 1-2"
          },
          {
            "v": "Data 1-3"
          }
        ]
      },
      {
        "c": [
          {
            "v": "Data 2-1"
          },
          {
            "v": "Data 2-2"
          },
          {
            "v": "Data 2-3"
          }
        ]
      }
    ]
  },
  "version": "0.6"
}

Once you get the headers, you can find the index:获取标题后,您可以找到索引:

const columnNeeded = databaseParsed.table.cols.findIndex(obj => obj.label === headerName/*"Emails"*/);

Then, use it in your function然后,在您的 function 中使用它

dataArray.push(databaseParsed.table.rows[i].c[columnNeeded].v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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