【问题标题】:Protect specified range in google Sheet with GAS使用 GAS 保护 google Sheet 中的指定范围
【发布时间】:2021-04-17 03:40:51
【问题描述】:
function Lock(){

  var sh = SpreadsheetApp.openById(ssID).getSheetByName('6');
  var protection = sh.protect().setDescription('BVDATA');
  var me = Session.getEffectiveUser();
protection.addEditor(me).setUnprotectedRanges(sh.getRange("F1:F").createTextFinder("^(?!OK).*$").matchEntireCell(true).useRegularExpression(true).findAll());
  protection.removeEditors(protection.getEditors());

  if (protection.canDomainEdit()){
  protection.setDomainEdit(false);
  }
}

@Tanaike 引导我做到这一点。现在我有一个问题,如果它的值为OK,该代码将自动保护 1 个单元格 F。如果F的值为OK,有没有办法保护F:G(如果F5OKF5:G5将受到保护)?我试过修改:

protection.addEditor(me).setUnprotectedRanges(sh.getRange("F1:G").CreateTextFinder("^(?!OK).*$").MatchEntireCell(true).useRegularExpression(true).findAll());

但它仅在值为OK 时保护单元格G。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    我相信你的目标如下。

    • 您想保护“F”和“G”列的单元格,只有当单元格值为OK时。

    为了实现这一点,下面的修改如何?

    发件人:

    protection.addEditor(me).setUnprotectedRanges(sh.getRange("F1:F").createTextFinder("^(?!OK).*$").matchEntireCell(true).useRegularExpression(true).findAll());
    

    收件人:

    protection.addEditor(me).setUnprotectedRanges(sh.getRange("F1:F").createTextFinder("^(?!OK).*$").matchEntireCell(true).useRegularExpression(true).findAll().map(r => r.offset(0, 0, 1, 2)));
    
    • 在此修改中,搜索范围使用offset 扩展。

    参考:

    【讨论】:

    • @Leti 感谢您的回复。很高兴您的问题得到解决。
    • @Leti 关于您的评论的新问题,我会检查它。当我能正确理解你的新问题时,我会想办法。
    • @Leti 关于你的新问题,我不得不为我糟糕的英语水平道歉。不幸的是,根据您的评论,我无法理解您的新问题。但我想试着理解它。当我能正确理解它时,我想考虑解决方案。非常抱歉,我无法尽快解决您的问题。
    • @Leti 感谢您的回复。从您的回复中,我仍然无法理解您的新问题。我认为这是由于我的英语水平不佳。我再次为此深表歉意。例如,通过包含示例情况,您可以将其作为新问题发布吗?通过这个,我想尝试理解它。而且,在那种情况下,包括我在内的用户也可以想到解决方案。当你能合作解决你的新问题时,我很高兴。可以配合吗?
    • @Leti 感谢您的回复。我不得不为我糟糕的英语水平道歉。不幸的是,我无法理解hihi. Your quotes :。可以问一下具体情况吗?
    猜你喜欢
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多