简体   繁体   English

由活动用户和所有者锁定单元格,而不是其他用户(谷歌脚本)

[英]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.

相关问题 Google 脚本锁定单元格 - Google Script Lock Cells Google Apps脚本库配额是针对所有者还是活动用户来衡量的? - Are Google Apps Script Library quotas measured against the owner or active user? 除了所有者之外,onOpen 不会对其他用户运行。 如何解决这个问题? 谷歌脚本气体 - onOpen is not running to other users except to the owner. How to fix this? Google Script GAS 不同用户的脚本锁定谷歌应用程序制造商 - Script lock for different users google app maker 如何使用Google脚本为当前用户锁定工作表 - How to lock a sheet for current user with Google script Google表格应用程序脚本锁定特定日期的单元格 - Google Sheets App Script lock cells for specific days Google Script 无法为一个用户找到文件,但可以为所有者找到文件 - Google Script can't find file for one user but can for owner Google Apps脚本:强制库以其所有者作为用户运行 - Google Apps Script: Force a library to run with its owner as the user 尝试创建一个 Apps 脚本,从 Google 表格中的几个特定单元格中提取数据并通过 MailApp 将其发送给其他用户 - Trying to create an Apps Script that pulls data from a few specific cells in Google Sheets and sends it via MailApp to other users 从特定用户的谷歌表格绘图触发时脚本失败,但适用于其他用户 - Script fails when triggered from google sheet drawing for a specific user, but works for other users
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM