我的Google Apps脚本保护整个工作表,而不是保护某些列并允许编辑者对其进行编辑

时间:2018-07-17 02:37:31

标签: google-apps-script google-sheets

我有这段代码,我希望某些列受到保护,并且只允许某些编辑者对其进行编辑。但是,我的代码仅使整个工作表除外,而我保护的列除外,而编辑者无法编辑任何其他列。我以为这是我脚本的一部分

protection.removeEditors(protection.getEditors());

if (protection.canDomainEdit()) {
    protection.setDomainEdit(false);
}

导致整个工作表受到保护,但是当我将其取出时,整个工作表仍然受到保护。有人可以帮我找出我的脚本的哪一部分有误以及如何解决?我对编码或编写脚本的知识不多,希望能对您有所帮助。

编辑:显然,编辑器可以编辑,但是当我在“数据”下选中“受保护的图纸和范围”时,一部分代码也使受保护的范围“仅查看”。如果有人只能帮助我摆脱这种看法,那我将非常感激。”

function OnOpen(){
    // Protect the active sheet except colored cells, then remove all other users from the list of editors.
    var ss = SpreadsheetApp.getActiveSheet();
    var range = ss.getRange("A1:B10");
    var range1 = ss.getRange("D1:E10");
    var protection = range.protect().setDescription('Sample protected sheet');
    var protection1 = range1.protect().setDescription('Sample protected sheet');

    // 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();
    var editor = ["email@gmail.com"]
    var editor1 =["email2@gmail.com"]

    if (me.getEmail() == editor){
        protection.addEditor(editor);  
    }

    if (me.getEmail() == editor1){
        protection1.addEditor(editor1);
    }

    protection.removeEditors(protection.getEditors());

    if (protection.canDomainEdit()) {
        protection.setDomainEdit(false);
    }
}

1 个答案:

答案 0 :(得分:0)

爱德华提供的答案。

function OnOpen(){
    // Protect the active sheet except colored cells, then remove all other users from the list of editors.
    var ss = SpreadsheetApp.getActiveSheet();
    var range = ss.getRange("A1:B10");
    var range1 = ss.getRange("D1:E10");
    var protection = range.protect().setDescription('Sample protected sheet');
    var protection1 = range1.protect().setDescription('Sample protected sheet');

    // 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); 
    protection1.addEditor(me); 
    protection.removeEditors(protection.getEditors()); 
      if (protection.canDomainEdit()) { 
        protection.setDomainEdit(false); 
      } 
      protection1.removeEditors(protection1.getEditors()); 
      if (protection1.canDomainEdit()) { 
        protection1.setDomainEdit(false); 
      } 
    }
      protection.addEditors(['user@gmail.com','user@gmail.com']); 
      protection1.addEditors(['user@gmail.com','user@gmail.com']); 
    }