Sourabh Choraria
Sourabh Choraria

Reputation: 2331

Unable to test add-on under 'Installed for current user' config (i.e. AuthMode.NONE)

I'm trying to build my first editor add-on where the same codebase is supposed to work on Docs, Sheets and Slides.

I'm still in the testing phases and that's where I've hit a roadblock. Here's the problem -

As per the documentation for Installed versus enabled, if one were to select a test config as Installed for current user (but not yet enabled it), the Menu is supposed to be visible (this would be under AuthMode.NONE); however, per my script, its throwing me an error that indicates -

Google Apps Script: You do not have permission to perform that action.

... and the Menu is not visible either.

Note: One ought to view this error on the browser console

Here's the codebase and manifest files -

Code.gs

var fileUI = SpreadsheetApp.getActiveSpreadsheet() ? SpreadsheetApp.getUi() : (DocumentApp.getActiveDocument() ? DocumentApp.getUi() : (SlidesApp.getActivePresentation() ? SlidesApp.getUi() : false));

function onInstall(e) {
  onOpen(e);
}

function onOpen(e) {
  if (fileUI) {
    var menu = fileUI.createAddonMenu();
    if (e && e.authMode == ScriptApp.AuthMode.NONE) {
      menu.addItem('Please Login', 'login');
    } else {
      menu.addItem('Hurray', 'itWorks');
    }
    menu.addToUi();
  }
}

function login() {
  fileUI.alert('Please login to access this add-on');
}

function itWorks() {
  fileUI.alert('Hurray! It works');
}

appscript.json (Manifest file)

{
  "timeZone": "Asia/Kolkata",
  "dependencies": {
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/documents.currentonly",
    "https://www.googleapis.com/auth/presentations.currentonly",
    "https://www.googleapis.com/auth/spreadsheets.currentonly"
  ],
  "exceptionLogging": "STACKDRIVER"
}

Remarks

I have a feeling that I'm missing something super basic here. I apologize if I've skipped over some part of the documentation that covers this. Any help would be appreciated. Thanks.

Edit note:

  1. Added a 3rd remark on oauthScopes
  2. Clarified the problem about menu not being visible

Upvotes: 0

Views: 129

Answers (1)

TheMaster
TheMaster

Reputation: 50462

When a script file is loaded into memory for execution of any function (onOpen or any other function), all global variables are loaded/executed. Your issue stems from attempting to get the active(getActive*()) document under AuthMode.None.

If you can get to Ui without getting to getActive() that's possible. But you're attempting to access the document inbetween:

SpreadsheetApp .getActiveSpreadsheet() ? SpreadsheetApp.getUi().

The bolded part is causing the issue. SpreadsheetApp.getUi() will work fine.

You can also try try{...}...catch(e){} instead.

References:

Upvotes: 2

Related Questions