Html 如何从 Google 表格导入数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16485255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:17:15  来源:igfitidea点击:

How do you Import data from a Google Sheets?

javascripthtmlgoogle-sheets

提问by CTOverton

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

如何获取 Google 电子表格的第一个单元格并将其作为带有 JavaScript 的字符串输出到 HTML 文档?

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

我有一个 HTML 文档,标签为:“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>

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

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

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

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

回答by Mogsdad

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.

您的解决方案将取决于您的数据源;你包含了google-spreadsheet,所以这里有一个关于这个的答案。只是说明一个显而易见的开始:谷歌电子表格不会是您服务器上的文件,而是位于谷歌云端硬盘中的“云”中。

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

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

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

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

Example

例子

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>

You can also see this as a jsfiddle here.

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

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

感谢GPSVisualizer,他们非常友好地发布了我可以用于此示例的公共谷歌电子表格。