将JSON响应中的标题键输出到Google电子表格列中

时间:2019-06-12 13:22:59

标签: json google-apps-script google-sheets nested tableheader

我正在尝试将JSON响应输出到Google Spreadsheet。 JSON响应如下所示:

[{
    "city": "Mumbai",
    "cityC": "MUM",
    "cat": {
      "Subs": null,
      "respC": 200,
      "respM": "OK",
      "resp": [{
          "CATID": "ABC53249",
          "CATDSCR": "FRIED ITEMS",
          "SEG": [{
              "SEGID": "DFG00985",
              "SEGDSCR": "FRIED FISH"
            },
            {
              "SEGID": "FKK24091",
              "SEGDSCR": "FRIED VEGGIES"
            },
            {
              "SEGID": "LGH65255",
              "SEGDSCR": "FRIED EGGS"
            }
          ]
        },
        {
          "CATID": "GIY35321",
          "CATDSCR": "BOILED ITEMS",
          "SEG": [{
              "SEGID": "QWW35442",
              "SEGDSCR": "BOILED FISH"
            },
            {
              "SEGID": "FKG35443",
              "SEGDSCR": "BOILED EGGS"
            }
          ]
        }
      ],
      "totRec": 0
    }
  },
  {
    "city": "Pune",
    "cityC": "PUN",
    "cat": {
      "Subs": null,
      "respC": 200,
      "respM": "OK",
      "resp": [{
          "CATID": "EFD53249",
          "CATDSCR": "FRUIT FLAVOURS",
          "SEG": [{
              "SEGID": "SLD00985",
              "SEGDSCR": "FRUIT JUICE"
            },
            {
              "SEGID": "DLS24091",
              "SEGDSCR": "FRUIT SALAD"
            },
            {
              "SEGID": "DDD65255",
              "SEGDSCR": "FRUIT MIX"
            }
          ]
        },
        {
          "CATID": "OLS35321",
          "CATDSCR": "MILK ITEMS",
          "SEG": [{
              "SEGID": "REW35442",
              "SEGDSCR": "MILK CHOCOS"
            },
            {
              "SEGID": "DES35443",
              "SEGDSCR": "MILK JUICE"
            }
          ]
        }
      ],
      "totRec": 0
    }
  }
]

以下代码可解析JSON,但需要在Google电子表格中显示标题。它还使用了我不需要的点(或非下划线)表示法。

     var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");  
     var oJSON = JSON.parse(response.getContentText());

     // flatten the json
     var rows = oJSON.map(function(d){
        return flatten(d);
      });

      // insert new rows
    //  sheet.insertRowsAfter(1, rows.length);
      
      // getting our headers
      var heads = sheet.getDataRange().offset(0, 0, 1).getValues()[0];
         
      // convert object data into a 2d array 
      var tr = rows.map (function (row) {
        return heads.map(function(cell){
          return row[cell] || "";
        });
      });

      // write result
      sheet.getRange(2, 1, tr.length, tr[0].length).setValues(tr);

    }

  

    /**
     * Flatten an object.
     * https://stackoverflow.com/a/19101235/1027723
     *
     * @param {Object} data object to flatten
     * @return {Object} the flattened result
     */
    function flatten(data) {
        var result = {};
        function recurse (cur, prop) {
            if (Object(cur) !== cur) {
                result[prop] = cur;
            } else if (Array.isArray(cur)) {
                 for(var i=0, l=cur.length; i<l; i++)
    //                 recurse(cur[i], prop ? prop+"."+i : ""+i);
                     recurse(cur[i], prop ? ""+i : ""+i);
                if (l == 0)
                    result[prop] = [];
            } else {
                var isEmpty = true;
                for (var p in cur) {
                    isEmpty = false;
    //                recurse(cur[p], prop ? prop+"."+p : p);
                    recurse(cur[p], prop ? p : p);
                }
                if (isEmpty)
                    result[prop] = {};
            }
        }
        recurse(data, "");
        return result;
    }

我的问题是我如何修改代码以提取列标题以及要输出到Google电子表格的行数据?

我的电子表格标题应该是JSON键:应该输出的值对,其中Key应该是标题,而其对应的值应该是每一行的单元格数据。

enter image description here

0 个答案:

没有答案