I am trying to select a specific sheet from the spreadsheet and attach it as a PDF to the email before sending it to the recipient.
Now, the code below works, but its a workaround using for loop and if-else to eliminate all other unwanted sheets in the from the report. This code also closes my active sheets and reopens
My main trouble is getting the specific sheet in the attachment so that I do not have to use a for loop
Any help is greatly appreciated! :)
function sendReport() {
var sheetName="Report";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++)
if (sheets[i].getSheetName() !== sheetName) {
sheets[i].showSheet();
}
var message = {
to: "example@gmail.com",
subject: "ICT Online Helpdesk Report",
body: "Hi team,\n\nPlease find the monthly report attached.\n\nThank you,\nBob",
name: "ICT Helpdesk",
attachments: [ ss.getAs(MimeType.PDF).setName("Monthly Ticket report")],
}
MailApp.sendEmail(message);
for (var i = 15; i < sheets.length; i++) {
sheets[i].showSheet()
}
}