2014-10-29 41 views
-1

我試圖通過匹配幾列來連接兩個工作表。我將它們都加載到我的腳本中,但後來我很確定我錯誤地使用了連接方法。我究竟做錯了什麼?使用google.visualisation.data.join和doGet() - ReferenceError:未定義「google」

這是整個代碼,部署爲一個Web應用程序。

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

謝謝指點!

回答

0

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

GAS通過Charts Service提供可視化服務。有一個tutorial幫助你開始。

取而代之的是,使用模擬google.visualisation.data.join的行爲的函數構建您的「加入」數據表,並返回合併的DataTable。您大概可以調整Script Examples site中的buildFromSpreadsheet(range)示例。

+0

感謝您的澄清。我遵循教程,它是有幫助的,並且據我看到我的代碼除了連接嘗試之外沒有問題。這是否意味着我將無法在腳本中使用google.visualisation.data.join? – Lukasz 2014-10-30 02:44:25

+0

不,您將無法直接從UiApp代碼調用任何外部JavaScript服務。我已經添加到答案了......你需要做出自己的「加入」。 – Mogsdad 2014-10-30 11:35:16

0

我得到了這個工作。取消註釋以檢查單個表格是否正確返回(這是我個案中的問題)。

<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>