gpt4 book ai didi

javascript - 在 Google 表格中提交后弹出表单不会关闭

转载 作者:行者123 更新时间:2023-11-28 00:42:53 26 4
gpt4 key购买 nike

我正在使用我的函数 openDialog 来显示自定义 HTML 表单。它会正常提交,但不会在提交表单后关闭弹出窗口。我希望此表单在提交后关闭,或者显示带有“关闭”按钮的“thank-you.html”。

.gs

function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('index.html');
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Pop up Form');
}

function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index').setTitle('Adding Rows');
}

function doPost(e) {
Logger.log(e);
}

function sendText(data) {
var sheet = SpreadsheetApp.openById("some id").getActiveSheet();
sheet.appendRow([data.form_field_1, data.form_field_2, data.form_field_3, data.form_field_4]);
return 'success!';
}

HTML

<html>

<head>
<base target="_top">
</head>

<body>
<form onsubmit="sendText(event)" id="test-form">
<div>
<label>Field 1</label>
<input type="text" name="form_field_1" placeholder="Field 1" />
</div>
<div>
<label>Field 2</label>
<input type="text" name="form_field_2" placeholder="Field 2" />
</div>
<div>
<label>Field 3</label>
<input type="text" name="form_field_3" placeholder="Field 3" />
</div>
<div>
<label>Field 4</label>
<input type="text" name="form_field_4" placeholder="Field 4" />
</div>
<div>
<button type="submit" id="submit-form">Submit</button>
</div>
</form>
<script>
function sendText(e) {
e.preventDefault();

var data = {
form_field_1: e.target['form_field_1'].value,
form_field_2: e.target['form_field_2'].value,
form_field_3: e.target['form_field_3'].value,
form_field_4: e.target['form_field_4'].value
}

google.script.run
.withSuccessHandler(function(response) {
console.log(response);
window.close();
})
.sendText(data);
}
</script>
</body>

</html>

不知道我做错了什么

最佳答案

通过这样做找到了修复。

<script>
function sendText(e) {
e.preventDefault();

var data = {
form_field_1: e.target['form_field_1'].value,
form_field_2: e.target['form_field_2'].value,
form_field_3: e.target['form_field_3'].value,
form_field_4: e.target['form_field_4'].value
}

google.script.run.withSuccessHandler(function(response) {
console.log(response);
google.script.host.close()
})
.sendText(data);
}
</script>

关于javascript - 在 Google 表格中提交后弹出表单不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52391958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com