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



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

以下是唯一不受保护的范围 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注释所在的位置,这是从工作表中删除保护并保存在内存中的点。之后,它们从内存中检索并放回工作表中。

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

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

  • 类保护

最新更新