I'm attempting to remove rows from a sheet based on criteria. I found the following code that works beautifully for what I need:
Delete a row in Google Spreadsheets if value of cell in said row is 0 or blank
However, it isn't clear how the column is being specified. I don't see a
e.range.columnStart == 3 like in other answers.
Any guidance on how to read it?
**
* Deletes rows in the active spreadsheet that contain 0 or
* a blank valuein column "C".
* 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 = 2; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] > 1.5 || row[2] < 1) {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
Related
I have two sheets (Sheet1 and Mirror) in a single google spreadsheet. I have some columns in the Mirror sheet which are being mirrored from Sheet1 using an array formula (={"Firstname";ARRAYFORMULA(INDIRECT("Sheet1!B2:B"))}).
I have rows being copied automatically from a google form into Sheet1. For some reason, periodically, about 500 empty rows are entered into the Mirror sheet and I have to manually delete them.
I have these googlesheet functions below that trigger on change. I have only deployed the function ReadRows and it works like a charm. Rather than deploying the second function (DeleteBlankRows) which might slow down my spreadsheet, I do like to know how I can figure out what is causing the extra periodic 500 blank rows to appear and stop it or I'd like any professional advice you can give.
Please see the sample scripts below. Thanks in advance.
function readRows() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
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[1] == 0 || row[1] == '') && (row[2] == 0 || row[2] == '')){
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function DeleteBlankRows() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Mirror');
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[1] == 0 || row[1] == '')){
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
Updated answer:
The issue is that you need to delete the rows backwards for Sheet1 and then you don't need to use DeleteBlankRows for Mirror anymore. Replace your code with this one:
function readRows() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var rows = sheet.getDataRange();
var values = rows.getValues();
for (var i = values.length - 1; i>=0; i--) {
var row = values[i];
if ((row[1] == 0 || row[1] == '') && (row[2] == 0 || row[2] == '')){
sheet.deleteRow(i+1);
}
}
};
To get rid of empty rows, you can simply filter them out before you copy them to the target sheet.
Change:
var values = rows.getValues();
to:
var values = rows.getValues().filter(r=>r[0]!='');
Assuming for every empty row, the cell in column A is also empty.
You can now remove the DeleteBlankRows unless you want to delete also the rows.
I would like to modify this code so that if there is any data(Numbers as well) in column 7, it will delete the entire row.
Currently whatever you put into the inverted commas in the If Statement, it deletes
what could you substitute that would do that ?
function DeleteCOMPLETED2() {
var sheet = SpreadsheetApp.getActive();
sheet.setActiveSheet(sheet.getSheetByName('RELOCATION'), true);
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[7] == "**if there is any data in column 7 then delete entire row**") {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}}}
Delete row if anything in column7
function DeleteCOMPLETED2() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('RELOCATION');
var values=sh.getDataRange().getValues();
var d=0;
values.forEach(function(r,i){if(r[6]){sh.deleteRow(i+1-d++);}});//column7
}
Every time I make edits to any of the 5 other spreadsheets, as soon as I finish editing a cell, I am returned to sheet name "HOLDING". I would like this to stop happening.
function onChange(e) {
DeleteRow(e);
}
function DeleteRow(e) {
// This Code will delete any "Y" in Column E.
var sheet = SpreadsheetApp.getActive();
sheet.setActiveSheet(sheet.getSheetByName('HOLDING'), true);
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[4] == "Y") {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
Thanks in Advance
The call to setActiveSheet() is what's causing the active sheet to change.
Try replacing the first couple lines of DeleteRow() with an approach that will allow you to interact with the HOLDING sheet without making it active:
function DeleteRow(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('HOLDING');
<...continue with the rest of the original implementation...>
I have been tasked by my company to make an update to our sheet. The script needs to clear certain cells in a row based on the value of another cell. I have managed to find a script that deletes the whole required row, however that causes an issue with the rest of the document.
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[14] == '' && row[6] == 'PREBOOKED') { // 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++;
}
}
};
Is it possible to modify this to instead of deleting the row it just clears the content of the cells D - P?
You can use below code with modifications if necessary.
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getDataRange().getValues();
values.forEach(function(v, i) {
// get row index
var row = i+1;
// look for required values in cells
if (v[14] == '' && v[6] == 'PREBOOKED') {
// might have to modify this line based on Sheets API
sheet.getRange('D'+row+':P'+row).clearContent();
}
});
}
i was able to do what i need with the following:
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[14] == '' && row[6] == 'PREBOOKED') { // 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.getRange((parseInt(i)+1), 6).clearContent();
sheet.getRange((parseInt(i)+1), 7).clearContent();
sheet.getRange((parseInt(i)+1), 10).clearContent();
sheet.getRange((parseInt(i)+1), 11).clearContent();
sheet.getRange((parseInt(i)+1), 12).clearContent();
rowsDeleted++;
}
}
};
thanks for your ideas folks!
In Google Sheets, I have found and edited code that allows me to delete rows if a cell value in a specified column is 0 or blank. I want to run this same task across 2 of the 4 sheets in my spreadsheet. How do I edit the code to have it function in multiple sheets simultaneously. The 2 sheets are not related, but I have it structured so the same column will be searched in both sheets for the 0.
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[4] == 0 || row[4] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
Class SpreadsheetApp getActiveSheet() returns a single sheet. To get all the sheets of your spreadsheet use Class Spreadsheet getSheets(), to get only certain sheets by their name use Class Spreadsheet getSheetByName(name).