首页 > 解决方案 > 取消保护受保护的工作表,以便其他人可以运行脚本然后再次保护工作表

问题描述

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

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

标签: google-apps-scriptgoogle-sheets

解决方案


使用 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注释所在的位置实际运行脚本的代码,这是从工作表中删除保护并保存在内存中的点。之后,它们会从内存中检索并放回工作表中。

但是,您必须小心超出运行时限制的实际脚本(请参阅配额)。如果发生这种情况,脚本将停止而不重新设置您的保护。

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


推荐阅读