-1

Can someone explain why the line Logger.log("%s EQUALS %s",col1[i],col2[i]); is never called? Im new to Javascript, but based on this SO post I am using the correct operator for comparison in the if statement. The values are guaranteed to be either integers, or an empty cell if that makes a difference.

function SetFilter(){
  var first_row_to_hide=4;
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var maxrows=sheet.getMaxRows();
  col1 = sheet.getRange(first_row_to_hide,3,maxrows,1).getValues();//getRange(row, column, numRows, numColumns) -- column: C
  col2 = sheet.getRange(first_row_to_hide,4,maxrows,1).getValues();//column: D
  for (var i = 1; i < col1.length; i++){
    Logger.log("%s ? %s",col1[i],col2[i]);
    if (col1[i] === col2[i]){
     Logger.log("%s EQUALS %s",col1[i],col2[i]); 
    }//sheet.hideRows(i+first_row_to_hide);
  }
  Logger.log("DONE");
}

example values in column1 and 2:

col1 col2 Log output of '%s ? %s'
1    2    [1.0] ? [2.0]
1    1    [1.0] ? [1.0]
0    0    [0.0] ? [0.0]
0    1    [0.0] ? [0.0]
4    5    [4.0] ? [5.0]
Tanaike
  • 181,128
  • 11
  • 97
  • 165
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • 1
    *"Can someone explain why the line ... is never called?"* ~ yes, because `col1[i]` and `col2[i]` are never [identical](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity) – Phil Feb 10 '17 at 04:29
  • 1
    What gets logged by `%s ? %s`? – Ry- Feb 10 '17 at 04:30
  • could you please add the value of `col1[i]` & `col2[i]`. Also, the strict equality operator needs the type of both operands to be the same. – Abhinav Galodha Feb 10 '17 at 04:30
  • Adding to @Agalo's comment, if they are objects, they must be the exact same object reference. Perhaps try `col1[i] == col2[i]` if you're only interested in comparing the values – Phil Feb 10 '17 at 04:31
  • @Phil, I tried it both ways, using `==` and `===` before I posted here, thanks for the additional explanation tho... I didn't know that! – Rilcon42 Feb 10 '17 at 04:50
  • @Agalo, I updated my code with sample values for each column. – Rilcon42 Feb 10 '17 at 04:50
  • 3
    Data retrieved by "setValues()" is 2 dimensional Array. How about a change from col1[i] === col2[i] to col1[i][0] === col2[i][0]? – Tanaike Feb 10 '17 at 04:54
  • @Ryan I updated my code to show the log output, thanks for the suggestion- I should have had it there from the beginning. – Rilcon42 Feb 10 '17 at 04:54
  • Next time, please read the fine manual ~ https://developers.google.com/apps-script/reference/spreadsheet/range#getvalues – Phil Feb 10 '17 at 04:55
  • Thanks @Tanaike that solved my problem..... never thought of the array being 2D – Rilcon42 Feb 10 '17 at 04:56
  • @Tanaike could you post your comment as a solution so I can accept it and resolve this question? – Rilcon42 Feb 13 '17 at 18:57
  • @Rilcon42 Thank you for your concern. I'll post it soon. – Tanaike Feb 13 '17 at 21:54

2 Answers2

0

You're using the strict comparison === which is only true if the operands are of the same type AND the contents are identical (where objects are concerned they must be referencing the exact same object).

Whereas the abstract comparison == converts the operands to the same type before comparing.

Likely col1[i] and col2[i] violate one of the above mentioned conditions. (in the event they are objects, == will also return false unless they reference the same object)

MDN Comparison operator documentation

roger
  • 1,091
  • 1
  • 7
  • 15
  • That makes sense, but they are both columns of integers (see my updated example above). Could I be missing something else, or am I misunderstanding you? – Rilcon42 Feb 10 '17 at 04:52
  • Bit of a side note, but as strange as it sounds javascript technically doesn't have integers - the type would show up as 'number'. You can check the types of your variables by using the 'typeof' operator. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof – roger Feb 10 '17 at 04:57
  • Looks like you got it solved in the comments above! - but checking the types could help prevent such confusion in the future. – roger Feb 10 '17 at 04:58
0

"setValues()" has 2 dimensional Array. So your script works by changing

from :

if (col1[i] === col2[i]){
 Logger.log("%s EQUALS %s",col1[i],col2[i]); 
}//sheet.hideRows(i+first_row_to_hide);

to :

if (col1[i][0] === col2[i][0]){
 Logger.log("%s EQUALS %s",col1[i],col2[i]); 
}//sheet.hideRows(i+first_row_to_hide);
Tanaike
  • 181,128
  • 11
  • 97
  • 165