首页 > 解决方案 > 函数 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()”

提前致谢

标签: formsgoogle-apps-scriptgoogle-sheets

解决方案


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]);
  }
}

推荐阅读