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

Google apps script 如何从受保护的单元格中删除编辑器或永久保护Google工作表中的单元格,google-apps-script,google-sheets,google-docs,Google Apps Script,Google Sheets,Google Docs,我试图在14张不同的纸上永久地锁定/保护某些细胞(1张对工人隐藏,用于配方食品)。我把它们都锁定了,如果我将它们作为编辑器添加到其中,没有人可以进行编辑。但它是一个模板,我为每个客户(和新客户)为员工制作了它的副本。在工作表上工作的员工和员工只允许为他们所做的工作编辑某些单元格 问题是,如果我的Workbook1在不同的工作表上锁定了X个单元格,复制一份,将其重命名为Workbook-Client#ID,然后将他们添加为将在此客户机上工作的员工John和Jane作为编辑;它们现在可以编辑每个单元

我试图在14张不同的纸上永久地锁定/保护某些细胞(1张对工人隐藏,用于配方食品)。我把它们都锁定了,如果我将它们作为编辑器添加到其中,没有人可以进行编辑。但它是一个模板,我为每个客户(和新客户)为员工制作了它的副本。在工作表上工作的员工和员工只允许为他们所做的工作编辑某些单元格

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

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

还是有更好的方法来修复这个bug

谷歌有一个删除用户并保护表单的例子,我尝试添加我需要的内容以使其工作,但当我作为电子表格的附加组件运行测试时,它没有做任何事情。我从电子表格中打开一个新的应用程序脚本项目,并输入示例


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

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

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(","));
  }
}
功能设置保护(){
允许的变量=[”example@gmail.com,exmaple2@gmail.com"];
添加保护(“表1”,“A1:A10”,允许);
}
功能editProtection(表名、范围、允许、限制){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheetByName(sheetname);
var范围=sheet.getRange(范围);
//删除此范围上以前的保护
var保护=sheet.getProtections(电子表格app.ProtectionType.RANGE);

对于(var i=0;i它必须作为脚本运行,而不作为附加组件运行

如果您已经锁定了工作表并进行了例外处理,则可以轻松使用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);
}
函数FixPermissions(){
//保护活动工作表,然后从编辑器列表中删除所有其他用户。将工作簿中的所有工作表放入数组
var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();
//使用for循环遍历每个工作表,并根据工作表的名称更改权限和标签
对于(变量i=0;i

现在,在菜单栏中,当您重新加载/加载标有权限的电子表格时,您将看到一个新项目。关闭时,我看到了示例,但我需要的更像示例#3,从那里可以看到一个类似于for each sheet循环的文档,它根据页面排除某些单元格,并通过按钮或其他方式执行。我不知道如何执行此操作这样做,我几乎不知道应用程序脚本。这些表是所有月份+年度概览+hiddeb公式表。我正试图合并这些示例以进行工作测试,但它到目前为止还不起作用。不,它不起作用。它没有说在任何地方给我代码。我正在演示一些尝试,以使用谷歌拥有的少量文档。他们的支持ort字面上是说来这里寻求帮助,所以不要粗鲁。我正在努力让它自己工作,同时寻求帮助;也许你看不到我在编辑。谷歌有最糟糕的文档;它只是这里的一个例子和一行解释。请原谅我试图在谷歌说的地方获得帮助。对不起,哟你觉得我很粗鲁;不是故意的。我指出了一个与这个问题相关的元帖子。我看到了你的编辑;但在悬赏之前没有代码,直到2小时前才有努力的证据。你之前已经收到了一个答案,但一小时前才承认,此时你的第一个评论似乎提供了更具体的说明代码的错误。您应该改进您的问题,而不是添加悬赏。您的问题仍然太广泛。您没有明确、具体的问题陈述。()谢谢,如果我只需要锁定几个单元格范围,这将是完美的,很遗憾,我需要锁定所有单元格,但大约10个。我甚至无法使示例代码正常工作:(.我想使用示例代码,只需对每个工作表重复它,然后添加
getSheetByName(sheetname);
而不是
SpreadsheetApp.getActiveSheet();
代码中有什么不起作用?错误是什么。您还可以在setProtection()中添加任意数量的函数)例如,您可以锁定所有工作表,然后将编辑器添加到这10个范围。代码运行正常,但结果不是很好
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);
}