javascript - 在 Google 脚本网络应用中添加 URL 参数

标签 javascript google-apps-script

现在已经尝试了几个小时 - 有没有办法在已部署的网络应用程序中向服务器端 Google 脚本上的 URL 添加参数,该参数对任何人(不仅是 Google 帐户)开放?

我想存储用户登录时的 key ,以便用户不必在每次刷新浏览器时再次登录。假设我当前的网址是: https://script.google.com/a/macros.../exec

我可以以某种方式在登录时添加 GAS 函数,以便将 URL 修改为类似的内容 https://script.google.com/a/macros.../exec?key=abcdef&name=John%20Doe

这样,如果 doGet(e) 函数再次运行(网站重新加载)并且临时生成的 key 与我的数据库中的用户名匹配,则用户将被定向到页面 B,如果 key 不匹配(或已在时间触发器上删除)用户将被定向到页面 A(登录页面)?

我还尝试找到将某些内容放入本地缓存的方法,但也无法弄清楚。Google CacheService 似乎只将我作为用户而不是客户端的 key 存储起来。

我想要实现的目标示例:

  function doGet(e){
    var name = e.parameter.name, output;
    if(name === undefined){
      /*/
      Add parameter name=john to URL.. 
      obviously what I intend to is to create a HtmlOutput on a login-page
      and then in a later (function login()) add this parameter there and not
      here, but if anyone can work this out for me I got the solution
     /*/
     } else {
     output = HtmlService.createHtmlOutput('Welcome back ' + name);
     }
    return output;
    }

最佳答案

这是一个可能的解决方案,根据提供的 URL 参数将用户名保存到 PropertiesService,如果已经存在,则获取 key 。

function doGet(e) {
  //use the session service to get the active users email
  var currentUser = e.parameter.name;

  var key = e.parameter.key;

  var props = PropertiesService.getUserProperties().getProperty(currentUser);

  if(props) {
    //do something with the key
  } else {
    //if there is no username key in the properties service, then add it
    PropertiesService.getUserProperties().setProperty(currentUser, key);
  }
}

以下是文档:

https://developers.google.com/apps-script/reference/properties/properties-service#getUserProperties()

关于javascript - 在 Google 脚本网络应用中添加 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43970514/

相关文章:

javascript - 如何根据数据维度更改条形颜色?

javascript - node-mysql 多次查询时插入速度慢

javascript - 谷歌应用脚​​本: Create Multiday-Events//Compare Values in Multiple Rows

google-apps-script - Google Spreadsheet onEdit - 限制在一个范围内

javascript - 有人可以帮我自动播放 slider 吗?

javascript - 如何在 JavaScript 中从二进制数据创建文件对象

javascript - Firefox 在 iframe 中打印 PDF 会引发错误

google-apps-script - 应用程序制作工具 : Public facing IP for UrlFetch?

jquery - 从 Google SpreadSheet 脚本调用 jQuery

google-apps-script - 如何暂停谷歌脚本中的onEdit功能