Skip to content

Instantly share code, notes, and snippets.

@mariusom
Last active August 7, 2018 20:49
Show Gist options
  • Save mariusom/5a86a4dcf62768d4b1a1af62f1d7e3c9 to your computer and use it in GitHub Desktop.
Save mariusom/5a86a4dcf62768d4b1a1af62f1d7e3c9 to your computer and use it in GitHub Desktop.
function emailExport() {
var sheetName = 'Enter your sheet name';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
/* Hiding the sheet will keep it from being included in the PDF that is generated below.
This negates the need to copy the single sheet to another spreadsheet. This will hide a single
sheet and print any others to the PDF*/
// sheet.hideSheet();
// Use the code below to hide ALL BUT ONE sheet, in case you have multiple sheets to hide.
function export(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName) {
sheet[i].hideSheet();
}
}
var file = DriveApp.createFile(ss.getBlob());
for (var i = 0; i < sheets.length; i++) {
sheet[i].showSheet();
}
return file;
}
// var pdf = DriveApp.getFileById(ss.getId());
// var theBlob = pdf.getBlob().getAs('application/pdf').setName('fileName');
//var folder = DriveApp.getFoldersByName('Name of your folder').next();
//var newFile = folder.createFile(theBlob);
var newfile = export(sheetName);
// sheet.showSheet(); // Unhide sheet after PDF is generated.
// Set email content
var emailOne = sheet.getRange(2, 5).getValues(); //Change the value to where the email data is in your sheet
var subject = 'Subject';
var message = 'Email Message';
// Email is generated and delivered
MailApp.sendEmail(emailOne, subject, message, {attachments:newFile.getBlob()});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment