0

I have been using a script like this

function incrementB2(){
    SpreadsheetApp.getActiveSheet().getRange('B2').setValue(SpreadsheetApp.getActiveSheet().getRange('B2').getValue() + 1);
}

I was hoping to be able to have the code grab the location/cell it is in and use that location or to be able to send a variable through the assign code such as.

function increment(CellName){
    SpreadsheetApp.getActiveSheet().getRange(CellName).setValue(SpreadsheetApp.getActiveSheet().getRange(CellName).getValue() + 1);
}

and then assign increment("B2") so that I can use the same script for multiple cells. Are either of these methods possible or is there some other way to not have to create incrementB2, incrementB3, incrementB4 functions?

Khoa Nguyen
  • 1,319
  • 7
  • 21

2 Answers2

1

It sounds like you want to use the function as a custom cell function and custom functions cannot change the value of another cell and they cannot determine what cell they are in. If that's not the case then it looks like you already have what you want as long as the CellName is A1Notation.

Custom Functions

Cooper
  • 59,616
  • 6
  • 23
  • 54
1

Unfortunately the "onclick" trigger that executes a Google Apps Script function assigned to a drawing hasn't an event object , so what you are looking is not possible.

One option is to use a checkbox an on edit simple or installable trigger.

Related

Resources

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    Oh I see what he was asking now. I wanted to pass a parameter through a menu or drawing. Sometimes I find it far to easy to completely miss the point. – Cooper May 24 '21 at 12:39