【问题标题】:using google.visualisation.data.join with doGet() - ReferenceError: "google" is not defined使用 google.visualisation.data.join 和 doGet() - ReferenceError: "google" is not defined
【发布时间】:2014-10-29 11:22:53
【问题描述】:

我正在尝试通过匹配几列来加入两个工作表。我将它们都加载到我的脚本中,但是我很确定我错误地使用了 join 方法。我做错了什么?

这是整个代码,部署为网络应用程序。

  function doGet() {

  //get the first table  
  var mainsheet = SpreadsheetApp.openById('1pGxg4bEaoNzkL-JS22GO2-eo7K4UlxPNcjD4Na-6Eik').getSheetByName('Form Responses 1');  
  var maintable = mainsheet.getDataRange();

  //get the second table
  var subsetsheet = SpreadsheetApp.openById('1pGxg4bEaoNzkL-JS22GO2-eo7K4UlxPNcjD4Na-6Eik').getSheetByName('Latest Rows');  
  var subsettable = subsetsheet.getDataRange();

  //join the tables - this is where i get the 'ReferenceError: "google" is not defined. ' error 
  var joinedtable = new google.visualisation.data.join(subsettable, maintable, 'left', [[0,1],[1,3],[2,4]],[],[2]);

  //make a chart from the joined table for display
  var joinedtablechart = Charts.newTableChart().setDataTable(joinedtable).build();

  //display the joined table  
  var siteProfiles = UiApp.createApplication();
  siteProfiles.add(joinedtablechart);
  return siteProfiles;
}

这是线索吗?

Uncaught ReferenceError: google is not defined

感谢指点!

【问题讨论】:

    标签: javascript google-apps-script


    【解决方案1】:

    google.visualisation.data.join 不是 Google Apps 脚本。

    通过Charts Service 在 GAS 中提供可视化服务。有一个tutorial 可以帮助您入门。

    相反,使用模拟google.visualisation.data.join 的行为并返回组合DataTable 的函数来构建您的“加入”数据表。您或许可以改编 Script Examples site 中的 buildFromSpreadsheet(range) 示例。

    【讨论】:

    • 感谢您的澄清。我已经按照教程进行操作,它很有帮助,据我所知,除了加入尝试之外,我的代码还不错。这是否意味着我根本无法在我的脚本中使用 google.visualisation.data.join?
    • 不,您将无法直接从 UiApp 代码调用任何外部 javascript 服务。我已添加到答案中...您需要自己“加入”。
    【解决方案2】:

    我得到了这个工作。取消注释以检查是否正确返回了各个表(这就是我的问题)。

    <html>
      <head>
        <!--Load the AJAX API-->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
    
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1.0', {'packages':['table']});
    
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawJoin);
    
    
    function drawJoin() {
    
           var querymain = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1pGxg4bEaoNzkL-JS22GO2-eo7K4UlxPNcjD4Na-6Eik/gviz/tq?gid=178275599');
           querymain.send(handleQueryResponseMain);
    
           function handleQueryResponseMain(responsemain) {
    
               if (responsemain.isError()) {
                       alert('Error in query: ' + responsemain.getMessage() + ' ' + responsemain.getDetailedMessage());
                       return;
                   }
    
               var datamain = responsemain.getDataTable();
    
    //           var chartmain = new google.visualization.Table(document.getElementById('chartmaindiv'));
    //           chartmain.draw(datamain, null);
    
               var querysubs = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1pGxg4bEaoNzkL-JS22GO2-eo7K4UlxPNcjD4Na-6Eik/gviz/tq?gid=1728586563');
               querysubs.send(handleQueryResponseSubs);
    
               function handleQueryResponseSubs(responsesubs) {
    
                   if (responsesubs.isError()) {
                       alert('Error in query: ' + responsesubs.getMessage() + ' ' + responsesubs.getDetailedMessage());
                       return;
                       }
    
                   var datasubs = responsesubs.getDataTable();
    
    //               var chartsubs = new google.visualization.Table(document.getElementById('chartsubsdiv'));
    //               chartsubs.draw(datasubs, null);
    
                   var joineddt = google.visualization.data.join(datamain, datasubs, 'inner', [[1,0],[3,1],[4,2]],[2],[2]);
    
                   var chartjoin = new google.visualization.Table(document.getElementById('chartjoindiv'));
                   chartjoin.draw(joineddt, null);
                   }
              }
        }       
    
    
        </script>
      </head>
    
      <body>
    <!--        <div id="chartmaindiv"></div>
        <div id="chartsubsdiv"></div>
    -->
        <div id="chartjoindiv"></div>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2014-03-03
      • 2018-03-01
      • 2017-07-29
      • 1970-01-01
      相关资源
      最近更新 更多