mardi 1 mai 2018

Apps Script doesn't compare 2 values in if-statement

I've created a new project that should compare a name from Sheet1 with a list of names in Sheet2 and check if the name is already in that list. For that I chose a for-loop to get through the list in Sheet2 and compare every list entry with the name from Sheet1. Only if the name already exists in the list stuff should happen.

function myFunction() {
  var tabSheet1 = 'Sheet1';
  var tabSheet2 = 'Sheet2';

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = ss.getSheetByName(tabSheet1);
  var sheet2 = ss.getSheetByName(tabSheet2);
  var lastRow1 = sheet2.getLastRow() + 1;
  var playerNameSheet1 = sheet1.getRange(1, 1).getValue();

  for (var j = 1; j < lastRow1; j++) {
    var playerNameSheet2 = sheet2.getRange(j, 1).getValue();
    if (playerNameSheet2 == playerNameSheet1) {
      ...stuff...
    }
  }
}

Now my problem is that it seems like the script isn't able to identify that a name already exists in the list. Both values (playerNameSheet1 and playerNameSheet2) are completely identical (no space or other hidden obstacles), however the script would never continue with stuff in the if-statement. My example name to test my script was "Oliver Baumann".

I'm a bit confused about it - even more, because another comparison a bit later in the script code works just fine.

I've already tried to change the operator into === but that wouldn't work either.

if (playerNameSheet2 === playerNameSheet1) {
   ...stuff...
}

I've also observed that if I put a dot behind both variables I'm only able to choose further functions with playerNameSheet2, but not with playerNameSheet1. Maybe I did a typing error and am just too blind to see it? I don't know. Anyone an idea how to resolve the issue?

The complete project can be found here. However, a lot of stuff is in german and very rudimental. I just started it and haven't got time to clean it up. Just so you don't wonder.

Aucun commentaire:

Enregistrer un commentaire