Parse JSON into multiple sheets using Fetch() Google sheets

别来无恙 提交于 2021-02-04 16:26:05

问题


I am trying to import a json file using Fetch() to populate multiple Google sheets I have been trying for a few hours but I do not know enough to do this

The json is dynamic so its data is always changing but its structure remains the same my json looks like

{
    "connections": [
    {
      "Id": 131527,
      "From": 49647,
      "To": 49644,
      "Name From": "Horus Max",
      "Name To": "Bob allison",
      "Initial Date": "9/21/2020 15:20",
      "Last Date": "9/21/2020 15:20",
      "Type": "",
      "Weight": 0,
      "Checkbox ZZZ": "",
      "Text Area": "",
      "Radio AAA": "value one AAA",
      "Select bbb": "value one sss"
    },
    { 

    },
    ],
   "elements": [
    {
      "Id": 49645,
      "Type": "Person",
      "Label": "Sally Yager",
      "First Name": "Sally",
      "Last Name": "Yager",
      "Description": "",
      "Segment": "555",
      "Image": null,
      "Project Name": "test222",
      "Initial Date": "09/29/2020 17:44",
      "Last Date": "09/29/2020 17:47",
      "Issues Checkbox": [
        "Option 1",
        "Option 6"
      ],
      "IssuesRadio": "Option 3",
      "Notes": "222"
    },
    {
    }
    ],
    "name": "My project name"
}

I need the elements data passed into a sheet called elements and the Connections data parsed into a sheet called Connections

Elements sheet

Connections sheet

What I get

I have

//=fetchdata("https://sum-app.net/projects/14312620200623668/download_data/kumu_json")
async function fetchdata(url) {
  var response = await UrlFetchApp.fetch(url);
  var responseText = await response.getContentText();
  var responseJson = JSON.parse(responseText);
  
  // Return something to display in the Google Sheet.  
  var rows = [Object.keys(responseJson)]; // Retrieve headers.
  var temp = [];
  for (var i = 0; i < rows[0].length; i++) {
    temp.push(responseJson[rows[0][i]]); // Retrieve values.
  }
  rows.push(temp);
  return temp
}

Here is a Google sheet with what I get and the desired outcome https://docs.google.com/spreadsheets/d/14vxiueXsUzFtkw22RHA7qVFE--PFVSwfj4zJIU7I3nk/edit?usp=sharing

Thanks


回答1:


Modification points:

  • It is not required to use await to UrlFetchApp and the response.
  • I think that in your script, each object and value from the top of response values are retrieved by temp.push(responseJson[rows[0][i]]);. responseJson.connections, responseJson.elements and responseJson.name are retrieved. By this, when =fetchdata("https://sum-app.net/projects/14312620200623668/download_data/kumu_json") is put to a cell, only test222 is shown. I think that this is the reason of your issue. For example, when temp.push(responseJson[rows[0][i]]); is modified to temp.push(JSON.stringify(responseJson[rows[0][i]]));, all values can be seen. But I think that this it not the result you expect.
  • In your goal, when you want to use the custom function, it is required to separate the script for "elements" sheet and "Connections" sheet.

When above points are reflected to your script, it becomes as follows.

Sample script:

Please copy and paste the folloiwng scripts. When you use this, please put =GETCONNECTIONS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json") and =GETELEMENTS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json") to the sheet of "connections" and "elements", respectively.

// =GETCONNECTIONS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json")
function GETCONNECTIONS(url) {
  var response = UrlFetchApp.fetch(url);
  var responseText = response.getContentText();
  var responseJson = JSON.parse(responseText);
  var connectionKeys = Object.keys(responseJson.connections[0]);
  
  // At JSON object, the order of keys are not guaranteed. So the following array including the keys might be suitable.
//  var connectionKeys = ["Id","From","To","Name From","Name To","Initial Date","Last Date","Type","Weight","Checkbox ZZZ","Text Area","Radio AAA","Select bbb"];
  var data = responseJson.connections.map(e => connectionKeys.map(f => e[f]));
  data.unshift(connectionKeys);
  return data;
}

// =GETELEMENTS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json")
function GETELEMENTS(url) {
  var response = UrlFetchApp.fetch(url);
  var responseText = response.getContentText();
  var responseJson = JSON.parse(responseText);
  var elementKeys = Object.keys(responseJson.elements[0]);

  // At JSON object, the order of keys are not guaranteed. So the following array including the keys might be suitable.
//  var elementKeys = ["Id","Type","Label","First Name","Last Name","Description","Segment","Image","Project Name","Initial Date","Last Date","Issues Checkbox","IssuesRadio","Notes"];
  var data = responseJson.elements.map(e => elementKeys.map(f => e[f]));
  data.unshift(elementKeys);
  return data;
}



回答2:


Here is a way to refresh the data, you can add this to a menu or timmer

function refreshGETDATA() {
var queryString = Math.random();

/* Populate Chosen Elements Sheet */
var cellFunction = '=GETELEMENTS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json",' + queryString + ')';
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Elements Sheet").getRange('A1').setValue(cellFunction);

/* Populate Chosen ConnectionsSheet */
var cellFunction = '=GETCONNECTIONS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json",' + queryString + ')';
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Connections Sheet").getRange('A1').setValue(cellFunction);

SpreadsheetApp.flush();
}


来源:https://stackoverflow.com/questions/64125353/parse-json-into-multiple-sheets-using-fetch-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!