I'm trying to write a custom Google Spreadsheets function in AppsScript that will take an array ("range") of arbitrary dimensions and return the unique values in a single column. The function takes an optional argument transpose
that should sort the output by rows instead of columns.
I've got this semi-working, but the transpose
method is only working on square arrays:
function GETUNIQUES(range, transpose) {
traspose = transpose || !0;
if (transpose) {
range = range.map(function(col, i) {
return range.map(function(row) {
return row[i];
});
});
}
var flat = [];
var clean = [];
for (i = 0; i < range.length; i++) {
for (j = 0; j < range[i].length; j++) {
flat.push(range[i][j]);
}
}
for (i = 0; i < flat.length; i++) {
flat[i] = String(flat[i]);
if (flat[i].length && clean.indexOf(flat[i].trim()) < 0) {
clean.push(flat[i].trim());
}
}
return clean;
}
Could someone help me with a solution for the transpose
method that follows row order without truncating results?