If statement 如何检查谷歌工作表/范围保护状态?

If statement 如何检查谷歌工作表/范围保护状态?,if-statement,google-apps-script,google-sheets,protection,If Statement,Google Apps Script,Google Sheets,Protection,我用@OMila aid编写了一个代码,用于限制某些用户的某些范围,同时保护工作表中的所有剩余范围不受编辑。。我想检查每个for循环迭代的工作表/范围的保护状态,如果它受保护==>iteration++(检查下一个工作表),如果未受保护,则运行脚本并保护范围。其目的是,当某些人制作新的工作表时,我希望脚本通过触发器自动运行,但当工作表数量增加时,每个电子表格的执行时间将增加,并可能达到谷歌报价限制,因此,我需要通过设置一个if条件来检查工作表保护状态,并按照前面描述的方式执行脚本。代码如下:

我用@OMila aid编写了一个代码,用于限制某些用户的某些范围,同时保护工作表中的所有剩余范围不受编辑。。我想检查每个for循环迭代的工作表/范围的保护状态,如果它受保护==>iteration++(检查下一个工作表),如果未受保护,则运行脚本并保护范围。其目的是,当某些人制作新的工作表时,我希望脚本通过触发器自动运行,但当工作表数量增加时,每个电子表格的执行时间将增加,并可能达到谷歌报价限制,因此,我需要通过设置一个if条件来检查工作表保护状态,并按照前面描述的方式执行脚本。代码如下:

  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);
    }    
    }
    }
功能表\u范围\u保护(){
var Veranda_测试=电子表格应用程序openById(“表格ID”);
var Veranda_Sheets=Veranda_Test.getSheets();
对于(var SheetNumb=0;SheetNumb
您可以使用
getProtections()
函数在创建新的保护之前检查哪些类型的保护已经到位

由于您只使用范围创建保护,因此可以使用
getProtections(SpreadsheetApp.ProtectionType.RANGE)
仅获取脚本创建的保护(所有保护都是范围,而不是工作表范围的保护)

您可以假设,如果您已经创建了第一个保护,那么您已经创建了所有这些保护。因此,代码如下所示:

var protections = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (protections.length==0) {
   //add protections here
}
var protections = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
var protectionNames = [];
for (var i=0; i<protections.length; i++) {
    protectionNames.push(protections[i].getDescription());
}

if (!protectionNames.includes('<name of protection i am about to create>') {
    //create protection '<name of protection i am about to create>'
} //else skip;
如果这不是一个安全的假设,您可以验证缺少哪些保护,然后像这样创建它们:

var protections = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (protections.length==0) {
   //add protections here
}
var protections = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
var protectionNames = [];
for (var i=0; i<protections.length; i++) {
    protectionNames.push(protections[i].getDescription());
}

if (!protectionNames.includes('<name of protection i am about to create>') {
    //create protection '<name of protection i am about to create>'
} //else skip;
var protections=Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
var-protectionNames=[];

对于(var i=0;i检查是否有任何范围受到保护对我来说是一个足够的指示,表明该工作表已按我想要的方式受到保护,因此我只需要代码部分来检查第一个/任何范围…代码现在将如何?@Samy我已更新了答案以解决您的评论。希望它对您有所帮助!记住接受对您有用的答案!哥们!!!非常感谢你,伙计!!我非常感谢你的帮助!五星