0

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);
   };
  }
3
  • Can you provide the script with which you tried to make it work? And a test sheet that visualizes your data structure? Commented Jan 8, 2020 at 12:52
  • Thank you for your reply. Here is the sheet and the script that I have been working with is contained in it: docs.google.com/spreadsheets/d/… Commented Jan 8, 2020 at 14:00
  • 1
    I have also added the script above Commented Jan 8, 2020 at 14:08

1 Answer 1

1

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);
    }
  }
}
4
  • 1
    amazing, thank you so much!!! I can't thank you enough for taking the time to fix this and also explain the process to me so I can learn for next time. I really, really appreciate your time. Commented Jan 8, 2020 at 15:36
  • No worries! I am glad it helped you. Commented Jan 8, 2020 at 16:04
  • Hi ziganotschka, thank you again for your code. It works well, but as my data set is so large (>10,000 rows) the script cuts out before finishing. I have added an updated script above, but I am getting an error -- "TypeError: Cannot read property 'length' of undefined", which refers to "var newRange = SHEET.getRange(1,1,newRangeVals.length, newRangeVals[0].length);" Do you happen to have any insight on why this might be happening? Commented Jan 9, 2020 at 13:08
  • If your file is very large, two things can happen: 1. Your script yimes out (becasue the maximum runtime per script for free accounts is 6 min developers.google.com/apps-script/guides/services/quotas). 2. The acquisition of values takes a long time and the asynchronisity of Apps Script becomes noticeable. The second can be resolved by inserting SpreadsheetApp.flush() between var values = dataRange.getValues(); and for(var i=values.length-1;i>0;i--){... Commented Jan 9, 2020 at 15:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.