如何查询谷歌电子表格为谷歌图表服务创建数据表

时间:2016-07-06 17:52:28

标签: google-sheets google-visualization

我正在尝试查询Google电子表格,以便为Google图表服务构建数据表。最终目标是将多个电子表格中的图形放入带有HTML服务的页面中。

function initialize() {
        var opts = {sendMethod: 'auto'};
        var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1LFhcZ5_vdghyWhuDNpnp__x2FcA_aUEIvFWOasRvEaA/edit#gid=0', opts);
        query.setQuery('select A, select B');
        query.send(handleQueryResponse);
        }

        function handleQueryResponse(response) {
          if (response.isError()) {
            alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
            return;
            }
        var data = response.getDataTable();

        var options = {'title':'How Much Pizza I Ate Last Night',
                       'width':400,
                       'height':300};
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
        }

当我使用.addRows和.addColumn手动构建数据表时,它可以正常工作。我猜这意味着我不知道从我的查询中得到一个数据表。我不知道为什么。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

" select语句"应该是......

select A, B

VS。

select A, select B

见下面的工作片段......



google.charts.load('current', {
  'callback': initialize,
  'packages': ['corechart']
});

function initialize() {
  var opts = {sendMethod: 'auto'};
  var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1LFhcZ5_vdghyWhuDNpnp__x2FcA_aUEIvFWOasRvEaA/edit#gid=0', opts);
  query.setQuery('select A, B');
  query.send(handleQueryResponse);
}

function handleQueryResponse(response) {
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }
  var data = response.getDataTable();

  var options = {
    'title': 'How Much Pizza I Ate Last Night',
    'width': 400,
    'height': 300
  };
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, options);
}

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
&#13;
&#13;
&#13;