I'm trying to iterate through a spreadsheet column while skipping the duplicated values. To do so, I use TextFinder to get all the cells with identical values and I add them to a "duplicates" array.
At every iteration, an if clause verifies if the current cell is part of the "duplicates" array and if so, it should just skip to the next iteration. However, it isn't the case; it proceeds through my if clause. I tried both "includes()" and "indexOf()".
Here's my code:
let duplicates = [];
for (let row = 1; row <= range.getNumRows(); row++) {
const cell = range.getCell(row, 1);
const value = cell.getValue();
if (duplicates.includes(cell) == false) {
let array = range.createTextFinder(value).findAll();
const cellIndex = array.indexOf(cell);
array.splice(cellIndex, 1);
duplicates.push(...array);
}
}
I'm honestly not sure why it doesn't work. However, I'm new to coding and it's probably a beginner's mistake.
Any help is appreciated! Thanks!
If you're only trying to find the unique values of a column, try:
function uniqueValuesOfColumn() {
return [...new Set(
SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(`Sheet1`)
.getRange(`A:A`)
.getValues()
.flat()
.filter(Boolean)
)]
/* Or
const uniqueValues = [...new Set(
SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(`Sheet1`)
.getRange(`A:A`)
.getValues()
.flat()
.filter(Boolean)
)];
*/
}
This converts all values of a column, with empty cells removed, into an array of unique values. This is done by 'converting' a Set of values into an array.