Reputation: 13
I have the following output from a Google Sheets API call:
{
"spreadsheetId": "xxxxxxxxxxxxxxx",
"valueRanges": [
{
"range": "Groups!A2:Z63",
"majorDimension": "ROWS",
"values": [
[
"Student",
"Bonfire",
"Megan’s Home. Please contact Small Group Leader for directions.",
"First Last, First Last",
"13-18",
"Coed",
"Single",
"September 7, 2016. Meets every Wednesday at 6:30 p.m.",
"This is a group",
"student",
"2109793"
]
]
]
}
There are about 50 other values in this list that I'm trying to iterate through in order to display. I've tried some variations of a for loop, such as...
$.getJSON("https://sheets.googleapis.com/v4/spreadsheets/xxxx/values:batchGet?ranges=Groups!2%3A63&key=xxxx", function(response){
for (i = 0; i < response.valueRanges.length; i++) {
var row = groups.values[i];
var output = '<div>'+ row[0] +'</div>'
}
}
I'm not familiar with looping through arrays, so I'm not sure where I'm going wrong. Any ideas?
Upvotes: 0
Views: 1170
Reputation: 226
Not sure exactly what output you're going for, but the following will save the first and second element of each row into the output variable. The output with the given object (I added a missing '}'):
<div>Student, Bonfire</div>
You need to loop through both the outer array of 'valueRanges' and the inner 'values' array.
for (var i = 0; i < response.valueRanges.length; i++) {
var values = response.valueRanges[i].values;
for (var j = 0; j < values.length; j++) {
var row = values[j];
var output = '<div>' + row[0] + ', ' + row[1] + '</div>';
console.log(output);
}
}
Upvotes: 2