Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Google Docs-File to an Excel-File (XLSX)

The image shows the code who is updated. 1

The var "xlsFile" is undefined, why? How can I convert the Googledocs-File to an Excel-File with (GoogleDocs) ScriptEditor

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

 function test(){
      var id = '#'
      exportToXls(id)
 }

    function exportToXls(id){
       var mute =  {muteHttpExceptions: true };
       var name = DriveApp.getFileById(id).getName()
       var url = 'https://docs.google.com/feeds/';
       var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',     mute).getBlob()
       var xlsfile = DocsList.createFile(doc).rename(name+'.xlsx')
    }
like image 579
dave872 Avatar asked Dec 03 '14 16:12

dave872


People also ask

How do I convert a Google Doc to Excel?

Right-click the Google Sheet and select “Download” from the menu. You will see your computer's standard “save” window to save your Excel file. Here, select a folder to save your file, enter a name for your file, and click “Save.” And your Google Sheet is now available as an Excel file on your computer.

Does Google Docs support XLSX?

Google Drive has a convenient feature for users who import Excel files frequently, allowing you to automatically convert Excel or XLSX Spreadsheets to Google Sheets. Start by opening Settings from the main Google Drive interface.


1 Answers

Using the Drive API, we can get more information about files than is available through the DriveApp methods. Check out the file data, especially exportLinks. Those links contain the magic that will let us get an XLS file. (For fun, put a breakpoint after file is assigned, and check what information you have to play with.)

This script uses the Advanced Drive Service, which must be enabled. A more complete version, with error checking, is available in this gist.

/**
 * Downloads spreadsheet with given file id as an Excel file.
 * Uses Advanced Drive Service, which must be enabled. * Throws if error encountered.
 *
 * @param {String}   fileId       File ID of Sheets file on Drive.
 */
function downloadXLS(fileId) {
  var file = Drive.Files.get(fileId);
  var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];

  var options = {
    headers: {
      Authorization:"Bearer "+ScriptApp.getOAuthToken()
    },
    muteHttpExceptions : true        /// Get failure results
  }

  var response = UrlFetchApp.fetch(url, options);
  var status = response.getResponseCode();
  var result = response.getContentText();
  if (status != 200) {
    // Get additional error message info, depending on format
    if (result.toUpperCase().indexOf("<HTML") !== -1) {
      var message = strip_tags(result);
    }
    else if (result.indexOf('errors') != -1) {
      message = JSON.parse(result).error.message;
    }
    throw new Error('Error (' + status + ") " + message );
  }

  var doc = response.getBlob();
  //DocsList.createFile(doc).rename(file.title + '.xlsx') // Deprecated
  DriveApp.createFile(doc).setName(file.title + '.xlsx');
}
like image 174
Mogsdad Avatar answered Oct 06 '22 18:10

Mogsdad