Delete a row in google spreadsheets based on value of cell

I used the following google spreadsheet script to remove a bunch of rows that had a set value:

/**
 * Deletes rows in the active spreadsheet that contain 'Yes' in column A
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function readRows() {
 var sheet = SpreadsheetApp.getActiveSheet();
 var rows = sheet.getDataRange();
 var numRows = rows.getNumRows();
 var values = rows.getValues();

 var rowsDeleted = 0;
 for (var i = 0; i <= numRows - 1; i++) {
 var row = values[i];
 if (row[0] == 'Yes') {
 sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
 rowsDeleted++;
 }
 }
};

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the readRows() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
 var sheet = SpreadsheetApp.getActiveSpreadsheet();
 var entries = [{
 name : "Remove rows where column A is 'True'",
 functionName : "readRows"
 }];
 sheet.addMenu("Script Center Menu", entries);
};

Published by

MikeGrace

I am a Geek! I hope you enjoyed this recent post.