forms - 函数 createInnerHTML - 如何与其他表单输入重复?
问题描述
我有以下代码;
.gs
function createInnerHTML() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("WELD DATE LOG");
var namesValues = names.getRange(2,13,names.getLastRow()-1).getValues();
var innerHTML = [];
for (var i=0;i<namesValues.length;i++){
innerHTML.push({value:''+ namesValues[i][0], text:namesValues[i][0]});
};
return innerHTML;
}
.html
<? var innerHTML= createInnerHTML(); ?>
<select name="JOINT" id="JOINT" aria-label="JOINT" aria-required="true" required="">
<option value=""></option>
<? innerHTML.forEach(function(option) { ?>
<option value="<?= option.value ?>"><?= option.text ?></option>
<? }); ?>
</select>
目的是通过电子表格中的一列更新表单选项,其中我有一些相关问题:
首先,如果单元格为空白,我需要从表单中删除该选项,因为它读取列表中的空白选项
例如,当用户打开表单中的选项列表时,它会给出 4 个选项,其中两个是空白的,两个是带值的,而不是只显示这两个值
其次,我如何在同一个脚本中重复相同的功能,但有其他表单问题,无论是选择还是复选框
例如,我有这个带有两个输入字段的 html 表单:
<? var innerHTML= createInnerHTML(); ?>
<select name="JOINT" id="JOINT" aria-label="JOINT" aria-required="true" required="">
<option value=""></option>
<? innerHTML.forEach(function(option) { ?>
<option value="<?= option.value ?>"><?= option.text ?></option>
<? }); ?>
</select>
<select name="WS" required>
<option value=""></option>
<option value="WS S01">WS S01</option>
<option value="WS S02">WS S02</option>
<option value="WS S03">WS S03</option>
<option value="WS S04">WS S04</option>
</select>
在 .gs 文件中,我需要使用两个表单输入重复提到的相同函数“function createInnerHTML()”
提前致谢
解决方案
if the cell is blank
如果单元格为空白,可以通过多种方式进行更正。您可以在 innerHTML 上使用 filter 方法,或者:
function createInnerHTML() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("WELD DATE LOG");
var namesValues = names.getRange(2,13,names.getLastRow()-1).getValues();
var innerHTML = [];
for (var i=0;i<namesValues.length;i++){
if(namesValues[i][0]) {
innerHTML.push({value:''+ namesValues[i][0], text:namesValues[i][0]});
}
};
return innerHTML;
}
how can I repeat the same function in the same script but with other form questions whether select or checkbox
您没有提供任何表单信息或电子表格数据,因此我们并不真正了解智能回答问题的具体细节。请提供其他信息和代码要求。
这就是我通常更新选择的方式:
GS:
function getSelectOptions() {
sortOptions();
var ss=SpreadsheetApp.openById(getGlobal('gSSID'));
var sh=ss.getSheetByName('Options');
var rg=sh.getDataRange();
var vA=rg.getValues();
var options=[];
for(var i=0;i<vA.length;i++)
{
options.push(vA[i][0]);
}
return vA;
}
function sortOptions() {
var ss=SpreadsheetApp.openById(getGlobal('gSSID'));
var sh=ss.getSheetByName('Options');
var rg=sh.getRange(2,1,sh.getLastRow()-1,1);
rg.sort({column:1,ascending:true});
}
js:
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]);
}
}
推荐阅读
- javascript - 如果RegExp中有量词,如何通过通用方法获取所有子环匹配?
- php - 使用内部连接和jeft连接时Yii2关系重复
- sql - 硬编码 ID 与查询
- javascript - 为什么在数字之前添加零会改变其值?
- javascript - 切换组件在 PartialView 中不起作用 - Asp.Net MVC
- file - SSIS 平面文件 csv 目标是 bcp csv 导出大小的两倍
- ruby-on-rails - 我可以在 rails db 中为特定字段添加时间戳吗?
- python - Matplotlib - 3D 图形只工作一次
- php - Facebook PHP API在登录时抛出异常
- python - 使用用户输入字符串中的不等式