Run Same Delete Row Script in Multiple Sheets in Google Sheets - google-apps-script

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).

Related

Hide A Row with Certain Text (Modify CODE) - Google Apps Script / Google Sheets

I have the below script that can delete a row with certain text, but could it hide a row with certain text.
function DeleteAnyNO() {
// This Code will delete any "NO" in Column C.
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[2] == 'NO') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
So in the above code, could you modify to hide all "NO" instead of delteting ?
As Always - Thanks in advance.
How about this modification? Please think of this as just one of several possible answers.
Modified script:
When your script is modified, how about the following modification?
From:
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
To:
sheet.hideRow(sheet.getRange("A" + (i + 1)));
In this case, in your script, sheet is Spreadsheet object. So I used hideRow() in Class Spreadsheet.
Also in this case, var rowsDeleted = 0; is not used.
Reference:
hideRow(row) of Class Spreadsheet
If this was not the direction you want, I apologize.
Since you have the rowId, you can use hideRows method to hide that particular row in the Google Sheet.
/** #OnlyCurrentDoc */
function hideRow(rowIndex) {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getActiveSheet().hideRows(rowIndex);
};

onChange Script keeps returning to certain SpreadSheet - Google pps Script / Google Sheets

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...>

More Efficient Script for Delete Row If Cell Contains "DEL"

I have a 50,000+ row Google Sheet that I update every day with new data. At the end of the day I run an "IF" function that helps me determine if I want to delete that row.
I then wrote a script that looks through the sheet and deletes any row that has "DEL" in the specific Column. The problem is that since I have so many rows, the script takes too long to run. Anyone have any ideas of a more efficient way to delete/clear a row if a cell has the letters "DEL"?
function deleteRows() {
var sheet = SpreadsheetApp.getActive().getSheetByName('DEL_test');
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[9] == 'DEL') { // Search cells in Column "J" and delete row if cell is equal to "DEL"
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
Browser.msgBox('COMPLETE');
};
You want to efficiently delete rows with DEL in the column "J".
How about using Sheets API? When Sheets API is used, several rows can be deleted by one API call. By this, the process cost will be able to be reduced. I think that there are several workarounds for your situation. So please think of this as just one of them.
When you use Sheets API, please enable Sheets API at Advanced Google Services and API console. You can see about how to enable Sheets API at here.
Modified script:
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('DEL_test');
var rows = sheet.getDataRange();
var values = rows.getValues();
var spreadsheetId = ss.getId();
var sheetId = sheet.getSheetId();
var reqs = values.reduceRight(function(ar, e, i) {
if (e[9] == 'DEL') {
ar.push({"deleteDimension":{"range":{
"sheetId": sheetId,
"dimension": "ROWS",
"startIndex": values.length - i - 1,
"endIndex": values.length - i,
}}});
}
return ar;
}, []);
Sheets.Spreadsheets.batchUpdate({"requests": reqs}, spreadsheetId);
Browser.msgBox('COMPLETE');
}
References:
spreadsheets.batchUpdate
DeleteDimensionRequest
If I misunderstood your question, please tell me. I would like to modify it.
Edit:
If you don't want to use Sheets API and you want to use clearContent(), how about this sample script? The flow of this sample script is as follows.
Retrieve all values.
Retrieve only the rows which have no "DEL" in the column "J".
Put the values.
Sample script:
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('DEL_test');
var rows = sheet.getDataRange();
var values = rows.getValues();
sheet.clearContents();
var val = values.filter(function(e) {return e[9] != 'DEL'});
sheet.getRange(1, 1, val.length, val[0].length).setValues(val);
Browser.msgBox('COMPLETE');
}
Note:
I'm not sure about your actual Spreadsheet. So if this was not the result you want, can you provide a sample Spreadsheet? By this, I would like to modify the script.
For the time being I have removed hope of using "deleteRow" as it looks to be a slow-to-operate function. That has lead me to using this script which works for the time being. Thank you to everyone gave me their time.
var spreadsheet = SpreadsheetApp.getActive().getSheetByName('DEL_test');
var rows = spreadsheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var lr = spreadsheet.getLastRow();
for (var i = 0; i < numRows; i++) {
if (values[i][9] == 'DEL') {
spreadsheet.getRange(i+1, 1, 1, 10).clearContent();
}
}

How to reference specific column in Apps Script

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++;
}
}
};

Google Sheets - Auto-Generate sheets named with a cell value in a row and populated with remaining row data

I'm trying to use the Google Sheets script editor to create a set of sheets from a form to create individual sheets named after the value of each cell in column "B" and populated with all the data in rows 1 and X, where X is the value of the row for each sheet.
What I have so far is:
function makeSheets() {
var ss = SpreadsheetApp.getActive();
var sheets = ss.getSheets();
sheets[0].getRange('B2:B100')
.getValues()
.forEach(function (el, i) {
if (el[0] && !ss.getSheetByName(el[0])) ss.insertSheet(el[0], sheets.length +i)
});
}
This will create the new sheets with the same header rows (row 1) one the new sheets. I don't understand the X you want to create.
function makeSheets() {
var ss = SpreadsheetApp.getActive();
var sheets = ss.getSheets();
var r1=sheets[0].getRange('1:1').getValues()
sheets[0].getRange('B2:B100')
.getValues()
.forEach(function (el, i) {
if (el[0] && !ss.getSheetByName(el[0])) ss.insertSheet(el[0], sheets.length +i).getRange('1:1').setValues(r1);
});
}