Google apps script error: "You do not have permission to call protect"

Google apps script error: "You do not have permission to call protect"

我正在试用我的第一个 Google 表格应用程序脚本。我正在尝试创建一个自定义函数(通过绑定脚本)来检查它所在的单元格是否受到保护。如果它受到保护,它应该将单元格的值更改为(至少现在)保护类型。

我可以成功 运行 文档中的简单演示脚本:

function DOUBLE(input) {
  return input * 2;
}

但是调用Range::protect的时候报错

"You do not have permission to call protect"

这是函数

function isProtected() {
 var range = SpreadsheetApp.getActiveRange();
 var protection = range.protect();
 return protection.getProtectionType();
}

我也遇到了与其他一些功能相同的权限错误,例如Session.getEffectiveUser()。 我认为由于这是一个绑定的自定义函数并且我是(唯一的)sheet 所有者,所以我能够调用这些方法。我错过了什么?谢谢

我试过将脚本复制粘贴到新 sheet 的脚本编辑器中,因为有些帖子很幸运,但我没有运气。

自定义函数 运行 匿名,因此隐含的结果是 他们不能做任何需要授权的事情

更改 range protection 需要授权,这就是为什么您不能使用它并获取您收到的消息...

getEffectiveUser()情况相同,也需要授权。

Reference in docs :

Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data