2016-02-20 128 views
1

我試圖在谷歌電子表格上獲得一個單元格的值,但它似乎不起作用。如何使用Google可視化API檢索Google電子表格單元格值?

這裏是我的javascript代碼:

<html> 

<script> 
    google.load('visualization', '1', {'packages':['corechart']}); 
    google.setOnLoadCallback(loadChart); 

    function loadChart() { 

      var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1sA7M5kG6Xo8YScD1Df38PIA_G0bvhGRdqoExXg0KJTs/edit#gid=0'); 

      query.send(handleQueryResponse); 
    } 

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

    var datatable = response.getDataTable(); 
    var result = datatable.getValue(0,0); 

    document.getElementById('output1').value = result; 
    document.getElementById('output2').innerHTML = result; 
    document.getElementById('output3').textContent = result.toString(); 
    } 

    </script> 
</head> 

<body> 

    Test 1: There are <input id="output1"> value <br> 

    Test 2: There are <p id="output2"> value </p> 

    <p> Test 3: There are <a id="output3"> </a> value </p> 

</body> 

的電子表格網址google spreadsheet

+0

你會收到一個錯誤?這有幫助嗎? - >'https://docs.google.com/spreadsheets/d/1sA7M5kG6Xo8YScD1Df38PIA_G0bvhGRdqoExXg0KJTs/gviz/tq?headers=1&tq=SELECT A' – WhiteHat

+0

嗨,是的。我收到「查詢中的錯誤:請求超時。」還嘗試使用您提供的鏈接以及相同的錯誤。 – manicdav

+0

來自[此問題]的評論(http://stackoverflow.com/q/35457016/5090771) - 查詢只是超時,因爲一些返回的JSON代碼包含一些空數據points_ – WhiteHat

回答

0

這是我想出了:

<html> 
<head> 
    <script type='text/javascript'> 
     window.onpageshow = function() { 
      google.load('visualization', '1', { 
       callback: function() { 
        var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1sA7M5kG6Xo8YScD1Df38PIA_G0bvhGRdqoExXg0KJTs/gviz/tq?tqx=out:html&tq?gid=0&headers=0&range=A1:C'); 
        query.send(displayData); 
       } 
      }); 

      function displayData(response) { 
       numval = response.getDataTable().getValue(0,0); 
       document.getElementById('data').innerHTML = numval; 
      } 
     } 
    </script> 
</head> 
<body> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <div id="data"></div> 
</body> 
</html> 
相關問題