user10637952
user10637952

Reputation: 73

Script to delete duplicate rows based on cell value in a column

I am fairly new to scripts.

I found the following script and would like it to be able to search in Column W for any duplicates and when it finds any to then delete the duplicated rows

function removeDuplicates() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getLastRow();
  var firstColumn = sheet.getRange(2, 2, rows, 1).getValues();
  for (var i = rows; i >= 2; i--) {
    if (firstColumn[i-1][0] == firstColumn[i-2][0]) {
      sheet.deleteRow(i);
    }
  }
}

Upvotes: 0

Views: 373

Answers (1)

J. G.
J. G.

Reputation: 1832

Try changing the following line:

var firstColumn = sheet.getRange("W:W").getValues();

Upvotes: 1

Related Questions