Home > Blockchain >  Hide columns based on checkbox value
Hide columns based on checkbox value

Time:07-07

I would like to implement a code to my production plan. Is there a way to hide column based on the checkbox true value? Lets say I added the checkboxes to the row 5, and I want to hide each column starting from M column separately upon ticking the checkbox. I implemented the code to hide the rows, but for columns it's getting tricky since I'm not experienced in google script.

Would it be perhaps also possible to add custom filter to show all hidden columns at once, lets say if I wanna make changes in one of them, and then hide all of them at once?

enter image description here

CodePudding user response:

Hide columns starting with M

function onEdit(e) {
  e.source.toast("Entry")
  const sh = e.range.getSheet();
  if (sh.getName() == "Enter Your Sheet Name" && e.range.columnStart > 12 && e.range.rowStart == 5 && e.value == "TRUE") {
    e.source.toast("Flag1");
    sh.hideColumns(e.range.columnStart);
    e.range.setValue("FALSE");
  }
}

CodePudding user response:

Yes, these is! If you use an onEdit function, you can use e.range.isChecked() to see if a checkbox was checked. After that, you'd just need to run activeSheet.hideColumns(editedColumn, 1) to hide the column.

This code should do the trick:

function onEdit(e) {
  var activeSheet = SpreadsheetApp.getActiveSheet()
  var editedRange = e.range
  var editedRow = editedRange.getRow();
  var editedColumn = editedRange.getColumn();

  //If the checkbox isn't checked, return
  if (!e.range.isChecked()) return;

  //If the edited row isn't in row 5, return
  if (editedRow != 5) return;

  //If the edited column is before M, return
  if (editedColumn < 12) return;
  
  //Hide the edited column
  activeSheet.hideColumns(editedColumn, 1)
}
  • Related