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
}
}
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
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.