jeudi 21 juin 2018

Unable to get Checked CheckBox values in Google sheet using google apps script

I am fairly new to google apps script.

I have made an HTML Form, whose values are being posted to a Google Sheet.

All is working fine with text boxes.

When I used a CheckBox, the values being represented in the sheet is showing undefined if a check box is unchecked.

I don't want that to happen.

Here is the code i have used.

index.html

<!DOCTYPE html> <html>   <head>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <script>
      // Prevent forms from submitting.

      function preventFormSubmit() {
        var forms = document.querySelectorAll('form');
        for (var i = 0; i < forms.length; i++) {
          forms[i].addEventListener('submit', function(event) {
            event.preventDefault();
          });
        }
      }
      window.addEventListener('load', preventFormSubmit);

      function handleFormSubmit(formObject) {
        google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
      }
      function updateUrl(url) {
        var div = document.getElementById('output');
        div.innerHTML = url;
      }


    </script>   </head>   <body>   <center>   <table>
    <caption>SPAC KYC FORM</caption>
    <form id="myForm" onsubmit="handleFormSubmit(this)">   
      <tr><td>Form Filled By: *</td><td><input type="checkbox" name="id0" value="Dealer"></td></tr>
      <tr><td>Form Filled By: *</td><td><input type="checkbox" name="id1" value="Trader"></td></tr>
      <tr><td> <input type="submit" value="Submit"></td></tr>

    </form>
    </table>
    <div id="output"></div>    </center>
       </body> </html>

Code.gs

    function doGet() {
  return HtmlService.createHtmlOutputFromFile('index');
}
function processForm(formObject) {

     var form_filled_by = formObject.id0;
     var form_filled_by2 = formObject.id1;


  //Insert in Spreadsheet
  var SpreadsheetKey = "1D6gMFBiSJqZvPNJvo-HMG52ZIj4NUPR8wiNGCg91tAk";

  var sheet = SpreadsheetApp.openById(SpreadsheetKey).getActiveSheet();  

  var lastrow = sheet.getLastRow();
  var lastcolumn = sheet.getLastColumn();



  var targetrange0 = sheet.getRange(lastrow+1,1,1,1).setValues([[form_filled_by + ", " +form_filled_by2]]);

  var htmlbody1 =  "<html><body>Form Filled with Value:" + form_filled_by + ", " + form_filled_by2 + "</body></html>"; 

  //Display on Webpage
  return HtmlService.createHtmlOutput(htmlbody1).getContent();
  //return alert(htmlbody1);
}

Here is a look at the gsheet

I need that undefined is not displayed in sheet.

Please Help.

I got a little help from this link How to get values of all Checked checkboxes in Google App Script

but I am unable to apply it.




Aucun commentaire:

Enregistrer un commentaire