0
votes

So I have a code that removes rows from the spreadsheet that contain specific words. How can I apply it not only to one column and search for only one word but to check several columns and several words?

function sort() { 
  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[21].indexOf('Wohnmobil') > -1) { 
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted); rowsDeleted++; 
    } 
  } 
};

With this code it looks through column V and removes rows with word "Wohnmobil".

How to make it check columns R and V for example and remove rows that contain words like "Wohnmobil", "WG", "Trailer", "Caravan"?

1

1 Answers

0
votes

You can try this,

function sort() { 
  var sheet = SpreadsheetApp.getActiveSheet(); 
  var rows = sheet.getDataRange(); 
  var values = rows.getValues(); 
  var rowsDeleted = 0; 
  var arrayOfWords = ['Wohnmobil', ....] //this is your array of words to search

  for (var i = values.length - 1; i >= 0; i--) {  
    var row = values[i]; 
    for (var j = 0; j < arrayOfWords.length; j++) {    //loop thru your word list
      if (row.indexOf(arrayOfWords[j]) > -1) {         // search every column 
        sheet.deleteRow(i+1);     //since i loop runs from bottom row, you don't need to account for row number changes 
        rowsDeleted++;   
        break; //go out of j loop since a word has already been found and the row deleted        
      }
    } 
  } 
};