5

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?

Community
  • 1
  • 1
Nazin
  • 827
  • 3
  • 16
  • 31

1 Answers1

1

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/

Kim T
  • 5,770
  • 1
  • 52
  • 79
  • This will work only for public spreadsheets. I'm considering private as code in my question states. – Nazin Jan 08 '15 at 21:36
  • For private spreadsheets you need to use the Google Drive API to Auth before retrieving a spreadsheet – Kim T Jan 09 '15 at 15:12
  • Oh don't tell... where do you think gapi.auth.getToken().access_token came from? – Nazin Jan 09 '15 at 15:53
  • What value would I use for "key"? Is this identical to "_ID_" in the original question? – jochen May 06 '15 at 15:58
  • @jochen yes you use the Spreadsheet ID as the key, and the spreadsheet needs to be set as public – Kim T May 07 '15 at 16:17