The code below works to convert and send a google sheets as an excel file over email, but the file does not have the actual excel, just Unauthorized Error 401.
function getGoogleSpreadsheetAsExcel(){
try { var ss = SpreadsheetApp.getActive();
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getId() + "&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("Horgstar1212@gmail.com", "Google Sheet to Excel", "The XLSX file is attached", {attachments: [blob]});}
catch (f) {
Logger.log(f.toString()); }
}
I believe it has something to do with authorization to access the file, but I am very new to coding and API's, so any help would be appreciated.