HTML服务 - 表单示例不起作用 [英] HTML Service - Form example doesn't work
问题描述
将表单作为参数传递给函数
将表单作为参数传递给函数,阻止该函数被调用。
https://developers.google.com/apps-script/guides/html/communication#forms
看看这一行in index.html
< input type =buttonvalue =提交
onclick =google。 script.run
.withSuccessHandler(updateUrl)
.processForm(this.parentNode)/>
请参阅 this.parentNode
参数?删除它可以调用 .processForm
。事实上,用其他形式替换它,也会让processForm被调用(albiet传递无用的测试字符串)。
我尝试了很多方法来传递作为processForm的参数形式,所有这些都会阻止processForm被调用。如果有人曾经将表单数据传递给Google脚本,请告诉我。 解决方案
这是我用于文件上传的解决方法从IFRAME模式中的表单。这支持多个文件:
code.gs
函数doGet( ){
return HtmlService.createHtmlOutputFromFile('index')。setSandboxMode(HtmlService.SandboxMode.IFRAME);
$ b 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 =fileid =myFilesname =myFilesmultiple />
< input type =buttonvalue =Submitonclick =SaveFiles()/>
< / div>
< script>
var reader = new FileReader();
var文件;
var fileCounter = 0;
reader.onloadend = function(){
google.script.run
.withSuccessHandler(function(){
fileCounter ++;
postNextFile();
))。saveFile(reader.result,files [fileCounter] .name);
}
函数SaveFiles(){
files = document.getElementById(myFiles)。files;
postNextFile();
$ b函数postNextFile(){if(fileCounter< files.length){reader.readAsDataURL(files [fileCounter]);} else {fileCounter = 0; alert (上传完成)}}
< / script>
I'd really like to use a form in my app, and the official Google example doesn't work.
Passing a form as a parameter to a function, prevents the function from being called.
https://developers.google.com/apps-script/guides/html/communication#forms
Look at this line in index.html
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateUrl)
.processForm(this.parentNode)" />
See the this.parentNode
parameter? Removing it allows .processForm
to be called. In fact, replacing it with anything OTHER than a form, will also let processForm be called (albiet passing useless test strings).
I've tried many ways of passing the form as a parameter to processForm, all of them stop processForm from being called. If anyone has ever passed form data to a Google Script, let me know.
Here is a workaround I have for file upload from a form in IFRAME mode. This supports multiple files:
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>
这篇关于HTML服务 - 表单示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!