Google apps script modify. Exception: Those rows are out of bounds

Viewed 86

sh.deleteRow( i + 1 - d++ ); this line gives me error // Exception: Those rows are out of bounds. What is the problem?

function removeDuplicates() {
    var sh = SpreadsheetApp.getActiveSheet();
    var dt = sh.getDataRange().getValues();
    var uA = [];
    var d  = 0;

    for( var i = dt.length - 1; i >= 0; i-- ) {
        if ( uA.indexOf( dt[ i ][ 0 ] ) == -1 ) {
            uA.push( dt[ i ][ 0 ] );
        } else {
            sh.deleteRow( i + 1 - d++ );
        }
    }
}
1 Answers

You don't need the d variable at all.

function removeDuplicates() {
    var sh = SpreadsheetApp.getActiveSheet();
    var dt = sh.getDataRange().getValues();
    var uA = [];
    // var d  = 0;

    for( var i = dt.length - 1; i >= 0; i-- ) {
        if ( uA.indexOf( dt[ i ][ 0 ] ) == -1 ) {
            uA.push( dt[ i ][ 0 ] );
        } else {
            sh.deleteRow( i + 1 );
        }
    }
}
Related