0

For the life of my I cannot figure out why this if statement never executes. The code below is after I have added in a bunch of logger stuff for debugging, but normally it would just read if(answerArray[z] == answerArray[z-1]). It seems like it should fire on the 3rd pass, but doesn't. Does anyone have a clue what is going on? I have also tried it with ===, but still no dice.

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var rawArray= sheet.getRange('D6:D'+lastRow).getValues();  
  var answerArray = rawArray.filter(function(n){return n != ''});   //remove blanks
  answerArray.sort();

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  var arrayContents = "";
  for (var z = 0;z<answerArray.length;z=z+1) {arrayContents = arrayContents + answerArray[z] + ", ";}

  Logger.log("RemoveDupes Started");
  Logger.log("Array Contents: " + arrayContents);
  Logger.log("answerArray[0] = "+answerArray[0]);
  for(var z = 1; z< answerArray.length; z=z+1){
     // Logger.log("answerArray["+z+"] = "+answerArray[z]);
      var current = answerArray[z];
      var previous = answerArray[z-1];
      Logger.log("Current = " + current+"; Previous = " + previous);
      if(current == previous) 
      {
        Logger.log("Duplicate Found");
          answerArray.splice(z,1);
          //answerArray.splice(z,1); //delete duplicate
          z=z-1;                  //reduce z to account for shortened array
      }
  }
  Logger.log("RemoveDupes Ended");

And here is the log:

11:40:53 AM Notice  Execution started
11:41:01 AM Info    RemoveDupes Started
11:41:01 AM Info    Array Contents: A, B, C, C, D, 
11:41:01 AM Info    answerArray[0] = A
11:41:01 AM Info    Current = B; Previous = A
11:41:01 AM Info    Current = C; Previous = B
11:41:01 AM Info    Current = C; Previous = C
11:41:01 AM Info    Current = D; Previous = C
11:41:01 AM Info    RemoveDupes Ended

Solution Thanks everyone for chiming in. I was able to solve it by casting the array to strings. I changed the top section to the following and it works.

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var rawArray = [""];
  var directInputArray = sheet.getRange('D6:D'+lastRow).getValues();  
  for(var z = 0; z< directInputArray.length; z=z+1){
     rawArray[z] = String(directInputArray[z]);  //cast all values as strings to remove meta-data
  }

  var answerArray = rawArray.filter(function(n){return n != ''});   //remove blanks
  answerArray.sort();

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  for(var z = 1; z< answerArray.length; z=z+1){
      if(answerArray[z] == answerArray[z-1]) {
          answerArray.splice(z,1);
          z=z-1;                  //reduce z to account for shortened array
      }
  }
DLove
  • 1
  • 1
  • 3
    What is `answerArray`? - it's used all over the place in your code, but the declaration is not shown. – Tom O. Mar 03 '21 at 22:26
  • Can you share a public copy of the sheet to try to reproduce it? – Kessy Mar 04 '21 at 11:49
  • 1
    @TomO. Thanks. That actually got me to a working solution. I added the declaration to the top so that you can see it. I am new to Google App Script, but my best guess is that .getRange().getValues() actually pulled in some sort of meta-data that does not show up in the string value. I was able to solve it by casting the values of answerArray to strings. – DLove Mar 04 '21 at 15:02

2 Answers2

0

Try this code...

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var answerArray = sheet.getRange('D6:D'+lastRow).getValues().filter(String).sort();   //remove blanks

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  let unique = answerArray.filter((item, i, ar) => ar.indexOf(item) === i);
  Logger.log(unique);

From here... How to get unique values in an array

James VB
  • 93
  • 7
0

The solution was posted on the question, so I'll add this here as community wiki so more people can benefit from it:

Solution Thanks everyone for chiming in. I was able to solve it by casting the array to strings. I changed the top section to the following and it works.

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var rawArray = [""];
  var directInputArray = sheet.getRange('D6:D'+lastRow).getValues();  
  for(var z = 0; z< directInputArray.length; z=z+1){
     rawArray[z] = String(directInputArray[z]);  //cast all values as strings to remove meta-data
  }

  var answerArray = rawArray.filter(function(n){return n != ''});   //remove blanks
  answerArray.sort();

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  for(var z = 1; z< answerArray.length; z=z+1){
      if(answerArray[z] == answerArray[z-1]) {
          answerArray.splice(z,1);
          z=z-1;                  //reduce z to account for shortened array
      }
  }
Kessy
  • 1,894
  • 1
  • 8
  • 15