I need to do the same operations on different cells and I am trying to write a generic script. I give a trivial example that explains my problem below.
Let's say that I want to click on a button and decrease the value of the cell A1 by 1. That's straightforward. I define the following function
function dec_cell_def(){
var cell = fr = SpreadsheetApp.getActiveSheet().getRange("A1")
cell.setValue(cell.getValue() - 1)
}
Then assign the "script" dec_cell_def
to any button and I am done.
Let's say now, that I want to do this for many different cells. I could of course write a function for each one, but this is hardly a solution. So I thought the following would work. I define the function cell_dec
that take as an argument the label of a cell and returns a function that decreases the value of the cell:
function dec_cell(label){
() => {
var cell = fr = SpreadsheetApp.getActiveSheet().getRange(label)
cell.setValue(cell.getValue() - 1)
}
}
However when I assign the "script" dec_cell("A1")
to a button, I am told that the script does not exist. What is the proper way to do that?
PS. I know that I can get the selected cell and act on it, but I would like to have a fixed target for my function.
Edit: I meant that I want to do this for few cells but not all at once. I would like to create a button for each cell and I would like to avoid copy-pasting the same code. I think that none of the suggested questions answer that.