英文:
Apps Script to Build a Data Protection and only allow Spreadsheet Owner to edit
问题
我正在寻找创建一个脚本的方法,该脚本将在用户的电子表格中创建一个受保护的范围。该脚本将由非所有者运行,并且基本上将锁定他们在特定范围内的编辑权限(最终将与OnEdit关联起来)。
最终目标是:
当用户编辑特定单元格时,该行将不再能够被任何人编辑,只有所有者可以编辑。
我现在专注于解决权限问题。
我拥有的脚本创建了受保护的范围,但将权限规则设置为“您和电子表格所有者”。
用户应不再能够编辑。我尝试将保护警告设置为false,但这仍然允许用户编辑。
用户触发了脚本,但不是电子表格所有者。我如何构建一个受保护范围,以在特定范围内删除他们的编辑权限?
function RemovePermission() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
// 定义要保护的数据范围
var range = sheet.getRange("b13:f13");
// 保护范围
var protection = range.protect().setDescription("Protected data range");
// 将电子表格所有者设置为唯一的编辑者
var editors = protection.getEditors();
for (var i = 0; i < editors.length; i++) {
protection.removeEditor(editors[i]);
}
protection.addEditor("输入所有者的电子邮件地址");
// 启用编辑时的警告
protection.setWarningOnly(true);
}
英文:
I'm looking to build a script that will create a protected range in the users spreadsheet. This script will be run from the NON-owner and essentially will lock them from a specific range (it will eventually be tied into an OnEdit).
The end goal is:
When a user edits a specific cell, that row will now longer be able to be edited by anyone except the owner, and the owner alone.
I'm focused on figuring out the permissions right now.
The script I have creates the protected range, but sets the permission rules to "You and the Spreadsheet Owner".
The user should no longer be able to edit. I tried setting the protection warning to false, but that still allows the user to edit.
The user is triggering the script, but is not the spreadsheet owner. How could I build a protection range that removes their editing access in a specific range?
function RemovePermission() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
// /
// Define the data range that you want to protect
var range = sheet.getRange("b13:f13");
// Protect the range
var protection = range.protect().setDescription("Protected data range");
// Set the spreadsheet owner as the only editor
var editors = protection.getEditors();
for (var i = 0; i < editors.length; i++) {
protection.removeEditor(editors[i]);
}
protection.addEditor("enter owner email");
// Enable warning when editing
protection.setWarningOnly(true);
}
答案1
得分: 2
根据Tanaike在评论中建议的方式修改代码,并通过可安装触发器运行它。代码将在创建触发器的用户帐户下运行。
不要通过onEdit(e)
简单触发器运行该函数。简单触发器在键盘上的用户帐户下运行。
英文:
Modify the code as Tanaike suggested in the comments, and run it through an installable trigger. The code will run under the account of the user who created the trigger.
Do not run the function through an onEdit(e)
simple trigger. Simple triggers run under the account of the user at the keyboard.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论