简体   繁体   English

在脚本中从电子表格中获取数据:“未定义数据表”

[英]Fetching data from spreadsheet in script: “Data table is not defined”

I'm trying to make a script using the Google Visualization App Geomap. 我正在尝试使用Google Visualization App Geomap制作脚本。 In the example code on the Google Developers page, they are using a hard coded table. 在Google Developers页面上的示例代码中,他们使用的是硬编码表。 What I want to do, is to make code use data from a Google Spreadsheet connected to a Google Form. 我要做的是使代码使用连接到Google Form的Google Spreadsheet中的数据。 More specifically, I want to use the data of the spreadsheet tab «countries» from the following spreadsheet: https://docs.google.com/spreadsheets/d/1l77TXctG6mgva1ggs3iR3eBNx949hzn9SiVjki1v59I/edit?usp=sharing . 更具体地说,我想使用以下电子表格中电子表格标签“国家/地区”的数据: https : //docs.google.com/spreadsheets/d/1l77TXctG6mgva1ggs3iR3eBNx949hzn9SiVjki1v59I/edit?usp=sharing

Somehow this does not work, and I get the error messages listed below. 不知何故,它不起作用,并且我收到下面列出的错误消息。

How can I collect the data from the spreadsheet in the right format? 如何以正确的格式从电子表格中收集数据? I have attempted to use this procedure to collect the data from the spreadsheet: https://google-developers.appspot.com/chart/interactive/docs/spreadsheets . 我试图使用此过程从电子表格中收集数据: https : //google-developers.appspot.com/chart/interactive/docs/spreadsheets

Error message on web page: 网页上的错误消息:

"Data table is not defined"

Error message in Safari Console: Safari控制台中的错误消息:

TypeError: undefined is not an object (evaluating 'new google.visualization.Query’)
drawChart help:113
(anonymous function) help:117

My code 我的密码

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["geomap"]});
  google.setOnLoadCallback(drawMap);

  function drawMap() {
    var opts = {sendMethod: 'auto'};

    // ***My code***
    var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1l77TXctG6mgva1ggs3iR3eBNx949hzn9SiVjki1v59I/edit#gid=957991050', 'sheet=countries', 'headers=1');
    var data = query.send(handleQueryResponse);

    var options = {};
    options['dataMode'] = 'regions';

    var container = document.getElementById('regions_div');
    var geomap = new google.visualization.GeoMap(container);

    geomap.draw(data, options);
  };

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

      return response.getDataTable();
    }

</script>

Hope there is someone out there who can help me with this (hopefully) not so hard task. 希望那里有人可以帮助我(希望如此)完成这项艰巨的任务。 Thank you very much in advance! 提前非常感谢您! :-) :-)

You can get data inside of a callback function handleQueryResponse 您可以在回调函数handleQueryResponse获取data

function handleQueryResponse(response) {
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
  } else {
    var container = document.getElementById('regions_div');
    var geomap = new google.visualization.GeoMap(container);
    var data = response.getDataTable();
    geomap.draw(data, options);
  }
}

Google provides example here . Google 在这里提供了示例。

Also check format which you get in response and use method google.visualization.arrayToDataTable to format data as in example if needed cause it format should be correct. 还要检查响应的格式,并使用google.visualization.arrayToDataTable方法格式化data (如需要的话), 例如 ,因为它的格式应该正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM