类保护脚本

时间:2016-07-22 15:26:19

标签: google-apps-script

我从here获取了信息 创建一个脚本,保护工作表中的一系列单元格不被编辑,但也排除了用户可以输入数据的区域。我的脚本看起来像这样。

function myFunction() {
    // Protect the active sheet except B2:C5, then remove all other users from the list of editors.
    var sheet = SpreadsheetApp.getActiveSheet();
    var protection = sheet.protect().setDescription('2016-07-21 Material Request Form Template');
    var unprotected = sheet.getRange('C9:D9');
    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);
    }
}

在读取var unprotected = sheet.getRange('C9:D9');的行上,这确实会在受保护的工作表中按预期解锁此范围,但我还要在此文档中包含其他范围,我想限制。如何输入或修改此行,以便锁定多个范围,即C9:D9A12:E12以及A24:K24

如果我只是复制此行并将其粘贴到新的单元格区域,它将覆盖先前未受保护的范围并激活刚刚粘贴的新行。

谢谢。

1 个答案:

答案 0 :(得分:0)

对于多个不受保护的范围,您需要在一次调用中将所有范围作为数组参数传递:

var range1 = sheet.getRange("A1:A6");
var range2 = sheet.getRange("B1:B6");

protection.setUnprotectedRanges([range1, range2]);

您的问题似乎已切换,您似乎最终会询问如何限制对多个范围的访问,而不是取消保护它们?为此,请保护整张纸,然后取消保护相关范围,以达到您的需要。