与Google Apps脚本的HtmlService一起使用 [英] work with Google Apps Script's HtmlService

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

问题描述

我看到了许多使用UiApp的示例,但无法弄清楚如何使用HtmlService.例如:

I see many examples of using UiApp, but can't figure out how to use HtmlService. For example:

var app = UiApp.getActiveApplication();
...
app.createListBox().setName('list').setId('list')
...
app.getElementById('list').clear(); 
app.getElementById('list').addItem(...);

如何使用HtmlService创建带有此类列表框的HTML文件?我会使用相同的方法吗?

With HtmlService, how can I create an HTML file with such a list box? Will I have the same methods?

推荐答案

HtmlService与UiApp根本不同,但与正常"广告非常相似. HTML开发,因为该应用程序主要基于客户端.因此,您可能从UiApp熟悉的方法在HtmlService中没有直接等效的方法.实际上,服务"指的是服务".只是提供了组装和显示用户界面的方法,并实现了它们与服务器端GAS功能之间的安全通信.

HtmlService is fundamentally different from UiApp, but aligns very closely with "normal" HTML development, as the app is primarily client based. As a result, the methods you may be familiar with from UiApp have no direct equivalent in HtmlService. In fact, the "service" just provides methods to assemble and display user interfaces, and enables secure communication between them and server-side GAS functions.

这是一个简单的示例,该示例创建一个带有使用模板HTML填充的列表的表单.提交后,它将被发布到doPost()函数. (注意:将脚本发布为Web应用程序.)

Here's a simple example that creates a form with a list that is populated using templated HTML. When submitted, it's POSTed to the doPost() function. (NOTE: Publish the script as a web app.)

// Adapted from http://stackoverflow.com/a/11300412/1677912

function doGet(e) {
  var template = HtmlService.createTemplateFromFile('Form.html');
  template.action = ScriptApp.getService().getUrl();
  template.listId = "browsers";
  template.datalist = ["Internet Explorer","Firefox","Chrome","Opera","Safari"];
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function doPost(e) {
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  template.browser = e.parameter.browsers;
  return template.evaluate();
}

Form.html

<html>
  <body>
    <h1>Select a browser</h1>
    <form action="<?= action ?>" method="post">
      <input list="<?= listId ?>" name="<?= listId ?>">
      <datalist id="<?= listId ?>">
        <? for (var i=0; i<datalist.length; i++) { ?>
          <option value="<?= datalist[i] ?>">
        <? } ?>
      </datalist> 
      <input type="submit" value="Submit" />
      </form>
  </body>
</html>

Thanks.html

<html>
  <body>
    <h1>Thanks</h1>
    <p>Thank you for your response.</p>
    Browser: "<?= browser ?>"<br/>
  </body>
</html>

这篇关于与Google Apps脚本的HtmlService一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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