Google脚本的onClick不适用于HTML表单 [英] onClick on Google Script not working for HTML Form
问题描述
我正在尝试创建一个HTML弹出窗口,然后将其填充到gsheet中.我基本上使用了
I'm trying to create a html pop up that will then populate a gsheet. I've basically used the same solution found here. For some strange reason though, it isn't working. When I click the submit button on the HTML pop up box after filling out all the data, it doesn't respond. I click, it doesn't close, append the data, or do anything.
已创建html弹出窗口,但提交"按钮不起作用.我几乎可以确定,这与我使用的onClick方法有关.
The html pop up is created, but the Submit button doesn't work. I am almost sure that it is has something to do with the onClick method I am using.
这是我正在使用的代码:
Here is the code I am working with:
function registerCustomer() {
var html = HtmlService.createHtmlOutputFromFile('customerMenu.html').setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi()
.showModalDialog(html, 'Add Customer');
}
function customerAdd(customerForm) {
var ss = SpreadsheetApp.getActiveSpreadsheet;
var cus_db = ss.getSheetByName("customer_db");
cus_db.appendRow([" ", customerForm.name_1, customerForm.name_2, customerForm.phone, customerForm.age, customerForm.channel]);
return true;
}
html文件
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
</head>
<br>
<customerForm>
First Name:<br>
<input type="text" name="name_1">
<br>
Second Name:<br>
<input type="text" name="name_2">
<br>
Phone Number:<br>
<input type="text" name="phone">
<br>
Age:<br>
<input type="number" name="age">
<br>
How did they hear about us?:<br>
<input type="text" name="channel">
<br><br>
<input type="submit" value="Add Customer" class ="submit"
onclick="google.script.run
.withSuccessHandler(google.script.host.close)
.customerAdd(this.parentNode)" />
</customerForm>
</html>
我已经为几乎所有解决方案搜寻了stackoverflow:
I've scoured stackoverflow for almost every solution:
-
我正在运行两个弹出窗口,因此我根据建议我尝试使用发现的'''document.forms [0]'''方法
I tried to use the '''document.forms[0]''' method found here also didn't work
我想念什么?
推荐答案
onClick适用于HTML表单的Google脚本
GS:
function registerCustomer() { var html = HtmlService.createHtmlOutputFromFile('ah2') SpreadsheetApp.getUi().showModelessDialog(html, 'Add Customer'); } function customerAdd(obj) { var ss = SpreadsheetApp.getActive(); var sh=ss.getSheetByName('Sheet15'); sh.appendRow(["", obj.name_1, obj.name_2, obj.phone, obj.age, obj.channel]); return true; }
html:
<!DOCTYPE html> <html> <head> <base target="_top"> <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> </head> <body> <form> First Name:<br> <input type="text" name="name_1" /> <br> Second Name:<br> <input type="text" name="name_2"/> <br> Phone Number:<br> <input type="text" name="phone"/> <br> Age:<br> <input type="number" name="age"/> <br> How did they hear about us?:<br> <input type="text" name="channel"/> <br><br> <input type="button" value="Add Customer" onClick="addCust(this.parentNode);" /> </form> <script> function addCust(obj) { google.script.run .withSuccessHandler(function(){google.script.host.close();}) .customerAdd(obj); } </script> </body> </html>
这篇关于Google脚本的onClick不适用于HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!