【问题标题】:Exporting google spreadsheet as xlsx using javascript使用 javascript 将谷歌电子表格导出为 xlsx
【发布时间】:2014-05-05 16:01:59
【问题描述】:

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

 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 访问链接。我还有其他可能下载文件吗?

Google Drive API 将导出网址显示为:

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

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

有没有其他方法可以只用 JS 下载这个文件?

【问题讨论】:

    标签: javascript google-drive-api google-spreadsheet-api


    【解决方案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/

    【讨论】:

    • 这仅适用于公共电子表格。在我的问题状态中,我正在考虑将私有代码作为代码。
    • 对于私人电子表格,您需要在检索电子表格之前使用 Google Drive API 进行身份验证
    • 哦,别说……你认为 gapi.auth.getToken().access_token 是从哪里来的?
    • 我会为“key”使用什么值?这是否与原始问题中的“ID”相同?
    • @jochen 是的,您使用电子表格 ID 作为密钥,电子表格需要设置为公开
    猜你喜欢
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多