We're writing a function to parse week number designations from a Google Spreadsheet column in the format of W#-W# (e.g. W1-10, W3-13, etc.) For some reason apps script is treating the <= operator almost as if it was an = operator.
The function:
for (var row = 0; row < qBow.length; row++) {
var wRange = qBow[row][6];
var wRsplit = wRange.split('W').join('');
var wNums = wRsplit.split('-');
var currentWnum = activeSheet.getName().substring(1);
Logger.log('row: ' + row + ' currentWnum: ' + currentWnum + ' wNums[0]: ' + wNums[0] + ' wNums[1]: ' + wNums[1]);
if (currentWnum >= wNums[0] && currentWnum <= wNums[1]) {
Logger.log('row: ' + row + ' currentWnum: ' + currentWnum + ' wNums[0]: ' + wNums[0] + ' wNums[1]: ' + wNums[1]);
}
}
Sample output from first Logger.log() :
row: 8 currentWnum: 4 wNums[0]: 3 wNums[1]: 13
...
row: 12 currentWnum: 4 wNums[0]: 4 wNums[1]: 13
...
row: 18 currentWnum: 4 wNums[0]: 3 wNums[1]: 13
...
row: 20 currentWnum: 4 wNums[0]: 3 wNums[1]: 13
Full output from second Logger.log() :
row: 106 currentWnum: 4 wNums[0]: 4 wNums[1]: 5
row: 138 currentWnum: 4 wNums[0]: 2 wNums[1]: 4
row: 139 currentWnum: 4 wNums[0]: 2 wNums[1]: 4
row: 151 currentWnum: 4 wNums[0]: 3 wNums[1]: 4
As you can see, it skipped all of the rows except for the ones where currentWnum is equal to wNum[1], and one row where wNum[1] is equal to 5. We tested it replacing wNums[1] in the conditional statement with a static number, and it behaves correctly. Any ideas what's going on here?
It looks like you're using <=
on strings. I'd expect those to be compared lexicographically - so "13" is less than "4" for example.
I suggest you convert all the values to numbers (e.g. with parseInt
) before performing any comparisons. That will go for every element in wNums
and also currentWnum
.