Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function sendEmail() {
try {
var ss = SpreadsheetApp.getActive();
var ssID = ss.getId();
var date = Utilities.formatDate(new Date(), "GMT+5", "dd/MM/yyyy")
var subject = "Subject of Email : " + date;
var message = "Message in email body"
var sheet = DriveApp.getFileById(ssID);
var sentbyName = "Name shown for email sentby";
var email = Session.getEffectiveUser();
// Get Editor and Owner emails for report.
var emails = sheet.getOwner().getEmail();
var editors = sheet.getEditors();
for (var i=0; i < editors.length; i++)
{
emails += ', ' + editors[i].getEmail();
}
Logger.log('Emails: ' + emails);
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ssID + "&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(emails, subject, message, {
attachments: [sheet.getAs(MimeType.PDF), blob],
name: sentbyName
});
} catch (ex) {
Logger.log(ex.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment