Question

So I'm trying to solve a problem with a sheet I have, it contains a Vlookup where people enter the date, their Intials, the custID and it displays a value from that row. (the original data is form responses, with emails, time stamps and 2 formulas working on the end)

This hasn't been the best solution so I was thinking about trying to automate this by showing the looked up value on the submission page, but this is quite beyond me.

So then I thought of perhaps an auto lookup via script that works as follows, (bearing in mind Col A is timestamp, B is email addy & column X has the value we want to return.)

I want it to start from last row, find the last x unique values from B, (possibly using A to find the greatest or last entry by date/time) and add row to an array for later (entire row isn't actually needed I guess, just B and X.)

Once it has it's 10 in the array, output Column B of array and Z of array)

Using my limited scripting knowledge and posts on here, this is where I'm at so far.

 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 = [];
 var counter = data.length;

while (targetData.length < uniqueToReturn)
  {



    if (data[counter][columnNo - 1] != "" && data[counter][columnNo - 1] != targetData.getDataRange.getValues()[columnNo - 1])
    {



      targetData.push(data.splice(counter, 1)[0]);
    }
    else
    {


      counter--;
    }
  }   



 }

So now I'm at a brick wall as to how to compare entries in my datarange to the array to avoid duplicates.

The 2nd part of the if statement, after the && isn't right. I'm trying to work it out.

If anyone could give me any pointers, or some good links, I'd appreciate it.

Also, I hope I've got it right and it's going backwards from last row. Logger did show correct row numbers etc when testing data.length

Will carry on searching the boards.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top