Google电子表格IMPORTHTML导入Null

时间:2016-02-28 01:56:40

标签: javascript html google-sheets pageload

我有一个从XML URL中提取数据的html文件。

当我通过浏览器浏览html时,我可以看到它在表格中加载数据。

然而,它会在GoogleSpread中提取NULL

无论出于何种原因,无法直接从Google电子表格中提取XML数据。我必须从我的网络服务器中取出它然后将其拉到Google电子表格。

这是html代码:

    <html>
    <head>
    <style>
    table, th, td {
      border: 1px solid black;
      border-collapse:collapse;
    }
    th, td {
      padding: 5px;
    }
    </style>
    </head>

 <body onload="loadXMLDoc()">

  <table>
        <tr>
            <td>2</td>
            <td id="demo"></td>
        </tr>
    </table>


<button type="button" onclick="loadXMLDoc()">Get my CD collection</button>

    <script>
    function loadXMLDoc() {
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          myFunction(xmlhttp);
        }
      };
      xmlhttp.open("GET", "http://www.w3schools.com/xml/cd_catalog.xml", true);
      xmlhttp.send();
    }
    function myFunction(xml) {
      var i;
      var xmlDoc = xml.responseXML;
      var table="<tr><th>Artist</th><th>Title</th></tr>";
      var x = xmlDoc.getElementsByTagName("CD");
      for (i = 0; i <x.length; i++) { 
        table += "<tr><td>" +
        x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
        "</td><td>" +
        x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
        "</td></tr>";
      }
      document.getElementById("demo").innerHTML = table;
    }
    </script>

    </body>
    </html>

以下是Google电子表格公式:

=IMPORTHTML("http://example.com/xml/myscript.html","table",1)

1 个答案:

答案 0 :(得分:0)

原因是因为它正在进行ajax调用 - 例如&#34;示例&#34; xml你的展示实际上正在调用&#34; http://www.w3schools.com/xml/cd_catalog.xml&#34;这就是带有实际数据的网址。

您可以使用

提取所需的数据
=IMPORTXML("http://www.w3schools.com/xml/cd_catalog.xml","//CD")