Ido Segal
Ido Segal

Reputation: 422

Google Sheets using JavaScript functions and check box

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 ?

Upvotes: 2

Views: 2188

Answers (2)

Toribio
Toribio

Reputation: 4078

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
    }
  }
}

Upvotes: 2

Adam Frank
Adam Frank

Reputation: 105

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).

Upvotes: 1

Related Questions