簡體   English   中英

如何從受保護的單元格中刪除編輯器或永久保護Google表格中的單元格

[英]How to Remove Editors from Protected Cells or Permanently Protect Cells in Google Sheets

我試圖永久地鎖定/保護14個不同紙張上的某些細胞(1個隱藏在工作人員面前的公式)。 我把它們都鎖定了,如果我把它們作為編輯器添加到它中,沒有人可以編輯。 但它是模板,我為每個客戶(和新客戶)為員工制作副本。 在工作表上工作的員工和員工只能編輯他們所做工作的某些單元格。

問題是如果我將Workbook1的X單元格鎖定在不同的工作表上,制作副本,將其重命名為Workbook - Client#ID ,然后將他們將在此客戶端工作的員工John和Jane添加為編輯; 他們現在可以編輯每個單元格,包括受保護的單元格(它們也作為編輯器添加到受保護的單元格中)。 它不會在原始文件上執行此操作,它只會發生在模板的副本上。 然后我必須通過所有13張紙並將它們從受保護的細胞中移除。

我正在嘗試使用腳本加載項自動快速刪除它們,我希望將其轉換為按鈕或稍后的內容...

或者有更好的方法來修復這個錯誤?

Google有一個刪除用戶並保持表單受保護的示例,我嘗試添加我需要的功能,但是當我將測試作為電子表格的附加組件運行時,它沒有做任何事情。 我從電子表格中打開一個新的應用程序腳本項目,然后從google輸入示例代碼

   // Protect the active sheet, then remove all other users from the list of editors.
 var sheet = SpreadsheetApp.setActiveSheet(January);
 var protection = sheet.protect().setDescription('Activity Log');
 var unprotected = sheet.getRange('A2:N7');
  protection.setUnprotectedRanges([unprotected]);

 // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
 // permission comes from a group, the script will throw an exception upon removing the group.
 var me = Session.getEffectiveUser();
 protection.addEditor(me);
 protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }

為此,您可以編寫腳本函數來設置保護范圍並為工作表添加編輯器。

請檢查示例應用程序腳本代碼,以便為下表中的范圍添加保護:

function addProtection()
{

// Protect range A1:B10, then remove all other users from the list of editors.
 var ss = SpreadsheetApp.getActive();
 var range = ss.getRange('A1:B10');
 var protection = range.protect().setDescription('Sample protected range');

// var me = Session.getEffectiveUser();
  // array of emails to add them as editors of the range
 protection.addEditors(['email1','email2']);
  // array of emails to remove the users from list of editors 
 protection.removeEditors(['email3','email4']);
}

希望有所幫助!

添加@ KRR的答案。

我將腳本更改為動態。

function setProtection() {
  var allowed = ["example@gmail.com,exmaple2@gmail.com"];
  addProtection("Sheet1","A1:A10",allowed);
}

function editProtection(sheetname,range,allowed,restricted) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetname);
  var range = sheet.getRange(range);

  //Remove previous protection on this range
  var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  for (var i = 0;i<protections.length;i++) {
    if (protections[i].getDescription() === sheetname + range){
      protections[i].remove();
    }
  }

  //Set new protection
  var protection = range.protect().setDescription(sheetname + range);

  // First remove all editors
  protection.removeEditors(protection.getEditors());

  // Add array of emails as editors of the range
  if (typeof(allowed) !== "undefined") {
    protection.addEditors(allowed.toString().split(","));
  }
}

您可以根據需要添加任意數量的選項,並使它們在onOpen上運行。 設置變量並根據需要多次調用editProtection。

您可以從電子表格編輯器動態獲取電子郵件。

此外,您可能還想添加另一個腳本來保護整個工作表並將您設置為所有者。 希望這可以幫助。

必須作為SCRIPT運行,而不是作為附加組件運行

如果您已經鎖定了工作表並進行了例外處理,則可以輕松使用Google的示例代碼。 我們可以使用for循環來查找所有工作表和名稱。 然后在腳本中添加一個按鈕以在開始時加載。

function FixPermissions() {
  // Protect the active sheet, then remove all other users from the list of editors. Get all sheets in the workbook into an array
 var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
//Use a for loop to go through each sheet and change permissions and label it according to the name of the sheet
  for (var i=0; i < sheets.length; i++) {
    var name = sheets[i].getSheetName()
    var protection = sheets[i].protect().setDescription(name);
    // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
    // permission comes from a group, the script will throw an exception upon removing the group.
    var me = Session.getEffectiveUser();
    protection.addEditor(me);
    protection.removeEditors(protection.getEditors());
    if (protection.canDomainEdit()) {
      protection.setDomainEdit(false);
    }
  } 
}


//A special function that runs when the spreadsheet is open, used to add a custom menu to the spreadsheet.

function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Fix Permission', functionName: 'FixPermissions'}
  ];
  spreadsheet.addMenu('Permissions', menuItems);
}

現在,在菜單欄中,當您重新加載/加載標有權限的電子表格時,您將看到一個新項目

暫無
暫無

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

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