Sam Tyurenkov
Sam Tyurenkov

Reputation: 440

Spreadsheet add-on Auth Error - document owner and active user do not match

in my spreadsheets add-on I have a check for document owner:

  var USEREMAIL = Session.getEffectiveUser().getEmail();
  var owner = SpreadsheetApp.getActive().getOwner().getEmail();
    if (USEREMAIL == owner) {
      menu.addItem('Owner menu', 'ownermenu');
      menu.addToUi();
    } else {
      menu.addItem('Not owner', 'notowner');
      menu.addToUi();
    } 

It is also a fallback for known problem where users are logged into multiple accounts. So I recently found out, that even if I'm a document owner and I'm only signed into one account, I still get Not owner menu.

The problem can be fixed with add-on reinstallation, but still, is it a Google bug? I don't see an obvious reason why it works properly after reinstallation, shouldn't be an issue on my side then.

Upvotes: 0

Views: 45

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11258

The owner's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e).

Therefore the call to getOwner would return a null inside onOpen.

Upvotes: 2

Related Questions