google-apps-script - 取消保护受保护的工作表,以便其他人可以运行脚本然后再次保护工作表
问题描述
我已经编译了我在工作表上运行的脚本列表。我不是我仍在学习的程序员,所以我使用了其他人的一些代码。
以下是唯一不受保护的范围 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 脚本中的保护感兴趣,建议您查看以下链接:
推荐阅读
- shell - Ping 测试大量设备 - 如果测试失败,请重置其 LAN 交换机端口
- haskell - 将斐波那契数列计算为固定点的复杂性
- javascript - 他们是否有任何直接银行到银行转账第三方 API 可用?
- c++ - 当应用程序最小化/隐藏时,是否可以在不将对话框置于前面的情况下进行 QProgressDialog::show?
- docusignapi - 我的系统和 Docusign 用户之间的用户映射
- angular - Jest 和 Serialize-ts Count 查找字段的类型:
- excel - 将文档添加到数据
- google-bigquery - BigQuery,如何按周末分组未开始
- xcode - dyld:找不到符号:__ZN3WTF19initializeThreadingEv - webkit MacOSX
- sql-server - 如何构建 Mulesoft-SqlServer 错误处理