I need to compare two cell values and act on them if they are different. My "if" statement always returns false when comparing the cell contents however, and I can't figure out why:
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1'); // apply to sheet name only
var testvalues = sheet.getRange('A1:A2').getValues(); // array of values to be tested
var testvalue1 = testvalues[0]; // The contents from A1
var testvalue2 = testvalues[1]; // The contents from A2
var test1 = testvalue1 == testvalue2; // True False test
var test2 = testvalue1 === testvalue2;// True False test
Browser.msgBox(testvalue1 + " and " + testvalue2 + " being the same is " + test1 + " and " + test2); // Sentence revealing outcome
};
test1 and test2 always return false no matter what the contents of cell A1 and A2 are (empty, number, text, different). If I edit the testvalues to this:
var testvalue1 = "We are the same value";
var testvalue2 = "We are the same value";
or
var testvalue1 = testvalues[0];
var testvalue2 = testvalues[0];
then suddenly the tests both return True as expected. Can anyone tell me what I'm missing in this, it's infuriating? Currently in A1 and A2 is the value 1 (before I potentially get asked about that).
The end goal is to use a border to partition different cell contents. I have the rest of it working perfectly but I've isolated the problem to the above concept.
getValues()
always returns a 2D array:[[A1],[A2]]
. Related question – TheMaster