Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var userProperties = PropertiesService.getUserProperties(); is not working properly for me?

I am creating userproperties with PropertiesService, as

      var userProperties = PropertiesService.getUserProperties();

for storing users single token for my add-on , When i set usertoken in userproperties with a key pair value as, userProperties.setProperty('USERTOKEN','token'); in an document.

Once i do this as per the userproperties scope i can retrieve the user properties value from any of the document by using userProperties.getProperty('USERTOKEN'); but,

When i do that the value is null (i.e), i cant retrieve the 'userProperties' value from other documents,

So that, the scope of the userproperties fails. The userproperties value is associated only with the particular document where it's created.

once my add-on is installed i used to check every time userproperties value for all documents ,

        if(value)
         {
          retrieve data;
          }
             else
             {
              authorize;

               }

Thus value is null and the user is made to authorize everytime for every new document. Since my add-on cant retrieve the value from userProperties.getProperty('USERTOKEN');

like image 385
vasanth vasu Avatar asked Jul 23 '14 11:07

vasanth vasu


2 Answers

According to the documentation at Properties Service - Saving Data:

"Script properties can also be saved via the script editor user interface by going to File > Project properties and selecting the Project properties tab. User properties and document properties cannot be set or viewed in the user interface."

I'm not sure what it means but I think it means that even if you are able to set the property using script, the value will not update if you view it in File > Project Properties > User Properties. But it doesn't mean that the property was not updated though. However, if it's a Script Property you set via script, the value will update in your view.

This I say because I tried it by setting it inside the doGet(). See the example below:

//Set properties as global variables
var userProperty = PropertiesService.getUserProperties();
var scriptProperty = PropertiesService.getScriptProperties(); 

function doGet() {

 userProperty.setProperty('uservar', 'Hello');
 scriptProperty.setProperty('scriptvar', 'World');

 Logger.log(userProperty.getProperty('uservar'));
 Logger.log(scriptProperty.getProperty('scriptvar'));

 return HtmlService.createTemplateFromFile('index').evaluate();
}

I did it inside doGet() so that I can check it simply by refreshing my WebApp page. Interestingly, the log shows the correct values (Hello, and World). But if I visit the File > Project Properties, only the Script Propery value was updated, the User Property remains the same as the original value I set it for.

Hope this helps.

like image 153
JP Alpano Avatar answered Oct 23 '22 17:10

JP Alpano


For some reason it seems that the PropertiesService does not work. Even when I use Google example in https://developers.google.com/apps-script/guides/properties#reading_data I am getting nulls. I have reverted back to the old UserProperties and it works, however it is supposed to be depreciated... PropertiesService seem to be working for script properties though.

like image 7
theworldismyoyster Avatar answered Oct 23 '22 17:10

theworldismyoyster