I've been working on this all morning and seeing evidence of what the above non-answers discuss. Senseful and I both want the ADDRESS of the passed cell, not the value. And the answer is very easy. It cannot be done.
I found some workarounds that rely on figuring out which cell contains the formula. It is difficult to say if this would have helped Senseful above. So what was I doing?
The data.
[A] [B] [C] [D] [E] [F] [H]
[1] Name Wins Losses Shots Points Fouls Most recent
WL Ratio
[2] Sophia 4 2 15 7 1 0
[3] Gloria 11 3 11 6 0 0
[4] Rene 2 0 4 0 0 0
[5] Sophia 7 4 18 9 1 1.5
Column H is Sophia's (Wins - PrevWins) / (Losses - PrevLosses)
(7 - 4) / (4 - 2) = 1.5
But we do not know what row Sophia previously appeared in.
This can all be done using VLOOKUP if you hard-code A as the name column.
After VLOOKUP, I was getting some #NA (name not found) and #DIV0 (denominator zero) and wrapped it with =IF(IF(...)) to show more palatable text in these conditions.
Now I had a monstrously large expression which was unwieldy and un-maintainable. So I wanted macro expansion (doesn't exist), or custom functions.
But when I made a helper SubtractPrevValue(cell), it was receiving "7" instead of B5.
There is no built-in way to get cell or range objects from the passed arguments.
If I make the user hand-enter the cell name in double quotes then I can do it...
SubtractPrevValue("B5"). But that really hamstrings copy/paste and relative cell features.
But then I found a workaround.
SpreadsheetApp.getActiveRange() IS THE CELL that contains the formula.
That is all I really needed to know. The row number.
The following function takes a NUMERIC column number and subtracts out the previous occurrence in that column.
function SubtractPrevValue(colNum)
{
var curCell = SpreadsheetApp.getActiveRange();
var curSheet = curCell.getSheet();
var curRowIdx = curCell.getRowIndex();
var name = curSheet.getRange(curRowIdx, 1).getValue(); // name to match
var curVal = curSheet.getRange(curRowIdx, colNum).getValue();
var foundRowIdx = -1;
for (var i=curRowIdx-1;i>1;i--)
{
if (curSheet.getRange(i, 2).getValue() == name)
{
return curVal - curSheet.getRange(i, colNum).getValue();
}
}
return curVal; //default if no previous found
}
But then I discovered that this is really REALLY slow. One and two second delays while it displays "Thinking..." So I'm back to the massively illegible, unmaintainable worksheet formula.