userHow to collect the values from html form for variable input values in google app script?

980 views Asked by At

I am creating the input fields with the add more feature which are then sent to the google script file, how can I collect the values of the fields as shown in code I implemented below..

google app script file

code.gs

function getFormValue(formValue) {
  var myarr= {};
  var count = formValue.count;
  for(var g = 1; g<=count; g++ )
  {
    user["I"+g] = formValue.user+g; // error, what to do here 
  }
 // code

}

index.html

    <script>
    $(document).ready(function() {
            var counter = 2;
            $("#addMoreUser").click(function() {
                if (counter > 7) {
                    alert("Only 7 Users are allowed");
                    return false;
                }
                var newRowDiv = $(document.createElement('div'))
                    .attr("id", 'rowDiv' + counter);
                newRowDiv.after().html('<div class="row" id="rowDiv" ><div class="col-md-3"><input class="form-control" placeholder="user'+ counter +' " name="user'+ counter +'" id="user'+ counter +'" type="text" value=""></div></div>');
                newRowDiv.appendTo("#rowDivGroup");
                $("#count").val(counter);
                counter++;
            });

      $( "#submitForm" ).submit(function() {
        google.script.run.withSuccessHandler(function(ret){
          console.log(ret);
        }).getFormValue(this); //"this" is the form element
      });
        });

    </script

        <form class="contact-form" id="myform">
          <input type="hidden" value="1" name="count" id="count"> 
          <div id="rowDivGroup">                            
          <div class="row" id="rowDiv">
          <div class="col-md-3">
           <input class="form-control" placeholder="Name of User" name="user1" id="user1" type="text" value=""></div></div></div>
        <a class="btn btn-sm btn-flat btn-success btn-rounded" id="addMoreUser">Add More Users</a>
        <input type="submit" class="btn btn-flat flat-color btn-rounded btn-sm" id="submitForm" value="Submit Details "> 
        </form> 
1

There are 1 answers

0
Alan Wells On

This code takes the value of an array, and creates an object:

function getFormValue(formValue) {
  formValue = ["someInput1", "someInput2", "someInput3", "someInput4", "someInput5", "someInput6", "someInput7"];
  Logger.log('formValue: ' + formValue);

  var myarr= {};
  var count = formValue.length;

  Logger.log('count: ' + count);

  var user = {};

  for(var g = 1; g<=count; g++ )
  {
    Logger.log('g: ' + g);
    var k = "user"+g;

    var userID = "I" + g;
    Logger.log("userID: " + userID);
    Logger.log("formValue: " + formValue[g-1]);

    user[userID] = formValue[g-1];
    Logger.log("The property value: " + user[userID]);
  }

}

I've run the code, and it works. The values for the array are hard coded for testing purposes.