如何将Google电子表格中的数据导入到Javascript? [英] How do you Import data from a Google Spreadsheet to Javascript?

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

问题描述

我只想知道如何使用google docs或excel文档获取电子表格的第一个单元格,并将其作为HTML中的一个字符串输出到HTML文档中。



我有什么:
标有Website.html的HTML文档

 <!DOCTYPE html> 
< html>
< head>
< script>
var textDisplay =导入电子表格单元格文本的一些代码;
document.getElementById(display)。innerHTML = textDisplay;
< / script>
< / head>
< body>
< p id =display>< / p>
< / body>
< / html>

我在另一个文件夹中标有Spreadsheet.xcel(或任何文件类型)那就是)
第一个单元格包含文本:Hello
如何使电子表格中的文本导入html文档的javascript?


$ b $感谢任何和所有的帮助!
-Rext

解决方案

您的解决方案将取决于您的数据源;您包括谷歌电子表格的问题这里有一个答案。只需说明明显的一点:Google电子表格不会是您的服务器上的文件,而是Google云端硬盘中的云。



您可以从你的javascript检索谷歌电子表格的内容,在这里有一些例子:





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





示例



  function loadData(){var url =https://docs.google.com/spreadsheet/pub?key=p_aHW5nOrj0VO2ZHTRRtqTQ&single=true& GID = 0&安培;范围= A1&安培;输出= CSV; xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function(){if(xmlhttp.readyState == 4&& xmlhttp.status == 200){document.getElementById(display)。innerHTML = xmlhttp.responseText; }}; xmlhttp.open( GET,网址,真实); xmlhttp.send(null);}  

 < html> <身体GT; < button type =buttononclick =loadData()>加载电子表格数据< / button> < div id =display>< / div> < / body>< / html>  



看到这是一个jsfiddle 这里



感谢 GPSVisualizer ,谁能够发布公开的Google电子表格我可以用这个例子。


I simply wish to know how to take the first cell of a spreadsheet either on google docs or from and excel document and output it as a string in javascript in an html document.

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

And I have another file in the same folder labeled: "Spreadsheet.xcel" (or whatever file type that is) The first cell contains the text: "Hello" How do I make the text in the spreadsheet import into the javascript of the html doc?

Thanks for any and all help! -Rext

解决方案

Your solution will depend on your data source; you included , 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.

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:

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.

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

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

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