Sudden Script Error with Email Google Sheet to PDF

52 Views Asked by At

I have a Google spreadsheet with a script button to email the sheet as a PDF to the emails in B5. This has been working great for almost a year, no code changed, then all of a sudden today the script stopped working and we have an error:

"Exception: The parameters (String, String, String, String, (class)) don't match the method signature for MailApp.sendEmail."

It appears there might have been an update to the MailApp.sendEmail call but I checked their page and I can't seem to find the update or differentiator between what they require and what my old code was; so I'm not sure how to update it correctly. I read through several postings here on Stack Overflow and Reddit where people had similar problems, but slightly different, and I wasn't able to find the solution. Can someone please help me?

Link to MailApp.sendEmail call: https://developers.google.com/apps-script/reference/mail/mail-app

Here is my code:

//EMAIL SHEET AS PDF
//Type First Last name into B4, must be spelled correctly for the query to pull records into the sheet.
//Enter Email Address into B5 separated by a comma
//Select Email Button


function emailPDFofTraining(){ // this is the function to call
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheets()[0]; // 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(0, shName, sh.getRange('B4').getValue(),"Mountain Training");
}
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 name = SpreadsheetApp.getActiveSheet().getRange('B3').getValue();
  var date = SpreadsheetApp.getActiveSheet().getRange('B6').getValue();
  

  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 + '-' + name + '.pdf');
  if (email) {
    var mailOptions = {
      attachments:blob, htmlBody:htmlbody
    }
MailApp.sendEmail(
      email, Session.getActiveUser().getEmail(), 
      subject+ pdfName + '-' + name + ".pdf')", 
      "Mountain Training Certification is attached for "+name+". .or support, contact the Mountain Project Manager or reply to this email.",
      mailOptions);
  }
}
1

There are 1 best solutions below

0
On

Issue:

You are providing five parameters to sendEmail. But out of all the different sendEmail methods, there is no implementation that accepts five parameters:

That's the cause of the error you're getting.

Solution:

I'm not sure what exactly you want to do, but my guess, for what it's worth, is that you wanted to use this method: sendEmail(recipient, subject, body, options), and that you provided two parameters for the recipient: email and Session.getActiveUser().getEmail().

In this case, you should choose which one of them you want to be the recipient, and remove the other one.

If you want the choice to depend on whether the argument email was provided, you could use a ternary operator:

email ? email : Session.getActiveUser().getEmail()