CodingBrunette
CodingBrunette

Reputation: 31

Problems with permissions in google sheets add-on

I have an add-on, which manipulates with data in Google Sheets. It works fine, but I am still seeing errors in Stackdriver logs for other users.

I can't replicate those errors during my tests, but the problems still exist for some users. Using try {} catch {} is not solving the problem, but just avoids getting error messages in Stackdriver logs.

Here is my code:

appsscript.json

{
  "timeZone": "Europe/Moscow",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Sheets",
      "serviceId": "sheets",
      "version": "v4"
    }]
  },
  "oauthScopes": [
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/script.scriptapp",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/userinfo.email"
  ],
  "exceptionLogging": "STACKDRIVER"
}

Code.gs

function onOpen(e) {
    var ui = SpreadsheetApp.getUi();
    SpreadsheetApp.getUi()
        .createAddonMenu()
        .addItem('Make some stuff', 'showMakesomeStuff')
        .addToUi();
    if (e && e.authMode !== ScriptApp.AuthMode.NONE) { // It looks that the problem is here. 
        myLinkCells();
        google_analytics('file opened');
    }
}

function myLinkCells() {
    var allTriggers =[];
    var allTriggers = ScriptApp.getProjectTriggers(); \\ This line triggers error; 
    var editTriggerSet = false;
        for (var i = 0; i < allTriggers.length; i++) {
            if (allTriggers[i].getEventType() == ScriptApp.EventType.ON_EDIT) 
            {
                editTriggerSet = true;
                break;
            }
        }
    if (editTriggerSet == false) ScriptApp.newTrigger("callOnEdit").forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
    }

function google_analytics(url) {
    UrlFetchApp.fetch('https://ssl.google-analytics.com/collect', OPTIONS); 
// This line also cause error. FYI OPTIONS is defined, but not in this example. 
}

Error message examples: "You do not have permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request" or "You do not have permission to call ScriptApp.getProjectTriggers. Required permissions: https://www.googleapis.com/auth/script.scriptapp".

I looked through existing questions, but could not find anything similar.

Upvotes: 0

Views: 572

Answers (1)

CodingBrunette
CodingBrunette

Reputation: 31

It seems I nailed the bug. It had nothing to do with permission, but with check AuthMode I replaced:

if (e && e.authMode !== ScriptApp.AuthMode.NONE) { // It looks that the problem is here. 
        myLinkCells();
        google_analytics('file opened');
    }

with

if (e && e.authMode == ScriptApp.AuthMode.NONE) {
            \\Do nothing; 
        }
        else
        {
            myLinkCells();
            google_analytics('file opened');

    }

Now waiting for logs to review.

UPDATE. According to https://developers.google.com/gsuite/add-ons/concepts/editor-auth-lifecycle UrlFetchApp.fetch will not work even in AuthMode.LIMITED.

It also seems impossible to install a trigger from onOpen() function.

Upvotes: 1

Related Questions