[英]Lock cells by active user and owner, not by other users (google script)
Locking range by owner is working fine, also other users can lock range cells which have access to edit.所有者锁定范围工作正常,其他用户也可以锁定有权编辑的范围单元格。
I need the script lock cells by active user and owner, not by other users.我需要活动用户和所有者的脚本锁定单元,而不是其他用户。 And on setting description I had set active user email and row number.
在设置描述中,我设置了活动用户 email 和行号。 It works only on owner, on others not working.
它仅对所有者有效,对其他无效。
Sorry for my bad English!对不起,我的英语不好!
function onEdit(e) {
let range = e.range; //get the range of edited cell
let row = range.getRow(); //get the row
let col = range.getColumn(); //get the column
let value = e.value; //get the new value of edited cell
let sheet = range.getSheet(); //get the sheet where edit is made
let userEmail = Session.getEffectiveUser();
if(col == 2 && row >= 2 && value == "yaroqli" || value == "yaroqsiz"){ //check the edited cell
var protectedRange = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE); //get all protection with type range
for (var i = 0; i < protectedRange.length; i++) { //loop
if (protectedRange[i].getDescription() == userEmail + ' - ' + row) { //row matching
protectedRange[i].remove(); //remove protection
}
}
let lock_range = `B${row}:J${row}`; //set lock range using row
let protection = sheet.getRange(lock_range).protect() //set protection
.setDescription(userEmail + ' - ' + row) //add description
let me = Session.getEffectiveUser();
protection.addEditor(userEmail);
protection.removeEditors(protection.getEditors()); //remove editors
if (protection.canDomainEdit())
protection.setDomainEdit(false);
}
else if(col == 2 && row >= 2 && value == null){ //check if the edited cell is empty
var protectedRange = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE); //get all protection with type range
for (var i = 0; i < protectedRange.length; i++) { //loop
if (protectedRange[i].getDescription() == userEmail + ' - ' + row) { //row matching
protectedRange[i].remove(); //remove protection
}
}
}
}
Not possible.不可能。
If a user has edit access they can lock ranges.如果用户有编辑权限,他们可以锁定范围。
As per the support article :根据支持文章:
Who can protect a range or sheet
谁可以保护范围或工作表
- If you own a spreadsheet : You can decide who can change ranges and sheets.
如果您拥有电子表格:您可以决定谁可以更改范围和工作表。
- If you can edit a spreadsheet : You can decide who can edit ranges and sheets but can't take permissions away from owners.
如果您可以编辑电子表格:您可以决定谁可以编辑范围和工作表,但不能剥夺所有者的权限。
- If you can view or comment on a spreadsheet : You won't be able to make any changes.
如果您可以查看或评论电子表格:您将无法进行任何更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.