If condition being ignored in a loop to send email

2019-03-04 04:10发布

This is the sheet I am using.

I am trying to send emails automatically for each employee on the Quota Capacity Recommendation form if Difference in Demand & Production is above 10 or below -10.

When I run the script the emails get sent even if the value is within the acceptable range (-9 to 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条回答
Animai°情兽
2楼-- · 2019-03-04 04:39
if(dif>10 || dif<10) 

if bigger than 10 or less than 10. So that is !=10.

查看更多
The star\"
3楼-- · 2019-03-04 04:48

It looks that the problem is that the values of column 10 (J) are percentages (the sample value es 20%) instead of integers 20.

You should have to change the value entered to be integers or the script tu use .1 instead or 10.

查看更多
登录 后发表回答