2016-06-29 85 views
0

我想创建一个仪表板来代表我在谷歌电子表格中的一些数据,并且我尽可能地遵循Google图表指南(我不是程序员)。我想出了下面哪个脚本无法运行的脚本,我希望能够帮助其实现。谷歌可视化脚本问题

这里的链接到我的电子表格: https://docs.google.com/spreadsheets/d/1L2wjSj0IYSrA-dwYYly6rjmtw_zMCB6l7FB5B-rcvZc/edit#gid=1303777890

我试图显示为柱形图在Sheet2中(数据2)列d和E(高亮),我还说范围内的URL数据查询“range=D:E

我想出的代码如下。我只是想让它显示图表。任何帮助表示赞赏。

enter code here<html> 

// Load the Visualization API and the corechart package. 
    google.charts.load('current', {'packages':['corechart']}); 

    // Set a callback to run when the Google Visualization API is loaded. 
    google.charts.setOnLoadCallback(drawChart); 

    // Callback that creates and populates a data table, 
    // instantiates the pie chart, passes in the data and 
    // draws it. 

function drawSheetName() { 
    var queryString = encodeURIComponent('SELECT D, E'); 

    var query = new google.visualization.Query(
     'https://docs.google.com/spreadsheets/d/1L2wjSj0IYSrA-dwYYly6rjmtw_zMCB6l7FB5B-rcvZc/edit#gid=1303777890range=D:E'); 
    query.send(handleSampleDataQueryResponse); 
} 

function handleSampleDataQueryResponse(response) { 
    if (response.isError()) { 
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage()); 
    return; 
    } 

    var data = response.getDataTable(); 
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div')); 
    chart.draw(data, { height: 400 }); 
} 
} 

回答

0

我想我已经找到了问题。

// Set a callback to run when the Google Visualization API is loaded. 
    google.charts.setOnLoadCallback(drawChart); 

    // Callback that creates and populates a data table, 
    // instantiates the pie chart, passes in the data and 
    // draws it. 

function drawSheetName() { 
    var queryString = encodeURIComponent('SELECT D, E'); 

    var query = new google.visualization.Query(
     'https://docs.google.com/spreadsheets/d/1L2wjSj0IYSrA-dwYYly6rjmtw_zMCB6l7FB5B-rcvZc/edit#gid=1303777890range=D:E'); 
    query.send(handleSampleDataQueryResponse); 
} 

您使用的drawSheetName()但没有把它在google.charts.setOnLoadCallback()

代码修订

google.charts.setOnLoadCallback(drawSheetName) 

那么对于

var query = new google.visualization.Query(
     'https://docs.google.com/spreadsheets/d/1L2wjSj0IYSrA-dwYYly6rjmtw_zMCB6l7FB5B-rcvZc/edit#gid=1303777890range=D:E'); 

格式改为

var query = new google.visualization.Query(
      'https://docs.google.com/spreadsheets/d/ID/gviz/tq?tq='+queryString); 

查询语言参考

通过Google Visualization API查询语言,您可以使用对数据源的查询执行各种数据操作。

Setting the Query in the Data Source URL

查询字符串可以被添加到使用tq参数的数据源URL。在URL参数中设置查询而不是在JavaScript中允许您轻松使用其他开发人员编写的可视化文件,并且仍然可以自定义查询。

查询字符串必须正确编码为URL参数。您可以使用JavaScript encodeURIComponent函数对URL进行编码,也可以使用本节末尾的编码工具手动对其进行编码。

有关其他信息和样品: