skip to Main Content

We had multiple scripts for one sheet and seemed to operate independently but once combined to one script, it seems not to work. It executes but no activity on sheet. Trying to get these on one script so we can add a custom menu to execute the script.

function removeRows() {
  var sheetName = "DBDR_AllDevicesTAB"; // Replace with your sheet name var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); var range = sheet.getDataRange(); var flVals = range.getFontLines();

  for (var i = flVals.length - 1; i >= 0; i--) {
    for (var j = 0; j < flVals[i].length; j++) {
      var cellFL = flVals[i][j]; // console.log(cellFL.getFontLine()) if (cellFL == "line-through") { sheet.deleteRow(i + 1); break; // Move to the next row if a strikethrough cellFL is found }

    }
  }
}

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

  }
};

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

  }
};

2

Answers


  1. Use OR Logic

    I combined your code into a single function and added a custom menu to easily run the script in the spreadsheet. Kindly copy and paste the code to check if it works on your end.

    It removes rows with strike-through text and rows with "Food System" on Column B and "Standalone" on Column H.


    Here is the code:

      function removeRows() {
      var sheetName = "DBDR_AllDevicesTAB";
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
      var range = sheet.getDataRange();
      var flVals = range.getFontLines();
      var values = range.getValues();
      var numRows = values.length;
      var rowsDeleted = 0;
    
      // Remove rows with strikethrough text
      for (var i = numRows - 1; i >= 0; i--) {
        for (var j = 0; j < flVals[i].length; j++) {
          var cellFL = flVals[i][j];
          if (cellFL == "line-through") {
            sheet.deleteRow(i + 1);
            rowsDeleted++;
            break;
          }
        }
      }
    
      var numRows = sheet.getLastRow();
      var values = sheet.getDataRange().getValues();
    
      //Remove rows with Food System or Standalone text
      for (var i = numRows - 1; i >= 0; i--) {
        var row = values[i];
    
        if ((row[1] && row[1].indexOf('Food System') > -1) || (row[7] && row[7].indexOf('Standalone') > -1)) {
          sheet.deleteRow(i + 1);
          rowsDeleted++;
        }
      }
    
    }
    
    function onOpen() {
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('Custom Menu')
        .addItem('Process Rows', 'removeRows')
        .addToUi();
    }
    

    Sample Output:

    Before running the script:
    Before

    After running the script:
    after


    References:

    Logical Operator

    onOpen(e)

    Login or Signup to reply.
  2. Try it this way:

    function readRows(input) {
      let d = 0;
      SpreadsheetApp.getActiveSheet().getDataRange().getValues().forEach((r,i) => {
        if (~r[0].indexOf(input)) {
          sh.deleteRow(i + 1 - d++)
        }
      });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search