Trying to sort and push only the distinct values into a new array and then find the length of the resulting array. See code below. The UniqueTeams.length is coming out to be 1, when it should be 5 (I thought).
var teams = [[formSS.getRange("F8").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("F12").getValue(),
formSS.getRange("F14").getValue(),
formSS.getRange("F16").getValue()]];
var uniqueTeams = removeDups(teams);
if(uniqueTeams.length < 5){
{SpreadsheetApp.getUi().alert(uniqueTeams);
return;}
}
function removeDups(array) {
var outArray = [];
array.sort();
outArray.push(array[0]);
for(var n in array){
if(outArray[outArray.length-1]!=array[n]){
outArray.push(array[n]);
}
}
return outArray;
}
uniqueTeams = Alabama (2),Maryland (10),Cleveland St (15),BYU (6),Liberty (13)
uniqueTeams.length = 1
CodePudding user response:
Get unique elements
function remdup() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const t = sh.getRange('F8:F16').getValues().flat();
let teams = [t[0], t[2], t[4], t[6], t[8]];
let s = new Set(teams)
teams = [...s];
Logger.log(teams.join(','))
}
CodePudding user response:
Cooper's answer it the best ultimate solution. But if you want just to fix your code you can try to change the line:
var uniqueTeams = removeDups(teams);
with:
var uniqueTeams = removeDups(teams[0]);
Since the teams
is a 2D array [[...]].