【问题标题】:How to use Google visualization API to retrieve google spreadsheet cell value?如何使用谷歌可视化 API 检索谷歌电子表格单元格值?
【发布时间】:2016-02-20 15:17:02
【问题描述】:

我正在尝试获取谷歌电子表格中单元格的值,但它似乎不起作用。

这是我的 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

【问题讨论】:

  • 您是否收到错误消息?这有帮助吗? --> https://docs.google.com/spreadsheets/d/1sA7M5kG6Xo8YScD1Df38PIA_G0bvhGRdqoExXg0KJTs/gviz/tq?headers=1&amp;tq=SELECT A
  • 嗨,是的。我收到“查询错误:请求超时”。还尝试使用您提供的链接及其相同的错误。
  • 来自this question 的评论 -- 查询刚刚超时,因为一些返回的 JSON 代码包含一些空数据点

标签: javascript google-sheets google-visualization


【解决方案1】:

这是我想出的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多