1
votes

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

1 Answers

1
votes

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