Prevent deletion of checkbox in sheet editable by many users

孤者浪人 提交于 2020-01-16 09:39:07

问题


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

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