0

I'm trying to figure out how to count the amount of times a group has clicked on a specific cell containing a cell.

I've gotten it to work with having a separate drawing next to it with the function attached to it, but I don't want to have a button needed for it.

function increment_t1() {
 SpreadsheetApp.getActiveSheet().getRange('F5').setValue(SpreadsheetApp.getActiveSheet().getRange('F5').getValue() + 1);
}

And this then pastes the result into column F5 for how many times the button has been pressed.

Is there any way to attach the script to either a cell that contains a text that can be copied normally, or a checkbox that you can check next to it that resets to false every time it's clicked but counts the clicks?

1

2 Answers 2

1

You might use onSelectionChange simple trigger together with a custom cell value (or another cell characteristic) to identify when certain part of your script should be executed. Here is an example:

/**
 * Increment the value of F5 every time a cell having webapplications
 * is selected
 */
function onSelectionChange(e){
  if(e.range.getValue() === 'webapplications'){
     const target =e.range.getSheet().getRange('F5');
     target.setValue(target.getValue() + 1);
  }
}

The only way to directly attach a script to a cell is by using it in a formula. This kind of scripts are called custom function. Unfortunately custom functions have limitations being of them that they can't use methods like setValue.

You might use triggers an event objects. There are two types, simple and installable. For this case, the trigger that is closer to what you are lookin is onSelectionChange. It's a simple trigger triggered when selecting one cell or multiple cells, it has an event object that including the range property that can be used to get information about the selected range like the cell value, but you could also add a note, the cell formatting, etc.

0

Is there any way to attach the script to either a cell that contains a text that can be copied normally

Google Sheets does not have that functionality. Apps Script functions can only be directly attached to custom menu items, drawings, images and triggers.

checkbox that you can check next to it that resets to false everytime it's clicked but counts the clicks?

That is fairly easy to do with the onEdit(e) simple trigger.

See the checkboxButtons_(e) script for sample code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.