gpt4 book ai didi

javascript - Google Apps 脚本 - HTML 服务 - "wait"用于客户端表单提交

转载 作者:行者123 更新时间:2023-12-03 03:39:16 25 4
gpt4 key购买 nike

我正在创建一个 container-bound绑定(bind)到 Google 电子表格的 Google Apps 脚本。在我的 GS 代码中,我使用 Google HTML Service在 Google 表格(即 HTML 表单)中显示对话框。

我遇到的问题是,当我在客户端渲染 HTML 表单时,GS 服务器端代码继续运行。相反,我想“暂停”服务器端代码并​​让它等待,直到用户提交表单。

可以这样做吗?

这是我到目前为止所拥有的:

代码.gs

function onOpen(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom')
.addItem('Generate Shipping Email', 'menuItem1')
.addToUi();
}

function menuItem1() {
var html = HtmlService.createHtmlOutputFromFile('index');
var ui = SpreadsheetApp.getUi()
ui.showModalDialog(html, 'Shipping Email Options'); // This line shows the modal in index.html

// Here is where I would like to "pause" until the HTML form is submitted by user clicking the button with id="button1id" ...

Logger.log("Line after showModalDialog"); // ... however, currently this code runs before the user clicks submit or cancel button in the html form
// Rest of code which should not run until user clicks submit or cancel button will go here
}

readyToSendEmail(shippingNeeded, notes) {
// Some non-relevant code goes here
}

index.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form class="form-horizontal">
<fieldset>

<!-- Form Name -->


<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="radios">Do you need expedited shipping?</label>
<div class="col-md-4">
<label class="radio-inline" for="radios-0">
<input type="radio" name="radios" id="shippingNeededYes" value="Yes" checked="checked">
Yes
</label>
<label class="radio-inline" for="radios-1">
<input type="radio" name="radios" id="shippingNeededNo" value="No">
No
</label>
</div>
</div>

<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Additional shipping notes:</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea" name="textarea"></textarea>
</div>
</div>

<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-4 control-label" for="button1id">Ready to send?</label>
<div class="col-md-8">
<button id="button1id" name="button1id" class="btn btn-primary" onclick="clickedSubmit()">Yes, send the email</button>
<button id="button2id" name="button2id" class="btn btn-default" onclick="google.script.host.close()">Cancel, do not send an email</button>
</div>
</div>

</fieldset>
</form>

<script>
function clickedSubmit() {
if(document.getElementById('shippingNeededYes').checked) {
var shippingNeeded = "Yes";
} else {
var shippingNeeded = "No";
}
var notes = document.getElementById('textarea');
google.script.run.readyToSendEmail(shippingNeeded, notes);
}
</script>

</body>
</html>

任何想法、想法或建议都值得赞赏!

最佳答案

我猜你有点困惑 js 函数在应用程序脚本中是如何工作的。

//This function will run when the spreadsheet is loaded
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom')
.addItem('Generate Shipping Email', 'menuItem1')
.addToUi();
}

//This entire function will run when you click on the menu item in the spreadsheet.
function menuItem1() {
var html = HtmlService.createHtmlOutputFromFile('index');
var ui = SpreadsheetApp.getUi()
ui.showModalDialog(html, 'Shipping Email Options');
}


//This is where you need to write the logic...this function will be called when you click on the button
function readyToSendEmail(shippingNeeded, notes) {

}

此外,最好将 type="button" 添加到两个按钮,否则它可以将其视为提交按钮。

关于javascript - Google Apps 脚本 - HTML 服务 - "wait"用于客户端表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45705505/

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