Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Visualization API Not Respecting GID or Sheet Parameter

I'm using the Google Visualization Javascript API to load a Chart from Google Sheets and display it in a div. My app is hosted on Google App Engine. I provide the URL to the sheet with the parameter gid=1 to specify the second sheet but the chart the gets displayed is the first sheet. Here's my simplified code (it's basically the example code provided in the documentation):

// sheetUrl is the URL of the Google sheet, e.g., http://https://docs.google.com/a/google.com/spreadsheet/ccc?key=0AobNU9T3MusKdGFqRHNJYkFnb3RuSkt4QlE#gid=1
// divId is the id of the <div> element I'm displaying in

google.load('visualization', '1.0', {packages: ['table']});
google.setOnLoadCallback(drawChart);

function drawChart() {
    var query = new google.visualization.Query(sheetUrl);
    query.send(handleQueryResponse);
}

function handleQueryResponse(response) {

    var data = response.getDataTable();
    var table = new google.visualization.Table(document.getElementById(divId));
    table.draw(data);
}

You can see the #gid=1 in the URL. I've also tried &gid=1 and &sheet='Volume', which is the name of the tab but when the page loads, the data from the first tab gets rendered.

I have noticed Google sheet urls in the form I have above but also in this form:

https://docs.google.com/spreadsheet/tq?key=0AobNU9T3MusKdGFqRHNJYkFnb3RuSkt4QlE

I haven't been able to find any documentation explicitly explaining the tq endpoint. I tried using a URL in this form but I get timeout error when trying to load the chart. Any one run into this problem or have insight in the tq thing? Thanks!

Edit 2014-02-17:

I've changed my URL to use the tq endpoint and I've tried the following parameters:

#gid=1
&gid=1
#sheet=Volume
&sheet=Volume

When I query for the url in the browser:

https://docs.google.com/spreadsheet/tq?key=0AobNU9T3MusKdGFqRHNJYkFnb3RuSkt4QlE&sheet=Volume

I get the appropriate sheet back. But when I use the Visualization API to query, I get the first sheet.

like image 747
ericso Avatar asked Feb 16 '14 20:02

ericso


2 Answers

The correct URL to pass to the Visualization API that will work with the new Google Sheets is this format:

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

You can use the gid query parameter to pass the ID of the worksheet you want to retrieve. So if your gid is 0, the URL would be:

https://docs.google.com/spreadsheets/d/{key}/gviz/tq?gid=0

You can find additional information regarding URL formats for both the old and new Google Sheets in this bug report.

Hope that helps.

like image 172
donnapep Avatar answered Nov 19 '22 04:11

donnapep


Referencing by sheet name also works as in

https://docs.google.com/spreadsheets/d/{key}/gviz/tq?sheet=MySheetName

Make sure spreadsheet is "new" vs "old" Google Sheets. I banged my head on that one as I thought my sheet was new. Once I created a new style sheet and used donnapep answer I was working again. I then confirmed using sheet=sheetname works as well which was what I was really after.

like image 24
Craig Avatar answered Nov 19 '22 04:11

Craig