Google Apps脚本HTML服务的新功能

时间:2015-12-08 05:30:24

标签: javascript html google-apps-script

我刚开始使用Google Apps脚本的HTML服务来创建UI。从非常基本开始,Google的文档似乎非常不完整(如果我错过了某些内容,请告诉我)。我按照这个例子:https://developers.google.com/apps-script/guides/html/reference/run#withUserObject(Object)并让它工作,但我不明白“这个”来自哪里(在HTML代码中)以及操作顺序如何在那里工作。

为了解决这个问题,我正在尝试制作一些可以放入文本的内容,按下按钮,它将在全部大写中显示相同的文本。这是我到目前为止所得到的:

Google Script:

function doGet() {
  return HtmlService.createHtmlOutputFromFile('index')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function capitalize(input){
        return input.toUpperCase();
      }
    </script>
  </head>
  <body>
    Put some text here: <input type="text" name="words"><br>
    <input name="button" type="button" value="CAPITALIZE" onclick="google.script.run
          .withSuccessHandler(capitalize)
          .withUserObject(words)"><br><br>
    Here is your text:
  </body>
</html>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

.gs的文档实际上非常好。不要进入任何语言的文档,期望每个用例都有“完整的解释”。

只有当您想要将数据传递到服务器端.gs函数时才需要

google.script.run(如您链接到的页面顶部所述)。

您要求的似乎是所有客户端操作,但不需要将数据传递给.gs函数。

尝试这些调整:

// get value of a text box and set it into html of a <span> element
function capitalize(){
  document.getElementById('userInput').innerHTML = 
  document.getElementById("words").value.toUpperCase();
}

onclick="capitalize()"><br><br>
Here is your text:<span id="userInput"></span>