脚本不会停止发送电子邮件

时间:2016-08-18 17:19:21

标签: google-apps-script

以下是我的脚本。它会发送电子邮件,但它会继续发送电子邮件,即使被引用的单元格已经显示“已发送”。谁能告诉我哪里出错了?

 
function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var range = sheet.getRange(2,1,lastRow,13);
  var data = range.getValues();
  
  for (i=0;i<data.length;i++){
    var employeeName = data [i][6];
    var startDate = data [i][2];
    var endDate = data [i][3];
    var oHours = data [i][4];
    var email = data [i][14];
    var emailAd = data [i][1];
    var response = data [i][9];
    if (response == "Approved" && email != "Sent"){
      var subject = "Overtime Request";
      var message = "Your request for overtime for " + employeeName + " has been " + response + ". This overtime should occur between " + startDate + " to " + endDate + " and should not exceed " + oHours + " hours.";
      MailApp.sendEmail(emailAd,subject,message);
      sheet.getRange(i+2,14).setValue("Sent");}
    else if(response=="Declined" && email != "Sent"){
        var subject = "Overtime Request";
      var message = "Your request for overtime for " + employeeName + " has been " + response + ".";
      MailApp.sendEmail(emailAd,subject,message); 
      sheet.getRange(i+2,14).setValue("Sent");}
    else if(email == "Sent"){return;}
  }}

1 个答案:

答案 0 :(得分:0)

现在,emailundefinedemail != "Sent"将始终评估为真。

在这一行:

  var range = sheet.getRange(2,1,lastRow,13);

getRange()方法是获取A到M列,然后是这一行:

var email = data [i][14];

您正在尝试获取列O的值。

  

请记住,当范围索引从1,1开始时,JavaScript数组   将从[0] [0]索引。

您只需将sheet.getRange(2,1,lastRow,13);更改为sheet.getRange(2,1,lastRow,15);,这样您就可以获得设置值"Sent"的值,并在if()语句中对其进行评估。