I feel that the following two functions should produce the same result, but function bob()
never finds name1
equal to name2
, even though the names in those cells are indeed equal.
The function bob2()
works fine, but I'm trying to speed things up by using arrays.
function bob()
{
var ss = SpreadsheetApp.getActiveSheet();
var name1 ;
var name2 ;
var AthName = ss.getRange(1,2,30).getValues();
for (var xx=1; xx<30 ;xx++)
{
name1=AthName[xx]
name2=AthName[xx+1]
//name1 never equals name2 here.
if (name1==name2) ss.getRange(xx,10).setValue(name2);
}
}
function bob2()
{
var ss = SpreadsheetApp.getActiveSheet();
var name1 ;
var name2 ;
for (var xx=1; xx<30 ;xx++)
{
name1 = ss.getRange(xx,2).getValue();
name2 = ss.getRange(xx+1,2).getValue();
//name1 does equal name2 here as expected.
if (name1==name2) ss.getRange(4+xx,10).setValue(name2);
}
}
What's wrong with bob()
?
The problem with bob()
is that you're setting AthName
to a two-dimensional list of values:
var AthName = ss.getRange(1,2,30).getValues();
Range.getValues()
always returns a two-dimensional list indexed by row and column.
This means that you're assigning lists to name1
and name2
:
name1=AthName[xx]
name2=AthName[xx+1]
Lists are equal only when they're the same list, ie , the variables refer to the same memory location. So name1 == name2
is going to be false no matter what the lists contain.
The fix is probably evident to you by now. Retrieve the value from each row:
name1 = AthName[xx][0];
name2 = AthName[xx+1][0];
(I've added spaces and semicolons for the sake of good JavaScript style.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.