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

前端 未结 2 1743
一向
一向 2021-01-12 11:00

I am creating userproperties with PropertiesService, as

      var userProperties = PropertiesService.getUserProperties();

for storing use

相关标签:
2条回答
  • 2021-01-12 11:39

    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.

    0 讨论(0)
  • 2021-01-12 11:48

    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.

    0 讨论(0)
提交回复
热议问题