Search code examples
google-apps-scriptv8google-apps-script-runtime

problem using MailApp.sendEmail(emailAddress, subject, message); with the new Chrome V8 runtime


I’m running some Google Apps Script that intereracts with a Google Sheet that extracts and compares some values as the sheet is updated and sends an email if certain criteria are exceeded. It has worked solidly for over a year but recently stopped and I’ve found the problem to be with the new Chrome V8 runtime environment. If I run the legacy java environment – it works, no problem.

The script looks for a condition whereby a threshold, set in the spreadsheet is exceeded – if so it should send an email. I have proven that the script identifies the threshold exceeded condition, that it can write and read values from the spreadsheet and that it can read and write the email address from a cell.

It seems that the MailApp.sendEmail(emailAddress, subject, message); doesn't work. Any suggestions greatly appreciated.

Script extract is as follows:

var DP_THRESH1 = doc.getSheetByName("DASHBOARD").getRange("L11"); //THRESHOLD
var DP_THESHOLD1 = DP_THRESH1.getValue();
var DP_SPREAD1 = doc.getSheetByName("DASHBOARD").getRange("H11"); //CALCULATED SPREAD
var DP_LASTSPREAD1 = DP_SPREAD1.getValue();
if (DP_LASTSPREAD1 < DP_THESHOLD1){
  // SET TO ZERO IS READY TO EMAIL STATE - THRESHOLD NOT EXCEEDED
  doc.getSheetByName("mailSheet").getRange('N2').setValue('0');
}
//EMAIL STATUS = 0 = READY TO EMAIL - 1 = HAVE SENT EMAIL
var EMAIL_status = doc.getSheetByName("mailSheet").getRange("N2"); 
var EMAIL_oneshot = EMAIL_status.getValue();
      
if ((DP_LASTSPREAD1 > DP_THESHOLD1) && (EMAIL_oneshot == 0)) {
  // IF THRESHOLD EXCEEDED AND NO EMAIL SENT
  doc.getSheetByName("mailSheet").getRange('N2').setValue('1'); 
  // Fetch the email address
  var emailRange =  doc.getSheetByName("mailSheet").getRange("B2");
  var emailAddress = emailRange.getValues();
  // Send Alert Email.
  var message = 'measured value is ' + DP_LASTSPREAD1;
  var subject = 'Possible Choke Alert';
  MailApp.sendEmail(emailAddress, subject, message);
}  

Solution

  • Issue:

    The script works in the Rhino runtime, because behind the scenes (if the 2D array has only one element) the script is converting the array to a string when calling sendEmail. This doesn't seem to be the case in V8, and you have to provide a string. Interestingly, this behaviour was already reported in Issue Tracker.

    In any case, you should be using getValue() instead of getValues(), if you want to get information from a single cell.

    Reference: