Last active
February 9, 2025 15:34
-
-
Save awp42/18fa0703060e71a028e4e6a223673aa5 to your computer and use it in GitHub Desktop.
Google Apps email backup - Excel format
This file contains 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
/** There is nothing in this to specifically only send the email daily. FRequency is controll via the triggers in google sheets **/ | |
function onlyToAddTheDriveScope(){ | |
DriveApp.getRootFolder() | |
} | |
function onOpen() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var menuEntries = [ {name: "Send Email", functionName: "sendEmail"}]; | |
ss.addMenu("Scripts", menuEntries); | |
}; | |
function sendEmail() { | |
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId(); | |
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName(); | |
//var email = Session.getUser().getEmail(); | |
var email = "DESTINATION EMAIL ADDRESS"; | |
/** Below will add in the date that the email was generated to the subject. Helpful when it is a daily message **/ | |
var curDate = Utilities.formatDate(new Date(), "GMT+10", "dd/MM/yyyy") | |
var subject = "EMAIL SUBJECT " + curDate ; | |
/** The below will be put into the body of the email **/ | |
var body = "This is an automatically generated email. Please save the attached file to a network location"; | |
var requestData = {"method": "GET", | |
"headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()} | |
}; | |
var url = "https://docs.google.com/spreadsheets/d/" | |
+ ssID + "/export?format=xlsx&id="+ssID; | |
var result = UrlFetchApp.fetch(url , requestData); | |
var contents = result.getContent(); | |
MailApp.sendEmail(email,subject ,body, {attachments:[{fileName:sheetName+".xlsx", content:contents, mimeType:"application//xlsx"}]}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment