admin管理员组文章数量:1391955
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
3 Answers
Reset to default 2You 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.
本文标签: javascriptGoogle Script Comparison of cells withalways gives out cells are differentStack Overflow
版权声明:本文标题:javascript - Google Script: Comparison of cells with === always gives out cells are different - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744756706a2623507.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论