I have a code:
function onEdit(e){
if (e.range.columnStart != 7 && e.range.columnStart != 8 || e.value <= 0) return;
let d = new Date();
if (e.range.columnStart == 7 && e.value == null){
e.range.offset(0,-2).setValue(null);}
else if (e.range.columnStart == 7 && e.value != null) {
e.range.offset(0,-2).setValue(d);}
else if (e.range.columnStart == 8 && e.value != null) {
e.range.offset(0,-3).setValue(d);}
}
function Lock(){
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var protection = sh.protect().setDescription('Lock');
var me = Session.getEffectiveUser();
protection.addEditor(me).setUnprotectedRanges(sh.getRange("I2:I").createTextFinder("^(?!Done).*$").matchEntireCell(true).useRegularExpression(true).findAll().map(r => r.offset(0, -3, 1, 3)));
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()){
protection.setDomainEdit(false);}
}
- After running 2 functions. I edit in column 7 or column 8, will put the current date in the specified cell (column 5 in sheet), and will lock the range if
I2:I
has the valueDone
. - Everything works great with owner. But collaborators who are authorized to edit the spreadsheet cannot,
onEdit(e)
cannot run (error : You are trying to edit a protected cell or object. Please contact the spreadsheet owner to disable protection if you need to edit). Because functionLock()
protected the sheet leaving only those cells where columnI2:I
is notdone
. - Is there any solution? Thanks!