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
How do you Import data from a Google 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 电子表格的内容,这里有一些示例:
- 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
- 在 Google 云端硬盘中,创建您的电子表格。
发布电子表格,为第一个单元格选择区域 A1。应该为您提供一个 URL,如:
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.
您也可以在此处将其视为 jsfiddle 。
Thanks to GPSVisualizer, who were kind enough to publish a public google spreadsheet I could use for this example.
感谢GPSVisualizer,他们非常友好地发布了我可以用于此示例的公共谷歌电子表格。