0

I am looking for a convert current google sheet into PDF and send mail ActiveSheet.

: This code looping all sheet into pdf and emailing me I want mail only active tab.


//this code working fine but looping all sheet
function sendReport() {

  var message = {
    to: "maneeshsinghal9810@gmail.com",
    subject: "Monthly sales report",
    body: "Hi team,\n\nPlease find the monthly report attached.\n\nThank you,\nBob",
    name: "Bob",
    setName: "New",
    attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("New Report")]
  }
  MailApp.sendEmail(message);
}


  // this method is not working  
  // var ss = SpreadsheetApp.getActiveSpreadsheet();
  // var singleSheet = ss.getActiveSheet()

Manish S
  • 111
  • 1
  • 7
  • 1
    I thought that these threads might be useful for your situation. https://stackoverflow.com/q/45781031 https://stackoverflow.com/q/42282117 https://stackoverflow.com/q/49197358 – Tanaike Apr 02 '21 at 11:53
  • SpreadsheetApp.getActiveSpreadsheet() gets the entire file. You will need to specify which sheet in the file with something like SpreadsheetApp.getActive().getSheetByName() or .getSheetByID() – Kris Apr 02 '21 at 15:28

1 Answers1

1

See if this helps

const sendReport = () => {
  const spreadsheet = SpreadsheetApp.getActive();
  const activeSheet = spreadsheet.getActiveSheet();
  const sheets = spreadsheet.getSheets();
  
  //hide all sheets except the active sheet
  sheets.forEach(sh => {
    if (sh.getName() !== activeSheet.getName()) sh.hideSheet()
  })

  const message = {
    to: "maneeshsinghal9810@gmail.com",
    subject: "Monthly sales report",
    body: "Hi team,\n\nPlease find the monthly report attached.\n\nThank you,\nBob",
    name: "Bob",
    setName: "New",
    attachments: [spreadsheet.getAs(MimeType.PDF).setName('New Report')]
  }

  MailApp.sendEmail(message);

  //unhide all sheets
  sheets.forEach(s => s.showSheet());
}
JPV
  • 26,499
  • 4
  • 33
  • 48