1

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> 
Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
Mr X
  • 1,637
  • 3
  • 29
  • 55
  • I think it should be `formValue.(user+g)`. – rpm Nov 19 '14 at 21:04
  • @user1989 tried this Execution failed: TypeError: [object Object] is not an XML object. (line 20, file "Code") [0.002 seconds total runtime] – Mr X Nov 19 '14 at 21:17
  • 1
    My bad. As per my knowledge, you can not access the JSON data as you are trying to do. In your code, you are trying to access user1, user2 etc by this, `formValue.user+g`, line, but every time it will try to look for the value of `user` variable, but not for user1, user2 and so on. For the current scenario in your current code, you will have to iterate over JSON data to get the value of your user1, user2 and so on. – rpm Nov 21 '14 at 01:44
  • 1
    Or you can do in this way, `var k = "user"+g;formValue[k];` – rpm Nov 21 '14 at 02:02
  • Your code is using a `count` property, but in JavaScript to get the length of an array, use the `length` property: `var count = formValue.length;` – Alan Wells Nov 23 '14 at 15:31
  • did you solve this issue @atjoshi ? – Harish Kumar Dec 26 '15 at 08:46

1 Answers1

0

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.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152