Skip to content Skip to sidebar Skip to footer

Pass Html Table To Google Spreadsheet. Get Cell Values Out Of A Dynamic Table

I have a table within a form that looks like this: The HTML and JavaScript This table needs to be put on a spreadsheet, the gs file is: //getValuesFromForm function getValuesFrom

Solution 1:

I would get the data out of the table, in the browser, from the client side using DOM methods and properties. This code processes your table before it is sent to the server side gs code. It's dynamic, so it doesn't matter what the table length or row length is. And you don't need to name your inputs with names or ids in order to reference them. This code has two loops in it. One FOR loop nested inside the other. The code goes through each row in the table, and for each row, gets all the inputs in that row. Then it creates a two dimensional array to send to the gs code. You might ask, "why not do the processing in the gs code?" Processing the data in the client side allows you to use the method getElementsByTagName(). You can't do that with server side code.

You could work with the FORM object in either the client side or server side JavaScript as an alternative, but there is a lot of data in the Form Object, and it's difficult to figure out and understand how the data in that Form Object is structured.

The end result of this code is an array of cell values inside another array.

[ [Row1_Cell1, Row1_Cell2], [Row2_Cell1, Row2_Cell2], etc ]

You will also need a nested FOR loop in the gs code. The FOR loops will eliminate the need to hard code lots of variable names for each row and cell.

Client side JavaScript

<scripttype="text/javascript">functionformSubmit() {
    console.log('formSubmit ran');

    var allTableRows = document.getElementsByTagName("tr");
    console.log('allTableRows: ' + allTableRows);

    var numbrOfRows = allTableRows.length;
    console.log('numbrOfRows: ' + numbrOfRows);

    var thisCellValue = "";

    var arryMaster = [];
    var arryOfTableRowVals = [];
    var thisRowData = "";

    for (var i = 1; i < numbrOfRows; i++) {// START with i = 1console.log('row count: ' + i);

      thisRowData = allTableRows[i].getElementsByTagName('input');
      console.log('thisRowData: ' + thisRowData);
      console.log('thisRowData.length: ' + thisRowData.length);

      arryOfTableRowVals = []; //reset the arrayfor (var j = 0; j < thisRowData.length; j++) {
        console.log('---- count j: ' + j);
        thisCellValue = thisRowData[j].value;

        arryOfTableRowVals.push(thisCellValue);

        console.log('---- arryOfTableRowVals: ' + arryOfTableRowVals[j]);
      };
      arryMaster.push(arryOfTableRowVals);
    }

    console.log(arryMaster[2][2]);
    google.script.run.getValuesFromForm(arryMaster);

  }
</script>

I put lots of console.log() statements in the code so that you can see the results of the code in your browsers console. The results will print to the console. You might want to comment those out, or delete them.

Post a Comment for "Pass Html Table To Google Spreadsheet. Get Cell Values Out Of A Dynamic Table"