Skip to content

Instantly share code, notes, and snippets.

@dDondero
Created December 8, 2015 19:03
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dDondero/285f8fd557c07e07af0e to your computer and use it in GitHub Desktop.
Save dDondero/285f8fd557c07e07af0e to your computer and use it in GitHub Desktop.
Google Apps script function to delete rows based on value in cell.
function deleteRows() {
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] == 'delete' || row[0] == '') { // This searches all cells in columns A (change to row[1] for columns B and so on) and deletes row if cell is empty or has value 'delete'.
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
@ragger1
Copy link

ragger1 commented Jun 13, 2023

Wow, it really does work. Thank you with that, Though, for some reason, It overrides the sum formula. Maybe its cause of numCols-1, I'm unsure. Could you check that out for me? I simply want it to override the "=IF(T36<10, "Unclear", "Clear")" formula if there is an EX in the row. But i guess it overrides the sum formula just next to it. Could you look into it? Thanks. Other than that, works just fine.

@ragger1
Copy link

ragger1 commented Jun 13, 2023

image
As an example here, I tried to implement it on another sheet and it gave me this result. Can this be solved by any chance man?
I was thinking about if we could adapt the "=IF(T36<10, "Unclear", "Clear")" in the script, so that we'd get a singular result instead of two?

@lewws66
Copy link

lewws66 commented Jun 13, 2023

May be you show me your script? and the file.
If you used the one I sent you https://docs.google.com/spreadsheets/d/1AARYUNgdRBE_67bT0bsGbOs1ULoA4I2BXGG8OrLynBo/edit?usp=sharing it should work for any number of rows of data, for the same number of columns, placed anywhere in the spreadsheet.

@ragger1
Copy link

ragger1 commented Jun 13, 2023

Ah, never mind man, I think It was me who made a mistake. It seems to be working perfectly fine for now. I really do appreciate your help and wish you an amazing day.

@lewws66
Copy link

lewws66 commented Jun 13, 2023

Same. Wishing you a good day!

@simesy
Copy link

simesy commented Jul 19, 2023

It might be worth updating your script to explain (in a code comment) that this is method does not necessarily have good performance. Ideally you should delete rows in batches. For more information https://developers.google.com/apps-script/guides/support/best-practices or this example https://stackoverflow.com/questions/73651127/speed-up-row-deletion-on-apps-script

@shifeau
Copy link

shifeau commented Oct 15, 2023

i've been trying to make this condition work for the past hour, and i have used the debugger to see what exactly is stored in each variable (as you can see on the right) and they do match, but the condition is FALSE, and i do not get why.
can someone help? (i have never used google script or javascript before)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment