0

I need to compare two variables in a loop and I am using the code below. It does not work. It seems that the code cannot find when the two variables are the same. I am not sure why.

  var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet3 = ss.getSheetByName("Approved");
  var sheet4 = ss.getSheetByName("ADD/REMOVE");
  var list2 = sheet4.getRange(2,4,sheet4.getLastRow()-1,1).getValues();
  var list3 = sheet3.getRange(2,8,sheet3.getLastRow()-1,1).getValues();
  
  for(var i = 0;i<list2.length+1;i++){
    
    for(var j=0;j<list3.length;j++){
      
      var x = list3[j];
      var y = list2[i];
     
      console.log(x);
      console.log(y);

      if(x == y){sheet3.getRange(j+2,9).setValue("OK");console.log("HELLO!");}    
    }
  }

I added the console messages to debug. "HELLO" is not there --> x is never equal to y. However, it should. Even the console shows x and y are equal when j = 8.

The code does not give an error, it just does not find when x = y. The values I am comparing are strings (emails)

Is there something wrong with code?

UPDATE: Solved by changing to:

  var x = list3[j][0];
  var y = list2[i][0];
Filippo
  • 320
  • 2
  • 5
  • 22
  • And what are the values in the two lists? – Taplar Jul 31 '20 at 19:57
  • I fixed the first point, but still no luck. The values in two list are emails. Such as: [ 'xxx@gmail.com' ]. x = y should be TRUE in 3 cases, but it is never TRUE. – Filippo Jul 31 '20 at 20:08
  • If the comparison is not true, then the strings are not exactly the same. Meaning they either have case differences or white space differences. – Taplar Jul 31 '20 at 20:10
  • they are exactly the same, but for some reason I had to add [0]. (list[i][0]). Which I do not get! – Filippo Jul 31 '20 at 20:17
  • 1
    Then they were arrays of strings, and not just strings. And arrays are only equal if they are actually the same array in memory. – Taplar Jul 31 '20 at 20:18

1 Answers1

1

try this:

function one() {
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sheet3=ss.getSheetByName("Approved");
  var sheet4=ss.getSheetByName("ADD/REMOVE");
  var list2=sheet4.getRange(2,4,sheet4.getLastRow()-1,1).getValues().map(function(r){return r[0];});
  var list3=sheet3.getRange(2,8,sheet3.getLastRow()-1,1).getValues().map(function(r){return r[0];});
  for(var i=0;i<list2.length+1;i++){
    for(var j=0;j<list3.length;j++){
      if(list2[i] == list3[j]){
        sheet3.getRange(j+2,9).setValue("OK");
        console.log("HELLO!");
      }    
    }
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • No, but I was able to make it work by having list2[i][0] vs list2[i] and list3[j][0] vs list3[j]. Which I totally do not understand because it is only one column of data, so why would I need to specify the [0]? – Filippo Jul 31 '20 at 20:16
  • 3
    All value arrays obtained using getValues() are returned in a 2 dimensional format. Every cell has a row and a column. – Cooper Jul 31 '20 at 20:17
  • ok noted. I had other cases where I was not using [0] and was still working. In those cases I was comparing an array value with a value. Example: list[i] == "x". So I guess I need to specify the 2 dimension only when comparing array values vs other array values. – Filippo Jul 31 '20 at 20:22