I'm struggling to find a solution to return the current cell a function is being written in.
For instance, when defining a range I want to make the first and second parameters (integer row and integer column) be dynamic.
If the cell I'm currently calling the function in is cell H1, then the integer row has to be 1 and the integer column has to be 8. Moving the function would also move the row and column.
sheet.getActiveCell()
and sheet.getCurrentCell()
is just returning null.
In the first image above: Lines tagged as 1 are where the parameters need to be dynamic. Box number 2 is where I am trying to get the current cells value.
function dynamicRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var completePercentRangeStart = sheet.getActiveCell().getValues();
var range = sheet.getRange(1, 8, 3, 1);
return completePercentRangeStart;
}
It not make sense to use getValues()
on sheet.getActiveCell()
because the active cell is a single cell and getValues()
return an Array of Arrays (2D Array). Instead of it use getValue()
.
By the other hand, it not makes sense to return the value of the active cell using a custom function as it's some sort of circular reference.
Regarding
How to return the cell the function is being written in?
Assuming that this means the reference of the cell with a formula like =whoAmI()
function whoAmI() {
var sheet = SpreadsheetApp.getActiveSheet();
return sheet.getActiveCell().getA1Notation();
}
If H1 has the formula, it will display H1