简体   繁体   中英

How to delete rows if contains a string only in a specific column with spreadsheet script

I have this spreadsheet with my sheet called "Products": https://docs.google.com/spreadsheets/d/1AflG3VgCFdqz1dRbP0JwGBrc5xXL9kwRBk13yEpekTM/edit#gid=1671517193

I find the initial script here: // How to delete row in Google Sheet if "Column A" cell CONTAINS given text

Column F contains my categories, but I want to delete all rows with the if contain the next strings: " CD" "MP3" "Todos"

I hope if possible do with these conditions: 1. Remove if contains the whole of part of the string. For example, if in the column you see: "COMPANY / Income / CD / in" the script will delete because the cell contains " CD"

  1. Ignore the letter case. For example, all of these will delete: " CD" " cd" " cD"

Here, my code is not working, because delete some correct rows. The Error is in the function findStringAndDeleteRow.

var sheetName = "Products"; 
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(sheetName);


function onOpen(){
    SpreadsheetApp.getUi() 
    .createMenu('COMPANY')
    .addItem('Products Cleaner Data', 'findStringAndDeleteRow')
    .addToUi();
  }

function findStringAndDeleteRow() {
  findStringAndDeleteRow_001();
  findStringAndDeleteRow_002();
  findStringAndDeleteRow_003();


}

function findStringAndDeleteRow_001() {
  var values = sheet.getDataRange().getValues();
    for(var i=values.length;i>0;i-=1){
     var lcVal=values[i-1][5].toLowerCase() //Change to all lower case
     var index = lcVal.indexOf(" cd"); //now you only have to check for  contains "glass"
     if (lcVal.indexOf(" cd") > -1){
     sheet.deleteRow(i)};
  }
}
function findStringAndDeleteRow_002() {
  var values = sheet.getDataRange().getValues();
    for(var i=values.length;i>0;i-=1){
     var lcVal=values[i-1][5].toLowerCase() //Change to all lower case
     var index = lcVal.indexOf("mp3"); //now you only have to check for  contains "glass"
     if (lcVal.indexOf("mp3") > -1){
     sheet.deleteRow(i)};
  }
}
function findStringAndDeleteRow_003() {
  var values = sheet.getDataRange().getValues();
    for(var i=values.length;i>0;i-=1){
     var lcVal=values[i-1][5].toLowerCase() //Change to all lower case
     var index = lcVal.indexOf("todos"); //now you only have to check for  contains "glass"
     if (lcVal.indexOf("todos") > -1){
     sheet.deleteRow(i)};
  }
}



I expect delete the rows contain any string like " CD" "MP3" "Todos"

In RED colors you can see the columns need be remove.

Here is the full working code I tested linking it to a copy of your Spreadsheet. I used the deleteCells() function which will delete the first 7 columns of each row that makes the match.

var sheetName = "Products"; 
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(sheetName);

function onOpen(){
  SpreadsheetApp.getUi() 
  .createMenu('COMPANY')
  .addItem('Products Cleaner Data', 'findAndDelete')
  .addToUi();
}

function findAndDelete() {
  var findArray = ["CD","MP3","TODOS"];
  var rowsDelete = [];
  var data = sheet.getRange(2, 5, sheet.getLastRow()-1 ,1).getValues();

  for(var i=0; i<data.length; i++) {
    var cell = data[i][0].toUpperCase(); 
    var cellArray = cell.split(" / ");  
    Logger.log(cellArray);

    for (var j=0; j<cellArray.length; j++) {
      var value = cellArray[j];

      if(findArray.indexOf(value) != -1) {
        Logger.log(value);
        rowsDelete.unshift(2 + i);
        break;
      }
    }
  }

  for(i=0; i<rowsDelete.length ; i++) {
    var position = sheet.getRange(rowsDelete[i], 1, 1, 7);
    position.deleteCells(SpreadsheetApp.Dimension.ROWS);
  }   
}

See the video explanation

Google SpreadSheet

Try this:

function findAndDelete(findArray,searchCol) {
  var findArray=findArray||["CD","MP3","Todos"];//default
  var searchCol=searchCol||"categories";//default
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet177');
  var hA=sh.getRange(1,1,1,sh.getLastColumn()).getValues()[0];
  var cObj={};
  for(var i=0;i<hA.length;i++) {
    cObj[String(hA[i]).toLowerCase()]=i+1;
  }
  var srg=sh.getRange(2,cObj[searchCol.toLowerCase()],sh.getLastRow()-1,1);
  for(var i=0;i<findArray.length;i++) {
    var f=srg.createTextFinder(String(findArray[i])).matchCase(true).findAll();
    var d=0;
    if(f.length>0) {
      for(var j=0;j<f.length;j++) {
        sh.deleteRow(f[j].getRow()-d++);
      }
    }
  }  
}

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