use*_*733 3 global-variables google-apps-script
我在使用 Google Apps 脚本时遇到此问题。\n我的电子表格中有一个菜单,其中有两个选项(设置密码和添加时间记录)。这些选项分别引发 UI 服务用户界面以提示其数据。为了访问添加时间记录,我之前会询问用户是否经过身份验证。我使用 scriptProperties = PropertiesService.getScriptProperties() 和 setProperty('authenticated', false) 来保存已验证的初始值。
\n\n1-我单击“设置密码”,我确实登录并关闭了用户界面。
\n\n2-我单击添加时间记录,我希望收到我的添加时间记录 Ui(因为我先设置密码),但我收到的是设置密码 UI。即使我再次登录,我也会一次又一次收到相同的 UI \xe2\x80\xa6。
\n\n就像每次我单击菜单选项时,无论我之前执行了什么操作,身份验证都会重置为 false。这是预期的行为吗?或者我\xe2\x80\x99m做错了什么?非常感谢你的帮助。
\n\nvar scriptProperties = PropertiesService.getScriptProperties();\nscriptProperties.setProperty('authenticated', 'false');\nfunction onOpen() {\n var ui = SpreadsheetApp.getUi();\n ui.createMenu('ProWorkflow')\n .addItem('Set Pasword', 'getPassword').addSeparator()\n .addItem('Add Time Record', 'isAuthenticated').addSeparator()\n .addToUi();\n}\nfunction isAuthenticated()\n{\n var scriptProperties = PropertiesService.getScriptProperties();\n var value = scriptProperties.getProperty('authenticated');\n if(value =='false'){\n getPassword(); // at the end of this function I set scriptProperties.setProperty('authenticated', \xe2\x80\x98true\xe2\x80\x99);\n }\n return addTimeRecord();\n}\nfunction getPassword(e)\n{\n \xe2\x80\xa6\n \xe2\x80\xa6\n \xe2\x80\xa6\n var scriptProperties = PropertiesService.getScriptProperties();\n var value = scriptProperties.getProperty('authenticated');\n value = 'true'; \n scriptProperties.setProperty('authenticated', value); //change de value of uthenticated \n return app.close(); \n}\nRun Code Online (Sandbox Code Playgroud)\n
如果您想以编程方式进行设置,我不清楚可以在哪里设置项目属性。我在脚本中添加了一个函数:
function setScriptProperties() {
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('KEY', 'Some value.');
}
Run Code Online (Sandbox Code Playgroud)
然后我使用 UI 从 GAS 界面“手动”运行该函数。
从 UI 调用该函数一次后,项目属性将被设置。
| 归档时间: |
|
| 查看次数: |
9953 次 |
| 最近记录: |