问题
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 isn't
- If it is
N4, then:- If the value is true (checkbox is checked), then hide columns
OtoR. - If the value is false (checkbox is unchecked), then show columns
OtoR.
- If the value is true (checkbox is checked), then hide columns
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