'I Can delete a row in active sheet when particular column has no value in it (blank column) . Can I do the same for particular spreadsheet(Sheet 2)?
/ Deletes rows in the sheet when column C is empty
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[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
I have found this function from stackoverflow which works fine for active sheet but I would like to run this function for particular spreadsheet. (Sheet name to be included in a app script).
Solution 1:[1]
Just in case:
function remove_rows_with_empty_first_cell() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
data = data.filter(row => row[0] != '');
sheet.clear().getRange(1,1,data.length,data[0].length).setValues(data);
}
It works fast, but if you have formulas on the sheet it's need a more complicated solution.
Solution 2:[2]
// Deletes any row whose first column is blank
// WARNING: Assumes any valid row has data in column 1
function deleteBlankRows() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("Sheet3"); // input sheet name here
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow();
for (var i = lastRow; i > 0; i--) {
var range = sheet.getRange(i,1);
var data = range.getValue();
if (data == '') {
sheet.deleteRow(i);
}
}
}
I found this script from stackoverflow. Thank you... Delete rows for particular sheet..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yuri Khristich |
| Solution 2 | Elango nithyanandam |
