1

I am trying to create variable if a checkbox is checked in a HTML form. But could not succeed to write it back.

Here is my complete HTML code "Index.html":-

<!DOCTYPE html> <html> <head> <base target="_top"> </head> <br> <h1>Add Row To Spreadsheet</h1><br /> <form id='myForm'> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td></td> <td><input type="checkbox" name="name1" id="name1"/>A</td> <td><input type="checkbox" name="name2" id="name2"/>B</td> <td><input type="checkbox" name="name3" id="name3"/>C</td> <td><input type="checkbox" name="name4" id="name4"/>D</td> </tr> <input type="button" value="Submit" onclick="google.script.run .withSuccessHandler(google.script.host.close) .itemAdd(this.parentNode)" /> </table> <br> </form> </html> 

Mentioned below is my "Code.gs:-

function openInputDialog() { var html = HtmlService.createHtmlOutputFromFile('Index').setSandboxMode(HtmlService.SandboxMode.IFRAME); SpreadsheetApp.getUi() .showModalDialog(html, 'Add Item'); } function itemAdd(form) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Sheet1"); var lastRow = sheet.getLastRow(); var match1 = sheet.getRange("B2").getValue(); var dataRange = sheet.getRange(1, 1,lastRow).getValues(); for(var i=0;i<dataRange.length;i++) { if(dataRange[i][0] == match1) { if(document.getElementById("name1").checked) { days="yes"; sheet.getRange(i+1, 2).setValue(days) } } } } 

Any Help is appreciated, I think html is complete but I am missing something in code.gs Kindly lt me know if you need any further clarification.

1 Answer 1

1

Your example should show your form element and submit button in order to verify that you are passing the form element correctly to your google apps script function.

itemAdd() is running on the server side, so it does not have access to DOM elements such as document.getElementById(). You need to access your value of interest from the form element.

Your checkbox elements need to have a value assigned to them

<input type="checkbox" name="name1" id="name1" value="name1val"/> 

Your call to itemAdd should pass the form element

.itemAdd(document.getElementById('myForm')); 

Once this is done, you should see your form values. You can inspect this by adding the following code.

Logger.log(form) 

I believe that the following will provide to access your value.

form["name1"] 
Sign up to request clarification or add additional context in comments.

5 Comments

How would i check on server side if the checkbox "name1" on html is checked.
The form object should contain a value if the checkbox has been selected. You may also need to add a value element to your checkbox elements. I recommend that you update your question to include the entire form element.
Ok I have just edited the complete code could you please check what I am missing.
and here is the link to my sheet if you would like to have a look.
Hello terrywb, its working but the submit button does not close the form its just passing the values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.