您如何从Google表格导入数据? [英] How do you Import data from a Google Sheets?
问题描述
如何获取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?
我有一个标记为"Website.html"的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"(或任何文件类型)的另一个文件.第一个单元格包含文本:"Hello".
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-spreadsheet ,这是一个答案.只需说明显而易见的内容即可: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电子表格的内容,SO中有一些示例:
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:
- 在Google云端硬盘中,创建电子表格.
-
发布电子表格,为第一个单元格选择范围A1.应该为您提供一个网址,例如:
- In Google Drive, create your spreadsheet.
Publish the spreadsheet, picking the range A1 for the first cell. You should be provided a URL like:
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 ,它很友善,可以公开发布我可以在此示例中使用的Google电子表格.
Thanks to GPSVisualizer, who were kind enough to publish a public google spreadsheet I could use for this example.
这篇关于您如何从Google表格导入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!