Google sheets allows you to write scripts in javascript, the same way excel lets you write VBA to automate tasks.
I'd like to make a cell in my google spreadsheet clickable, so that when it is clicked, my function will run.
I've found that it's possible to create custom buttons by inserting a drawing, but I'd really like this to be any time a particular cell is clicked, rather than a button which can move around and looks awkward in the sheet.
Is it possible?
Thanks for any advice.
Google sheets allows you to write scripts in javascript, the same way excel lets you write VBA to automate tasks.
I'd like to make a cell in my google spreadsheet clickable, so that when it is clicked, my function will run.
I've found that it's possible to create custom buttons by inserting a drawing, but I'd really like this to be any time a particular cell is clicked, rather than a button which can move around and looks awkward in the sheet.
Is it possible?
Thanks for any advice.
Share Improve this question asked Jul 13, 2014 at 21:50 johncorserjohncorser 9,84219 gold badges64 silver badges106 bronze badges 3- I don't think you can do that but what you can do is instruct the user to enter your formula/custom function into the cell as described at developers.google./apps-script/… . Similarly, you could have the cell pre-populated with the formula and simply ask the user to edit it and then press enter to run the function. – Mark Silverberg Commented Jul 13, 2014 at 21:53
- Thanks Skram, this seems to be the correct answer. If you add it as an answer, I would mark it accepted. Thanks! – johncorser Commented Jul 21, 2014 at 15:14
- 1 done (and hope it helped!) – Mark Silverberg Commented Jul 21, 2014 at 19:15
3 Answers
Reset to default 4There is not an onClick event for cells but what you can do is instruct the user to enter your formula/custom function into the cell as described at https://developers.google./apps-script/execution_custom_functions?hl=es#using.
Similarly, you could have the cell pre-populated with the formula and simply ask the user to edit it and then press enter to run the function.
What you are searching for, is onSelectionChange(), it runs automatically when a user changes the selection in a spreadsheet.
Try the following code and read the developers link.
/**
* The event handler triggered when the selection changes in the spreadsheet.
* @param {Event} e The onSelectionChange event.
*/
function onSelectionChange(e) {
// Set background to red if a single empty cell is selected.
var range = e.range;
if(range.getNumRows() === 1
&& range.getNumColumns() === 1
&& range.getCell(1, 1).getValue() === "") {
range.setBackground("red");
}
}
You can draw you own button and attach a sript to it. Edit it all transparent, and write no text.
Then place it on the cell you wish, resizing to fit inside the cell's area.
Add A Google Sheets Button To Run Scripts