3
votes

I am trying to find a working code that will automatically hide a row if the checkbox in column F of that row is checked.

I have tried every script I have found and nothing seems to work. Unfortunately I am not code savvy and I am unable to find the issue.

This is what I currently have:

function onOpen() {
  var s = SpreadsheetApp.getActive().getSheetByName("Checklists");
  s.showRows(1, s.getMaxRows());

  s.getRange('F2:F200')
    .getValues()
    .forEach( function (r, i) {
    if (r[0] == "TRUE") 
      s.hideRows(i + 1);
    });
}

The sheet I am working on is "Checklists" and the column that contains the checkbox is F. The value of the checkbox is either TRUE or FALSE. If the value is TRUE, I want that row to be hidden.

Can someone please help!!!

1
How would the checkbox become unchecked (the value changed to False) so that the row will become un-hidden?PeterT
@PeterT I would not need to uncheck it. Once it is complete, I don't need to see it again however I don't want to delete it as we keep a record of everything.KimberlyGB

1 Answers

1
votes

The quick test I was able to run was to set up a column of checkboxes in column F, then to create a function that catches each edit event on the sheet. This will immediately catch when the user checks a box and will then hide that row.

The trick with using the onEdit event is with determining which cell was actually changed. In your case, you only want to fully follow your logic if the change happens to a checkbox in column F. In my code, I've been using a function to make sure the change is in the desired range. The function looks like this:

function isInRange(checkRange, targetCell) {
  //--- check the target cell's row and column against the given
  //    checkrange area and return True if the target cell is
  //    inside that range
  var targetRow = targetCell.getRow();
  if (targetRow < checkRange.getRow() || targetRow > checkRange.getLastRow()) return false;

  var targetColumn = targetCell.getColumn();
  if (targetColumn < checkRange.getColumn() || targetColumn > checkRange.getLastColumn()) return false;

  //--- the target cell is in the range!
  return true;
}

So then all your onEdit function has to do is to make a quick call when the edit event is fired to see if the change falls within the range you're looking for. In this case, I set up a variable with my range to check:

var thisSheet = SpreadsheetApp.getActiveSheet();
var checkRange = thisSheet.getRange("F2:F200");  
if (isInRange(checkRange, eventObj.range)) {

After that, it's just a matter of picking the row number and hiding or showing. Here's the full example solution:

function isInRange(checkRange, targetCell) {
  //--- check the target cell's row and column against the given
  //    checkrange area and return True if the target cell is
  //    inside that range
  var targetRow = targetCell.getRow();
  if (targetRow < checkRange.getRow() || targetRow > checkRange.getLastRow()) return false;

  var targetColumn = targetCell.getColumn();
  if (targetColumn < checkRange.getColumn() || targetColumn > checkRange.getLastColumn()) return false;

  //--- the target cell is in the range!
  return true;
}

function onEdit(eventObj) {
  //--- you could set up a dynamic named range for this area to make it easier
  var thisSheet = SpreadsheetApp.getActiveSheet();
  var checkRange = thisSheet.getRange("F2:F200");  
  if (isInRange(checkRange, eventObj.range)) {
    //--- so one of the checkboxes has changed its value, so hide or show
    //    that row
    var checkbox = eventObj.range;
    var rowIndex = checkbox.getRow();
    Logger.log('detected change in checkbox at ' + checkbox.getA1Notation() + ', value is now ' + checkbox.getValue());
    if (checkbox.getValue() == true) {
      Logger.log('hiding the row');
      thisSheet.hideRows(rowIndex, 1);
    } else {
      Logger.log('showing the row');
      thisSheet.showRows(rowIndex, 1);
    }
  }
}