有没有办法使用脚本锁定特定的Google Apps工作表?
我有这段代码可以将当前电子表格重命名为您在输入框中输入的内容。
// The code below will rename the active sheet to what you enter in the input box
var myValue = Browser.inputBox("Enter: LastMonth - Year");
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);
SpreadsheetApp.getActiveSpreadsheet().getRange("A1").setValue(myValue);
我可以在上面的代码中添加什么来锁定我刚刚重命名的相同工作表,只允许我编辑该工作表。这可能吗?
答案 0 :(得分:6)
// Enables sheet protection for this sheet
var sheet = SpreadsheetApp.getActiveSheet();
var permissions = sheet.getSheetProtection();
permissions.setProtected(true);
sheet.setSheetProtection(permissions);
请参阅电子表格服务 - Class PageProtection
答案 1 :(得分:2)
下面的代码Spreadsheet Services,如@ScampMichael所说。
将用户添加到可以编辑工作表的用户列表中(如果已受保护。
)// Add the "user@example.com" user to the list of users who can edit this sheet
var sheet = SpreadsheetApp.getActiveSheet();
var permissions = sheet.getSheetProtection();
permissions.addUser('user@example.com');
permissions.setProtected(true);
sheet.setSheetProtection(permissions);
答案 2 :(得分:1)
由于Class PageProtection目前已被弃用,您可以使用Class Protection来实现相同目标:
var sheet = SpreadsheetApp.getActiveSheet();
sheet.protect();
要在需要时取消保护,您可以使用以下代码段:
var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection && protection.canEdit()) {
protection.remove();
}