问题
I am using checkboxes in a sheet (and their functionality in themselves are exactly what I need).
The problems I am having is that the users who are able to edit the sheet, sometime (by mistake) delete the checkbox instead of simply checking/un-checking the checkbox.
Hence, I want the users to use the checkboxes, but not be able to delete them.
Is this possible somehow?
FYI: it is not possible to use Google Forms in the case.
回答1:
Here you have an example on how to achive it using onOpen triggers
function onOpen(e)
{
var range = SpreadsheetApp.getActive().getSheets()[0].getRange(1,4,5);
var values = range.getValues();
for ( var val in values ) {
if( values[val] != true && values[val] != false ) {
values[val]= false;
}
}
range.insertCheckboxes(values);
}
You have to specify where are the checkboxes placed, in the code example provided there are 5 of them, starting from row 1 to row 5, and they are placed on the 4th column, so called D.
This is specified on the getRange(1,4,5) function:
getRange(InitialRow, Initial Column, Number of Rows)
for more documentation around the use of getRange() use the following reference.
This onOpen function will be executed every time that the spreadsheet is opened, and if on the specified fields there aren't false
or true
statements, it means that we lost the checkbox somehow, so it will introduce them again.
来源:https://stackoverflow.com/questions/58997474/prevent-deletion-of-checkbox-in-sheet-editable-by-many-users