Created
September 5, 2019 13:37
-
-
Save bcawrse/3d5249ce0643e59509e4d5bc2c585d4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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