Checkbox

110 views
Skip to first unread message

gilbert_RGI

unread,
Dec 28, 2019, 9:45:12 AM12/28/19
to Google Apps Script Community

Hello experts

The code below allows me to have a dialog with chekboxes, when I click on a box the small text below clearly indicates which box is checked, I would like to have another text when I uncheck a box previously checked.
How can I do.
Thank you for your collaboration

Gilbert

function caseacocherOnADialog() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var Nm = ["Texte ","Formule ","Nombre","Booléen"];
  var html='<html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"><script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script></head>';
  for(var i=0;i<4;i++) {
  html+=Utilities.formatString('<br /><input type="checkbox" name="rgroup" id="%s" value="%s" onChange="getSelected();" />%s','id'+ Number(i+1),'A' + Number(i+1),Nm[i],+ Number(i+1),'');
  }
  html+='<body><br /><br /><input type="button" value="Fermé" onClick="google.script.host.close()" /><div id="msgdiv"></div>';
  html+='<script>function getSelected(){ var selected=document.querySelector(\'input[name="rgroup"]:checked\').value;document.getElementById("msgdiv").innerHTML="<br />Vous avez selectionné " + selected  + ".<br />";}</script></body></html>'; 
  var ui=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(ui, 'Dialogue Checkbox');
}


gilbert_RGI

unread,
Jan 6, 2020, 3:51:42 AM1/6/20
to Google Apps Script Community
a little up There is no one to give me a hand I would like to know how to trigger an event at the click thank you so much

Steve Webster

unread,
Jan 6, 2020, 8:55:56 AM1/6/20
to google-apps-sc...@googlegroups.com
Does this help?


Kind Regards,

Steve Webster
SW gApps LLC, President 
Google Product Expert in: Google Apps Script, Drive, and Docs 
Google Vendor (2012-2013) || Google Apps Developer Blog Guest Blogger 
Add-ons: Text gBlaster and Remove Blank Rows


On Mon, Jan 6, 2020 at 3:51 AM gilbert_RGI <gilbe...@gmail.com> wrote:
a little up There is no one to give me a hand I would like to know how to trigger an event at the click thank you so much

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/da5b80d2-bed1-4fc2-80a9-83ff054786d1%40googlegroups.com.

gilbert_RGI

unread,
Jan 7, 2020, 7:46:50 AM1/7/20
to Google Apps Script Community

thank you so much but it doesn't help me, I can't adapt this code in mine Do you have another solution

Alan Wells

unread,
Jan 7, 2020, 11:00:21 AM1/7/20
to Google Apps Script Community
When posting a question, you don't need to address the group as experts, or thank anyone in advance.  And this is not a collaboration.  People may or may not provide help, depending upon what they want to do.  It's better to simply state your issue/need and provide good information.

I have come up with a solution to your need.  The solution begins with passing the "this" key word to the function, which, in this case, is the check box html element.  You will seein the function, that the code tests for whether the box is checked or not, and creates an appropriate message.

onChange="getSelected(this);

Because the checkbox element is being passed to the function, the query is not needed:

var selected = document.querySelector('input[name="rgroup"]:checked').value;

The above line of code has been removed.

I seperated out the HTML into it's own file.  It makes the code easier to read and maintain.

Create a new file named "H_MyDlg" and put the following html into it.

<!DOCTYPE html>

<html>
 
<head>
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
   
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
 
</head>

 
 
<body>
   
<br/>
   
<br/>
   
    999_Replace_Me_999
   
   
<br/>
   
<br/>

   
   
<input type="button" value="Fermé" onClick="google.script.host.close()" />
   
   
<div id="msgdiv"></div>

   
   
<script>
     
function getSelected(chkElmt){
       
var isChecked,msg,value;
       
        console
.log(chkElmt.checked)
       
        isChecked
= chkElmt.checked;
        value
= chkElmt.value;
       
       
if (isChecked) {
          msg
= 'Box ' + value + ' was just checked ';
         
       
} else {
          msg
= 'Box ' + value + ' was just UN checked ';
       
}
       
       
//var selected = document.querySelector('input[name="rgroup"]:checked').value;
        msg
= '<br />Vous avez selectionné ' + msg  + '.<br />';
        document
.getElementById("msgdiv").innerHTML = msg;
     
}
     
   
</script>
   
 
</body>
</html>



Change your gs code to the following:

function caseacocherOnADialog() {
 
var inputsHtml;
 
 
var ss = SpreadsheetApp.getActive();
 
var sh = ss.getActiveSheet();

 
 
var Nm = ["Texte ","Formule ","Nombre","Booléen"];

 
 
var html = HtmlService.createHtmlOutputFromFile("H_MyDlg").getContent();
 
 
//Logger.log('html: ' + html)
 
//Logger.log('html: ' + typeof html)
 
  inputsHtml
= "";

 
 
for(var i=0;i<4;i++) {

    inputsHtml
+= Utilities.formatString('<br /><input type="checkbox" name="rgroup" id="%s" value="%s" onChange="getSelected(this);" />%s','id'+ Number(i+1),'A' + Number(i+1),Nm[i],+ Number(i+1),'');
 
}
 
  html
= html.replace('999_Replace_Me_999',inputsHtml);
 
 
//Logger.log('html: ' + html)
 
 
var ui = HtmlService.createHtmlOutput(html);
 
 
SpreadsheetApp.getUi().showModelessDialog(ui, 'Dialogue Checkbox');
}

gilbert_RGI

unread,
Jan 8, 2020, 5:39:15 AM1/8/20
to Google Apps Script Community
aj.addons

Impeccable that's exactly what I wanted


thank you for helping me

Gilbert

Alan Wells

unread,
Jan 8, 2020, 8:45:56 AM1/8/20
to Google Apps Script Community
Thank you for letting me know.  I'm glad it it helped you.
Reply all
Reply to author
Forward
0 new messages