簡體   English   中英

取消保護受保護的工作表,以便其他人可以運行腳本,然后再次保護工作表

[英]Unprotecting protected sheet so that others can run script and then protect the sheet again

我已經編譯了我在工作表上運行的腳本列表。 我不是程序員,我還在學習,所以我使用了其他人的一些代碼。

以下是唯一未受保護的范圍 B2:C2,N5:N43,但要讓其他腳本運行整個工作表,則需要取消保護並再次受保護。

使用 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注釋所在的HERE YOU CAN RUN YOUR SCRIPT實際運行您的腳本代碼,這是從工作表中刪除保護並保存在內存中的點。 之后,它們從內存中檢索並放回工作表中。

但是,您必須小心超出運行時限制的實際腳本(請參閱配額)。 如果發生這種情況,腳本將停止而不重新設置您的保護。

如果您對 Google Apps Scripts 中的保護感興趣,我建議您查看以下鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM