Question

I work for a Google Apps Reseller and I implement gadgets for our clients.

We have several gadgets that use Google Visualization API to query a spreadsheet (with a dynamic id) and display the result in a table.

We are using this piece of code:

function drawVisualization() {
  var oldSpreadsheetUrl = "https://docs.google.com/spreadsheet/tq?key=<ss_id>";
  var newSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/<ss_id>/gviz/tq";
  var query = new google.visualization.Query(oldSpreadsheetUrl);
  query.setQuery("SELECT A,B");
  query.send(handleQueryResponse);
}

function handleQueryResponse(response) {
  if(response.isError()) {
    console.log(response.getMessage());
  } else {
    console.log("NUMBER OF COLUMNS: " + response.getDataTable().getNumberOfColumns());
  }
}

And testing with this sample spreadsheet (New Google Spreadsheet)

SPREADSHEET ID: 1qMvY3y4MkwoK1UvCA9A8dW4BbmhH1iMwWqGLEM5vjJk


As long as we query an old spreadsheet everything works ok, but if we query a new spreadsheet (with the standard url - oldSpreadsheetUrl) the request fails with a timeout exception and we get a 404 exception. This is the message we get in the Browser Console: http://i.imgur.com/h4DjyQ6.png

We were able to make the query work anyway, by using this new url in the datasource:

"https://docs.google.com/spreadsheets/d/<spreadsheet_id>/gviz/tq"

But with this notation the setQuery directive is ignored and all of the three columns are returned instead of the first two ("SELECT A,B").

We are using this in several gadgets and therefore want to know:

  1. How can we overcome the setQuery bug?
  2. Is there a programmatic way to know if a spreadsheet is a new version or an old version?

Thank you in advance.

Regards,

Riccardo

Was it helpful?

Solution

I ran into this same problem a few weeks ago when we did some testing with the new Google Sheets. Basically, the old Google Sheets expect a URL with the following format:

https://spreadsheets.google.com/tq?key={key}

While the new Google Sheets expects a URL with this format:

https://docs.google.com/spreadsheets/d/{key}/gviz/tq

You should be able to use the domain to distinguish between old and new Google Sheets. Failing that, you could always look for /gviz/ to tell if the new Google Sheets are being used. You can find additional information in this bug report.

In regards to your other point about setQuery, I've just noticed this morning while debugging my own issue that column IDs are being returned by the Visualization API as "Col 0", "Col 1" etc. instead of "A", "B" etc. So that's likely why your ("SELECT A,B") query isn't working. I've also just logged a bug for this issue.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top