Newbie
Newbie

Reputation: 95

How to pass an array in javascript using google app script

How i can pass a value from multiple option and make it an array into the Code.gs. I cant figure out how it will works, but see my code if there is a logic need to input.

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>

SCRIPT

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

Upvotes: 2

Views: 1271

Answers (1)

iJay
iJay

Reputation: 4273

Your approach is correct. See what I would do;

SCRIPT

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

Upvotes: 3

Related Questions