如何将Google电子表格中的数据导入到Javascript? [英] How do you Import data from a Google Spreadsheet to 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检索谷歌电子表格的内容,在这里有一些例子:
以字符串形式检索一个数据单元格的基本思想:
- 在Google云端硬盘中创建电子表格。
-
发布电子表格,选择范围A1为第一个单元格。您应该提供以下网址:
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&安培;范围= 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 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.
You can retrieve contents of google spreadsheets from your javascript, and there are examples here in SO:
- Getting value of a cell from google docs spreadsheet into javascript
- Access Google-apps public spreadsheet via Javascript
Basic idea for retrieving one cell of data as a string:
- 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
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屋!