I’m currently working on writing a script in Google Apps Script to protect cells on edits made by users other than the owner.
Here’s the code I tried:
function onEdit(e) {
var range = e.range
var me = Session.getActiveUser()
range.setNote('Last Modified: ' + new Date() + 'n' + me)
var effectiveUser = Session.getEffectiveUser()
var protection = range.protect()
protection.removeEditors(me)
protection.removeEditors(effectiveUser)
protection.setDomainEdit(false)
}
Ideally, this is supposed to protect whatever cell or cells are edited by the user.
This works, but is incredibly inconsistent, and the user is still able to edit the protected cell after waiting for a few seconds. If the user is fast enough, they can keep making changes before the script kicks in, if at all. Is this a code issue, or is it just rooted in the speed at which the API is called?
warlockwillard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.