如何从 Google 表格导入数据? [英] How do you Import data from a Google Sheets?

查看:37
本文介绍了如何从 Google 表格导入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Google 电子表格的第一个单元格作为字符串用 JavaScript 输出到 HTML 文档?

How to take the first cell of a Google spreadsheet and output it as a string with JavaScript to an HTML document?

我有一个 HTML 文档,标签为:Website.html"

I have an HTML document labeled: "Website.html"

<!DOCTYPE html>
<html>
     <head>
        <script>
          var textDisplay = Some code to import spreadsheet cell text;
          document.getElementById("display").innerHTML = textDisplay;
        </script>
     </head>
    <body>
        <p id="display"></p>
    </body>
</html>

和同一文件夹中的另一个文件,标记为:Spreadsheet.xcel"(或任何文件类型).第一个单元格包含文本:你好".

and another file in the same folder labeled: "Spreadsheet.xcel" (or whatever file type that is). The first cell contains the text: "Hello".

如何将电子表格中的文本导入到 HTML 文档的 JavaScript 中?

How do I make the text in the spreadsheet import into the JavaScript of the HTML document?

推荐答案

您的解决方案将取决于您的数据源;你包括了 ,所以这是一个关于这个的答案.只是说明一个显而易见的开始:Google 电子表格不会是您服务器上的文件,而是位于 Google 云端硬盘中的云"中.

Your solution will depend on your data source; you included google-spreadsheet, so here's an answer about that. Just stating the obvious to start: A google spreadsheet would not be a file on your server, instead it would be in "the cloud" in a Google Drive.

您可以从您的 javascript 中检索 google 电子表格的内容,这里有示例:

You can retrieve contents of google spreadsheets from your javascript, and there are examples here in SO:

以字符串形式检索一个单元格数据的基本思想:

Basic idea for retrieving one cell of data as a string:

  • 在 Google 云端硬盘中,创建您的电子表格.
  • 发布电子表格,为第一个单元格选择区域 A1.应该为您提供一个 URL,如:

  • In Google Drive, create your spreadsheet.
  • Publish the spreadsheet, picking the range A1 for the first cell. You should be provided a URL like:

https://docs.google.com/spreadsheet/pub?key=SPREADSHEET_KEY&single=true&gid=0&range=A1&output=csv

function loadData() {
  var url="https://docs.google.com/spreadsheet/pub?key=p_aHW5nOrj0VO2ZHTRRtqTQ&single=true&gid=0&range=A1&output=csv";
  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status==200){
      document.getElementById("display").innerHTML = xmlhttp.responseText;
    }
  };
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

<html>
  <body>
    <button type="button" onclick="loadData()">Load Spreadsheet Data</button>
    <div id="display"></div>
  </body>
</html>

您也可以在此处将其视为 jsfiddle.

You can also see this as a jsfiddle here.

感谢 GPSVisualizer,他们很友好地发布了一个公开的谷歌我可以在这个例子中使用电子表格.

Thanks to GPSVisualizer, who were kind enough to publish a public google spreadsheet I could use for this example.

这篇关于如何从 Google 表格导入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆