2

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);
}
Newbie
  • 75
  • 9
  • Why are you invoking the `Array` constructor? Just use the array literal syntax: `var x = [ /** whatever you are initializing it with */ ];` https://stackoverflow.com/q/1094723/9337071 – tehhowch Jul 23 '18 at 01:42
  • if ill make it that way inside the code.gs, then how i can make the value:` function getArray(getArrayMonth){}` into an array? – Newbie Jul 23 '18 at 02:07
  • With both methods your variable `arrMonth` **is** an `Array`. If you are getting some error message that makes you think it is not, you should include that very relevant detail in your question. I'll also point out that you likely want to access the `length` *property*, rather than invoke the (**non-existent**) `length` *function*. – tehhowch Jul 23 '18 at 03:14
  • Actually, i need an array for loop to get those months selected by the user to filter data based what months selected. for example, the user selected January and Ferbruary which is equivalent to (0 and 1), then the function `google.script.run.getArr(getArrayMonth)` which `getArrayMonth = 1,2`. So when it comes to code.gs. – Newbie Jul 23 '18 at 03:19
  • You need to provide more details in your question. You show code (good), but have yet to articulate what the problem you are encountering is (bad). Show the logs of what data is sent, what data is received, what you think each logged content should actually be, any error messages, etc. – tehhowch Jul 23 '18 at 03:45

1 Answers1

3

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);
}
iJay
  • 3,925
  • 5
  • 31
  • 62