问题
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
- Add-on works perfectly when tested under the other two config criteria (where both have
AuthMode.LIMITED
):- Enabled in current document
- Installed and enabled
- The problem persists across all 3 editors: Docs, Sheets and Slides
- I only require read access for current docs that are being used by the add-on (not sure if I need to edit something there)
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:
- Added a 3rd remark on oauthScopes
- Clarified the problem about menu not being visible
回答1:
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
.
var fileUI = SpreadsheetApp.getActiveSpreadsheet() ? SpreadsheetApp.getUi() : (DocumentApp.getActiveDocument() ? DocumentApp.getUi() : (SlidesApp.getActivePresentation() ? SlidesApp.getUi() : false));
Reference:
Authorization modes - Access to document: No under AuthMode: "NONE" table
Related answer - May not work.
来源:https://stackoverflow.com/questions/59426913/unable-to-test-add-on-under-installed-for-current-user-config-i-e-authmode-n