title: Google-Spreadsheet Script: Send Email with attached XLSX version of Spreadsheet
body: <p>for backup purpose, I'd like to use the below script to convert a Google-Spreadsheet to XLSX and send it to an email address of choice. This script has worked a few weeks ago for some spreadsheets I use. From some other spreadsheets though, I receive the email with a "broken" attachement. Currently, I can not get it to work again, and I suspect a compatibility problem with the code.</p>
  
  <p>Can someone please help me?</p>
  
  <pre><code>    function getGoogleSpreadsheetAsExcel(){
        try {
          var ss = SpreadsheetApp.getActive();
          var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getId() + "&amp;exportFormat=xlsx";
          var params = {
            method      : "get",
            headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
            muteHttpExceptions: true
          };
  
          var blob = UrlFetchApp.fetch(url, params).getBlob();
          blob.setName(ss.getName() + ".xlsx");
          MailApp.sendEmail("amit@labnol.org", "Google Sheet to Excel", "The XLSX file is attached", {attachments: [blob]});
        } catch (f) {
        Logger.log(f.toString());
        }
      }
  </code></pre>
  
  <p>A sample of a "broken" attachement is attached as a printscreen. It still comes in as XLSX, but it's HTML code that says the Document doesn't exist. The link generate by the script is correct though. And when done manually(copy/paste the generated link into browser), it works just fine.</p>
  
  <p>Broken attachement: </p>
  
  <p><a href="https://i.stack.imgur.com/sUSQJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sUSQJ.png" alt="enter image description here"></a></p>