2

I am working on a chrome extension that gets some variables from a google sheets spreadsheet. I finally got the api setup correctly and can successfully query the data for the first variable from a cell in that spreadsheet. The problem for me is I am not familiar with the format the data is in.

Here is my code to query the google spreadsheet:

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://sheets.googleapis.com/v4/spreadsheets/*spreadsheetID*/values/A2?key=*API Key*", false);
xhr.send();
var result = xhr.responseText;

The output I receive is:

{
  "range": "Sheet1!A2",
  "majorDimension": "ROWS",
  "values": [
    [
      "https://www.access.com/home/"
    ]
  ]
}

the URL https://www.access.com/home is what I am trying to parse from the spreadsheet. If I change the code to parse the responseText in json I can get the value for range, just not for the "values" data to get the URL:

var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://sheets.googleapis.com/v4/spreadsheets/*spreadsheetID*/values/A2?key=*API Key*", false);
    xhr.send();
    var result = JSON.parse(xhr.responseText);
    var data = result.range;

What am I doing wrong and how do I correctly parse the URL?

*While the post at Access / process (nested) objects, arrays or JSON does a great job at explaining how to access nested objects that are named, I was unable to figure out that how to access the nested array in my object because it didn't have a name.

Erik
  • 23
  • 3
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Sebastian Simon Dec 26 '17 at 03:32

1 Answers1

3

var data = result.values[0][0];

Chris Riebschlager
  • 1,323
  • 1
  • 8
  • 12
  • 1
    This is exactly what I needed. Thank you for taking the time to answer my first question! – Erik Dec 27 '17 at 00:55