获取不将所有数据从 json 输出到 Google 工作表

问题描述

我正在使用 fetch 将数据从 json 输出到 Google 工作表 我从 @Tanaike 从 here 获得,并从 here 添加到 @Rubén

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","Your personal connection to this organization": "I work there","Your personal feelings about this organization": ""
    },

输出第一段(A)共有的数据 (仅显示输出标题

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

部分 (B) 中的内容未在 data输出

"Tell us about the relationship between your organization and this organization": [
        "We give funds to this organization",

部分 (C) 中的内容未在 data输出

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

我需要 (只显示标题

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

杰森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,c[k] instanceof Array?c[k].join("|"):c[k]);
    });
    html+='<br /><hr>';
  }); 
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(600),"Connnections");
}

enter image description here

进入电子表格:

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',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
}