如何根据单元格中的日期创建Google App脚本14天提醒电子邮件给自己

时间:2018-11-01 10:25:58

标签: javascript

请您帮我一下吗?我想自Google工作表中的日期起每隔14天向自己发送一封电子邮件,其中包含一两个单元格中的一些其他信息。我有下面的代码,但是当我从excel vba复制datediff时,我不知道如何在Google应用程序脚本中使用datediff。我还认为我的代码结构不正确,并且电子邮件将始终以当前代码形式发送。

function sendEmails() {

var today = new Date().toLocaleDateString();  // Today's date, without 
time
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = 999;   // Number of rows to process
// Fetch the range of cells A2:B999
var dataRange = sheet.getRange(startRow, 1, numRows, 999)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var reminderDate = row[0].toLocaleDateString();  // Sale start date
var propertyAddress = row[1];  // The address of the property
var subject = row[1]; // Email subject
var myemail = 'test@test.com';
}


   function setTrigger(){

  var reminderDate = row[0].toLocaleDateString();  // Sale start date
  //This trigger will set 14 days reminder trigger set in 
 createFourteenDaysTrigger function.
  ScriptApp.newTrigger('createFourteenDaysReminderTrigger')
      .timeBased()
      .atDate(reminderDate) //format -> .atDate(2017, 04, 01) 
      .atHour(12)
      .create();
 }

 function createFourteenDaysReminderTrigger() {
   //This trigger will send emails after 14 days
   ScriptApp.newTrigger('sendReminderEmail')
     .timeBased()
     .everyDays(14)
     .create()
}

function sendReminderEmail(){

  MailApp.sendEmail(myemail, subject, propertyAddress);// Sending of 
the email
 SpreadsheetApp.flush();
  }
  }

1 个答案:

答案 0 :(得分:0)

您可以在App脚本中使用基于时间的触发器每隔14天发送提醒电子邮件,为此,您需要创建2个触发器,第一个触发器用于在提醒日期启动触发器,第二个触发器用于在每个提醒日期创建提醒14天,

尝试以下示例代码。

     function setTrigger(){

      //Your other code goes here

      var reminderDate = row[0].toLocaleDateString();  // Sale start date
      //This trigger will set 14 days reminder trigger set in createFourteenDaysTrigger function.
      ScriptApp.newTrigger('createFourteenDaysReminderTrigger')
          .timeBased()
          .atDate(reminderDate) //format -> .atDate(2017, 04, 01) 
          .atHour(12)
          .create();
     }

     function createFourteenDaysReminderTrigger() {
       //This trigger will send emails after 14 days
       ScriptApp.newTrigger('sendReminderEmail')
         .timeBased()
         .everyDays(14)
         .create()
    }

    function sendReminderEmail(){

      MailApp.sendEmail(myemail@me.com, 14 Day Sales Chase Due, propertyAddress);// Sending of the email

    }