I have the following code that is working well except that it will only export/email the first sheet in the Spreadsheet (left most sheet) but I need it to send the active sheet that is being edited by the user (the spreadsheet has 12 monthly sheets that a user will export each month). The onEditTrigger function is working as intended. Thanks for any help you can offer.
Question #1 - The exportAndSend function - how do I structure this so that only the active sheet currently being edited by the user will export to PDF?
Question #2 - The exportAndSend function - When it does export the PDF, it exports about 18 pages, where as all the data is within the first 60 lines. How do I get the exported PDF to format so that it only exports the first 60 lines and not a whole bunch of extra empty pages.
Question #3 - the sendEmail function - The attachment is called "Mail Attachment". Is there a way to have the attachment renamed in the email to the variable pdfName?
function onEditTrigger(e){
const specificCell = ("G1");
const specificValue = ("Yes");
let cellCheck = (e.range.getA1Notation() == specificCell)
let valueCheck = (SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('G1').getValue() == specificValue)
if (!(cellCheck && valueCheck)) {
return
}
else {
exportAndSend();
}
}
function exportAndSend () {
var month = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
var coachName = " CoachName ";
var pdfName = coachName + month + " 2022 Mileage";
//How do I get the sheet actively being edited (active sheet) to export to a PDF??
//Is there a way to format the PDF to only export the first 60 lines of the active sheet
let mileage = DriveApp.getFileById('SheetID');
let blob = mileage.getAs('application/pdf');
let pdf = DriveApp.getFolderById('GoogleFolderID')
.createFile(blob)
.setName(pdfName);
sendEmail_(pdf);
}
function sendEmail_(pdf) {
var month = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
var coachName = " CoachName ";
var pdfName = coachName + month + " 2022 Mileage";
GmailApp.sendEmail('
em...@email.com',
coachName + month + " Mileage",
coachName + " Mileage for " + month + " 2022" + '\n\n', {
attachments: [pdf]
});
}