【问题标题】:How to get a json with key->value from google spreadsheets?如何从谷歌电子表格中获取带有键->值的json?
【发布时间】:2017-08-04 17:07:31
【问题描述】:

现在我正在使用它从工作表中获取数据。

fetch('https://sheets.googleapis.com/v4/spreadsheets/DOC_ID/values/!A2:Z999?key=API_KEY')
        .then(function(response) {
            return response.json();
        }).then(function(json) {
            console.log(json)
        });

因此,我得到了一个值数组 - 行。

[Row Name1,Row Name2,Row Name3,],
[val11, val12, val13],
[val21, val22, val23],
[val31, val32, val33]

有没有办法把它作为 key->value json?喜欢这个:

{
"Row Name1":"val11",
"Row Name2":"val12",
...
},
{
"Row Name1":"val21",
"Row Name2":"val22",
...
},

谢谢

【问题讨论】:

    标签: json google-sheets google-spreadsheet-api


    【解决方案1】:

    不,所有 Google Sheets API 方法都以双精度数组的形式返回(并接受)值。例如,转换为对象数组必须在客户端进行

    var values = [["Row Name1", "Row Name2", "Row Name3"], [1,2,3], [4,5,6]]; // sample values
    var dict = values.slice(1).map(row => row.reduce(function(acc, cur, i) {
      acc[values[0][i]] = cur;
      return acc;
    }, {}));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多