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 Answers1

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); 
  }
}
Marios
  • 26,333
  • 8
  • 32
  • 52
  • Doesn't work. I get the error "v.toLowerCase is not a function" – Rob Popolizio Feb 18 '21 at 06: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 Feb 18 '21 at 08:13
  • No errors anymore, but when I try to run th macro nothing happens. No rows are been deleted – Rob Popolizio Feb 22 '21 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 Feb 22 '21 at 10:29
  • Didn't help. Still nothing happens when I run the macro. Check it out: https://docs.google.com/spreadsheets/d/1YEWAYZFdt1meHSLGy757RybxVBoyF12Xw3gspRKQJrM/edit#gid=0 – Rob Popolizio Feb 25 '21 at 05:11
  • Now it's even worse. I can't even see the macro anymore. I debug and run the script, but for some reason the save button is unclickable and when I run the script the macro doesn't appear in the macros menu on the spreadsheet – Rob Popolizio Feb 27 '21 at 07:33
  • @RobPopolizio lol dude.. I didn't even open your file because you didn't give access. I would advice you to familiarize yourself with Google Apps Script and create the project step by step to understand what each step does. That is the only way you can learn. – Marios Feb 27 '21 at 10:45
  • I tried to open it with another account and it works. The link is on editor mode and opened to anyone – Rob Popolizio Feb 28 '21 at 11:04