使用 javascript 将谷歌电子表格导出为 xlsx

Naz*_*zin 5 javascript google-spreadsheet-api google-drive-api

似乎使用新版本的谷歌电子表格不再可能使用 JS 下载整个谷歌电子表格。到目前为止,我一直在使用这种方法(对于之前创建的文件仍然可以正常工作):

 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();
Run Code Online (Sandbox Code Playgroud)

我找到了新的下载网址

https://docs.google.com/spreadsheets/d/_ID_/export?format=xlsx&id=_ID_
Run Code Online (Sandbox Code Playgroud)

但不幸的是,没有Access-Control-Allow-Origin标题,因此无法使用 JS 访问该链接。我还有其他可能下载文件吗?

Google Drive API 将导出 url 显示为:

https://docs.google.com/spreadsheets/export?id=_ID_&exportFormat=xlsx
Run Code Online (Sandbox Code Playgroud)

但也没有Access-Control-Allow-Origin标题。

是否有其他可能仅使用 JS 下载此文件?

Kim*_*m T 1

您应该能够使用以下网址下载它:

https://docs.google.com/spreadsheet/pub?key=XXX&output=xls
Run Code Online (Sandbox Code Playgroud)

我创建了一个可以在这里使用的示例:

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);
Run Code Online (Sandbox Code Playgroud)

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