简体   繁体   中英

How to delete rows that do not containing specific text within A column using Google Sheets App?

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

Your code needs the following modification to work in the way you desire

  1. Use indexOf() . This is a Javascript method that allows you to verify either a sub-string is contained in a string. In your case, it allows you to verify either "| My Text Here" is contained in your cells in column A.

  2. Loop "backwards" from the end row to the start row. Why? Because otherwise the deletion of rows will mess up the row indices of the remaining rows.

function deleteFunction(){
  //declarations
  var sheetName = "Title"; 
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName(sheetName);
  var dataRange = sheet.getDataRange();
  var values = dataRange.getValues();
  for(var i=values.length-1;i>0;i--){
    var myValue=values[i][0];
          Logger.log(i);
    if( myValue.indexOf("| My Text Here")==-1){
      Logger.log(myValue);
      sheet.deleteRow(i+1);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM