从Google Drive电子表格单元格获取数据

Getting data from a Google Drive Spreadsheet Cell

本文关键字:获取 数据 单元格 电子表格 Google Drive      更新时间:2023-09-26

我正试图使用以下javascript代码在来自Google Drive电子表格单元格的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; });

当我有一个带有"demo"id的HTML标记时,这很好,但我需要将该值作为变量,这样我就可以在页面中的任何地方显示它,而不是在特定的HTML元素中。

您需要指定async:false,因为否则,在您尝试使用变量之前,可能不会设置该变量。此外,由于某些范围原因,在窗口的上下文中引用变量(即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/