获取多个选择值,然后使用Google App脚本将值发送到Google工作表

时间:2019-07-09 14:07:10

标签: google-apps-script

我在Google工作表中有一个列表,可以选择作为html中的选项。选择设置为多个。如何将所选值放在Google工作表的单个单元格中?非常感谢你,我非常需要这个。我是菜鸟程序员。

对于html:

<div class="row"> 
<div class="input-field col s4">
<select id="posApp" multiple="multiple"> 
<option value="" disabled>Choose your Position</option>
<?!= list2; ?>
</select> 
<span id="posResult">Select Position</span> 
<label></label>
</div> <!-- CLOSE TIME FIELD --> 
</div> <!-- CLOSE ROW -->

用于Google脚本(从Google表格中创建选择列表):

function doGet(e){

  var ss = SpreadsheetApp.openByUrl(url);
  var ws2 = ss.getSheetByName("Pos_App");
  var list2 = ws2.getRange(1,1,ws2.getRange("A1").getDataRegion().getLastRow(),1).getValues();
  var htmlListArray2 = list2.map(function(r){return '<option>' + r[0] + '</option>'; }).join('');

  var tmp = HtmlService.createTemplateFromFile("page");
  tmp.list2 = htmlListArray2;
  return tmp.evaluate();

}

对于使用从Materialize CSS中进行选择的初始化的javascript:

<script>
     document.addEventListener('DOMContentLoaded', function() {
       var timeSelect = document.querySelectorAll('select');
       //timeSelect.onChange = selectThem;
       var instances = M.FormSelect.init(timeSelect);


       function selectThem(){
       var selectedOne = instances.getSelectedValues();
       }

       //var instance = M.FormSelect.getSelectedValues();

     });
</script>

1 个答案:

答案 0 :(得分:1)

尝试一下:

html:

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <select id="posApp" multiple="multiple"> <option value="" disabled>Choose your Position</option>

code.gs

function getSelectOptions()
{
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Options');
  var rg=sh.getRange('A1');
  return rg.getValue().split(',');
}

javascript

function updateSelect(vA){
  var select = document.getElementById("posapp"); 
  for(var i=0;i<vA.length;i++)
  {
    select.options[i] = new Option(vA[i],vA[i]);
  }
}

$(function() {
        google.script.run
          .withSuccessHandler(updateSelect)
          .getSelectOptions();
      });