Bunny
Bunny

Reputation: 323

Apps Script not deleting rows

When I run the following function in my sheet:

    function removeDuplicateRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var column = sheet.getRange('D580:D');
  var data= column.getValues();
  var newData = new Array();
  for(i in data){

    var row = data[i];
    var duplicate = false;
    for(j in newData){

      if(row.join() == newData[j].join()){

        duplicate = true;

      }

    }
    if(!duplicate){
      newData.push(row);

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

}

Nothing happens, it just times out. It seems to be due to my 'column' variable when I identify the range. If I leave it blank it runs but I don't want it to hit the first 580 rows. Anybody out there know why this happens?

Thanks for any help,

Upvotes: 0

Views: 51

Answers (1)

Cooper
Cooper

Reputation: 64040

Try this:

function removeDuplicates() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getRange(580,4,sh.getLastRow(),1);
  var vA=rg.getValues();
  var uA=[];
  var dC=0;
  for(var i=0;i<vA.length;i++) {
    if(uA.indexOf(vA[i].join())==-1) {
      uA.push(vA[i].join());
    }else{
      sh.deleteRow(i + rg.getRow() - dC);
      dC++;
    }
  }
}

Upvotes: 1

Related Questions