[我几乎有一个HTML表单选择器,它会根据来自Google表格列的数据创建一个数组(完成),然后将该数据填充到一个下拉框(完成)。
我整个周末一直在尝试找出如何捕获下拉选项的选择,然后将其汇总到变量中。
这是启动HTML弹出窗口的Google脚本代码:
function ClientDropDownHTML() {
var template = HtmlService.createTemplateFromFile('index');
var htmlDlg = template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME)
// var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// var lastRow = sheet.getLastRow();
// var myArray = sheet.getRange('A2:A' + lastRow).getValues();
.setWidth(500)
.setHeight(150);
SpreadsheetApp.getUi()
.showModalDialog(htmlDlg, 'Select An Existing Client');
}
HTML下拉列表运行。这是我的HTML表单的代码,该代码在Google表格数据上运行数组并将其填充为选择器:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="myForm">
<select id="selectClient">
<option>Choose an Existing Client</option>
<? var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); ?>
<? var clientindex = sheet.getRange('T2').getValue(); ?>
<? var myArray = sheet.getRange('V2:V' + clientindex).getValues(); ?>
<? for (var i = 0; i < myArray.length; ++i) { ?>
<option> <?=myArray[i]?> </option>
<? } ?>
</select>
</form>
</body>
</html>
这是我被困住的地方。作为后盾,我需要确保可以捕获选择,因此我尝试将其记录-我尝试了类似此处的操作:https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement#Example
我最终在HTML上显示以下内容:
<? var select = document.getElementById('selectClient'); ?>
<? console.log(select.selectedIndex); ?>
<? console.log(select.options[select.selectedIndex].value) ?>
但是,如果执行此操作,只会出现“未定义文档”错误。
我已经在本网站上阅读了不同的问题,所有解决方案似乎都不适合我。
总之,我需要一种方法:
参考用户从HTML中的选择将其存储在变量中能够将该变量移回Google表格。
非常感谢聪明的编码人员:)
从选择标签中进行选择
GS:
function getClientList() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet15');
var vA=sh.getRange(2,1,sh.getLastRow()-1,1).getValues();
return vA;
}
function selectClient(client) {
SpreadsheetApp.getUi().alert('Client is ' + client);
}
function runTwo() {
var ui=HtmlService.createHtmlOutputFromFile('ah2');
SpreadsheetApp.getUi().showModelessDialog(ui, 'Select Client')
}
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<form id="myForm">
<select id="sel1" onchange="selClient()"></select>
<script>
$(function(){
google.script.run
.withSuccessHandler(function(vA){
updateSelect(vA);
})
.getClientList();
});
function updateSelect(vA,id){
var id=id || 'sel1';
var select = document.getElementById(id);
select.options.length = 0;
for(var i=0;i<vA.length;i++)
{
select.options[i] = new Option(vA[i],vA[i]);
}
}
function selClient() {
google.script.run.selectClient($('#sel1').val());
}
</script>
</form>
</body>
</html>