简体   繁体   English

根据cell值发送email

[英]Send email based on cell value

I am trying to have this script loop per column.我试图让这个脚本在每列中循环。 If a date in the column is the same as today's date, the script will send an email using the persons name and email address detailed in the same row.如果列中的日期与今天的日期相同,脚本将使用同一行中详述的人员姓名和 email 地址发送 email。

Table of Expiry Dates有效期表

function myFunction() 
{
  var spreadSheet = SpreadsheetApp.getActiveSheet();
  var dataRange = spreadSheet.getDataRange();

  var data = dataRange.getValues();
  var date = Utilities.formatDate(new Date(), "GMT", "dd/MM/yyyy")
  
  
  var engcheck1 = dataRange.getColumn[2];
  
  for (var i = 1; i < data.length; i++)
  if (engcheck1 == date)
  {
      var row = data[i];
      var emailAddress = row[7]; //position of email header — 1
      var name = row[1]; // position of name header — 1
      var subject = "Your Currency for Engineering is now expired";
      var text = "Please note that your currency has expired today (" + date +"). You are now unauthorised to carry out any engineering tasks until you have been signed off by a member of the engineering team.";
      var message = "Dear " + name + "," + "\n\n" + text + "\n\n" + "Please get back in currency at your earliest convenience." + "\n\n" + "Many thanks," + "\n" + "Dominic Paul"
      MailApp.sendEmail(emailAddress, subject, message);
  }(i);
}

Without the 'If' statement, the script will loop through each row and sending out an email address.如果没有“If”语句,脚本将遍历每一行并发送一个 email 地址。 When I include the 'If' statement, nothing is sent to the email addresses.当我包含“If”语句时,没有任何内容发送到 email 地址。 Either I am using the if statement incorrectly or I am not targeting the column accurately.要么我错误地使用了 if 语句,要么我没有准确地定位列。 I tried creating a variable engcheck1 for column 2 ONLY but no email is sent despite today being one of the dates in column 2 (C).我尝试仅为第 2 列创建变量 engcheck1,但没有发送 email,尽管今天是第 2 (C) 列中的日期之一。

I did not understand, what is var engcheck1 = dataRange.getColumn[2];我不明白,什么是var engcheck1 = dataRange.getColumn[2]; . .
If you need to send and email if date is the same as the value in C column, provided column C has type Plain text , this is the answer:如果您需要发送 email 如果dateC列中的值相同,前提是列C的类型为Plain text ,这就是答案:

function myFunction() {
  var spreadSheet = SpreadsheetApp.getActiveSheet();
  var dataRange = spreadSheet.getDataRange();

  var lastRow = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getNumRows();
  var dateData = spreadSheet.getRange("C1:C" + lastRow).getDisplayValues().flat();
  var data = dataRange.getValues();
  
  var date = Utilities.formatDate(new Date(), "GMT", "dd/MM/yyyy")

  for (var i = 1; i < data.length; i++) {
    var rowDate = dateData[i];
    var row = data[i];
    // if the date in row is the same as date variable
    if (rowDate === date) {
      var emailAddress = row[7]; //position of email header — 1
      var name = row[1]; // position of name header — 1
      var subject = "Your Currency for Engineering is now expired";
      var text = "Please ...";
      var message = "Dear ...";
      MailApp.sendEmail(emailAddress, subject, message);
    };
  }
}

Try this:尝试这个:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var rg = sh.getDataRange();
  var vs = rg.getValues();
  const dt = new Date();
  var dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
  vs.forEach((r,i) => {
    let d = new Date(r[2]);
    let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();
    if(dv == dtv ) {
      //send you email
    }
  })
}

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

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