javascript - Google Script: Comparison of cells with === always gives out cells are different - Stack Overflow

I want to pare two cells and depending on whether they are equal or not send out different e-mails. Aft

I want to pare two cells and depending on whether they are equal or not send out different e-mails. After having read several articles on this topic I unfortunately do not see why the the following parison with === always gives out as a result that the cells are different, even if I pare the same cells:

function SendEmail() {
 var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow(); 
  var ui = SpreadsheetApp.getUi(); //  var file =     SpreadsheetApp.getActiveSheet(); 
  if (sheet.getRange(2,10) === sheet.getRange(2,10)) {
    MailApp.sendEmail("[email protected]", "test", "not equal!");
  } else {
    MailApp.sendEmail("[email protected]", "test", "equal!");
  }
  }

It also does not work if I use !==.

Any hint is highly aprreciated - thanks!

I want to pare two cells and depending on whether they are equal or not send out different e-mails. After having read several articles on this topic I unfortunately do not see why the the following parison with === always gives out as a result that the cells are different, even if I pare the same cells:

function SendEmail() {
 var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow(); 
  var ui = SpreadsheetApp.getUi(); //  var file =     SpreadsheetApp.getActiveSheet(); 
  if (sheet.getRange(2,10) === sheet.getRange(2,10)) {
    MailApp.sendEmail("[email protected]", "test", "not equal!");
  } else {
    MailApp.sendEmail("[email protected]", "test", "equal!");
  }
  }

It also does not work if I use !==.

Any hint is highly aprreciated - thanks!

Share Improve this question edited Mar 9, 2017 at 15:01 JSS asked Mar 9, 2017 at 14:58 JSSJSS 131 gold badge1 silver badge4 bronze badges 3
  • 1 The getRange() calls return objects, and two distinct objects are always unequal. – Pointy Commented Mar 9, 2017 at 14:59
  • That helps a bit, how do I have to address the cells in order to get the parison I am looking for? – JSS Commented Mar 9, 2017 at 15:02
  • Change it to ge the values: ` if (sheet.getRange(2,10).getValue() === sheet.getRange(2,10).getValue()) {` See this thread for more details – Karl_S Commented Mar 9, 2017 at 15:05
Add a ment  | 

3 Answers 3

Reset to default 2

You need to pare the values in the cells rather than the Ranges themselves.

If you are dealing with single cells, this is straightforward:

if(sheet.getRange(2,10).getValue() === sheet.getRange(2,10).getValue())

However if you want to pare ranges with multiple cells it is more plex, as you'll need to pare arrays of values.

I noticed you were paring the same cells? I changed it to pare cells J2 & K2 and to log the differences. I hope this helps.

function SendEmail() {
 var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow(); 
  var ui = SpreadsheetApp.getUi(); //  var file =     SpreadsheetApp.getActiveSheet(); 
  Logger.log(sheet.getRange(2,10).getValue());// logs value of J2
    Logger.log(sheet.getRange(2,11).getValue());// Logs the value of K2
 if(sheet.getRange(2,10).getValue() === sheet.getRange(2,11).getValue()) {
    // MailApp.sendEmail("[email protected]", "test", "equal!");
    Logger.log('equal');
  } else {
   //  MailApp.sendEmail("[email protected]", "test", "not equal!");
    Logger.log('not equal');
  }
  }

The return type for the getRange() method is Range, which inherits from Object. Objects are reference types and will never be considered equal unless the variables being pared point to the same object instance.

As mentioned, you need to call the getValue() method on the range that will return the contents of the cell and pare the values.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744756706a4591926.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信