Google apps script HTML服务-表单示例不';行不通
我真的很想在我的应用程序中使用一个表单,但是官方的Google示例不起作用 将窗体作为参数传递给函数会阻止函数被调用 请看index.html中的这一行Google apps script HTML服务-表单示例不';行不通,google-apps-script,Google Apps Script,我真的很想在我的应用程序中使用一个表单,但是官方的Google示例不起作用 将窗体作为参数传递给函数会阻止函数被调用 请看index.html中的这一行 <input type="button" value="Submit" onclick="google.script.run .withSuccessHandler(updateUrl) .processForm(this.parentNode)" /> <div> <input
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateUrl)
.processForm(this.parentNode)" />
<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>
请参见this.parentNode
参数?删除它可以调用.processForm
。事实上,用表单以外的任何内容替换它,也将允许调用processForm(albiet传递无用的测试字符串)
我尝试了许多方法将表单作为参数传递给processForm,所有这些方法都阻止调用processForm。如果有人曾将表单数据传递给Google脚本,请告诉我。
此.parentNode
不是要传递给.processForm()
的元素。确保输入类型包含在
标记中,如示例所示:
<form id="myForm">
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateUrl)
.processForm(this.parentNode)" />
</form>
Ugh。好吧,在谷歌几乎发布了一个bug之后,我通过搜索他们的bug数据库发现这是一个已知的文件上传bug——如果你的表单没有文件上传,那么表单就可以工作了
此外,如果您真的希望在表单中上载文件,还有一个解决方法
我无法理解他们为什么不删除表单示例的文件部分。这是唯一阻止它工作的东西,没有必要证明这一点。在尝试学习他们的语言时,我可以节省3个小时,因为我一直认为这是我的错。以下是我在IFRAME模式下从表单上传文件的一个解决方法。这支持多个文件:
代码.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
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateUrl)
.processForm(this.parentNode)" />
<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>
var reader=new FileReader();
var文件;
var fileCounter=0;
reader.onloadend=函数(){
google.script.run
.withSuccessHandler(函数(){
fileCounter++;
postNextFile();
}).saveFile(reader.result,files[fileCounter].name);
}
函数SaveFiles(){
files=document.getElementById(“myFiles”).files;
postNextFile();
}
函数postNextFile(){if(fileCounter
我说这是一行代码,而不是完整的代码-可以在链接中找到,如前所述。另外,这个.parentNode
确实有效。我在谷歌的bug网站上找到了答案。看看我贴的答案。哦,与谷歌产品合作的乐趣。它温暖了我的灵魂。这里有一个类似的帖子: