使用HtmlService在Google Apps脚本中保存状态的位置 [英] Where to save states in google apps script with HtmlService

查看:85
本文介绍了使用HtmlService在Google Apps脚本中保存状态的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下发布的网络应用,其中包含 code.gs

I have the following published webapp with code.gs

var queryString

function doGet(e) {
  queryString = e.queryString  

  //logger only works if the return value is commented out
  Logger.log(queryString)

  return HtmlService.createHtmlOutputFromFile('index.html')
}

function getQueryString() {
  // this prints "a=1" on the html
  // return "a=1"

  // this prints "undefined" on the html
  return queryString
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function onSuccess(queryString) {
        var div = document.getElementById('output');
        div.innerHTML = queryString;
      }
    </script>
  </head>
  <body>
    <button type="button" onclick="javascript:google.script.run.withSuccessHandler(onSuccess).getQueryString();">show query string</button>
    <div id="output"></div>
  </body>
</html>

按下按钮时,网页如下所示

When the button is pressed, the webpage looks as follows

但是,我希望显示查询字符串.在调用doGet()期间,查询字符串的值将保存到全局变量queryString中.用户按下按钮后,服务器端功能getQueryString应该为客户端html页面提供全局变量queryString的值,但不是. 似乎每次使用新初始化的变量调用服务器端函数时都会生成一个新的上下文

However, I expect the display of the query string. The value of the query string is saved to the global variable queryString during the call of doGet(). Once the user presses the button, the server side function getQueryString should provide the client side html page with the value of the global variable queryString, but it doesn't. It seems like a new context is generated everytime a server side function is called with newly initialized variables

对我来说,使用PropertiesService似乎有些矫kill过正.解决该问题的最佳方法是什么?

The use of PropertiesService seems overkill to me. What is the best way to solve that problem?

推荐答案

尽管先前的回答是正确的,但我相信从问题的角度来看,这可能是有用的.

Although the previous answer is correct, I believe that in the spirit of the question, this might be useful.

  • 您可以使用google.script.historygoogle.script.url推送/替换/获取状态客户端.这类似于典型的JavaScript history.pushState,但有额外的限制.
  • You can push/replace/retrieve state client side using google.script.history and google.script.url. This is similar to typical JavaScript history.pushState, but with extra restrictions.

(使用google.script.url检索查询字符串)

(Using google.script.url to retrieve querystring)

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function onSuccess() {
        var div = document.getElementById('output');
        google.script.url.getLocation(function(location){
        var obj = location.parameter;
        div.innerHTML= Object.keys(obj).map(e=>e+'='+obj[e]).join('&');
        }); 
      }
    </script>
  </head>
  <body>
    <button type="button" onclick="onSuccess();">show query string</button>
    <div id="output"></div>
  </body>
</html>

参考文献:

  • WebApp浏览历史记录指南
  • JavaScript历史记录API
  • References:

    • WebApps browing history guide
    • Javascript History API
    • 这篇关于使用HtmlService在Google Apps脚本中保存状态的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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