I am an amateur programmer having a hard time with a little project. I would like to get a data from a Google spreadsheet, modify the data, and make a Google Chart (using google chart api).
The below code works perfectly in Firefox but Chrome and IE don't allow the dataTable outside the scope of the handleQueryResponse(e) function. My first thought was to return the dataTable from the handleQueryResponse(e) function but that is called inside of a function (.send()).
Any help would be greatly appreciated.
function getDataTable(link) {
var dataTable= new google.visualization.DataTable();
var queryTrip = new google.visualization.Query(link);
queryTrip.send(handleQueryResponse);
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
dataTable = response.getDataTable();
// I can verify this has the data through debugging in Chrome
}
// Outside of the function handleQueryResponse the dataTable is gone
return dataTable;
}
queryTrip.sendis asynchronous. In that case you would returndataTablefrom your function before it was set by the callback. That isreturn dataTable;is executed beforedataTable = response.getDataTable();. You cannot return a value from a function if asynchronous function calls are involved. You should makegetDataTableaccept a callback which you call ones the result is ready and pass it to the callback.