我有下面的代码;
。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]);
}
}
好,我尝试了以下代码,它们起作用了:
。html
<? var innerHTML= createInnerHTML1(); ?>
<select name="List1" required="">
<option value=""></option>
<? innerHTML.forEach(function(option) { ?>
<option value="<?= option.value ?>"><?= option.text ?></option>
<? }); ?>
</select>
<? var innerHTML= createInnerHTML2(); ?>
<select name="List2" required="">
<option value=""></option>
<? innerHTML.forEach(function(option) { ?>
<option value="<?= option.value ?>"><?= option.text ?></option>
<? }); ?>
</select>
。gs
function createInnerHTML1() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("sheet name");
var namesValues = names.getRange(2,14,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;
}
function createInnerHTML2() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("sheet name");
var namesValues = names.getRange(2,11,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;
}
依此类推,您可以在.gs文件中重复相同的功能,而.html文件中会有很多输入。再次感谢库珀