如何使用Google App脚本在JavaScript中传递数组

时间:2018-07-23 00:34:31

标签: javascript google-apps-script

如何将多个选项中的值传递给Code.gs。我无法弄清楚它是如何工作的,但是如果有逻辑需要输入,请查看我的代码。

HTML

<select class="js-example-basic-multiple col-md-2 btn btn-default" data-placeholder="Choose Month" name="select_month" id="select_month"
    multiple="multiple">
    <option value=""></option>
    <option value="0" style="color:black;">January</option>
    <option value="1">Febuary</option>
    <option value="2">March</option>
    <option value="3">April</option>
</select>

脚本

var getArrayMonth = $('#select_month').val();
google.script.run.getArr(getArrayMonth);

CODE.gs

function getArr(getArrayMonth) {
    var arrMonth = new Array(getArrayMonth);
    var length = arrMonth.length();
    Logger.log(length);
}

1 个答案:

答案 0 :(得分:3)

您的方法是正确的。看看我会做什么;

脚本

var selectbox = document.getElementById("select_month");
var getArrayMonth = getSelectValues(selectbox);// to get the selected multiple items as an array
google.script.run.getArr(getArrayMonth);

    function getSelectValues(select) {
      var result = [];
      var options = select && select.options;
      var opt;

      for (var i=0, iLen=options.length; i<iLen; i++) {
        opt = options[i];

        if (opt.selected) {
          result.push(opt.value || opt.text);
        }
      }
      return result;
  }

CODE.gs

function getArr(getArrayMonth) {
   Logger.log(getArrayMonth);
   //var arrMonth = new Array(getArrayMonth);
   var length = getArrayMonth.length;
   Logger.log(length);
}