DEV Community

Cover image for How to export Google Spreadsheet as MS Excel File
kaushikmw
kaushikmw

Posted on

How to export Google Spreadsheet as MS Excel File

Many times we want to send the Google Spreadsheet as a MS Excel attachment to external email id. There is one way to the file as blob as below

DriveApp.getFileById('AASD12asdA3as').getBlob()
but this will return the file as "PDF" by default.
So to get the file in MS Excel format, one can use below code
var oExportedFile = UrlFetchApp.fetch( Drive.Files.get('AASD12asdA3as').exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
{
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
}
);

Well, this will give us a MS Excel file but the name will be "exports.xlsx", irrespective of the original Google Sheet name! So, to set a custom name, add below line

var oXslFile = DriveApp.createFile(oExportedFile.getBlob()).setName('Custom File Name');

To get MS Document or presentation file, all you need to do is to change the parameter passed to exportLinks property. To know all the possible values, please visit this link

Hope this helps you!
Regards

Top comments (0)