我有一个小型的Google脚本,在电子表格中的某些单元格被编辑后,它会运行几个秒。
我的问题是,是否有可能阻止当前用户在脚本运行时对电子表格进行进一步更改。
所以应该是这样的:
发布于 2016-12-12 16:23:52
您可以使用类保护:https://developers.google.com/apps-script/reference/spreadsheet/protection
根据上面的链接改编,您可以尝试:
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();
}
}https://stackoverflow.com/questions/41103337
复制相似问题