如何保护Google工作表中每个特定用户的范围?

时间:2019-10-01 14:08:46

标签: google-apps-script google-sheets spreadsheet named-ranges

我正在尝试使用Google脚本针对其工作簿中的每个工作表对所有工作表和特定范围应用保护(并且以后可以通过触发器将其应用于新添加的工作表)

问题是,在保护整个工作表免受任何人侵害之后,我想为不同的用户分配不同的范围,但是人们可以对其范围进行编辑!

我似乎不知道为什么它不起作用。.它相反地起作用,允许所有用户编辑除受保护范围之外的所有范围。

我希望第一个编辑器能够编辑他的QC_Range, 第二位能够编辑其PLN_Range的编辑器 并限制所有其他单元格和整个工作表的编辑访问权限(仅使其成为视图)。

function Sheet_Ranges_Protection(){

  var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
  //var workbookB = SpreadsheetApp.getActiveSpreadsheet();

  var Veranda_Sheets = Veranda_Test.getSheets();
 for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++) 
  {          
    var Shprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.SHEET);

    var QC_Range         = Veranda_Sheets[SheetNumb].getRange("E1:G5");
    var PLN_Range        = Veranda_Sheets[SheetNumb].getRange("A1:C5");

    if (Shprotection == true)
      SheetNumb++;

    else if (Shprotection == false)
    {
      var Shprotection = Veranda_Sheets[SheetNumb].protect().setDescription('Sheet Protection');    

      var Rangesprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);

      if (Rangesprotection == false)
      { 
        var QCprotection = QC_Range.protect().setDescription('QC Protection');  
        var me = Session.getEffectiveUser(); 
        QCprotection.addEditor(me); 
        QCprotection.removeEditors(QCprotection.getEditors()); 

        if (QCprotection.canDomainEdit())
        { 
          QCprotection.setDomainEdit(false); 
          QCprotection.addEditors(['Editor1@gmail.com']); 
        }

        var PLNprotection = PLN_Range.protect().setDescription('PLN Protection');
        var me = Session.getEffectiveUser();  
        PLNprotection.addEditor(me); 
        PLNprotection.removeEditors(PLNprotection.getEditors()); 

        if (PLNprotection.canDomainEdit()) 
        { 
          PLNprotection.setDomainEdit(false);
          PLNprotection.addEditors(['Editor2@gmail.com']);
        }

      }
      else
        SheetNumb++;

    //  Shprotection = true;
    //  Rangesprotection = true;

      // 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.

    }
  }
}

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了重新设计,我认为它可以完成您现在所假装的工作。在这种情况下,我不会进行纸张保护,而是分别保护所有范围:

function Sheet_Ranges_Protection() {
  var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
  var Veranda_Sheets = Veranda_Test.getSheets();

  for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++) {

    var me = Session.getEffectiveUser();

    // Define ranges that will be protected for everyone
    var range1 = Veranda_Sheets[SheetNumb].getRange(6, 1, Veranda_Sheets[SheetNumb].getMaxRows(), Veranda_Sheets[SheetNumb].getMaxColumns());
    var range2 = Veranda_Sheets[SheetNumb].getRange(1, 8, 5, Veranda_Sheets[SheetNumb].getMaxColumns());
    var range3 = Veranda_Sheets[SheetNumb].getRange(1, 4, 5);
    var ranges = [range1, range2, range3];

    // Set protection for all the sheet minus QC/PLN ranges
    for(var i = 0; i < ranges.length; i++) {
      var rangeProtection = ranges[i].protect().setDescription('Range protection');
      rangeProtection.addEditor(me);
      rangeProtection.removeEditors(rangeProtection.getEditors());
      if (rangeProtection.canDomainEdit()) {
        rangeProtection.setDomainEdit(false);
      }
    }

    var QC_Range         = Veranda_Sheets[SheetNumb].getRange("E1:G5");
    var PLN_Range        = Veranda_Sheets[SheetNumb].getRange("A1:C5");

    // Set protection for QC range
    var QC_protection = QC_Range.protect().setDescription('QC protection');
    QC_protection.removeEditors(QC_protection.getEditors());
    QC_protection.addEditor('Editor1@gmail.com');
    if (QC_protection.canDomainEdit()) {
      QC_protection.setDomainEdit(false);
    }

    // Set protection for PLN range
    var PLN_protection = PLN_Range.protect().setDescription('PLN protection');
    PLN_protection.removeEditors(PLN_protection.getEditors());
    PLN_protection.addEditor('Editor2@gmail.com');
    if (PLN_protection.canDomainEdit()) {
      PLN_protection.setDomainEdit(false);
    }    
  }
}

我希望这对您有用!