Skip to content

Instantly share code, notes, and snippets.

@awp42
Last active February 9, 2025 15:34
Show Gist options
  • Save awp42/18fa0703060e71a028e4e6a223673aa5 to your computer and use it in GitHub Desktop.
Save awp42/18fa0703060e71a028e4e6a223673aa5 to your computer and use it in GitHub Desktop.
Google Apps email backup - Excel format
/** 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