So here's what I came up with (assuming I understood the problem correctly):
function lastUniqueList(){
var sheetName = "SheetNameHere"; // sheet name with data
var columnNo = 2; // A = 1, B = 2 Column that we want the unqiue values from. IE last x unique email addresses.
var uniqueToReturn = 10; //how many unique entries, ie, how many unique emails.
var uniqueLookup = 24; // Column number that has the value we want looked up and returned.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var targetData = [];
for (var counter=data.length-1; counter >= 0 && targetData.length < uniqueToReturn; counter--) {
if (data[counter][columnNo - 1] == "") {
continue;
}
if (targetData.some(function (element, index, array) {
return data[counter][columnNo - 1] == element[columnNo - 1];
})) {
continue;
}
targetData.push(data[counter]);
}
}
The secret sauce is in that Array.prototype.some method. It's asking if there are any elements in targetData
that pass some test. The test is having a value at columnNo
matching the one we're thinking of inserting. That's how we guarantee the unique values.
I also converted the while loop to a for loop because it's neater.
Here's the jsFiddle where I tested my solution.