0

Scripts newbie here. I have a list of URLs in column A

I want tot create a button that helps me delete a row whenever the cell in column A contains one of a list of keywords. I was trying the code below to delete all rows where A contains "blogspot".

It is still not working. I get this error: "TypeError: thisVal.indexOf is not a function"

However, I'm not even sure I can add multiple strings in addition to "blogspot". For example, I would like to delete all rows containing blogspot, wordpress and wix.

    function cleanRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Master');

  var colA_Vals = sh.getRange(1, 1, sh.getLastRow(), 1).getValues();

  var i = 0,
      j = 0,
      thisVal,
      chkVal,
      arrayValsToChk = ['blogspot'];

  for (i=colA_Vals.length;i>0;i-=1) {
    thisVal = colA_Vals[i-1][0];
    //Logger.log('i: ' + i);

    for (j=0;j<arrayValsToChk.length;j+=1) {
      chkVal = arrayValsToChk[j].toLowerCase() //Change to all lower case;

      if (thisVal.indexOf(chkVal) !== -1) {
        ss.deleteRow(i);
      };
    };
  };
};

1 Answer 1

1

Explanation:

Your goal is to delete the rows for which the cells in column A contain a string in the arrayValsToChk list.

  • You need to get the position of the rows which satisfy the condition, namely string in cell A is included in arrayValsToChk. You can use map and filter to get the indexes.

  • Delete the rows backwards as it is also explained why in this thread.

Solution:

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName("Master");
  const colA_Vals = sh.getRange(1, 1, sh.getLastRow(), 1).getValues();
  const arrayValsToChk = ['blogspot','wordpress','wix']; // list of strings
  const indexToDlt= colA_Vals.flat()
                    .map((v,i)=>arrayValsToChk.includes(v.toString().toLowerCase())?i:'')
                    .filter(v=>v!='');
  for (let i = indexToDlt.length - 1; i>=0; i--) {
   sh.deleteRow(indexToDlt[i]+1); 
  }
}
8
  • Doesn't work. I get the error "v.toLowerCase is not a function" Commented Feb 18, 2021 at 6:24
  • @RobPopolizio you need to enable V8 environment. If you are using the old (legacy) editor go to Run => Enable new Apps Script runtime powered by Chrome V8. If you are using the new editor, click on the Settings button to the left of the editor and mark the option Enable Chrome V8 runtime.
    – Marios
    Commented Feb 18, 2021 at 8:13
  • No errors anymore, but when I try to run th macro nothing happens. No rows are been deleted Commented Feb 22, 2021 at 10:27
  • @RobPopolizio if you don't get any error then make sure that column A has one of these words in either upper or lower case: blogspot,wordpress,wix . Make sure you don't have extra spaces or spelling mistakes.
    – Marios
    Commented Feb 22, 2021 at 10:29
  • Didn't help. Still nothing happens when I run the macro. Check it out: docs.google.com/spreadsheets/d/… Commented Feb 25, 2021 at 5:11

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