从工作表抛出的Google图表-选择特定范围后,给定轴上的所有序列都必须具有相同的数据类型错误

时间:2019-02-26 02:49:20

标签: charts google-visualization

Sheets and Range Selection

当我尝试在工作表范围为“ A:B”的代码下运行时,它可以工作,而在选择“ E:F”时,则抛出错误。 请参考图片中的示例数据,并可以访问电子表格以检查数据。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Excel Pull Test</title>
    <script type="text/javascript" src="https://www.google.com/jsapi"> 
  </script>
    <script type="text/javascript">
  // google.charts.load('current', {'packages':['corechart']});

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

  google.setOnLoadCallback(drawSheetName);

   // Load the Visualization API and the corechart package.


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

function drawSheetName() {
      var queryString = encodeURIComponent('SELECT A:B10');

      var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1Di7YkPars5zJs512RhM1wLEhfxUQfavgs6Z2GCIppV4/edit#gid=0&headers=1&tq='+queryString);

      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.BarChart(document.getElementById('chart_div'));
      chart.draw(data, { height: 400 });
    }
  </script>
</head>

<body>
  <div id="chart_div"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在工作表的网址中


更改-> edit#-到-> gviz/tq?

如...

var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1Di7YkPars5zJs512RhM1wLEhfxUQfavgs6Z2GCIppV4/gviz/tq?gid=0&headers=1&tq='+queryString);

这将使您选择特定的列...

请参阅以下工作片段,该片段仅选择列A ...

google.charts.load('current', {
  packages:['table']
}).then(drawSheetName);

function drawSheetName() {
  var queryString = encodeURIComponent('SELECT A');

  var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1Di7YkPars5zJs512RhM1wLEhfxUQfavgs6Z2GCIppV4/gviz/tq?gid=0&headers=1&tq='+queryString);

  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.Table(document.getElementById('chart_div'));
  chart.draw(data, { height: 400 });
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>


还,您使用的是旧版的Google图表。
jsapi应该不再使用,请参见update library loader code
这只会更改load语句。

来自...

<script src="https://www.google.com/jsapi">

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

到...

<script src="https://www.gstatic.com/charts/loader.js"></script>

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