Bob Hardball
Bob Hardball

Reputation: 137

Apps Script: Move duplicates to another sheet instead of deleting them

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++);
    }
  }
}

Upvotes: 0

Views: 290

Answers (1)

Cooper
Cooper

Reputation: 64100

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 
    }
  }
}

Upvotes: 1

Related Questions