2014-05-05 38 views
4

看来,使用新版本的Google电子表格不再可能使用JS下载整个Google Spreadsheet。到目前为止,我一直在用这个方法(它仍然工作正常,这是先前创建的文件):将google电子表格导出为使用javascript的xlsx

var xhr = new XMLHttpRequest(); 
xhr.open('GET', 'https://docs.google.com/feeds/download/spreadsheets/Export?key=' + id + '&exportFormat=xlsx'); 

xhr.responseType = 'arraybuffer'; 
xhr.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token); 

xhr.onload = function() { 
     ... 
}; 

xhr.send(); 

I have found the new download url

https://docs.google.com/spreadsheets/d/_ID_/export?format=xlsx&id=_ID_ 

但遗憾的是没有Access-Control-Allow-Origin头这样的链接无法访问使用JS。有没有其他的可能性,我可以下载文件?

谷歌云端硬盘API显示出口网址为:

https://docs.google.com/spreadsheets/export?id=_ID_&exportFormat=xlsx 

但也没有Access-Control-Allow-Origin头。

是否有任何其他的可能性,只使用JS下载此文件?

回答

-1

你应该能够使用这个网址下载:

https://docs.google.com/spreadsheet/pub?key=XXX&output=xls 

我已经创建了一个在这里工作的例子:

var xhr = new XMLHttpRequest(); 
xhr.onreadystatechange = function() { 
    if (this.readyState === 4 && this.status === 200) { 
     console.log('success', this.responseText.length); 
    } 
}; 
xhr.open('GET', 'https://docs.google.com/spreadsheet/pub?key=0AsnymCBa0S5PdEtZdnEzcjlTenBIcldRNFMtSUdNUkE&output=xls', true); 
xhr.send(null); 

http://jsfiddle.net/kmturley/b47wno47/

+0

这将只能用于公共电子表格。我正在考虑私人代码在我的问题状态。 – Nazin

+0

对于私人电子表格,您需要在检索电子表格之前使用Google Drive API进行身份验证 –

+0

哦,不要告诉...您认为gapi.auth.getToken()。access_token来自哪里? – Nazin

相关问题