1

I am a beginner trying to read a Google Sheet in a Javascript app using: https://docs.google.com/spreadsheets/d/mySheetID/gviz/tq?tq=Select%20*%20where%20A%20=%20%22Nan%22&tqx=out:JSON

I can access that row in my sheet and save it as JSON giving me a file with the content headed "/O_o/ google.visualization.Query.setResponse..." This is the file I cannot further process in javascript.

I see in: converting Google Visualization Query result into javascript array that the solution appears to be: "If you add a header named X-DataSource-Auth in your request, the Visualization API will respond in JSON format". After a day of googling I am quite unable to find where I am supposed to put such a header and what its syntax should be.

But then I'm 82 years old and this stuff gets more difficult with each passing year... Can someone point me in the right direction?

1 Answers1

2

once you receive the response from the query,
you can convert the response to a google data table.

var dataTable = response.getDataTable();

and the google data table has a method to convert the data table to JSON.

var jsonData = dataTable.toJSON();

this will return a JSON string.

if you then would like to work with the JSON using JavaScript,
you can parse the string...

jsonData = JSON.parse(jsonData);

the JSON will be an object with two properties, cols and rows.
you can see an example of the JSON result here...

see this fiddle for a working example using the following code...

https://jsfiddle.net/WhiteHat/5mu9wnbz/1/

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1M3wQgKg3JBF6_hzv1xWONP7HWVYoOvJ1jPbB27IUg94/gviz/tq?gid=0&headers=1');
  query.send(function (response) {
    if (response.isError()) {
      console.log('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
      return;
    };

    var dataTable = response.getDataTable();
    var jsonData = dataTable.toJSON();

    jsonData = JSON.parse(jsonData);
    document.getElementById('cols').innerHTML = jsonData.cols.length;
    document.getElementById('rows').innerHTML = jsonData.rows.length;
    console.log(jsonData);
  });
});
WhiteHat
  • 59,912
  • 7
  • 51
  • 133
  • Thank you for that. Your demo on your data works fine. When I run the code, the first line "google.charts.load('current', {" gives this error: test.js:3 Uncaught ReferenceError: google is not defined. Googling the error does not yield any fix but I assume I am missing a library. – John Murphy Oct 17 '20 at 17:47
  • yes, need to add --> `` – WhiteHat Oct 18 '20 at 16:39
  • I failed to reply thinking I was taking too much of your time after your latest fix failed. I tried several other potential solutions to no avail. My latest error after including the charts follows: – John Murphy Oct 23 '20 at 21:25
  • Failed to load resource: net::ERR_FAILED jsapi_compiled_default_module.js:251 Uncaught Error: Error handling Query: XhrHttpError: Request Failed, status=0, url=https://docs.google.com/spreadsheets/d/1M3wQgKg3JBF6_hzv1xWONP7HWVYoOvJ1jPbB27IUg94/gviz/tq?gid=0&headers=1&tqx=reqId%3A0 at jsapi_compiled_default_module.js:379 at e.XD (jsapi_compiled_default_module.js:258) at gvjs_ok (jsapi_compiled_default_module.js:261) at gvjs_lk (jsapi_compiled_default_module.js:261) at gvjs_bk.Nna (jsapi_compiled_default_module.js:260) at gvjs_$j (jsapi_compiled ... – John Murphy Oct 23 '20 at 21:28
  • I think I didn't include the first two lines of the error message: Access to XMLHttpRequest at 'https://docs.google.com/spreadsheets/d/1M3wQgKg3JBF6_hzv1xWONP7HWVYoOvJ1jPbB27IUg94/gviz/tq?gid=0&headers=1&tqx=reqId%3A0' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. docs.google.com/spreadsheets/d/1M3wQgKg3JBF6_hzv1xWONP7HWVYoOvJ1jPbB27IUg94/gviz/tq?gid=0&headers=1&tqx=reqId%3A0:1 – John Murphy Oct 23 '20 at 21:40
  • docs.google.com/spreadsheets/d/1M3wQgKg3JBF6_hzv1xWONP7HWVYoOvJ1jPbB27IUg94/gviz/tq?gid=0&headers=1&tqx=reqId%3A0:1 Failed to load resource: net::ERR_FAILED – John Murphy Oct 23 '20 at 21:41
  • Success! I used the Chrome extension Moesif Origin & CORS Changer that I found here: https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9 This will suffice for my development. I'll figure out how it may affect users later on. Thanks for your help and if you can find a better fix, please let me know. – John Murphy Oct 24 '20 at 00:42