google-apps-scriptgoogle-sheetsgoogle-sheets-custom-function

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


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.


Solution

  • 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