如果条件在循环中被忽略以从工作表发送电子邮件

时间:2019-01-04 19:55:52

标签: google-apps-script google-sheets

This is the sheet I am using

如果需求和生产差异大于10或小于-10,我尝试在“配额容量建议”表上自动为每位员工发送电子邮件。

当我运行脚本时,即使值在可接受的范围内(-9到9),也会发送电子邮件。

function checkValue() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Quota Capacity Recommendation");
  var num = sheet.getRange(2, 11).getValue(); //number of employees

  for (var i = 2; i < num; i++) { //loop to get each employee
    var ss = SpreadsheetApp.getActive();
    var sheet = ss.getSheetByName("Quota Capacity Recommendation");
    var dif = sheet.getRange(i, 10).getValue(); //value to check
    var rc = sheet.getRange(i, 9).getValue();
    var name = sheet.getRange(i, 1).getValue();

    if (dif > 10 || dif < -10) //condition
    {
      //GmailApp.sendEmail("user@domain.com",name+ " Quota Recommendation", name+ " has recommended quota change of " +rc);
      GmailApp.sendEmail("otheruser@domain.com", name + " Quota Recommendation", name + " has recommended quota change of " + rc);
      //MailApp.sendEmail("differentuser@domain.com",name+ " Quota Recommendation", name+ " has recommended quota change of " +rc);
    }
  }
}

2 个答案:

答案 0 :(得分:1)

if(dif>10 || dif<10) 

如果大于10或小于10,则为!= 10。

答案 1 :(得分:1)

问题似乎在于第10列(J)的值是百分比(样本值es 20%)而不是整数20。

您应该将输入的值更改为整数,或者使用.1或10代替脚本。