首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在脚本运行时防止单元格的编辑

在脚本运行时防止单元格的编辑
EN

Stack Overflow用户
提问于 2016-12-12 14:41:58
回答 1查看 5.7K关注 0票数 4

我有一个小型的Google脚本,在电子表格中的某些单元格被编辑后,它会运行几个秒。

我的问题是,是否有可能阻止当前用户在脚本运行时对电子表格进行进一步更改。

所以应该是这样的:

  1. 单元格被编辑
  2. 脚本正在运行,阻止当前用户的用户交互/更改。
  3. 脚本已完成
  4. 解除了当前用户的用户交互/更改。
EN

回答 1

Stack Overflow用户

发布于 2016-12-12 16:23:52

您可以使用类保护:https://developers.google.com/apps-script/reference/spreadsheet/protection

根据上面的链接改编,您可以尝试:

代码语言:javascript
运行
复制
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();  
}

编辑

这对我起了作用:

代码语言:javascript
运行
复制
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(); 
  }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41103337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档