Google apps script 在脚本运行时防止编辑单元格
我有一个小的GoogleApps脚本,在电子表格中的某些单元格被编辑后运行几秒钟 我的问题是,是否可以防止当前用户在脚本运行时对电子表格进行进一步更改 所以它应该是这样的:Google apps script 在脚本运行时防止编辑单元格,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我有一个小的GoogleApps脚本,在电子表格中的某些单元格被编辑后运行几秒钟 我的问题是,是否可以防止当前用户在脚本运行时对电子表格进行进一步更改 所以它应该是这样的: 单元格已编辑 脚本正在运行,当前用户的用户交互/更改被阻止 脚本完成了 当前用户的用户交互/更改被取消阻止 您可以使用类保护: 根据以上链接改编,您可以尝试: function pro(){ var sheet = SpreadsheetApp.getActiveSheet(); var protection = s
您可以使用类保护: 根据以上链接改编,您可以尝试:
function pro(){
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script will throw an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
//YOUR CODE TO RUN
SpreadsheetApp.flush();
protection.remove();
}
编辑
这对我很有用:
function onEdit(){
//Adapted from: https://developers.google.com/apps-script/reference/spreadsheet/protection
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var me = Session.getActiveUser().getEmail();
if (me != 'owner email'){
var protection = sheet.protect().setDescription('Sample protected sheet');
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
//YOUR CODE TO RUN
SpreadsheetApp.flush();
protection.remove();
}
}
这对有效用户不起作用。它将锁定所有其他编辑器(但不是所有者),但不会阻止用户在脚本运行时进行编辑。@SpenceEaston我相信它确实有效,可能不是最好的解决方案,但它确实有效。您可以在这里尝试:是的,如果用户编辑速度慢,并且他们不担心出现错误框,那么这种方法是有效的。还可以考虑这样一种场景:代码删除所有编辑器以阻止UI,然后在删除更新的权限之前失败/超时/配额超时。实际上,正确的解决方案是“不要将用户锁定在用户界面之外”,但如果您必须使用更优雅的解决方案,如模式对话框。@SpenceEaston是的,如果用户编辑速度慢,并且他们不会被不断的对话框所困扰,则模式对话框可以工作。还可以考虑在删除不可撤销的UI对话框之前代码失败/超时/配额超时的场景。在这两种情况下,OP都可以添加更多代码来解释这些情况。无论如何,我认为我们已经探索了一些可能的解决方案供OP选择,让我们就此为止。我建议使用一个不可撤销的UI对话框,解释正在发生的事情,然后在脚本完成运行时以编程方式关闭。