var userProperties = PropertiesService.getUserProperties();对我来说工作不正常? [英] var userProperties = PropertiesService.getUserProperties(); is not working properly for me?

查看:88
本文介绍了var userProperties = PropertiesService.getUserProperties();对我来说工作不正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PropertiesService创建userproperties,如

  var userProperties = PropertiesService.getUserProperties(); 

用于存储用户单个令牌用于我的加载项,当我使用密钥对在userproperties中设置usertoken值为,userProperties.setProperty('USERTOKEN','token');在一个文档中。

一旦我按照userproperties范围来做这件事,我可以通过使用userProperties.getProperty('USERTOKEN')从任何文档检索用户属性值;但是,

当我这样做的值为空(即),我不能从其他文件检索'userProperties'值,

因此,userproperties的作用域失败。 userproperties值只与创建它的特定文档相关联。



一旦安装了我的附加组件,我用来检查每次对所有文档的userproperties值,如果(价值)
{
检索数据; b

  
}
其他
{
授权;




$ b $ p
$ b

因此,值为空,并且用户每次都被授权为每个新文件。由于我的附加组件无法从userProperties.getProperty('USERTOKEN');

解决方案

a href =https://developers.google.com/apps-script/guides/properties#modifying_data =noreferrer>属性服务 - 保存数据:



还可以通过脚本编辑器用户界面保存脚本属性,方法是转到文件>项目属性并选择项目属性选项卡。不能在用户界面中设置或查看用户属性和文档属性。



我不确定这是什么意思,但我认为这意味着即使您可以使用脚本设置该属性,该值也不会如果您在文件>项目属性>用户属性中查看它,则更新。
但这并不意味着该物业并未更新。但是,如果它是通过脚本设置的脚本属性,则该值将在您的视图中更新。



这是我说的,因为我通过在doGet() 。请参阅下面的示例:

  //将属性设置为全局变量
var userProperty = PropertiesService.getUserProperties();
var scriptProperty = PropertiesService.getScriptProperties();

函数doGet(){

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

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

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

我在doGet ),以便我可以通过刷新WebApp页面来检查它
有趣的是,日志显示了正确的值(Hello,and World)
但是如果我访问文件>项目属性,只有脚本Propery值已更新,User Property保持与我设置的原始值相同。



希望这有助于您。


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');

解决方案

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.

这篇关于var userProperties = PropertiesService.getUserProperties();对我来说工作不正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆