Google App Script Array Delete Duplicate Value From Top
Solution 1:
I think this will do it.
function removeDuplicates() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var row=rg.getRow();
var col=rg.getColumn();
var vA=rg.getValues();
var nA=[];
var duplicate=true;
for(var i=0;i<vA.length;i++)
{
duplicate=false;
for(var j=0;j<nA.length;j++)
{
if(vA[i][1]==nA[j][1])
{
duplicate=true;
nA[j]=vA[i];
}
}
if(!duplicate)
{
nA.push(vA[i]);
}
}
rg.clearContent();
sh.getRange(row, col, nA.length, nA[0].length).setValues(nA);
}
The outer loop is iterating through all of the rows of the active sheet and each time through it sets duplicate to false. The inner loop searches through nA[] looking for columnB matches if it finds one it sets duplicate to true. If duplicate is true then it doesn't get added to nA[]. The first time through nA.length is 0 so the inner loop doesn't do anything duplicate is false and so that element gets added to nA[]. It keeps doing this until there are no more rows and the rows that are in nA become the unique row. That's the way it use to run when I first did it. But since you wanted to keep the last duplicate instead of the first then I added nA[j]=vA[i];
which replaces the current element with the current match.
Just setup some fake data and play with it and you'll start to see how it works.
Post a Comment for "Google App Script Array Delete Duplicate Value From Top"