javascript - 如何从 HTML 模式对话框中获取函数返回
问题描述
下面的代码创建了一个带有 HTML 模式对话框的下拉列表。这段代码工作得很好,但我想修改它以通过第一个名为 ( getForm
return) 的函数从表单中获取提交的值。
例如:
var valueFromForm = getForm();
console.log("valueFromForm"); //Expected to print what the user selected on the form
代码现在如何工作:
getForm
使用 HTML 自定义对话框创建 HTML 服务sheetNames
从另一个电子表格中获取所有工作表名称,并使用 HTML 中的工作表名称构建一个下拉列表- 用户从下拉列表中选择并提交一个元素后,然后
myFunction
调用functionToRunOnFormSubmit
functionToRunOnFormSubmit
将提交的值从表单写入单元格。
代码.js:
function getForm() {
try {
var output = HtmlService.createHtmlOutputFromFile('HTML_Menu');
SpreadsheetApp.getUi().showModalDialog(output,'Select source');
}
catch(err) {
Logger.log(err);
}
}
function getSheets() {
try
{
var ss = SpreadsheetApp.openById("1tC95-rc1rXzM4VA87IPB_8FNPjQtP3zn9HfOBJtoC5U");
var names = sheetnms(ss);
return names;
}
catch(err){
Logger.log(err);
}
}
function functionToRunOnFormSubmit(fromInputForm) {
console.log(fromInputForm);
var ss = SpreadsheetApp.getActive();
ss.getSheetByName("test").getRange(1, 1).setValue(fromInputForm.Class);
return fromInputForm.Class;
};
function sheetnms(ss){return ss.getSheets().map(function(x) {return x.getName();});}
HTML_Menu.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
<select name="Class" id="class-selector" autofocus="autofocus" autocorrect="off" autocomplete="off">
</select>
<input type="submit" value="Submit" onclick="myFunction(this.parentNode)">
</form>
<p id="addDeck"></p>
<script>
(function () { google.script.run.withSuccessHandler(sheetNames).getSheets(); }());
function sheetNames(names) {
var select = document.getElementById("class-selector");
for( var i=0; i<names.length; i++ ) {
var option = document.createElement("option");
option.text = names[i];
select.add(option);
}
}
function myFunction(obj) { // Modified
var x = document.getElementById("class-selector").value;
document.getElementById("addDeck").innerHTML = x;
google.script.run // Modified
.withSuccessHandler(() => google.script.host.close())
.functionToRunOnFormSubmit(obj);
}
</script>
</body>
</html>
解决方案
推荐阅读
- perl - Perl -M 找不到模块的硬编码路径
- sql - 是否有任何优化的方法来编写 SQL 查询来查找两个数据集之间的差异?
- c# - 是否可以像加载 XML 文件一样使用 XDocument 的功能加载 DGML 文件?
- ios - 有没有捷径可以知道,当我运行我的应用程序时,我在哪个视图控制器中?
- java - Leadtools.RasterException:本机库 Ltkrnx.dll 已加载到另一个类加载器中
- c# - 为什么你不能运行一个任务并用另一种方法捕获它
- sql - 在sql中转换时间
- c++ - 如何分别在R和C++中做两个矩阵的矩阵乘积
- sql - rexp_like - 似乎无法理解它是如何工作的
- javascript - 与服务器的连接不断失败连接 ETIMEDOUT 94.195.191.104:27017