如何从受保护的单元格中删除编辑器或永久保护 Google 工作表中的单元格

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

我正在尝试永久 lock/protect 14 个不同 sheet 上的某些单元格(其中 1 个单元格对公式内容的工作人员隐藏)。我将它们全部锁定,如果我将它们作为编辑器添加到其中,则没有人可以编辑。但它是模板,我为每个客户(和新客户)为员工制作副本。在 sheet 上工作的员工和员工只能编辑他们所做工作的某些单元格。

问题是如果我有 Workbook1 X 单元锁定在不同的 sheet 上,制作一个副本,将其重命名为 Workbook - Client#ID,然后将他们添加为雇员 John 和 Jane ,谁将在这个客户上工作,作为编辑;他们现在可以编辑每个单元格,包括受保护的单元格(他们也被添加为受保护单元格的编辑者)。它不会在原件上执行此操作,只会发生在模板制作的副本上。然后我必须检查所有 13 sheets 并将它们从受保护的单元格中删除。

我正在尝试使用脚本插件快速自动删除它们,我想稍后将其变成按钮或其他东西...

或者有更好的方法来修复这个错误吗?

Google 有一个删除用户并保持 sheet 受保护的示例,我尝试添加使其工作所需的内容,但当我 运行 测试作为传播的附加组件sheet。我从我的 spreadsheet 打开一个新的应用程序脚本项目并输入示例 code from 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);
 }

为此,您可以编写一个脚本函数来设置保护范围并为 sheet 添加编辑器。

请检查示例应用程序脚本代码以添加对下面 sheet 范围内的保护:

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。

您可以从 spreadsheet 编辑器中动态获取电子邮件。

您可能还想添加另一个脚本来保护整个 sheet 并将您设置为所有者。 希望这有帮助。

必须 运行 作为 SCRIPT 并且 NOT 作为 附加组件.

如果您已经锁定了工作表并进行了例外处理,则可以轻松使用 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);
}

现在,当您 reload/load 标记为 Permissions

的电子表格时,您将在菜单栏中看到一个新项目