【问题标题】:How to Remove Editors from Protected Cells or Permanently Protect Cells in Google Sheets如何从受保护的单元格中删除编辑器或永久保护 Google 表格中的单元格
【发布时间】:2015-10-07 21:10:08
【问题描述】:

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

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

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

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

Google 有一个删除用户并保持工作表保护的示例,我尝试添加我需要使其工作的内容,但是当我将测试作为电子表格的附加组件运行时,它没有做任何事情。我从我的电子表格中打开一个新的应用脚本项目并输入示例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);
 }

【问题讨论】:

  • 它没有说在任何地方给我代码。我正在展示一些尝试使用谷歌所拥有的小文档。他们的支持字面意思是来这里寻求帮助,所以不要粗鲁。我正在努力让它自己工作并同时寻求帮助;也许你没有看到我在编辑。谷歌拥有最糟糕的文档;它只是这里的一个例子和 1 班轮作为解释。请原谅我试图在谷歌所说的地方获得帮助。
  • 对不起,你觉得我很粗鲁;不打算。我指出了与这个问题相关的元帖子。我看到你的编辑;而且在提供赏金之前没有代码,直到 2 小时前没有任何努力的证据。您已收到先前的答复,但仅在 1 小时前才确认,此时您的第一条评论似乎提供了更具体的代码规范。你应该改进你的问题,而不是增加赏金。你的问题还是太笼统了。您没有明确、具体的问题陈述。 (Ref)

标签: google-apps-script google-sheets google-docs


【解决方案1】:

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

请检查示例应用程序脚本代码以在下表中为范围添加保护:

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']);
}

希望有帮助!

【讨论】:

  • 关闭,我看到了示例,但我需要的更像是示例#3 from there doc in like a for each sheet loop,根据页面排除某些单元格并通过按钮完成或者其他的东西。而且我不知道该怎么做,我几乎不知道应用程序脚本。
  • 表格是所有月份 + 年度概览 + hiddeb 公式表。
  • 我正在尝试合并示例以进行工作测试,但目前还没有工作
【解决方案2】:

添加@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(","));
  }
}

您可以根据需要添加任意数量的选项,并使它们在打开时运行。设置您的变量并根据需要多次调用 editProtection。

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

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

【讨论】:

  • 谢谢,如果我只需要锁定几个单元格范围,这将是完美的,遗憾的是我需要锁定 10 个单元格范围。我什至无法让示例代码工作:(。我想使用示例代码并为每张纸重复它并添加getSheetByName(sheetname);而不是SpreadsheetApp.getActiveSheet();
  • 代码中什么不起作用?什么是错误。您也可以在 setProtection() 中添加任意数量的函数并运行它。例如,您可以锁定所有工作表,然后将编辑器添加到这 10 个范围
  • 代码有效,但结果并不像我最终弄清楚的设置那样简单或干净。这适用于从范围的受保护权限中删除除 X 用户之外的所有用户。结果是它必须是单独的工作表,并且只是范围。我试图保护除了几个单元格或单元格范围(5 个不同的小范围/单元格)之外的所有单元格。这可以做到,但是之后在板上看起来很乱,边栏上有很多受保护的块(至少对于我的床单来说),这很难管理。使用保护工作表循环并事先在每张工作表上设置排除项会更干净
  • 澄清一下,我的第一条评论是指谷歌的示例代码,我的错误是试图将它作为附加组件而不是脚本运行。我认为新的附加部分是这些天脚本在所有更改后的工作方式。
【解决方案3】:

必须作为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);
}

现在,当您重新加载/加载标有权限

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-27
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    相关资源
    最近更新 更多