受保护的范围可以保护静态单元格范围或命名范围。受保护的工作表可能包含未受保护的区域。
保护范围 A1:B10,然后从编辑器列表中删除所有其他用户。
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var protection = range.protect().setDescription('Sample protected range');
在删除其他用户之前,请确保当前用户是编辑者。否则,如果用户的编辑权限来自某个组,则脚本会在删除该组时抛出异常。
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
删除用户有权编辑的电子表格中的所有范围保护。
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
if (protection.canEdit()) {
protection.remove();
}
}
保护活动工作表,然后从编辑器列表中删除所有其他用户。
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');
在删除其他用户之前,请确保当前用户是编辑者。否则,如果用户的编辑权限来自某个组,则脚本会在删除该组时抛出异常。
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}