【发布时间】:2019-05-25 21:45:46
【问题描述】:
我有一个 Google 工作表,其中包含我想根据日期和时间防止编辑的特定工作表。我看到了针对特定单元格执行此操作的代码,但我想为整个工作表执行此操作并保留编辑器列表(只是防止公共编辑)。
我尝试使用在此处找到的一些代码:Locking cells in Google Sheets at a specific time,但不确定如何针对我的用例修改此代码。
编辑:
仔细查看此页面:https://developers.google.com/apps-script/reference/spreadsheet/protection 似乎以下代码会有所帮助:
// Protect range A1:B10, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var protection = range.protect().setDescription('Sample protected range');
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
但是,如何声明特定的工作表名称?我的电子表格中有多个选项卡。 getActive 要拉什么?
【问题讨论】:
-
欢迎。如果您还没有这样做,请从阅读 developers.google.com/apps-script/guides/sheets 开始。然后回来编辑您的问题,使其更具体。
-
我添加了更多上下文,希望对您有所帮助。
标签: google-apps-script google-sheets