Alex Gherardelli
Alex Gherardelli

Reputation: 21

How to remove all rows matching a criteria in Google Sheets with Google Apps Script?

I'm working on a simple email management system using Google Sheets, Forms and Apps Scripts, but I'm new to Javascript/Google Apps Script and I've been stuck on this problem for quite a bit now.

So, I have a spreadsheet with contacts, listing their name, email and group, that looks like this:

| Name  | Email             | Group  |
|-------|-------------------|--------|
| John  | [email protected]  | A      |
| Bill  | [email protected]  | A      |
| Janet | [email protected] | B      |
| Mary  | [email protected]  | B      |
| Mary  | [email protected]  | Delete |
| Bill  | [email protected]  | Delete |
| Janet | [email protected] | A      |

I am trying to write a script that programmatically finds all emails that are marked with "Delete" and removes all rows containing those emails. In this case, I would need to eliminate all rows containing [email protected] and [email protected] and keep John and Janet. Expected output:

| Name  | Email             | Group  |
|-------|-------------------|--------|
| John  | [email protected]  | A      |
| Janet | [email protected] | B      |
| Janet | [email protected] | A      |

The problem is that I cannot find a way to filter my sheet in this way and my script keeps copying everything, including the rows I would like to remove. This is the code I wrote so far:

function removeDelRows() {

    var ss = SpreadsheetApp.getActiveSheet(); // Gets active sheet
    var data = ss.getDataRange().getValues(); // Get values in active sheet

    var toDelete = new Array();
    var newData = new Array();

    // Put in toDelete array all emails marked as "Delete"
    for (var i = 0; i < data.length; i++) {
        if (data[i][2] == "Delete") {
            toDelete.push(data[i][1]);
        }
    }
    // !BUGGY!
    // Find all rows that contains emails to be deleted and remove them 
    for (var j = 0; j < data.length; j++) {
        for (var k = 0; k < toDelete.length; k++) {
            if(data[j][1] == toDelete[k]){} // Do nothing
            else{
                newData.push(data[j]); // THIS CONDITION IS NOT BEHAVING AS EXPECTED; NEWDATA IS A COPY OF DATA
            }
        }
    }
  Logger.log(newData); // newData should contains only rows that do not match toDelete ones

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

I'm sure the solution is quite trivial, but I've been banging my head for hours and hours and I cannot figure out this!

Upvotes: 2

Views: 1782

Answers (1)

Chris Chen
Chris Chen

Reputation: 1293

How about using deleteRow()?

    var ss = SpreadsheetApp.getActiveSheet(); // Gets active sheet
    var data = ss.getDataRange().getValues(); // Get values in active sheet

    nextLine: for( var i = data.length-1; i >=0; i-- ) { 
      if( data[i][2] !== "Delete" ) continue nextLine;
      ss.deleteRow(i+1);
    }

Upvotes: 2

Related Questions