Home > Back-end >  Apps Script: Move duplicates to another sheet instead of deleting them
Apps Script: Move duplicates to another sheet instead of deleting them

Time:01-10

I found this amazing masterpiece of @Cooper to search for duplicates based on values in 1 column. Now instead of removing these duplicates as the code does, I want to move them to another sheet. Any idea how this can be done?

Thanks :).

Here is the code:

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][0])==-1) {
      uA.push(dt[i][0]);
    }else{
      sh.deleteRow(i 1-d  );
    }
  }
}

CodePudding user response:

Move row

function removeDuplicates() {
  const ss = SpreadsheetApp.getActive();
  const dsh = ss.getSheetByName('Destination');
  var sh=SpreadsheetApp.getActiveSheet();
  var dt=sh.getDataRange().getValues();
  var uA=[];
  let d = 0;
  for(var i=0;i<dt.length;i  ) {
    if(uA.indexOf(dt[i][0])==-1) {
      uA.push(dt[i][0]);
    }else{
      dsh.appendRow(dt[i]);//append
      sh.deleteRow(i 1-d  );//delete 
    }
  }
}
  •  Tags:  
  • Related