简体   繁体   English

如果 3 列中的任何日期 = 今天,Google Sheets Appscript 发送电子邮件

[英]Google Sheets Appscript Send Email if any of the Dates in 3 columns = today

I'm trying to write a code that will automatically trigger an email if either column C , D or E (dates) equals today's date, I've been doing some research on Or logical operators for Js but is not working, I'm getting a "SyntaxError: Unexpected token" error, is it either on the condition for today or is it because the Or operators (||) ?我正在尝试编写一个代码,如果列 C、D 或 E(日期)等于今天的日期,它将自动触发电子邮件,我一直在对 Js 的或逻辑运算符进行一些研究,但没有工作,我收到“SyntaxError: Unexpected token”错误,是今天的条件还是因为 Or 运算符 (||) ?

Updated Script This now is working fine defining today's date using const currenttime = new Date(), but I'm getting an email for each line item instead of just those with today's date, any clues?更新的脚本现在使用 const currenttime = new Date() 定义今天的日期工作正常,但是我收到了每个行项目的电子邮件,而不仅仅是具有今天日期的项目,有任何线索吗?

function sendEmails() {
  const sheet1 = SpreadsheetApp.getActive();
    const sheet = sheet1.getSheetByName('Sheet1');
  const dataRange = sheet.getRange("A1:E1000");
  const data = dataRange.getValues();
  const currenttime = new Date();

  data.forEach(function (rowData) {
    const recipient = rowData[0];
    const emailAddress = rowData[1];
    const Notes = rowData[5];
    const date = rowData[2];
      const date2 = rowData[3];
        const date3 = rowData[4];

    if (date == currenttime || date2 == currenttime || date3 == currenttime) {return}

    const greeting = 'Dear ' + recipient + ',\n'
    const customerMessage = 'Please follow up with ' + Notes + ' By today!';
    const greatJobMessage = 'Thanks';

    const message = [greeting, customerMessage, greatJobMessage].join('\n');
    const subject = 'Reminder to Follow up!';
    MailApp.sendEmail("ecmoranavila@gmail.com", subject, message);
  }) 
}

There are some issues with your script that need to be addressed in order for it to work.您的脚本存在一些问题需要解决才能使其正常工作。

First of all, your if condition is not doing anything.首先,你的if条件没有做任何事情。 It has an empty return (?) and then it gets closed.它有一个空的 return (?) 然后它被关闭。 The code that sends the email is out of the condition so it will always run.发送电子邮件的代码不符合条件,因此它将始终运行。

Second, the Date declaration returns an string like this: Fri Mar 13 12:30:50 GMT+01:00 2020 , so it is not useful unless the dates in your Sheet should also specify hours, minutes and seconds (miss one second and the email will not be sent).其次,日期声明返回一个这样的字符串: Fri Mar 13 12:30:50 GMT+01:00 2020 ,所以它没有用,除非你的工作表中的日期还应该指定小时、分钟和秒(错过一秒和不会发送电子邮件)。


Fixes:修复:

  1. Get the data of the Sheet with getDisplayValues() .使用getDisplayValues()获取 Sheet 的数据。 Otherwise the script will get the date in a completely different format than the Sheet:否则脚本将以与工作表完全不同的格式获取日期:

    const data = dataRange.getDisplayValues();

    In my Sheet, the dates look like this: 13-03-2020 , but you can use any other format .在我的工作表中,日期如下所示: 13-03-2020 ,但您可以使用任何其他格式


  1. Format the Date of the current time.格式化当前时间的日期。 As I said before, new Date() returns a long String.正如我之前所说, new Date()返回一个长字符串。 You can format it with the Utilities class to make it coincide with the dates written in the Sheet:您可以使用Utilities 类对其进行格式化,使其与工作表中写入的日期一致:

    const currenttime = Utilities.formatDate(new Date(), 'UTC', 'dd-MM-yyyy');


  1. Change your if condition so it covers the rest of the code.更改您的if条件,使其涵盖其余代码。 Also, remove the return as it's not doing anything:此外,删除return因为它没有做任何事情:
  if (date == currenttime || date2 == currenttime || date3 == currenttime) {

   const greeting = 'Dear ' + recipient + ',\n'
   const customerMessage = 'Please follow up with ' + Notes + ' By today!';
   const greatJobMessage = 'Thanks';

   const message = [greeting, customerMessage, greatJobMessage].join('\n');
   const subject = 'Reminder to Follow up!';
     MailApp.sendEmail("ecmoranavila@gmail.com", subject, message)}
 }); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM