2

Is it possible to test a Google Apps Script that uses DocumentProperties? The testing documentation seems to indicate this is possible:

If your add-on uses the Properties service, properties will persist and remain available the next time the test configuration is run.

However, when I attempt to use DocumentProperties, I get the following error:

Google Apps Script: You do not have permission to call getDocumentProperties

This is the code I am trying:

function appFolderExists(){
    PropertiesService.getDocumentProperties().getProperties();
    return true;
}

function onOpen() {
    FormApp.getUi().createMenu('My Addon')
        .addItem('My Addon', 'showSidebar')
        .addToUi();
    if(!appFolderExists()){
        createAppFolder();
    }
}

My addon is currently deployed as a test (for a form). It is installed but not enabled.

I want to add more logic to appFolderExists down the line, but I keep getting stuck since I can't seen to access the DocumentProperties. Am I doing something wrong or is this just an inconvenient limitation of testing addons?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Zak
  • 2,688
  • 4
  • 29
  • 45
  • 1
    Since you are installed not enabled, you are running `onOpen(e)` in `AuthMode.none`. In this authorization mode you cannot access documentProperties as described [here](https://developers.google.com/apps-script/add-ons/lifecycle#authorization_modes). So it is working as expected – Jack Brown May 10 '17 at 04:19
  • 1
    Also, this example [code](https://developers.google.com/apps-script/add-ons/lifecycle#opening) in google documentation might give you a better idea of how to work with documentProporties in add on based on authorization mode. – Jack Brown May 10 '17 at 04:24

1 Answers1

6

The issue is caused by testing the add on as installed but not enabled. Thus runs the code in authmode.None, in this mode you cannot access document Properties

Solutions:
1) Run the test as installed and enabled
2) Determine the authmode and run the relevant code:

function open(e){

if (e.authMode == ScriptApp.AuthMode.NONE){
// Code that does not access document properties
} else {
var prop = PropertiesService.getDocumentProperties().getProperties();
// Some code that uses document properties
}
}
Jack Brown
  • 5,802
  • 2
  • 12
  • 27
  • Can you explain what code to write in the `if (e.authMode == ScriptApp.AuthMode.NONE)` block, so that I can change the Authmode to `enabled` to users? – pauljeba Jun 25 '20 at 04:31