简体   繁体   English

使用 Google APP 脚本在 Google SpreadSheet 中导入 JSON object

[英]Import JSON object in Google SpreadSheet using Google APP Script

I writing a script which calls REST API to get JSON to object as response and then import that JSON object in Google SpreadSheet. I writing a script which calls REST API to get JSON to object as response and then import that JSON object in Google SpreadSheet. I have written the code to call the REST API and get the JSON object.我已经编写了代码来调用 REST API 并获得 JSON ZA8CFDE6331BD59EB62AC96F8911 Now I have to write such a code that will write the JSON object in a Google SpreadSheet.现在我必须编写这样一个代码,它将在 Google 电子表格中写入 JSON object。 Please see below code:请看下面的代码:

function myFunction() {


    var options = {};
    options.headers = {"Authorization": "Basic " + Utilities.base64Encode("username" + ":" + "password")}
    options.muteHttpExceptions=true;


    // var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});

    var response=UrlFetchApp.fetch("url", options);

    var dataAll=JSON.parse(response.getContentText())

    Logger.log(dataAll)

    /*
    var sheet = Sheets.newSpreadsheet();
    sheet.properties = Sheets.newSpreadsheetProperties();
    sheet.properties.title = "Demo";
    var spreadsheet = Sheets.Spreadsheets.create(sheet);
    */
}

This is code to call get the JSON object.这是调用获取 JSON object 的代码。 The lines of code which have been commented out is for calling Google Sheets API.已注释掉的代码行用于调用 Google Sheets API。 I want such a script which will import the data in dataAll object, which is JSON object in Google SpreadSheet.我想要这样一个脚本,它将导入 dataAll object 中的数据,即 Google SpreadSheet 中的 JSON object。

The JSON object is as below: JSON object如下:

[
{
    "id": 3428,
    "type": "SUPER",
    "firstName": "Nikhil",
    "lastName": "Dingane",
    "loginName": "nikhildingane",
    "domain": "openspecimen",
    "emailAddress": "nikhil@krishagni.com",
    "instituteName": "Krishagni Institute",
    "primarySite": "KSPL site",
    "admin": true,
    "instituteAdmin": false,
    "manageForms": true,
    "cpCount": 0,
    "creationDate": 1586333347000,
    "activityStatus": "Active"
},
{
    "id": 3439,
    "type": "NONE",
    "firstName": "NikhilUpdated",
    "lastName": "Dingane",
    "loginName": "nikhildingane1",
    "domain": "openspecimen",
    "emailAddress": "nikhil.nikhil.dingane@gmail.com",
    "instituteName": "Krishagni Institute",
    "primarySite": "KSPL site",
    "admin": false,
    "instituteAdmin": false,
    "manageForms": true,
    "cpCount": 0,
    "creationDate": 1586427573000,
    "activityStatus": "Active"
}
]

I want to write this JSON in Google SpreadSheet我想在 Google SpreadSheet 中写这个 JSON

Assuming that dataAll is a 2D array of values假设 dataAll 是一个二维数组值

You can do it is as following with the SpreadsheetApp:您可以使用 SpreadsheetApp 执行以下操作:

var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(1,1, dataAll.length, dataAll[0].length);
range.setValues(dataAll);

References:参考:

How to transform a JSON object into a 2D array如何将 JSON object 转换为二维数组

  • Use the Object.keys() method to access the key-value pairs of each of the JSON array elements使用Object.keys()方法访问每个 JSON 数组元素的键值对
  • Use eg map to transform them into an array使用例如map将它们转换为数组
  • Assemble the single arrays as desired to set them into the sheet根据需要组装单个 arrays 以将它们设置到表中
 var array = [];
  for (var i = 0; i< json.length; i++){
    var newJson = Object.keys(json[i]).map(function (key) { 
      return [key, json[i][key]]; 
    }); 
    for(var j = 0; j< newJson.length; j++){
      array.push(newJson[j]);
    }
  }
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();  
  var range = sheet.getRange(1,1, array.length, array[0].length);
  range.setValues(array);

Range.setValues() is used to the set data as a two dimensional array to the sheet. Range.setValues()用于将数据设置为工作表的二维数组 Use Object.values and Object.keys with Array.map to convert array of objects to 2D array.使用Object.values和 Object.keys 和Array.map将对象数组转换为二维数组。

 const data = [ { "id": 3428, "type": "SUPER", "firstName": "Nikhil", "lastName": "Dingane", "loginName": "nikhildingane", "domain": "openspecimen", "emailAddress": "nikhil@krishagni.com", "instituteName": "Krishagni Institute", "primarySite": "KSPL site", "admin": true, "instituteAdmin": false, "manageForms": true, "cpCount": 0, "creationDate": 1586333347000, "activityStatus": "Active" }, { "id": 3439, "type": "NONE", "firstName": "NikhilUpdated", "lastName": "Dingane", "loginName": "nikhildingane1", "domain": "openspecimen", "emailAddress": "nikhil.nikhil.dingane@gmail.com", "instituteName": "Krishagni Institute", "primarySite": "KSPL site", "admin": false, "instituteAdmin": false, "manageForms": true, "cpCount": 0, "creationDate": 1586427573000, "activityStatus": "Active" } ]; const out = [Object.keys(data[0]),...data.map(Object.values)]; console.info(out); //SpreadsheetApp.getActive().getSheets()[0].getRange(1,1,out. length,out[0].length).setValues(out)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将JSON从应用程序引擎导入到Google电子表格 - Import JSON from app engine to google spreadsheet 如何使用Google App脚本在Google驱动器中的特定文件夹中生成Google电子表格的JSON? - how to generate json of google spreadsheet at specific folder in google drive using google app script? 使用HTTP GET和Google App脚本访问Google电子表格的标题 - Accessing headers of a google spreadsheet using HTTP GET and Google App Script Google App脚本-访问JSON对象 - Google App Script - Accessing JSON object 在 Google App Script 中获取错误以从 JSON api Webservice 获取数据并将它们写入谷歌电子表格 - Getting error in Google App Script to fetch data from JSON api Webservice and write them to google spreadsheet 在AngularJS App中加载JSON(加载谷歌电子表格) - Load JSON in AngularJS App (loading google spreadsheet) 如何在 Google 电子表格的 Google Apps 脚本中解析 JSON - How to parse JSON in Google Apps Script for Google Spreadsheet 使用 Google Apps 脚本从 JSON object 创建 Google 表格 - Create Google Sheet from JSON object using Google Apps Script 将 JSON 从 Hubspot Analytics API 写入谷歌电子表格 - 使用谷歌应用程序脚本 - Write JSON from Hubspot Analytics API into a Google Spreadsheet - using google app scripts 使用Jquery在Google Spreadsheet上访问JSON - Accessing JSON on Google Spreadsheet using Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM