From other examples, I have a function that returns the name of a Named Range for the current cell - but only if the range consists of a single cell.
I started trying to parse the starting and ending Col and Row of the range, but ran into problems as soon as cols went to more than one character, eg "AA".
Then I thought that if I could get the cell's "R1C1" notation, I could use getCell() to test if it exists in the range. However, I can't find an equivalent function of getA1Notation() to get the R1C1 of the current cell. How is it obtained pls?
function c_GetCellNamedRange() {
const ui = SpreadsheetApp.getUi();
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const cell = sheet.getCurrentCell().getA1Notation();
ui.alert("Current Cell: " + cell);
//get all the name ranges of the spreadsheetsheet
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
// loop through all the names range of the sheet
for(i=0;i<namedRanges.length;i++){
/*
// this only works if range is a single cell! How to loop through?
if(namedRanges[i].getRange().getA1Notation() == A1Notation) {
rangeName = namedRanges[i].getName();
break;
}
*/
// get the current cell R1C1 notation, then use getCell() to test if
// it exists in the named range?
//var rv = namedRanges[i].getCell(cell.getRow(), cell.getColumn())
var rv = namedRanges[i].getCell(cell.getRowIndex(), cell.getColumn())
if (!rv ) {
rangeName = namedRanges[i].getName();
break;
}
}
if ( !rangeName ) rangeName = "none"
ui.alert("Current Cell: " + cell + " \r\n\r\n Named Range: " + rangeName);
}