HTML服务 - 表单示例不起作用

时间:2015-01-25 23:19:32

标签: google-apps-script

我真的很想在我的应用中使用某个表单,Google的官方示例也不起作用。

将表单作为参数传递给函数,可以防止调用该函数。

https://developers.google.com/apps-script/guides/html/communication#forms

在index.html中查看此行

<input type="button" value="Submit"
  onclick="google.script.run
      .withSuccessHandler(updateUrl)
      .processForm(this.parentNode)" />

请参阅this.parentNode参数?删除它可以调用.processForm。实际上,将其替换为除表单之外的任何内容,也会调用processForm(通过无用的测试字符串传播)。

我已尝试过多种方法将表单作为参数传递给processForm,所有这些方法都会阻止processForm被调用。如果有人将表单数据传递给Google Script,请告诉我。

3 个答案:

答案 0 :(得分:1)

唉。几乎在向Google发布错误之后,我发现通过搜索他们的错误数据库发现这是文件上传的已知错误 - 如果您的表单没有文件上传,表单将起作用。

此外,如果您确实希望在表单中上传文件,则有一种解决方法。

https://code.google.com/p/google-apps-script-issues/issues/detail?id=4610

为什么他们不仅仅删除表单示例的文件部分超出了我。这是阻止它工作的唯一因素,并没有必要证明这一点。在试图学习他们的语言的时候可以救我3个小时,认为这是我一直以来的错。

答案 1 :(得分:1)

以下是我在IFRAME模式下从表单上传文件的解决方法。这支持多个文件:

code.gs

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

}

function saveFile(data,name) {

  var contentType = data.substring(5,data.indexOf(';'));
  var file = Utilities.newBlob(Utilities.base64Decode(data.substr(data.indexOf('base64,')+7)), contentType, name);
  DriveApp.getRootFolder().createFile(file);

}

的index.html

<div>
 <input type="file"  id="myFiles" name="myFiles" multiple/>
 <input type="button" value="Submit" onclick="SaveFiles()" />
</div>

<script>

  var reader = new FileReader();
  var files;
  var fileCounter = 0;



  reader.onloadend = function () {
   google.script.run
    .withSuccessHandler(function(){
      fileCounter++;      
      postNextFile();
    }).saveFile(reader.result,files[fileCounter].name);

  }



function SaveFiles(){
  files = document.getElementById("myFiles").files;  
  postNextFile();
 }


function postNextFile(){if(fileCounter < files.length){reader.readAsDataURL(files[fileCounter]);}else{fileCounter=0;alert("upload done")}}

</script>

答案 2 :(得分:0)

this.parentNode不是<form>元素,而是您要传递给.processForm()的元素。确保您的输入类型包含在<form>标记中,就像示例中一样:

<form id="myForm">
  <input type="button" value="Submit"
      onclick="google.script.run
        .withSuccessHandler(updateUrl)
        .processForm(this.parentNode)" />
</form>