I have this function written in VBA that I would like to convert to Apps Script, but I have not found anything similar to declare a variable of static type to keep the range object saved in the rngLast variable with multiple executions of function onSelectionChange (e):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static rngLast As Range
If Not Application.Intersect(Target, Range("A1:Z100")) Is Nothing Then
If Not rngLast Is Nothing Then
If rngLast.Address <> Target.Address Then
MsgBox "Lost focus " & rngLast.Address
End If
End If
MsgBox "Got focus " & Target.Address
Set rngLast = Target
End If
End Sub
In Apps Script I wrote it like this:
var rngLast;
function onSelectionChange(e) {
var spreadsheet = SpreadsheetApp.getActive();
var sh = spreadsheet.getActiveSheet();
var rowActCell = e.range.getRow();
var colActCell = e.range.getColumn();
if((rowActCell >=1 && rowActCell <=100) && (colActCell >=1 && colActCell <= 26)) {
if(rngLast != undefined || rngLast != null) {
if(rngLast.getA1Notation() != e.range.getA1Notation()) {
Browser.msgBox("Lost focus " + rngLast.getA1Notation());
}
}
Browser.msgBox("Got focus " + e.range.getA1Notation());
rngLast = e.range
}
}
but I can't keep the value of e.range saved in rngLast so it doesn't work. In practice, every time onSelectionChange (e) is activated by changing the selection cell, rngLast assumes the undefined state. To test how it works I replaced: var rngLast; with var rngLast = SpreadsheetApp.getActive().getActiveSheet().getRange("A1");
I hope with my bad English that I have explained myself ;-)
Thanks bye Gabriele