Mask
Mask

Reputation: 573

Auto check the checkbox on meeting certain condition

Is there a way to auto check the checkbox present in html on meeting certain condition from Google spreadsheet.

I have a JavaScript that matches a cell and if its value is 1 or any defined condition then "name1" checkbox should get automatically checked.

This is my file named 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 match1 = sheet.getRange("B2").getValue();

   if(match1 === "1")
   {
    autocheck the checkbox "name1"; //cant figure out what to use here
   }       
Logger.log(form);

}

My Index.html File looks like this:-

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>

<form id='myForm'>
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
        <tr>
            <td></td>
            <td><input type="checkbox" name="name1" id="name1" value="1"/>xyz</td>
            <td><input type="checkbox" name="name2" id="name2"/>abc</td>
            <td><input type="checkbox" name="name3" id="name3"/>qwe</td>
            <td><input type="checkbox" name="name4" id="name4"/>zxc</td>
         </tr>
    </tbody>
</table>
<br><br>
  <input type="button" value="Submit" onclick="google.script.run
          .withSuccessHandler(google.script.host.close)
          .itemAdd(this.parentNode)" />
</form>
</html>

Upvotes: 2

Views: 498

Answers (1)

Ritesh Nair
Ritesh Nair

Reputation: 3355

You can't check the checkbox from code.gs alone. Please refer the code below.

code.gs

function getValues(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var match1 = sheet.getRange("B2").getValue();
  return match1;
}

index.html --- add the below script in html page

<script>
  google.script.run.withSuccessHandler(checkDefault).getValues();

  function checkDefault(val){
    var checkBoxName = "name"+val;
    document.getElementById(checkBoxName).checked = true;
  }

</script>

Upvotes: 3

Related Questions