我正在尝试使用 javascript 和 jQuery 从 Google Doc 电子表格中获取数据,以便对数字进行一些数学计算。
通过我得到的下一个代码,用于公共(public)电子表格:
function getdata( key, wid, f )
{
return $.getJSON(
'//spreadsheets.google.com/feeds/cells/' +
key + '/' + wid + '/public/basic?alt=json-in-script&callback=?',
function( data ){
/* the content of this function is not important to the question */
var entryidRC = /.*\/R(\d*)C(\d*)/;
var retdata = {};
retdata.mat = {};
for( var l in data.feed.entry )
{
var entry = data.feed.entry[ l ];
var id = entry.id.$t;
var m = entryidRC.exec( id );
var R,C;
if( m != null )
{
R = new Number( m[ 1 ] );
C = new Number( m[ 2 ] );
}
var row = retdata.mat[ R ];
if( typeof( row ) == 'undefined' )
retdata.mat[ R ] = {};
retdata.mat[ R ][ C ] = entry.content;
}
if( typeof( f ) != 'undefined' )
f( retdata )
else
console.log( retdata );
}
);
}
当尝试使用私有(private)数据时,我得到了 XML 格式的数据(使用 URL:
'//spreadsheets.google.com/feeds/cells/'+ key + '/' + wid + '/private/basic'
)。该测试还检查可用性、防火墙、权限设置和当前用户的登录状态。
但是添加最后一部分:?alt=json-in-script&callback=f
以获取 JSON 格式的数据,抛出一个未找到,错误 404。 (如果只添加 alt=json
也会得到)。
情况总结:
public private
XML yes yes
JSON yes Question
在 http://code.google.com/intl/es/apis/gdata/docs/json.html 中描述了 JSON 对 google 的使用
google spreadsheet api 的使用在 http://code.google.com/intl/es/apis/spreadsheets/data/3.0/reference.html#WorksheetFeed
有什么方法可以在不公开文档的情况下使用 javascript 获取 GDoc 电子表格的 JSON 数据?
提前致谢
最佳答案
"Note: Retrieving a feed without authentication is only supported for published spreadsheets. "
很遗憾,因为这将是一个非常有用的功能。实际上,我很高兴了解到至少从已发布的文档中这是可能的。
关于javascript - 有什么方法可以在不公开文档的情况下使用 jquery 获取 Google Doc SpreadSheet 的 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7527720/