发送Slack通知在特定日期之前的n天

huangapple go评论64阅读模式
英文:

Send Slack notification n days before certain date

问题

使用此代码,在Apps Script中,当我的电子表格的E列中有一个格式为(DD/MM/YYYY) GMT+01:00(罗马时间)的到期日期时,我可以向一个频道发送Slack通知。

在这里,if (days_left == 2) 我可以设置通知前到期日期的天数,它可以正常工作。

我遇到的问题是,即使日期已过期,它也能正常工作。例如,如果我的到期日期是2022年12月20日,我设置了if (days_left == 2),今天是2022年12月18日,通知就会正常到达。但如果今天是2022年12月22日(所以到期日期2022年12月20日已经过期),通知仍然会到达。

出了什么问题?

我尝试更改时区,但没有成功,通知仍然会发送给已经过期的日期。

const sendSlackAlert = () => {
  const reminderText = getReminderText();
  if (reminderText === "") {
    return;
  }
  var payload = {
    "channel": "#test",
    "text": `测试消息`,
    "attachments": [{
      "text": getReminderText(),
      "mrkdwn_in": ["text"]
    }]
  }

  var options = {
    "method": "post",
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };

  var results = UrlFetchApp.fetch(SD_SUBSCRIPTION_WEBHOOK, options);

}

const getReminderText = () => {
  var sheet = SpreadsheetApp.getActive().getSheetByName('domains');
  var startRow = 2;
  var numRows = sheet.getLastRow() - 1;
  var numColumns = sheet.getLastColumn();
  var dataRange = sheet.getRange(startRow, 1, numRows, numColumns);
  var data = dataRange.getValues();
  let sendMessageText = "";
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var today = new Date(), // 今天的日期
      exp_date = row[4] // 到期日期

    var cert_details =
    {
      company_name: row[0],
      cert_company_name: row[1],
      cert_company_email: row[4]
    };

    var t2 = new Date(exp_date);
    t2.setHours(0, 0, 0, 0);
    var t1 = new Date(today);
    t1.setHours(0, 0, 0, 0);

    var difference_ms = Math.abs(t2.getTime() - t1.getTime());
    var days_left = Math.round(difference_ms / (24 * 3600 * 1000));

    cert_details.days_left = days_left;

    if (days_left == 2) {
      sendMessageText += `\n${cert_details.company_name}将在* 2天 *内到期`;
    }
  }

  return (sendMessageText == "") ? "" : sendMessageText
}
英文:

With this code, in Apps Script, I could send a slack notification to a channel, when in col E of my spreadsheet there is an expiring date in format (DD/MM/YYYY) GMT+01:00 (Rome Time).

<br>Here if (days_left == 2) I could set the number of days before the expiring date for the notification and it works fine.
<br> The problem I have that this works also if the date is expired, and so, for example if my expiring date is 20/12/2022, and I set if (days_left == 2) and today is 18/12/2022, the notification arrives fine, but if today is 22/12/2022 (so the expiring date 20/12/2022 is already expired) the notification arrives anyway.

What is wrong?<br>
I am trying to change my Timezone, but without success, the notification arrives also for the date already expired.

const sendSlackAlert = () =&gt; {
const reminderText = getReminderText();
if( reminderText === &quot;&quot; ){
return;
}
var payload = {
&quot;channel&quot; : &quot;#test&quot;,
&quot;text&quot; : `test message`,
&quot;attachments&quot;: [{
&quot;text&quot;: getReminderText(),
&quot;mrkdwn_in&quot;: [&quot;text&quot;]
}]
}
var options = {
&quot;method&quot; : &quot;post&quot;,
&quot;contentType&quot; : &quot;application/json&quot;,
&quot;payload&quot; : JSON.stringify(payload)
};
var results = UrlFetchApp.fetch( SD_SUBSCRIPTION_WEBHOOK , options);
}
const getReminderText = () =&gt; {
var sheet = SpreadsheetApp.getActive().getSheetByName(&#39;domains&#39;);
var startRow = 2;
var numRows = sheet.getLastRow() - 1;
var numColumns = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, numRows, numColumns);
var data = dataRange.getValues();
let sendMessageText = &quot;&quot;;
for (var i = 0; i &lt; data.length; ++i) {
var row = data[i];
var today = new Date(), // today&#39;s date
exp_date = row[4] // exp date
var cert_details = 
{
company_name:row[0],
cert_company_name:row[1],
cert_company_email:row[4]
};
var t2 = new Date(exp_date);
t2.setHours(0,0,0,0);
var t1 = new Date(today);
t1.setHours(0,0,0,0);
var difference_ms = Math.abs(t2.getTime() - t1.getTime());
var days_left = Math.round(difference_ms/(24*3600*1000));
cert_details.days_left = days_left;
if (days_left == 2) {
sendMessageText += `\n${cert_details.company_name} will expire in *2 days*`;
}
}
return (sendMessageText == &quot;&quot;) ? &quot;&quot; : sendMessageText
}

答案1

得分: 1

I have solved removing Math.abs to the function,

and so:

 var difference_ms = Math.abs(t2.getTime() - t1.getTime());

become

 var difference_ms = (t2.getTime() - t1.getTime());
英文:

I have solved removing Math.abs to the function,

and so:

 var difference_ms = Math.abs(t2.getTime() - t1.getTime());

become

 var difference_ms = (t2.getTime() - t1.getTime());

huangapple
  • 本文由 发表于 2023年6月18日 20:28:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定