Home > Enterprise >  My code just decided to stop working and I don't know why. (javascript google appscript)
My code just decided to stop working and I don't know why. (javascript google appscript)

Time:06-08

Thanks in advance to anyone who can help with my issue. I'm not a professionnal I just code when I don't want to do a task anymore.

So I'm running a script to send automaticaly emails 3 days after I see a client. Two days ago, I see that my emails are not been sent at the correct date. So I enter different date to see what's the problem is and the code just send reply for any given date in my google sheet. All date but the 31/05/2022 for some reason.

So I just waited a couple of days to fix it and when I came back the code just stop working completely. Meaning it doesn't send any email anymore.

I'm using the following code with a time trigger and I dont see why it doesnt work anymore.

function MailingAutoJuillet() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Juillet");
  var startRow = 3; 
  var numRows = sheet.getLastRow()-1; 
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn()); 
  var data = dataRange.getValues(); 


  var EMAIL_SENT = 'EMAIL_SENT';
  var NO_EMAIL = 'NO_EMAIL';
  for (var i = 0; i < data.length;   i) {
    var row = data[i];
    var date = new Date();
    var sheetDate = new Date(row[2]);

    Sdate = Utilities.formatDate(date,'Europe/Paris','EEE, MMM d, yyyy')
    SsheetDate = Utilities.formatDate(sheetDate,'Europe/Paris','EEE, MMM d, yyyy')

    if (Sdate >= SsheetDate 3){
      if (row[13] != EMAIL_SENT)
        if (row[13] != NO_EMAIL) { 
          const HTMLTemplate = HtmlService.createTemplateFromFile("HTML Mail de rappel")
          const HTMLforemail = HTMLTemplate.evaluate().getContent()
          var emailAddress = row[9];
          var emailText = "Text";
          var subject = "Text";
          var option={
                htmlBody:HTMLforemail
          };

          GmailApp.sendEmail(emailAddress,subject,emailText,option);

          sheet.getRange(startRow i,14).setValue("EMAIL_SENT");
        }
    }
  }
}

CodePudding user response:

Attempting to use greater than operator to compare strings

You cannot compare dates in this manner

Sdate = Utilities.formatDate(date,'Europe/Paris','EEE, MMM d, yyyy')
SsheetDate = Utilities.formatDate(sheetDate,'Europe/Paris','EEE, MMM d, yyyy')

The above are strings and cannot be compared with greater than operators

if (Sdate >= SsheetDate 3){

Take a look at the getTime() or valueOf() methods that return milliseconds which can be compared in the manner that you wish.

CodePudding user response:

use the function Logger.log() to know what exactly you code are doing. Since you don't have any clue I would recommend starting knowing the values of each variable: Logger.log("varname=",varname);

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Juillet"); ex: Logger.log("sheet=",sheet);

Also, I don't know how exactly is your sheet, but let my try to understand what is happening here:

var sheetDate = new Date(row[2]); //row is expected to be an array

but

var row = data[i]; so... row receive here the values in data? It's data an 2d array?

Also, check your executions. It's the fourth icon on the left panel in the new IDE

CodePudding user response:

You can't compare dates that way.

SsheetDate = Utilities.formatDate(sheetDate,'Europe/Paris','EEE, MMM d, yyyy')

...is going to produce a string value, like "Tue, Jun 7, 2022". You can't add the number three to that to get a date three days later. All you'll be doing is comparing something like "Tue, Jun 7, 2022" to "Tue, Jun 7, 20223", and they will compare as strings, not as dates or numbers.

A day is 86,400,000 milliseconds. When Sdate is three or more days after SsheetDate, Sdate.getTime() > SsheetDate.getTime() 259200000.

There are more elegant, easier-to-read ways to compare dates, but those methods are more complicated and/or depend on particular date/time libraries.

  • Related