0

I am using this code: https://stackoverflow.com/a/45781391/13875010 I only want to send sheet #3 to the email address located in cell A1 on sheet three. I run the script and nothing happens. What am I doing wrong?

function sendSheetToPdfwithA1MailAdress(){ // this is the function to call
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheets()[3]; // it will send sheet 0 which is the first sheet in the spreadsheet.
  // if you change the number, change it also in the parameters below
  var shName = sh.getName()
  sendSpreadsheetToPdf(3, shName, sh.getRange('A1').getValue(),"test email with the address in cell A1 ", "This is it !");
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = spreadsheet.getId()  
  var sheetId = sheetNumber ? spreadsheet.getSheets()[3].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
      + '&portrait=true'    // orientation, false for landscape
      + '&fitw=true'        // fit to width, false for actual size
      + '&sheetnames=true&printtitle=false&pagenumbers=true'  //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 +")", 
      "html content only", 
      mailOptions);

MailApp.sendEmail(
      Session.getActiveUser().getEmail(), 
      "FRWD "+subject+" (" + pdfName +")", 
      "html content only", 
      mailOptions);
  }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • At first, I have to apologize for my poor English skill. If `the email address located in cell A1 on sheet three` is the cell "A1" on the 3rd tab in Google Spreadsheet, when you modify from `var sh = ss.getSheets()[3];` and `var sheetId = sheetNumber ? spreadsheet.getSheets()[3].getSheetId() : null; to `var sh = ss.getSheets()[2];` and `var sheetId = sheetNumber ? spreadsheet.getSheets()[2].getSheetId() : null;`, respectively, what result will you obtain? Because the start index of array is `0` and `ss.getSheets()[2]` is the 3rd tab. – Tanaike Feb 16 '21 at 02:03
  • Thank you so much! This fixed it! – Leah Greenberg Feb 16 '21 at 15:50
  • Thank you for replying. I'm glad your issue was resolved. When your issue was resolved, can you post it as an answer? By this, it will be useful for other users how have the same issue. – Tanaike Feb 17 '21 at 00:30

1 Answers1

0

I was using the wrong number...Sheet 3 is number 2.