I'm new to Google Apps Scripts so I am sorry if my question is redundant.
I am working on a variant of the scripts here:
Google Sheets: delete rows containing specified data
(Here is the script that I have been editing -- note the empty IF value.)
function onOpen(){
}
function deleteFunction(){
var sheetName = "Title";
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(sheetName);
var dataRange = sheet.getDataRange();
var numRows = dataRange.getNumRows();
var values = dataRange.getValues();
var rowsDeleted = 0;
for (var i = 2; i <= numRows; i++){
var rowValues = values[i-1].toString();
if (rowValues == ""){
sheet.deleteRow(i - rowsDeleted);
rowsDeleted++;
}
I would like to delete rows in a sheet named "Title" that do not contain "| My Text Here". The text is found in a string of text, such as "Here is the string of text that could be random | My Text Here". So if "| My Text Here" is not found in a cell, I want that whole row to be deleted.
The data set I want this to work with will have ~10,000 rows and I want this script to either run when the sheet is opened or once a day.
I have tried to make this work, but I think I'm on the wrong track and so would really apprecaite the communities help!
I can attach a test sheet if needed.
Thank you in advance for your help and guidance
New script:
function main() {
var SS = SpreadsheetApp.getActive();
var SHEET = SS.getSheetByName("Title");
var RANGE = SHEET.getDataRange();
var DELETE_VAL = "| TEST TEXT HERE";
var COL_TO_SEARCH = 0; //Zero is first
var deleteSelectedRows = removeThenSetNewVals();
};
function removeThenSetNewVals(){
var SS = SpreadsheetApp.getActive();
var SHEET = SS.getSheetByName("Title");
var RANGE = SHEET.getDataRange();
var rangeVals = RANGE.getValues();
var DELETE_VAL = "| TEST TEXT HERE";
var COL_TO_SEARCH = 0; //Zero is first
var newRangeVals = [];
for(var i = 0; i < rangeVals.length; i++){
if(rangeVals[i][COL_TO_SEARCH] == DELETE_VAL){
newRangeVals.push(rangeVals[i]);
};
};
RANGE.clearContent();
var newRange =
SHEET.getRange(1,1,newRangeVals.length, newRangeVals[0].length);
newRange.setValues(newRangeVals);
};
}