0
The goal of the following code snippet is to take a date that is in column 5 subtract it from today’s date and, if the module of this division is equal to zero, an email is sent. In case, I want an email sent every three months while the if
main content, in the column tipoAlerta
to string
Trimestral
and in the column monitoramento
to string
Em andamento
. I know the problem is in the stretch dataRange[i][5]-dataHoje)%90==0
, because, once I remove, the code works. The logic is right, the new Date()
also returns the current date. So, I do not know where I can is wrong, as the email is not sent. Although, no error message appears.
for (var i=0;i<numRows-startRow;i++){
var monitoramento = dataRange[i][10]
var tipoAlerta = dataRange[i][15]
//var dataHoje = new Date(); --- Varivel anterior que deu //problemas
var dataHoje = dataRange[i][17] //Nova variavel. OK.
if (tipoAlerta == 'Trimestral' && monitoramento=='Em Andamento') {
if ((dataRange[i][5]-dataHoje)%90==0) {
MailApp.sendEmail({
to: emailAddress.toString(),
subject: subject,
htmlBody: message
});
}
}
Before the second if, do a
console.log((dataRange[i][5]-dataHoje)%90)
and see what it shows.– Sam
Not allowed. Both console.log and Logger.log(), gives the message Missing ; before declaration. (line 50, "Code" file). And the 50 line is just the console line.()
– Rogér
It is. You have to see what it shows
(dataRange[i][5]-dataHoje)%90
. Like you said, the problem might be there.– Sam
[19-02-07 07:28:32:207 EST] false [19-02-07 07:28:32:209 EST] false
– Rogér
I used Logger.log instead of console.log, and it returned me FALSE and FALSE, which means that the two conditions I’m testing are false and that’s why the code.
– Rogér
See the values of "typoAlerta" and "monitoring" before the if to see what it shows, if it really shows what you want. Any divergence will prevent the if from being accessed.
– Sam
When entering Logger.log((dataRange[i][5]-dataHoje)%90) before the first IF, as suggested, the log shows me FALSE 250 times. But I remember, according to the code, when I do this, it ends up falling into the FOR.
– Rogér
I found that the problem is in the variable var dataHoje = new Date(); For this, I created a column in the spreadsheet that stores a Spreadsheet formula with today’s date, in this case, =TODAY() and replaces new Date() with it (I have already updated the code). But the point is that I really need to use New Date(), or something equivalent in the code. New Date() returns today, so I don’t know why it didn’t work.
– Rogér
I solved it another way: I created a column I called expire = dataRange[i][7] in the code. In this column, I subtract in the Spreadsheet column F, which has the future date and =TODAY(), today’s date. When this difference is 90, the email is sent normally.
– Rogér