Autolock Google Sheets cells after the first entry

喜夏-厌秋 提交于 2021-01-01 08:40:36

问题


I'm trying to use Google Sheets as a sales register for my business. However, there is a specific detail I'd like to configure about the input of information, but couldn't sort it out yet.

I created a table where you enter all the information (such as the date, item sold, total amount, etc.). Everything works perfectly with the table. However, I would like it to automatically prevent any changes to the information in any cell, right after said information has been entered. Let's say someone puts a quantity, then chooses another cell and tries to change the quantity again, but can't due to the protection.

I found some information about autolock at a certain time here: Locking cells in Google Sheets at a specific time but that's not what I need. Also, I'm a beginner and I think some things have changed in Google Sheets because I couldn't make it work, even with the lock by time. I would really appreciate any help you can offer.


回答1:


You could try installing an on edit trigger for the below code.

It will remove all editors (except for the user that installs the trigger and the owner of the sheet) from any and all edited ranges. It will also add a description to the protection so you'll know when it was done.

function protectOnEdit(event) {

  var range = event.range;
  var timeZone = Session.getScriptTimeZone();
  var stringDate = Utilities.formatDate(new Date(), timeZone, 'dd/MM/yy HH:mm');
  var description = 'Protected on ' + stringDate;
  var protection = range.protect().setDescription(description);

  // below code taken directly from Google's documentation (second comment is my own):

  // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
  // permission comes from a group, the script throws an exception upon removing the group.

  var me = Session.getEffectiveUser();
  //user who installed trigger

  protection.addEditor(me);
  protection.removeEditors(protection.getEditors());
  if (protection.canDomainEdit()) {
    protection.setDomainEdit(false);
  }
}

Reference



来源:https://stackoverflow.com/questions/53999853/autolock-google-sheets-cells-after-the-first-entry

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