[英]My code just decided to stop working and I don't know why. (javascript google appscript)
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.因此,我正在运行一个脚本,以便在看到客户 3 天后自动发送电子邮件。 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.
所以我输入不同的日期以查看问题出在哪里,并且代码只是在我的 google 工作表中发送任何给定日期的回复。 All date but the 31/05/2022 for some reason.
由于某种原因,除了 2022 年 5 月 31 日以外的所有日期。
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.
这意味着它不再发送任何 email。
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");
}
}
}
}
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.查看返回毫秒数的 getTime() 或 valueOf() 方法,可以按照您希望的方式进行比较。
use the function Logger.log() to know what exactly you code are doing.使用 function Logger.log() 来了解您的代码到底在做什么。 Since you don't have any clue I would recommend starting knowing the values of each variable: Logger.log("varname=",varname);
由于您没有任何线索,我建议您开始了解每个变量的值:Logger.log("varname=",varname);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Juillet"); var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Juillet"); ex: Logger.log("sheet=",sheet);
例如: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]); var sheetDate = new Date(行[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
它是新 IDE 中左侧面板上的第四个图标
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". ...将生成一个字符串值,例如“2022 年 6 月 7 日星期二”。 You can't add the number three to that to get a date three days later.
您不能将数字3 添加到三天后的日期。 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.
您要做的就是将“2022 年 6 月 7 日星期二”与“20223 年 6 月 7 日星期二”之类的内容进行比较,它们将作为字符串进行比较,而不是日期或数字。
A day is 86,400,000 milliseconds.一天是 86,400,000 毫秒。 When
Sdate
is three or more days after SsheetDate
, Sdate.getTime() > SsheetDate.getTime() + 259200000
.当
Sdate
三天或更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.有更优雅、更易于阅读的方法来比较日期,但这些方法更复杂和/或依赖于特定的日期/时间库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.