Google app scripts: email a spreadsheet as excel

It looks like @Christiaan Westerbeek’s answer is spot on but its been a year now since his post and I think there needs to be a bit of a modification in the script he has given above.

var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];

There is something wrong with this line of code, maybe that exportLinks has now depreciated. When I executed his code it gave an error to the following effect:

TypeError: Cannot read property “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” from undefined.

The workaround is as follows:

The URL in the above line of code is basically the “download as xlsx” URL that can be used to directly download the spreadsheet as an xlsx file that you get from File> Download as > Microsoft Excel (.xlsx)

This is the format:

https://docs.google.com/spreadsheets/d/<<<ID>>>/export?format=xlsx&id=<<<ID>>>
where <<>> should be replaced by the ID of your file.

Check here to easily understand how to extract the ID from the URL of your google sheet.

Leave a Comment