取消保护受保护的工作表,以便其他人可以运行脚本,然后再次保护工作表

时间:2019-12-04 14:36:18

标签: google-apps-script google-sheets

我已经编译了在工作表上运行的脚本列表。我不是正在学习的程序员,所以我使用了其他人的代码。

以下是唯一不受保护的范围B2:C2,N5:N43,但要运行其他脚本,则需要重新保护整个工作表。

1 个答案:

答案 0 :(得分:0)

使用Google Apps脚本,您可以修改脚本,以便在运行脚本之前取消保护工作表和范围,并在运行后重新保护它们。您可以使用如下代码:

function unProtectAndProtect() {
  var sheetProtections = SpreadsheetApp.getActive().getProtections(SpreadsheetApp.ProtectionType.SHEET);
  var rangeProtections = SpreadsheetApp.getActive().getProtections(SpreadsheetApp.ProtectionType.RANGE);

  var protectionData = {
    sheetProtections: [],
    rangeProtections: []
  };

  for (var i=0; i<sheetProtections.length; i++) {
    var protection = {};
    protection['editors'] = sheetProtections[i].getEditors();
    protection['description'] = sheetProtections[i].getDescription();
    protection['range'] = sheetProtections[i].getRange();
    protection['unprotected ranges'] = sheetProtections[i].getUnprotectedRanges();
    protection['candomainedit'] = sheetProtections[i].canDomainEdit();
    protection['iswarningonly'] = sheetProtections[i].isWarningOnly();

    sheetProtections[i].remove();
    protectionData.sheetProtections.push(protection);
  }

  for (var i=0; i<rangeProtections.length; i++) {
    var protection = {};
    protection['editors'] = rangeProtections[i].getEditors();
    protection['description'] = rangeProtections[i].getDescription();
    protection['range'] = rangeProtections[i].getRange();
    protection['unprotected ranges'] = rangeProtections[i].getUnprotectedRanges();
    protection['candomainedit'] = rangeProtections[i].canDomainEdit();
    protection['iswarningonly'] = rangeProtections[i].isWarningOnly();

    rangeProtections[i].remove();
    protectionData.rangeProtections.push(protection);
  }

  try {
    /**
    *
    *  HERE YOU CAN RUN YOUR SCRIPT
    *
    **/
  catch(e) {
    Logger.log("Caught exception: " + e.toString());
  }

  for (var i=0; i<protectionData.sheetProtections.length; i++) {
    var sheet = protectionData.sheetProtections[i]['range'].getSheet();
    var protection = sheet.protect()
                          .setDescription(protectionData.sheetProtections[i]['description'])
                          .setRange(protectionData.sheetProtections[i]['range'])
                          .setUnprotectedRanges(protectionData.sheetProtections[i]['unprotected ranges'])
                          .setDomainEdit(protectionData.sheetProtections[i]['candomainedit'])
                          .setWarningOnly(protectionData.sheetProtections[i]['iswarningonly']);

    var protectionEditors = protectionData.sheetProtections[i]['editors'];

    // add Editors
    for (var j=0; j<protectionEditors.length; j++) {
      protection.addEditor(protectionEditors[j]);
    }
  }

  for (var i=0; i<protectionData.rangeProtections.length; i++) {
    var range = protectionData.rangeProtections[i]['range'];
    var protection = range.protect()
                          .setDescription(protectionData.rangeProtections[i]['description'])
                          .setDomainEdit(protectionData.rangeProtections[i]['candomainedit'])
                          .setWarningOnly(protectionData.rangeProtections[i]['iswarningonly']);

    var protectionEditors = protectionData.rangeProtections[i]['editors'];

    // add Editors
    for (var j=0; j<protectionEditors.length; j++) {
      protection.addEditor(protectionEditors[j]);
    }
  }

}

想法是在HERE YOU CAN RUN YOUR SCRIPT注释所在的位置实际运行脚本的代码,这是从工作表中删除保护并将其保存在内存中的时间。然后,将它们从内存中检索出来并放回工作表中。

但是,您必须注意实际脚本超出了运行时限制(请参见quotas)。如果发生这种情况,脚本将停止,而无需重新设置保护。

如果您对Google Apps脚本中的保护感兴趣,建议您查看以下链接: