首页 > 解决方案 > 有没有办法使用谷歌脚本为某个范围设置编辑器?

问题描述

我正在尝试为工作表上的某个范围设置编辑器。我发现这个名为 getEditors() 的函数给出了某个定义范围内所有编辑器的列表。我们也可以设置编辑器吗?

标签: javascriptgoogle-apps-scriptgoogle-sheets

解决方案


Protection类具有不同的方法,允许您修改电子表格范围的编辑器。

要设置编辑器,您可以使用方法addEditor(),要删除编辑器,请使用removeEditor

样品

// 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);
}

推荐阅读