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 figu
The .getValues()
method returns a 2-dimensional array, and so you aren't referencing the values within each cell correctly in your array.
Let's assume that both A1
and A2
contain the string "THIS"
.
Running the following line:
var testvalues = sheet.getRange('A1:A2').getValues();
will assign the array [[THIS], [THIS]]
to testvalues
.
You need to change:
var testvalue1 = testvalues[0]; // The contents from A1
var testvalue2 = testvalues[1]; // The contents from A2
to:
var testvalue1 = testvalues[0][0]; // The contents from A1
var testvalue2 = testvalues[1][0]; // The contents from A2
I hope this is helpful to you!
The following line of code is incorrect, below it is corrected.
var testvalue1 = testvalues[0][0]; // The contents from A1
var testvalue2 = testvalues[1][0]; // The contents from A2
You are originally grabbing the entire row instead of a single cell.