Posted 8 July 2025, 12:35 pm EST - Updated 8 July 2025, 12:47 pm EST
Hi Mescius Support Team,
I’m working with SpreadJS and TypeScript, and I’m trying to get all the named ranges defined at the workbook level, not just from individual worksheets.
Specifically, I need to:
- Get the name of each named range.
- Retrieve the corresponding range reference, ideally in A1-style format (e.g.,
).$C$25:$H$27
I wrote the following code to extract the info from
getCustomNames()
and manually calculate the cell reference:
function getWorkbookNameInfos() : WorkbookNameRangeInfo[] | null {
if(!_workbook ){
return null;
}
const nameInfos = _workbook.getCustomNames();
const workbookNameInfos: WorkbookNameRangeInfo[] = nameInfos.map((nameInfo: GC.Spread.Sheets.NameInfo) => {
const rangeExpression = nameInfo.getExpression();
return {
//@ts-ignore
sheetName: rangeExpression.source.getName(),
//@ts-ignore
rangeName: nameInfo.getName(),
//@ts-ignore
dimensions: {rows: rangeExpression.getRange().rowCount, columns: rangeExpression.getRange().colCount},
//@ts-ignore
startRow: rangeExpression.row,
//@ts-ignore
startColumn: rangeExpression.column,
//@ts-ignore
endRow: rangeExpression.endRow,
//@ts-ignore
endColumn: rangeExpression.endColumn,
//@ts-ignore
startRowRelative: rangeExpression.rowRelative,
//@ts-ignore
startColumnRelative: rangeExpression.columnRelative,
//@ts-ignore
endRowRelative: rangeExpression.endRowRelative,
//@ts-ignore
endColumnRelative: rangeExpression.endColumnRelative,
//@ts-ignore
cells: calculatesWorkbookCellRange(
rangeExpression.row,
rangeExpression.column,
rangeExpression.endRow,
rangeExpression.endColumn,
rangeExpression.rowRelative,
rangeExpression.columnRelative,
rangeExpression.endRowRelative,
rangeExpression.endColumnRelative
),
};
});
return workbookNameInfos;
}
function calculatesWorkbookCellRange(startRow: number, startColumn: number, endRow: number, endColumn: number, startRowRelative: boolean, startColumnRelative: boolean, endRowRelative: boolean, endColumnRelative: boolean): string {
if(!_workbook ){
return '';
}
function getColumnLetter(col: number): string {
let letter = '';
col += 1;
while (col > 0) {
let rem = (col - 1) % 26;
letter = String.fromCharCode(65 + rem) + letter;
col = Math.floor((col - 1) / 26);
}
return letter;
}
const startCell = `${getColumnLetter(startColumn)}${startRow + 1}`;
const endCell = `${getColumnLetter(endColumn)}${endRow + 1}`;
return startCell === endCell ? startCell : `${startCell}:${endCell}`;
}
This approach works, but I had to use a lot of
@ts-ignore
due to some internal properties not being exposed in the typings. I’m also calculating the A1-style range string manually.
My question:
Is there an official or built-in method in SpreadJS to directly get the A1-style range reference from a named range or expression, without having to manually calculate it?
And is there a TypeScript-safe way to access these properties?
Thanks again for the support!
Best regards,
Luis