Home > Enterprise >  Remove rows based on duplicates in a single column in Google Sheets with script
Remove rows based on duplicates in a single column in Google Sheets with script

Time:11-04

I'd like to remove all duplicates of row based on the second column data.

SO OF THIS SCREENSHOT row 7 and 8 would be removed. Any help would be greatly appreciated.

P.S. In my case I have columns from A to F and rows from 1 to 30000.

Thank you.

I tried this code but the big one problem is that it ever use the first column and i dont know how to put it to do use the secont column T-T

`

`function removeDuplicates() {
var sheet = SpreadsheetApp.getActiveSheet();

    var data = sheet.getDataRange().getValues();
    var newData = [];
    var ids = [];
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1');
    for (var i in data) {
      var row = data[0][1];
      var duplicate = false;
      if (ids.indexOf(row[0]) > -1) {
        duplicate = true;
      } else {
        duplicate = false;
        ids.push(row[0]);
      }
      if (!duplicate) {
        newData.push(row);
      }

}
sheet.clearContents();
sheet.getRange(1, 1, newData.length, newData\[0\].length).setValues(newData);
}`

`

CodePudding user response:

Remove Duplicate Rows based upon value in column2

function removeDupes() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const vs = sh.getDataRange().getValues();
  let d = 0;
  let u = []
  vs.forEach((r, i) => {
    if (~u.indexOf(r[1])) {
      sh.deleteRow(i   1 - d  )
    } else {
      u.push(r[1]);
    }
  });
}
  • Related