Search code examples
google-apps-scriptgoogle-sheetsexport-to-excel

Automatically export Google Spreadsheet to Excel


I want a Google Apps Script that automatically exports a Google Spreadsheet that I have on Google Drive to .xls or .xlsx by using a trigger that is either time-driven (say every 30 minutes) or event-driven (on every edit made on file). I was based on an old question here on Stack Overflow but since the function oAuthConfig is discontinued, I think I would open a new thread based on OAuth1 library.

Here are all the steps I followed:

  1. While the file was open I went to Tools -> Script Editor... and then I followed these instructions to install the latest OAuth1 library (v.12 and the library project key I used is Mb2Vpd5nfD3Pz-_a-39Q4VfxhMjh3Sh48). I have also tried with the Development mode enabled or disabled.

  2. Then I tried to change the above script based on all the changes of the Google API (see here, here, here, and here).

  3. I ran every function except googleOAuth_(...) and granted whatever permissions they asked for.

  4. Finally, I created a trigger from Resources -> Current project's triggers which calls the function myOnEdit on every edit to the spreadsheet and notifies me on e-mail if it fails.

Here is the script:

function myOnEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var r = s.getActiveCell();
  if( r.getColumn() != 1 ) { //checks the column
    var row = r.getRow();
    var time = new Date();
    time = Utilities.formatDate(time, ss.getSpreadsheetTimeZone(), "MM/dd/yy, hh:mm:ss");
    var id = ss.getId();
    s.getRange('A' + row.toString()).setValue(time); 
    var url = 'https://docs.google.com/feeds/';
    var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',
                                googleOAuth_('docs',url)).getBlob()
    DriveApp.createFile(doc).setName('newfile.xls')
  }
}

function authorise(){
  // function to call to authorize googleOauth
  var id=SpreadsheetApp.getActiveSpreadsheet().getId();
  var url = 'https://docs.google.com/feeds/';
  var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',
                            googleOAuth_('docs',url)).getBlob()
}

function googleOAuth_(name,scope) {
  var service = OAuth1.createService(name);
  service.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  service.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  service.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  service.setConsumerKey('anonymous');
  service.setConsumerSecret('anonymous');
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}

So, it seems that the code doesn't produce any errors but there are 2 problems:

  1. Even if the file newfile.xls is created, it is not what I want. Basically, when I open it on Excel I get the message "The file format and extension of "newfile.xls" don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?", then it waits for a while and when the file opens it seems like the Google Login page in Excel format.

  2. Each time I make an edit on a row, the cell that corresponds to the first column of that row changes to the current timestamp like "07/03/16, 03:58:30" on the original spreadsheet.


Solution

  • Partial Short answer

    XLS is not supported by Google Sheets/Google Drive.

    Explanation

    AFAIK .XLS is not supported by the current version of Google Sheets. Anyway I just tried a URL with the following structure directly on my browser's address bar:

    https://docs.google.com/spreadsheets/d/spreadsheet-key/export?exportFormat=xls
    

    and got

    Sorry, unable to open the file at this time error message