How to hide columns in Google Sheet with checkboxes

ε祈祈猫儿з 提交于 2020-12-15 06:02:42

问题


Could someone help me figure out how to be able to hide/unhide columns by using a checkbox in Google Sheets.

For example, the checkbox is located in N4 and I want to hide/unhide column from O to R:

Thank you!


回答1:


Answer:

You need to use an onEdit() trigger.

Code Sample:

function onEdit(e) {
  if (e.range.getA1Notation() != "N4") return;

  if (e.value == "TRUE") {
    SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().hideColumns(15, 4);
  }
  else if (e.value == "FALSE") {
    SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().showColumns(15, 4);
  }
}

Function run-down:

  • Checks to see if the edited cell was N4
    • If it isn't N4, do nothing.
  • If it is N4, then:
    • If the value is true (checkbox is checked), then hide columns O to R.
    • If the value is false (checkbox is unchecked), then show columns O to R.

If you wish it to be the other way around then swap the "TRUE" and "FALSE" values in the conditional.

I hope this is helpful to you!

References:

  • Event Objects | Apps Script | Google Developers
  • Simple Triggers | Apps Script | Google Developers
  • Class Sheet: hideColumns(columnIndex, numColumns)
  • Class Sheet: showColumns(columnIndex, numColumns)


来源:https://stackoverflow.com/questions/62650294/how-to-hide-columns-in-google-sheet-with-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!