A defined name is an alias for a cell or a range, so a formula can say SUM(SalesData).
The Workbook holds them, and they round-trip through both reogrid-json and XLSX.
Defining a name
using unvell.ReoGrid.Core;
using unvell.ReoGrid.Core.Formula;
// workbook scope - the same name works from every sheet
wb.DefineName("TaxRate", "Sheet1!B1");
wb.DefineName("SalesData", "Sheet1!A2:C100", comment: "monthly sales");
// sheet scope - shadows a workbook-scoped name of the same spelling
wb.DefineName("Total", "Sheet2!D10", scope: "Sheet2");
ws.SetFormula(0, 0, "SUM(SalesData)");
The address is sheet-qualified (Sheet1!B1). A name can point at a single cell or a range.
Scope
| Scope | How | Visible to |
|---|---|---|
| Workbook | omit scope | every sheet |
| Sheet | scope: "Sheet2" | formulas on that sheet only |
When the same name exists at both workbook and sheet scope, the sheet-scoped one wins from that sheet (it shadows the other). This matches Excel.
Reading and removing
string? address = wb.GetName("TaxRate"); // "Sheet1!B1"
string? scoped = wb.GetName("Total", scope: "Sheet2");
foreach (DefinedNameInfo n in wb.GetNames())
Console.WriteLine($"{n.Name} [{n.Scope ?? "workbook"}] = {n.Address}");
wb.RemoveName("TaxRate");
DefinedNameInfo is a record of Name / Scope / Address / Comment. A null Scope
means workbook scope.
Resolving to a range
// resolve to a range (sheet scope wins)
RangePosition r = ws.ResolveRange("SalesData");
// ask the registry directly when you also need the target sheet
if (wb.Names.TryResolve("SalesData", fromSheet: ws.Name, out NameResolution res))
Console.WriteLine($"{res.Sheet} {res.R1},{res.C1} - {res.R2},{res.C2}");
ws.ResolveRange(name) gives you a RangePosition, which carries no sheet information.
When the name points at another sheet and you need to know which, use the NameResolution
that wb.Names.TryResolve(...) returns.
Defining many at once
// define many at once with Bulk (it re-resolves once instead of every time)
wb.Names.Bulk(() =>
{
for (int i = 0; i < 100; i++)
wb.DefineName($"Row{i}", $"Sheet1!A{i + 1}");
});
Following structural changes
| Operation | What happens |
|---|---|
| Inserting/deleting rows or columns | the name’s range shifts |
| Renaming a sheet | the target sheet name follows |
| Deleting a sheet | the target is lost; formulas using it become #REF! |
| Redefining a name | formulas using it recalculate |
Persistence
- reogrid-json — round-trips as
definedNames(compatible with the web edition) - XLSX — round-trips as
<definedNames>, at both workbook and sheet scope
Excel’s built-in names (_xlnm.*) do not appear in the list of defined names. The
exceptions are the print area (_xlnm.Print_Area) and print titles
(_xlnm.Print_Titles), converted into PrintSettings.PrintArea and
PrintSettings.RepeatRows / RepeatColumns on load and back again on save
(Printing and Page Setup). The other built-in names are not
supported.