获取未将所有数据从 json 输出到 Google sheet

Fetch not outputting all data from json into Google sheet

我正在使用 fetch 将数据从 json 输出到 Google sheet 这是我从@Rubén

by @Tanaike and added to from 那里得到的
function GETCONNECTIONS(url) {
  var response = UrlFetchApp.fetch(url);
  var responseText = response.getContentText();
  var responseJson = JSON.parse(responseText);
  var connectionKeys = Object.keys(responseJson.connections[0]);
 
  var data = responseJson.connections.map(e => connectionKeys.map(f => {
      return e[f] instanceof Array ? e[f].join("|") : e[f];
  })); 
  data.unshift(connectionKeys);

数据来自调查,这是我的输出的一般问题示例

json 看起来像 (A)

 "connections": [
    {
      "Id": 1024034,
      "From": 4806,
      "To": 3817,
      "Name From": "Sian Young",
      "Name To": "Richard Iron Cloud",
      "Initial Date": "7/11/2017 20:32",
      "Last Date": "9/27/2017 17:38",
      "Type": "person-person",
      "Weight": 2,
      "Checkbox": ""
    },

文件后面 (B)

{
      "Id": 1053141,
      "From": 28264,
      "To": 28267,
      "Name From": ". Arts & Science School",
      "Name To": ". Helpline Center",
      "Initial Date": "10/05/2019 19:56",
      "Last Date": "10/05/2019 19:56",
      "Type": "organization-organization",
      "Weight": 0,
      "Tell us about the relationship between your organization and this organization": [
        "We give funds to this organization",
        "We have the same parent organization"
      ],
      "Other options": ""
    },

及以后 (C)

{
      "Id": 2000002,
      "From": 3824,
      "To": 28265,
      "Name From": "Adrienne Benjamin",
      "Name To": ". New Vision Foundation",
      "Initial Date": "6/28/2019 18:30",
      "Last Date": "6/28/2019 18:30",
      "Type": "person-organization",
      "Weight": 0,
      "Your personal connection to this organization": "I work there",
      "Your personal feelings about this organization": ""
    },

仅输出第一部分 (A) 的公共数据 (仅显示输出的 headers)

Id | From | To | Name From | Name To | Initial Date | Last Date | Type | Weight Checkbox

data

中未输出部分 (B) 中的内容
"Tell us about the relationship between your organization and this organization": [
        "We give funds to this organization",
        "We have the same parent organization"
      ],

(C) 部分的内容未在 data

中输出
"Your personal connection to this organization": "I work there",
      "Your personal feelings about this organization": ""

我需要 (仅显示 headers)

Id | From | To | Name From | Name To | Type | Weight | Checkbox | Tell us about the relationship between your organization and this organization | Other options | Your personal connection to this organization | Your personal feelings about this organization | I currently have no role in this workstream, but I'm interested in learning more and potentially joining it | My role in relation to this Network | Other - Optional | ig_Initial Date | ig_Last Date

Json https://script.googleusercontent.com/macros/echo?user_content_key=wdu1jsOZR1FXIfljo3adgITKSaGdmtr1XIlQod-5LkFJWKz8bKUXkpou62XfN7sJmOU2ZL5mbB-wDd1NnpkpSFeY76BiaFOWOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa8TJX5ydsDPljxBccW800JGJdC8wte9w6JgdINX__ZvEr9Orxvk8od_4WhINrS1UdLSF_Mvn60p3tS3F0PU0G3gjE2x5FoVILaIZk7KcgdZtoRLOZR3Hw6iE83c7Od3ZCxsVPxPhxNqP0kDfbpY946Xf1vOmg1loOnII8WbYyB8pF1RKHM0quOFuNx6rQg6xpZxZOYNDLHNpptndeU9o4Ltwxj0PG1J6Gi6JRSk24HcIK3NRq9Lx1wjBamNAgHqD0QjxXNn1u2wARfAJMFmXoJFD90btlBFN0NmArM7a8XstaiDzN7sfEhqeAd4e6zlqwKKjICN9fnW0pS3kNK_FXA-SMsrpnGMp1g&lib=MDmgpdXHxuQ1dJ2wjGRxaDhUC8xcaHA7W

如何从json

输出ALL数据

谢谢

我做的有点不同,但希望这能帮助您可视化数据:

function getDataFromJSON() {
  const url="https://script.googleusercontent.com/macros/echo?user_content_key=wdu1jsOZR1FXIfljo3adgITKSaGdmtr1XIlQod-5LkFJWKz8bKUXkpou62XfN7sJmOU2ZL5mbB-wDd1NnpkpSFeY76BiaFOWOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa8TJX5ydsDPljxBccW800JGJdC8wte9w6JgdINX__ZvEr9Orxvk8od_4WhINrS1UdLSF_Mvn60p3tS3F0PU0G3gjE2x5FoVILaIZk7KcgdZtoRLOZR3Hw6iE83c7Od3ZCxsVPxPhxNqP0kDfbpY946Xf1vOmg1loOnII8WbYyB8pF1RKHM0quOFuNx6rQg6xpZxZOYNDLHNpptndeU9o4Ltwxj0PG1J6Gi6JRSk24HcIK3NRq9Lx1wjBamNAgHqD0QjxXNn1u2wARfAJMFmXoJFD90btlBFN0NmArM7a8XstaiDzN7sfEhqeAd4e6zlqwKKjICN9fnW0pS3kNK_FXA-SMsrpnGMp1g&lib=MDmgpdXHxuQ1dJ2wjGRxaDhUC8xcaHA7W";
  const jsn=UrlFetchApp.fetch(url).getContentText();
  const obj=JSON.parse(jsn);
  const keys=Object.keys(obj.connections[0]);
  //SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(keys.join('<br />')), "Keys");
  var html=""; 
  obj.connections.forEach(function(c){
    keys.forEach(function(k){
      html+=Utilities.formatString('<br />%s:%s',k,c[k] instanceof Array?c[k].join("|"):c[k]);
    });
    html+='<hr>';
  }); 
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Connnections");
  
}

这个版本的数据更容易可视化:

function getDataFromJSON() {
  const url="https://script.googleusercontent.com/macros/echo?user_content_key=wdu1jsOZR1FXIfljo3adgITKSaGdmtr1XIlQod-5LkFJWKz8bKUXkpou62XfN7sJmOU2ZL5mbB-wDd1NnpkpSFeY76BiaFOWOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa8TJX5ydsDPljxBccW800JGJdC8wte9w6JgdINX__ZvEr9Orxvk8od_4WhINrS1UdLSF_Mvn60p3tS3F0PU0G3gjE2x5FoVILaIZk7KcgdZtoRLOZR3Hw6iE83c7Od3ZCxsVPxPhxNqP0kDfbpY946Xf1vOmg1loOnII8WbYyB8pF1RKHM0quOFuNx6rQg6xpZxZOYNDLHNpptndeU9o4Ltwxj0PG1J6Gi6JRSk24HcIK3NRq9Lx1wjBamNAgHqD0QjxXNn1u2wARfAJMFmXoJFD90btlBFN0NmArM7a8XstaiDzN7sfEhqeAd4e6zlqwKKjICN9fnW0pS3kNK_FXA-SMsrpnGMp1g&lib=MDmgpdXHxuQ1dJ2wjGRxaDhUC8xcaHA7W";
  const jsn=UrlFetchApp.fetch(url).getContentText();
  const obj=JSON.parse(jsn);
  const keys=Object.keys(obj.connections[0]);
  //SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(keys.join('<br />')), "Keys");
  var html=""; 
  obj.connections.forEach(function(c,j){
    html+=Utilities.formatString('<br />Connection: %d',j+1);
    keys.forEach(function(k,i){
      html+=Utilities.formatString('<br />%s-<strong>%s</strong>:%s',i+1,k,c[k] instanceof Array?c[k].join("|"):c[k]);
    });
    html+='<br /><hr>';
  }); 
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(600), "Connnections");
}

进入电子表格:

function getDataFromJSON() {
  const url="https://script.googleusercontent.com/macros/echo?user_content_key=wdu1jsOZR1FXIfljo3adgITKSaGdmtr1XIlQod-5LkFJWKz8bKUXkpou62XfN7sJmOU2ZL5mbB-wDd1NnpkpSFeY76BiaFOWOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa8TJX5ydsDPljxBccW800JGJdC8wte9w6JgdINX__ZvEr9Orxvk8od_4WhINrS1UdLSF_Mvn60p3tS3F0PU0G3gjE2x5FoVILaIZk7KcgdZtoRLOZR3Hw6iE83c7Od3ZCxsVPxPhxNqP0kDfbpY946Xf1vOmg1loOnII8WbYyB8pF1RKHM0quOFuNx6rQg6xpZxZOYNDLHNpptndeU9o4Ltwxj0PG1J6Gi6JRSk24HcIK3NRq9Lx1wjBamNAgHqD0QjxXNn1u2wARfAJMFmXoJFD90btlBFN0NmArM7a8XstaiDzN7sfEhqeAd4e6zlqwKKjICN9fnW0pS3kNK_FXA-SMsrpnGMp1g&lib=MDmgpdXHxuQ1dJ2wjGRxaDhUC8xcaHA7W";
  const jsn=UrlFetchApp.fetch(url).getContentText();
  const obj=JSON.parse(jsn);
  const keys=Object.keys(obj.connections[0]);
  let vA=[];
  //var html=""; 
  obj.connections.forEach(function(c,j){
    //html+=Utilities.formatString('<br />Connection: %d',j+1);
    vA[j]=[];//create array for each row
    keys.forEach(function(k,i){
      //html+=Utilities.formatString('<br />%s-<strong>%s</strong>:%s',i+1,k,c[k] instanceof Array?c[k].join("|"):c[k]);
      vA[j].push(c[k] instanceof Array?c[k].join('\n'):c[k]);//push in row values
    });
    //html+='<br /><hr>';
  }); 
  //SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(600), "Connnections");
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  sh.clearContents();//clear sheet
  vA.unshift(keys);//put keys in as headers
  sh.getRange(1,1,vA.length,vA[0].length).setValues(vA);//load the data
}