首页 文章

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

提问于
浏览
2

我试图永久地锁定/保护14个不同纸张上的某些细胞(1个隐藏在工作人员面前的公式) . 我把它们都锁定了,如果我把它们作为编辑器添加到它中,没有人可以编辑 . 但它是模板,我为每个客户(和新客户)为员工制作副本 . 在工作表上工作的员工和员工只能编辑他们所做工作的某些单元格 .

问题是,如果我在不同的工作表上锁定X单元格 Workbook1 ,制作副本,将其重命名为 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);
 }

3 回答

  • 3

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

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

    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

    添加@ 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 .

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

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

  • 0

    MUST 作为 SCRIPTNOT 作为 add-on 运行 .

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

    现在,在菜单栏中,当您重新加载/加载标有 Permissions 的电子表格时,您会看到一个新项目

相关问题