google-sheetsgoogle-sheets-formula

Checking boxes manually when also checking them with an 'if' statement


I'm trying to use an if statement for a checkbox, however I'd like to be able to check the box to true manually.

I am putting together a tracker for a game, and have used multiple checkboxes. I would like for the previous checkbox to check "TRUE" if the next one is selected true. I have a function code;

=IF(OFFSET(C2,0,1)=TRUE, TRUE)

However, I would like to be able to check the previous checkbox as "TRUE" manually, but not to be reversed manually back to false.


Solution

  • When a checkbox in Google Sheets has a formula, it loses its ability to be directly toggled.

    To do what you'd like here is a sample script.

    function onEdit(e){
      const source = e.source.getActiveSheet();
      const range = e.range;
      if(!(range.getColumn() - 1) == 0){
        if(source.getRange(range.getA1Notation()).isChecked()) {
          source.getRange(range.getRow(), range.getColumn() - 1).check()
        }
      }
    }
    

    To use this script go to the Menu Bar > Extension > Apps Script then paste the code to the editor.

    Sample Output:
    enter image description here

    Reference: