简体   繁体   English

从Google云端硬盘电子表格单元获取数据

[英]Getting data from a Google Drive Spreadsheet Cell

I am attempting to display some data in a HTML document coming from a Google Drive Spreadsheet cell by using the following javascript code: 我正在尝试使用以下JavaScript代码在来自Google云端硬盘电子表格单元格的HTML文档中显示一些数据:

var URL = "https://docs.google.com/spreadsheet/pub?key=0AhQNAOBkQ63ldFl6N0tDeTFDYWVXeF8yQ0kwdWtoZnc&gid=0&single=true&gid=0&output=txt&range=";
$.ajax(URL + "D2").done(function(result){ document.getElementById("demo").innerHTML=result; });

This works fine when I have an HTML tag with a "demo" id, but I need the value as a variable so I can display it anywhere in the page and not in a specific HTML element. 当我有一个带有“ demo” ID的HTML标记时,这很好用,但是我需要将该值作为变量,以便可以在页面中的任何位置而不是在特定的HTML元素中显示它。

You'll need to specify async:false because otherwise the variable probably won't end up getting set until after you've tried to use it. 您需要指定async:false,因为否则,除非您尝试使用该变量,否则该变量可能最终不会被设置。 Also, for some scope reasons it's easiest to reference a variable in the context of the window (ie window.variable ). 同样,由于某些范围的原因,最容易在窗口的上下文中引用变量(即window.variable )。

var URL = "https://docs.google.com/spreadsheet/pub?key=0AhQNAOBkQ63ldFl6N0tDeTFDYWVXeF8yQ0kwdWtoZnc&gid=0&single=true&gid=0&output=txt&range=";

window.variable = "test";

$.ajax({url:URL + "D2",async:false}).done(function(result){ window.variable = result; });

document.getElementById("demo").innerHTML = window.variable;

http://jsfiddle.net/8Dk4Y/1/ http://jsfiddle.net/8Dk4Y/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM