user1413
user1413

Reputation: 537

Cannot find function getMonth in object 33463

I have created a script to send emails to a specific people with Birthday Reminder. This use to work till day before yesterday. I don't know why am I getting this error that Cannot find function getMonth, Can anyone tell where is the mistake

function emailAlert() {

  // Short circuit if email notice is set to "No". This causes an error and the script stops.
  if (turnOnEmailNotice.toLowerCase() == "no")
  { 
    Logger.log("The Email Notification is NOT turned ON. System will Exit.");
    exit 
  } 

  //Get the total number of filled row in the sheet.
  var currentRowAT = 2;
  var currentCellValueAT = "start";
  while (currentCellValueAT != ""){
    if (currentCellValueAT = birthdaysSheet.getRange("G" + currentRowAT).getValue() != ""){
    currentRowAT = currentRowAT +1;
    }
  }
  var birthdaysSheetLastRow = currentRowAT - 1;

  // Get today's Date (with Month, Date and Year)
  var today = new Date();
  var todayMth = today.getMonth()+1;
  var todayDate = today.getDate();
  var todayYear = today.getFullYear();


  // Check sheet cell for match to alertDate, k is the current row number being checked. Starting at 2 as the row #1 is for the headers
  for (k=2; k < birthdaysSheetLastRow + 1; k++)
  {
     var targetBday = new Date();
     targetBday = birthdaysSheet.getRange("P" + k).getValue();

     // If Birthday is not speicified, continue with the next row
     if (targetBday == ""){continue};

     var unadjTargetBday = new Date();
     var unadjTargetBdayMth = targetBday.getMonth()+1;
     var unadjTargetBdayDate = targetBday.getDate();
     var unadjTargetBdayYear = targetBday.getFullYear();
     var unadjTargetBday = targetBday;


     targetBday.setDate(targetBday.getDate()-daysInAdvance); // Calculating how many days in advance you want to trigger the notification. This is set in Settings Tab.
     var targetBdayMth = targetBday.getMonth()+1;
     var targetBdayDate = targetBday.getDate();

     if (targetBdayMth + " " + targetBdayDate == todayMth + " " + todayDate)
     {
       var targetBirthDateYearsOld = (today.getYear() - unadjTargetBday.getYear())-1900;
       prepareAndSendEmail(k, targetBirthDateYearsOld);       
     }
   } 
}

Upvotes: 3

Views: 2501

Answers (2)

Wim den Herder
Wim den Herder

Reputation: 1305

getValue will return the type string/date/number depending on the cell type in the spreadsheet (see menu Format -> Number). To be sure, just always convert to Date Type. This is the right way to convert it:

var targetBday = new Date(birthdaysSheet.getRange("P" + k).getValue());

Upvotes: 3

Wicket
Wicket

Reputation: 38436

Short answer

Check that the cell value is a valid date for Google Sheet.

Explanation

From https://developers.google.com/apps-script/reference/spreadsheet/range#getvalue (emphasis mine)

getValue()

Returns the value of the top-left cell in the range. The value may be of type Number, Boolean, Date, or String depending on the value of the cell. Empty cells will return an empty string.

To avoid this kind of problems, you may include some sort of data validation. You could use build-in features like conditional formatting, data validation, or something like a on edit script together with try / catch or a redundant validation in your emailAlert script.

Upvotes: 2

Related Questions