Google 应用程序脚本。根据日期过滤数据

问题描述 投票:0回答:1

A snippet of the table我正在尝试根据日期过滤值班公鸡列表并向指定的员工发送提醒。

我运行脚本时没有遇到任何错误,但没有发送邮件。

下面是使用的代码块。

// Declaring variables

var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get active spreadsheet
var schedule = ss.getSheetByName("Duties & Timelines"); // Get the schedule sheet

function sendReminderEmail() {

  var currentDate = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "yyyy-MM-dd");

  var data = schedule.getRange("A:J").getValues();
  
  const due = schedule.getRange("A:J").getValues().filter(function (row) {
    var cell = schedule.getRange("I2:I");
    cell.setNumberFormat('yyyy-mm-dd');
    return cell == currentDate;
  }); // Filter to get row with reports that are due

  due.forEach(function(item) {


    // Set the email details
    const recipientEmail = "[email protected]";
    const subject = "Monthly Reminder";
    const body = "This is your monthly reminder.";



    // Send the email
    MailApp.sendEmail({
      to: recipientEmail,
      subject: subject,
      body: body
    });
  })
}

date google-sheets google-apps-script filter format
1个回答
0
投票

使用 Date().valueOf() 获取 0000 处的日期;

function sendReminderEmail() {
 const ss = SpreadsheetApp.getActive();
 const sh = ss.getSheetByName("Duties & Timelines");
 var vs = schedule.getRange("A2:J"+ sh.getLastRow()).getValues();
 const dt = new Date();
 const dv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//date at midnight
 const due = vs.filter(row => {
    let d = new Date(r[8]);
    let date = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();//date at midnight
    return d == dv;
  }).filter(e => e); 
 due.forEach(function(item) {
   const recipientEmail = "[email protected]";
    const subject = "Monthly Reminder";
    const body = "This is your monthly reminder.";
   MailApp.sendEmail({
      to: recipientEmail,
      subject: subject,
      body: body
    });
  })
}
© www.soinside.com 2019 - 2024. All rights reserved.