【问题标题】:How do I protect a cell in google sheets after they are edited, and then unprotect when they are empty again?如何在编辑后保护 google 工作表中的单元格,然后在它们再次为空时取消保护?
【发布时间】:2023-01-10 14:10:48
【问题描述】:

这是我到目前为止运行的代码:

function onEdit(e){
  let protection = e.range.protect();
  protection.removeEditors(protection.getEditors());
  if (protection.canDomainEdit())  {
  protection.setDomainEdit(false);
   }
}

这会在单元格被编辑后锁定它们,但是即使在单元格被清空后,单元格仍然锁定到只有一个编辑器。我怎样才能做出这个改变,这样如果它再次为空,它就会解除自我保护?

非常感谢!

【问题讨论】:

  • 如果其他人有这个问题=这里是答案。
  • }function onEdit(e){ if (e.value == null){ let prot = SpreadsheetApp.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i in prot){ if (prot[i].getRange().getA1Notation() == e.range.getA1Notation()) prot[i].remove(); } } else { 让保护 = e.range.protect(); protection.removeEditors(protection.getEditors());如果 (protection.canDomainEdit()) protection.setDomainEdit(false); } }
  • 这是缺少的代码

标签: google-sheets


【解决方案1】:

这是我自己的问题的答案:

(我为什么要创建这个功能。我正在使用 Google 表格创建一个包含 50 多个用户的日程表。用户可以选择他们的偏好并将他们的名字写在日程表中。然后一旦受到保护,他们的名字就无法从日程表中删除除了他们自己。)

-- 这可以使用“应用程序脚本”扩展添加到工作表中。

下面是代码。前半部分在编辑后保护单元格。编辑单元格后,只有用户(和管理员)可以编辑单元格。编辑后,它仍然受到保护,不会被所有其他人更改。然后,一旦用户删除单元格的内容,后半部分就会取消保护。这将导致该小区再次可供另一个用户使用。

此代码产生一个循环,在用户编辑时受到保护,在用户删除时保护被解除。

`function onEdit(e){
let protection = e.range.protect();
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit())  {
protection.setDomainEdit(false);
}
}function onEdit(e){
if (e.value == null){
  let prot = SpreadsheetApp.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
  for (let i in prot){
    if (prot[i].getRange().getA1Notation() == e.range.getA1Notation())
      prot[i].remove();
  }
} else {
  let protection = e.range.protect();
  protection.removeEditors(protection.getEditors());
  if (protection.canDomainEdit())
    protection.setDomainEdit(false);
}
}
````

【讨论】:

    【解决方案2】:

    重新编辑受保护的单元格后出现问题。它复制了受保护的范围。如何防止重复?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多