簡體   English   中英

由活動用戶和所有者鎖定單元格,而不是其他用戶(谷歌腳本)

[英]Lock cells by active user and owner, not by other users (google script)

所有者鎖定范圍工作正常,其他用戶也可以鎖定有權編輯的范圍單元格。

我需要活動用戶和所有者的腳本鎖定單元,而不是其他用戶。 在設置描述中,我設置了活動用戶 email 和行號。 它僅對所有者有效,對其他無效。

對不起,我的英語不好!

截屏

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
      }
    }  
  }
}

不可能。

如果用戶有編輯權限,他們可以鎖定范圍。

根據支持文章

誰可以保護范圍或工作表

  • 如果您擁有電子表格:您可以決定誰可以更改范圍和工作表。
  • 如果您可以編輯電子表格:您可以決定誰可以編輯范圍和工作表,但不能剝奪所有者的權限。
  • 如果您可以查看或評論電子表格:您將無法進行任何更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM