Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting google spreadsheet as xlsx using javascript

It seems that with new version of Google spreadsheet it is no longer possible to download entire Google Spreadsheet using JS. So far I have been using this method (which still works fine for files which were created earlier):

 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_

But unfortunately there is no Access-Control-Allow-Origin header so the link cannot be accessed using JS. Is there any other possibility I can download the file?

Google Drive API displays export url as:

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

But there is also no Access-Control-Allow-Origin header.

Is there any other possibility to download this file using only JS?

like image 246
Nazin Avatar asked Nov 23 '22 21:11

Nazin


1 Answers

You should be able to download it using this url:

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

I've created an example that works here:

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/

like image 119
Kim T Avatar answered Dec 15 '22 22:12

Kim T