The purpose is to be able to execute some code by checking the Checkbox. Checked
visually remains you that the process is already executed. If you Uncheck you are warned of consequences to do it.
Checking a checkbox creates a protection protection.setWarningOnly(true)
, but unchecking protection.remove()
does not erase this protection, as programmed in the AppsScript code.
I reduced the problem to a minimum Sheet, with a minimum code. the sheet is only a cell ("A1" in the example) with a checkbox. (You can Add More...) and a trigger 'OnEdit' to protectCells(e).
function protectCell(e) {
var sheet = e.source.getActiveSheet();
var eRange = e.range;
var protection = eRange.protect();
var isChecked = eRange.getValue();
//Browser.msgBox("Checkbox state: " + isChecked);
if (isChecked) {
// Lock the cell when the checkbox is checked
protection.setWarningOnly(true);
//Browser.msgBox("Protection set with warning.");
} else {
// Unlock the cell when the checkbox is unchecked
protection.remove();
//Browser.msgBox("Protection removed.");
}
}
you can test it at : 1
Duplicated protections
As written in the doc, calling
range.protect()
duplicates the protection:Therefore call
.protect()
only after removing all existing protections. Use the description as key to avoid removing protections not set by the script.