HTMLService表单和响应

时间:2015-03-01 18:58:57

标签: google-apps-script

Google脚本新手。我不能为我的生活获得一个表单工作的谷歌脚本。文档没有帮助,我找不到任何完整的例子。

在表格中,我去了脚本部分。我创建了一个基本的“hello world”html。我添加了一个函数useMyForm,它将调用包含该表单的脚本。提交后,我无法获取表单来调用具有onFormSubmit的其他脚本。

有人可以给我写一个表单,其中包含一个条目和对将处理数据的函数的调用吗?也许我的函数不应该在单独的脚本中。

我写c,php,java .....这个谷歌脚本的东西正在杀了我。

1 个答案:

答案 0 :(得分:1)

以下是文档中的示例代码,如果您有不明白的地方,请发布您自己的代码版本: https://developers.google.com/apps-script/guides/html/communication#forms

Code.gs

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

function processForm(formObject) {
  var formBlob = formObject.myFile;
  var driveFile = DriveApp.createFile(formBlob);
  return driveFile.getUrl();
}

的index.html

<script>
  function updateUrl(url) {
    var div = document.getElementById('output');
    div.innerHTML = '<a href="' + url + '">Got it!</a>';
  }
</script>
<form id="myForm">
  <input name="myFile" type="file" />
  <input type="button" value="Submit"
      onclick="google.script.run
          .withSuccessHandler(updateUrl)
          .processForm(this.parentNode)" />
</form>
<div id="output"></div>

让我花费一些时间来掌握的一件事是当你运行google.script.run.withSuccessHandler(updateUrl).processForm(this.parentNode)

.processForm(variable)将在.gs文件中运行一个函数。 该函数的返回(在.gs文件中)被发送到.withSuccessHandler(updateUrl)函数。在上面的示例中,function updateUrl(url)

中的“url”变量