2

Now I am using this to get the data from sheet.

fetch('https://sheets.googleapis.com/v4/spreadsheets/DOC_ID/values/!A2:Z999?key=API_KEY')
        .then(function(response) {
            return response.json();
        }).then(function(json) {
            console.log(json)
        });

And as a result I am getting an array of values - rows.

[Row Name1,Row Name2,Row Name3,],
[val11, val12, val13],
[val21, val22, val23],
[val31, val32, val33]

Is there a way to get this as key->value json? Like this one:

{
"Row Name1":"val11",
"Row Name2":"val12",
...
},
{
"Row Name1":"val21",
"Row Name2":"val22",
...
},

Thanks

1 Answer 1

6

No, all Google Sheets API methods return (and accept) values as double arrays. Conversion to an array of objects would have to happen client-side, for example

var values = [["Row Name1", "Row Name2", "Row Name3"], [1,2,3], [4,5,6]]; // sample values
var dict = values.slice(1).map(row => row.reduce(function(acc, cur, i) {
  acc[values[0][i]] = cur;
  return acc;
}, {}));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.