I was working on some automation and wanted to remove the duplicate rows in my google sheet by comparing it on basis of 3rd column. I found one code which is working flawlessly but it does not remove the old entry in sheet, it removes the latest one. I wanted to keep the latest one from the duplicates.
This is the code which I found for appscript by Cooper:
function removeDuplicates() {
var sh=SpreadsheetApp.getActiveSheet();
var dt=sh.getDataRange().getValues();
var uA=[];
var d=0;
for(var i=0;i<dt.length;i++) {
if(uA.indexOf(dt[i][2])==-1) {
uA.push(dt[i][2]);
}else{
sh.deleteRow(i+1-d++);
}
}
}
Can anyone help me with the code which does the same work "removing duplicate rows (Keeps latest entry removes the old entry) based on column" ?
From I wanted to keep the latest one from the duplicates.
, when the latest one is the last row, in your script, how about the following modification?
function removeDuplicates() {
var sh = SpreadsheetApp.getActiveSheet();
var dt = sh.getDataRange().getValues();
var uA = [];
for (var i = dt.length - 1; i >= 0; i--) {
if (uA.indexOf(dt[i][2]) == -1) {
uA.push(dt[i][2]);
} else {
sh.deleteRow(i + 1);
}
}
}