I'm trying to render json data to a google sheet using javascript.
I have tried searching through loads of documentation and I've been trying for hours. I do get one line of data come into my sheet but I need to render the whole json dataset in the correct rows and columns
function renderJSON() {
/* This function collects JSON data from a specified endpoint
and renders the data to a spreadsheet */
// Fetch JSON data from the endpoint
var url = 'http://dummy.restapiexample.com/api/v1/employees';
var response = UrlFetchApp.fetch(url); // Get the JSON data
var object = JSON.parse(response.getContentText());
//Logger.log(response);
// Define an array of all object keys
var rows = []; // Declare an empty array
rows.push(object);
var headerRow = Object.keys(rows);
// Define an array of all object values
var rows = headerRow.map(function(key){return rows [key]});
// Define the contents of the range
var contents = [
headerRow,
rows
];
// Select the range and set its values
var ss = SpreadsheetApp.getActive();
var rng = ss.getActiveSheet().getRange(1, 1, contents.length,
headerRow.length)
var i;
for (i = contents[0]; i < contents.length; i++) {
rng.setValues([i]);
}
}
I expect the whole of the json data to be in perfect format for a google sheet with the correct heading and everything aligned in the correct columns. Currently there is no output to the sheet.
contentscontains what you expect it to?contentshas only 2 items in it: the header, and the rows, with the rows being an object in their own right. So by iterating through the contents ofcontents, you're putting in the header row -- and then a blob of JSON representing all the other rows.