首页 > 解决方案 > 如果条件无法正常运行

问题描述

我的脚本正在向列中的给定收件人发送消息,即使已经发送了一个。目标是在活动工作表上搜索并检查活动行的两列是否符合if条件。如果是这样,则执行这些MailApp.sendEmail()行。

但是,即使该行不符合条件,它也会发送另一封电子邮件。我认为这是由于我如何定位for-loop 和if,或者我的代码中缺少某些内容。

目标:如果两列的活动行符合条件 > 发送电子邮件。
其他任何内容 > 忽略/跳过行。

function sendOnEdit(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dump");
  var startRow = 1;  // Bypass firts Row that contain Column Titles
  var dataRange = sheet.getDataRange();
  var data = dataRange.getValues();

  for (var rownum = startRow; rownum < data.length; rownum++) { //loop to search for reso == x && emailSent != y
    var casenum = data[rownum][5]; // Active Row for Case# Column 
    Logger.log("Captured Case#: " + casenum);
    var emailAddress = data[rownum][1];  // grabs the email address / Email Address Col
    Logger.log("Captured Email: " + emailAddress );

    var reso = data[rownum][12]; // check cell value / on Spreadsheet reso column is on Data Validation that's either "Yes" or empty.
    Logger.log("Captured Y/N: " + reso);
    var emailSent =data[rownum][17]; // Current active cell for col "DONE" 
    Logger.log("Captured DONE?: " + emailSent);
    var x = "Yes";
    var y = "DONE";

    if ( reso == x && emailSent != y ) {  // if this is true it should send email with details below if not it will do nothing.
      var body = "Hello!";
      body += '<p></p>';
      //Add to body ...

      var subject = "Message Subject" + casenum;

      MailApp.sendEmail(emailAddress, subject, body, 
          {name: 'Automatic Teamline Email', htmlBody: body});
      sheet.getRange(rownum + 1, 19).setValue("DONE"); // This will set value of current cell to "DONE"
    }
  }
}

标签: google-apps-scriptgoogle-sheets

解决方案


推荐阅读