向数据库发送请求(谷歌表)

时间:2017-12-19 18:17:10

标签: javascript google-apps-script google-sheets get xmlhttprequest

我在google工作表中创建了一个脚本。它在Google表格(有点数据库)中提供了我的表格中的单元格结果。我希望结果发送到我的html页面,到目前为止我已尝试使用XMLHttprequest但没有成功。

我做错了什么?

以下是Script

<script>
function load() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "https://script.google.com/macros/s/AKfycby5RzvUWSEOjerJpouxN72wpsgpsF8IWQo2AvjZUdRPcqskz28/exec", true);
  xhttp.send();
}
</script>
<html>
<body>

<h1>Members:</h1>

<button type="button" onclick="load()">Request data</button>

<p id="demo"></p>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

从谷歌阅读电子表格:

  • 下载名为Tabletop的脚本并将其添加到您的HTML中。 (自述文件为here
  • 在Google文档中,转到File菜单,然后选择Publish to the web。然后点击Start publishing。将显示一个网址,例如https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE&output=html,即您将使用的网址。
  • 然后用这个称呼它:

<script type="text/javascript">
  var public_spreadshseet_url = 'https://docs.google.com/spreadsheet/pub?YOUR_URL_HERE';
  
  // YOUR_URL_HERE sample: hl=en_US&hl=en_US&key=0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE&output=html
  // You get this URL from google docs after you publish the spreadsheet


  function init() 
  {
      Tabletop.init(
      { 
          key: public_spreadshseet_url,
          callback: showInfo,
          simpleSheet: true
      })
  }
  
  // This function gets called to give output
  function showInfo(data)
  {
      alert("Successfully processed!")
      console.log(data);           <-- Here is the spreadsheet data
  }  
</script>
<html>
<body>

<h1>Members:</h1>

<button type="button" onclick="init()">Request data</button>

<p id="demo"></p>

</body>
</html>