Formatting date error in email – Google Apps Script

Posted on

Formatting date error in email – Google Apps Script – Whether you’re just starting out with Google Sheets or are a seasoned pro, sooner or later one of your formulas will give you a formula or script parse error message rather than the result you want. However, every Formula user, irrespective of whether it’s a beginner or expert, has invariably come across a formula parse error in Google Sheets at least once in their life (and if you haven’t. you soon will). It can be frustrating, especially if it’s a longer formula where the formula parse error may not be obvious. In this article, we will talk about some common script or formula parse errors in Google Sheets, like Formatting date error in email – Google Apps Script, and how to fix it.

I just want to format the dates in row[17] and row[18] to format to 02/04/2021 or MM/DD/YYYY. I’m unsure as why it doesn’t work and I’ve researched for hours. Any advice would be great since I’m teaching myself.

function sendOutcomeEmail() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Outcomes");
  var startRow = 3;
  var numRows = sheet.getLastRow();
  var dataRange = sheet.getRange(startRow, 1, numRows, 21);
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var OutcomeDate = Utilities.formatDate(row[17],"GMT-5","MM/dd/yyyy");
    var AdjDate = Utilities.formatDate(row[3],"GMT-5","MM/dd/yyyy");
    var emailAddress = row[19];
    var message = 
"The outcome was recorded on "+ OutcomeDate
+"Participant: " + row[5]
+"n"+"NYSID: " + row[6]
+"n"+"Docket Number: " + row[7]
+"n"+"Adjournment Date: " + AdjDate
+"n"+"Adjournment Part: "+row[4]
+"Outcome: "+row[18];
    var emailSent = row[20];
    if (emailSent !== EMAIL_SENT && row[18] !== "") {
      var subject = 'ATI Report Notice: Outcome Update';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 21).setValue(EMAIL_SENT);

Answer :