Google Visualization API不支持GID或工作表参数

时间:2014-02-16 20:45:25

标签: javascript google-app-engine google-visualization google-spreadsheet-api

我正在使用Google Visualization Javascript API从Google表格中加载图表并将其显示在div中。我的应用程序托管在Google App Engine上。我使用参数gid=1为工作表提供URL以指定第二个工作表,但显示的图表是第一个工作表。这是我的简化代码(它基本上是文档中提供的示例代码):

// 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);
}

您可以在网址中看到#gid=1。我还尝试了&gid=1&sheet='Volume',这是标签的名称,但是当页面加载时,第一个标签中的数据会被渲染。

我已经注意到我在上面的表格中的Google表格网址,但也是以这种形式:

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

我无法找到任何明确解释tq端点的文档。我尝试使用此表单中的URL,但在尝试加载图表时出现超时错误。任何人都遇到这个问题或者对tq事情有所了解?谢谢!

编辑2014-02-17:

我已更改网址以使用tq端点,我尝试了以下参数:

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

当我在浏览器中查询网址时:

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

我得到了合适的纸张。但是当我使用Visualization API进行查询时,我得到了第一张表。

2 个答案:

答案 0 :(得分:6)

传递给可与新Google表格配合使用的Visualization API的正确网址是以下格式:

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

您可以使用gid查询参数传递要检索的工作表的ID。因此,如果您的gid为0,则网址为:

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

您可以在this bug report中找到有关旧版和新版Google表格的网址格式的其他信息。

希望有所帮助。

答案 1 :(得分:5)

按工作表名称引用也适用于

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

确保电子表格是&#34; new&#34; vs&#34; old&#34; Google表格。当我认为我的床单是新的时,我猛烈地抓住那个。一旦我创建了一个新的样式表并使用了donnapep的答案,我就再次工作了。然后我确认使用了sheet = sheetname,这就是我真正想要的。

相关问题