2

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.

7
  • 1
    Have you verified that contents contains what you expect it to? Commented Sep 27, 2019 at 22:30
  • 1
    Are you getting error messages? Or is it completing without error, but it doesn't look like you expect? Commented Sep 27, 2019 at 22:30
  • The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 2. Commented Sep 27, 2019 at 22:35
  • contents contains the json data using Logging.log(contents) Commented Sep 27, 2019 at 22:38
  • I think I may see the problem. contents has 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 of contents, you're putting in the header row -- and then a blob of JSON representing all the other rows. Commented Sep 28, 2019 at 0:30

1 Answer 1

6

How about this modification? In this modification, at first, the header values are retrieved from the 1st element. And the values are retrieved from the headers. Then, the values which added the headers are put to the active sheet.

Modified script:

function renderJSON() {
  var url = 'http://dummy.restapiexample.com/api/v1/employees';
  var response = UrlFetchApp.fetch(url);
  var object = JSON.parse(response.getContentText());

  var headers = Object.keys(object[0]);
  var values = object.map(function(e) {return headers.map(function(f) {return e[f]})});
  values.unshift(headers);
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}

If I misunderstood your question and this was not the result you want, I apologize.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, that worked with no further modifications :)
@j86h Thank you for replying. I'm glad your issue was resolved.
great code chunk - works like a charm
@Liquidice Thank you for checking it. I'm glad about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.