2016-06-29 86 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進行編碼,也可以使用本節末尾的編碼工具手動對其進行編碼。

有關其他信息和樣品: