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

旧城冷巷雨未停 提交于 2019-11-26 14:49:27

问题


I'm trying out my first Google Sheets Apps Script. I'm trying to make a custom function (via a bound script) that will check if the cell it's in is protected. If it is protected, it should change the cell's value to (for now at least) the protection type.

I can successfully run the simple demo script in the docs:

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

But when calling Range::protect, I can an error

"You do not have permission to call protect"

Here's the function

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

I also get the same permissions error with some other functions, e.g. Session.getEffectiveUser(). I figured that since this is a bound custom function and I'm the (only) sheet owner, I'd be able to call these methods. What am I missing? Thanks.

I've tried copy-pasting the script into the script editor of a new sheet, since some posts have had luck with that, but no luck for me.


回答1:


Custom function run anonymously so the implicit consequence is that they can not do anything that requires authorization.

Changing a range protection requires an authorization , that's why you can't use it and get the message you get...

Same situation for getEffectiveUser(), it requires an authorization too.

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



来源:https://stackoverflow.com/questions/36365447/google-apps-script-error-you-do-not-have-permission-to-call-protect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!