I want to use the "check box" operation of google sheets, and when the check box is true, then call to a function. Someone can help me with that ?
-
What do you mean by Check Box? As in an input field checkbox?Luay– Luay2018-11-22 20:44:12 +00:00Commented Nov 22, 2018 at 20:44
-
A new feature that enable to create a clickable check box that return true or false on a click. I want to associate the check box to a function.Ido Segal– Ido Segal2018-11-22 20:48:52 +00:00Commented Nov 22, 2018 at 20:48
-
Are you trying to do this with HTML and Javascript? The google-sheets part is confusing me, does it need to relate to google-sheets or do you want an example of check boxes?Luay– Luay2018-11-22 20:50:44 +00:00Commented Nov 22, 2018 at 20:50
2 Answers
Supposing your checkbox is at A1, you could use this script to test when it's checked or unchecked (modified):
function onEdit(e) {
var range = e.range;
if (range.getA1Notation() == 'A1') {
var value = range.getValue();
range.setNote('Changed on ' + new Date() + ' to ' + range.getValue());
if (typeof value === 'boolean' && value == true) {
// Do something or call a function when the tick box is checked
}
}
}
Comments
While I'm unsure of exactly what you want to do in javascript, the Google Script editor may be useful for you.
In a Google Sheet, go to Tools > Script Editor. It should open a new page and create a new project with a blank function. This is where you can make new functions to be run within Google Sheets.
As an example I made a function called getSum:
function getSum(a,b) {
return a+b;
}
If you save this script and go back to Sheets, you can do =getSum(1,2) and it will return 3
If you wanted to integrate this with a Tick Box, you could do =IF(A1,getSum(1,2),getSum(2,2))
In this case, when the tick box is checked, it will run the first statement, and return 3 , when the box is unchecked, it will return 4
I'm not entirely sure on what you are trying to achieve with JavaScript, but this is one way to introduce custom functions (using Google Script).