I have a Google spreadsheet with multiple tabs. I have a button on each sheet that calls the functions to create and email a pdf of the active sheet. When the sheets (tabs) are in either the first or second position, the pdf that generates is all the sheets instead of just the active sheet. How can I adjust so it only sends the active sheet each time? It appears to me that the issue is with using the Index number and the getSheets() function. Here is the code:
function sendSheetToPdf(){ // this is the function to call
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet(); // it will send the active sheet.
// if you change the number, change it also in the parameters below
var shName = sh.getName()
var shNum = (ss.getActiveSheet().getIndex() - 1)
sendSpreadsheetToPdf(shNum, sh.getRange('I5').getValue(), sh.getRange('F12').getValue(),sh.getRange('I3').getValue(), sh.getRange('B6').getValue());
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;
var url_base = spreadsheet.getUrl().replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=A4' // paper size
+ '&printtitle=false'
+ '&scale=4'
+ '&portrait=false' // orientation, false for landscape
+ '&sheetnames=false'
+ '&pagenumbers=false' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '.pdf');
if (email) {
var mailOptions = {
attachments:blob, htmlBody:htmlbody
}
MailApp.sendEmail(
email,
subject+" (" + pdfName +")",
htmlbody,
mailOptions);
MailApp.sendEmail(
Session.getActiveUser().getEmail(),
"FRWD "+subject+" (" + pdfName +")",
htmlbody,
mailOptions);
}
}