How to export active sheet only from Google Apps Script

661 views
Skip to first unread message

Trent Magis

unread,
Jun 20, 2022, 4:17:19 PM6/20/22
to Google Apps Script Community
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]
                       });
  }

                       
                       
                       
                       
  

Stephen Schlager

unread,
Jun 20, 2022, 10:02:53 PM6/20/22
to Google Apps Script Community
Did you see this post:

It should do everything you want.
You'll just need to make very small modifications and choose which options on the export URL you want to choose.

I got a sample working in a few minutes with the poster's code.

Trent Magis

unread,
Jun 21, 2022, 5:36:22 PM6/21/22
to Google Apps Script Community
This post worked great, thanks for sharing.
Reply all
Reply to author
Forward
0 new messages