Names, merges, hyperlinks and outlines
Defined names, merged ranges, hyperlinks, row and column outlines, and sheet protection are set on the Workbook and Sheet handles described in Sheets, cells and structure.
Define a name and use it
A2:A4 holds three sales figures. A defined name gives that range a label you can use in formulas.
Powered by the real Formulon engine (WASM) — it runs entirely in your browser, nothing is uploaded.
What you can do
- Define a name with workbook or sheet scope and use it in a formula with
wb.setDefinedName. - Merge a range with
ws.addMergeand list or remove merges. - Set a hyperlink to a URL or to a location in the workbook: Hyperlink ranges.
- Hide rows and columns and set outline levels from 0 to 7.
- Set the collapsed flag on the row or column that summarises an outline group.
- Read, replace or update sheet protection.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
using wb = formulon.Workbook.createDefault()
const ws = wb.sheet(0)
ws.setValues('A1', [[10], [20], [30]])
wb.setDefinedName({ name: 'Sales', formula: 'Sheet1!$A$1:$A$3' }) // workbook scope
wb.setDefinedName({ name: 'Rate', formula: '0.1', scope: 0 }) // local to sheet 0
ws.setFormula('B1', '=SUM(Sales)*(1+Rate)')
ws.addMerge('A5:C5')
ws.setHyperlink({ target: 'https://formulon.libraz.net', display: 'Docs', range: 'A6' })
ws.setHyperlink({ target: null, display: 'Back to top', location: 'Sheet1!A1', range: 'A7' })
ws.setRowHidden(2, 2, true) // first row, last row
ws.setColumnOutline(3, 5, 1) // first column, last column, level
console.log(ws.getValue('B1')) // 66from formulon import DefinedNameInput, HyperlinkInput, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:A3"] = [[10], [20], [30]]
wb.set_defined_name(DefinedNameInput("Sales", "Sheet1!$A$1:$A$3")) # workbook scope
wb.set_defined_name(DefinedNameInput("Rate", "0.1", scope=0)) # local to sheet 0
ws["B1"] = "=SUM(Sales)*(1+Rate)"
ws.add_merge("A5:C5")
ws.set_hyperlink(HyperlinkInput(range="A6", target="https://formulon.libraz.net", display="Docs"))
ws.set_hyperlink(HyperlinkInput(range="A7", target=None, display="Back to top", location="Sheet1!A1"))
ws.set_row_hidden(2, 2, True)
ws.set_column_outline(3, 5, 1)
print(ws["B1"]) # 66.0Python uses the snake_case form of each name.
Defined names
Setting and reading names
wb.setDefinedName({ name, formula, scope, comment, hidden }) writes one name. scope is a SheetRef (an index, a name, or a Sheet) for a name local to that sheet; an omitted or null scope is workbook scope.
A defined-name formula is accepted with or without a leading = and read back with it. Two inputs are rejected:
- A formula that starts with
==fails withStatusCode.ParserUnexpectedToken. - An empty formula fails with
StatusCode.InvalidArgument.
Remove a name with wb.removeDefinedName(name, scope?). wb.listDefinedNames() enumerates names with their scope (null for workbook scope, otherwise the index of the sheet the name is local to), and wb.getDefinedName(name, scope?) returns one or null; scope takes the same values as on setDefinedName. Python uses set_defined_name(DefinedNameInput(...)), list_defined_names(), and get_defined_name(name, scope=None).
Replacing a name
comment and hidden are part of the same record. Writing a name that already exists in that scope replaces it as a whole, so an omitted comment or hidden returns to its default (no comment, visible); pass the attributes again when only the formula changes.
wb.resolveDefinedNameKey(input) (Python resolve_defined_name_key) returns the { name, scope } key the same call to setDefinedName would store under, without changing the workbook, and throws whatever the set would throw. Because a set replaces the formula, comment and hidden flag together, an edit that keeps the comment and hidden flag resolves the key, reads the entry stored there, and then sets:
const key = wb.resolveDefinedNameKey(input);
const prev = wb.getDefinedName(key.name, key.scope);
wb.setDefinedName({ ...input, comment: prev?.comment ?? null, hidden: prev?.hidden ?? false });Name collisions and lookup
A name being added, renamed or copied collides with an existing one when the two are equal under Excel's text comparison, which folds letter case, full- and half-width forms and hiragana / katakana: Budget and BUDGET collide, and so do データ and データ.
A lookup by name takes the first of these that matches:
- An exact match.
- The one entry that differs only in letter case (Unicode simple case folding).
- The single entry equal under the full comparison.
Two such entries with no closer match resolve nothing. A formula's =ä resolves a name Ä.
Every named collection (sheets, tables, table columns, table and cell styles, protected ranges and pivots included) follows the same rule; person ids compare by letter case only.
Multi-area names
A name whose body is a bare comma list, such as Sheet1!$A$1,Sheet1!$B$1, is one reference over both areas, as Excel stores it, whether it is loaded from a file or set with setDefinedName, and it keeps that spelling when saved. A dangling or doubled comma is still refused with a parse error.
With 4 in A1 and 6 in B1, =SUM(U) over such a name U is 10 and =INDEX(U,1,1,2) reads the second area, 6; INDEX over a name whose areas sit on different sheets is #VALUE!. A multi-area print area or set of print titles survives an .xlsx or .xlsb save.
Built-in names without the prefix
A built-in name typed without its _xlnm. prefix, such as Print_Area in any case, reaches the built-in name visible from the sheet the formula is entered on and is stored with the prefix: with a print area of A1:B3 on Sheet1, =ROWS(Print_Area) there is 3.
- A defined name with exactly the typed spelling still wins, and a sheet-local name still hides a workbook one.
- A sheet-qualified name, built-in or not, looks in that sheet's names first: when the sheet defines it, the qualifier is kept; otherwise it reaches the workbook-scope name and is stored as
[0]!Name, which the formula text shows with the workbook's file name in place of[0]. The entry sheet's own built-in is never used for a qualified name. - A built-in name that resolves to nothing keeps its canonical spelling, so
print_titlesis stored asPrint_Titles, and evaluates to#NAME?. - The formula text read back shows such a name without the prefix, so a stored
=ROWS(_xlnm.Print_Area)reads as=ROWS(Print_Area). AREASof a name that does not exist is#NAME?.
Built-in names and profile locales
A bare built-in name in a cell formula read from a file reaches the built-in only when the workbook is opened under a profile whose locale spells that name in English, such as en_US or ja_JP. Under de_DE, which spells Criteria as Suchkriterien, a stored =ROWS(Criteria) binds only a user-defined name of that spelling and is #NAME? when there is none. A formula typed in English binds the bare built-in name under every profile.
setDefinedName stores a name that is the profile locale's spelling of a built-in name as that built-in: under en_US or ja_JP, Print_Area is stored as _xlnm.Print_Area, and under de_DE so is Druckbereich, while the English Print_Area stays an ordinary name there. en_US keeps the letter case typed (PRINT_AREA is stored as _xlnm.PRINT_AREA); every other locale stores the canonical case.
A new Print_Area or Print_Titles addressed to workbook scope (scope omitted or null; the locale spelling or the _xlnm. form) is placed by the active sheet:
| Active sheet | Result |
|---|---|
| Worksheet | Becomes local to the active worksheet, with the body as typed, whatever sheet the formula names, so ws.getPrintArea() on that worksheet reads it. |
| Chartsheet | Stays at workbook scope, the one way to give a chartsheet a print area or print titles, since setPrintArea and setPrintTitles refuse one. |
| Dialog sheet or macro sheet | Throws StatusCode.InvalidArgument. |
A new one scoped to a sheet that is not a worksheet also throws StatusCode.InvalidArgument. One addressed to a sheet's scope stays there, an existing entry is updated where it is, and other built-in names keep the requested scope. getDefinedName and removeDefinedName resolve the typed spelling the same way, with the scope still part of the key.
Workbook.load(bytes, { profileId }) reads a file the same way: a defined name stored without _xlnm. but spelled as that profile locale's built-in loads as the built-in, and the formulas naming it follow, unless that built-in is already visible from the name's scope.
A workbook-scope Druckbereich loads as _xlnm.Print_Area under de_DE, and =ROWS(Druckbereich) reads back =ROWS(Print_Area); under en_US, ja_JP and the default profile it stays an ordinary name, and so does a sheet-scoped Druckbereich on a sheet that already has its own _xlnm.Print_Area.
Merges
ws.listMerges() lists every merge on a sheet. ws.removeMerge(range) removes each merge that overlaps the range ('A1:B2' also works) and throws StatusCode.NotFound when none does, and ws.clearMerges() removes all of them.
Hyperlinks
ws.setHyperlink(link) (Python set_hyperlink) sets the hyperlink covering exactly link.range, which is required. A link on that same range is replaced in place; any other is appended, so overlapping links coexist, as in Excel. A hyperlink takes a URL as target, or a null target and an A1 location for a destination inside the workbook.
ws.listHyperlinks()reads them back in the order they were added (file order after a load), each with itsrangeand anulltargetfor an in-workbook link.ws.getHyperlink(cell)reads the earliest-added link that covers the cell.ws.removeHyperlink(range)removes every link that intersects the range, each one whole, and throwsStatusCode.NotFoundwhen none does.ws.clearHyperlinks()removes them all (clear*does nothing when there is nothing to clear).ws.removeHyperlinkAt(index)removes one link andws.insertHyperlinkAt(index, link)puts one in at a position (Pythonremove_hyperlink,remove_hyperlink_at,insert_hyperlink_at);indexis the position in the orderlistHyperlinks()returns.
Hyperlink ranges covers the cell style and the stored target.
Hidden rows, columns and outlines
ws.setRowHidden() / setColumnHidden() and setRowOutline() / setColumnOutline() take an inclusive span (first, last, ...) and change layout only. The outline level runs from 0 to 7, the deepest Excel stores; a higher level fails with StatusCode.InvalidArgument. Ordinary formulas still read hidden rows, as SUM does above; SUBTOTAL 101–111 skips them.
ws.setRowCollapsed() / setColumnCollapsed() (Python: set_row_collapsed() / set_column_collapsed()) set the collapsed flag on the row or column that summarises an outline group, and ws.listRowOverrides() / ws.listColumnOverrides() report it. <sheetFormatPr outlineLevelRow outlineLevelCol> is derived from the deepest row and column levels on every save, XLSX and XLSB, so Excel shows the outline gutter's symbols.
Sheet protection
ws.getSheetProtection() returns the <sheetProtection> record, or null when the sheet is unprotected.
ws.setSheetProtection(input)replaces the record as a whole.ws.setSheetProtection(null)removes protection.ws.updateSheetProtection(patch)changes only the keys it names.
The allow* flags start from Excel's defaults: selecting locked and unlocked cells is allowed and everything else is not, so ws.setSheetProtection({ allowFormatCells: true }) protects the sheet and also lets users format cells. The engine stores the flags and does not enforce locks.
Hyperlink ranges
A hyperlink covers the inclusive rectangle in link.range, a CellRect or an A1 range ('B2' for one cell); leaving it out is a TypeError. ws.setHyperlink({ target, range: 'A1:C1' }) (Python ws.set_hyperlink(HyperlinkInput(range='A1:C1', target=...))) replaces the link on exactly that range and appends any other, so links on different but overlapping ranges coexist. A read-back Hyperlink carries its range and passes back to the setter as is, and both OOXML and XLSB preserve the full span.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
using wb = formulon.Workbook.createDefault()
const ws = wb.sheet(0)
const links = () => ws.listHyperlinks().map((link) => `${link.target} ${wb.formatReference(link.range)}`)
ws.setHyperlink({ target: 'https://example.com/a', range: 'A1' })
ws.setHyperlink({ target: 'https://example.com/b', range: 'A1' }) // same range: replaced
ws.setHyperlink({ target: 'https://example.com/c', range: 'A1:C1' }) // another range: appended
console.log(links()) // [ 'https://example.com/b A1', 'https://example.com/c A1:C1' ]
console.log(ws.getHyperlink('A1').target) // https://example.com/b
ws.removeHyperlink('A1') // removes both
console.log(ws.listHyperlinks().length) // 0from formulon import HyperlinkInput, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
def links():
return [f"{link.target} {wb.format_reference(link.range)}" for link in ws.list_hyperlinks()]
ws.set_hyperlink(HyperlinkInput(range="A1", target="https://example.com/a"))
ws.set_hyperlink(HyperlinkInput(range="A1", target="https://example.com/b")) # same range: replaced
ws.set_hyperlink(HyperlinkInput(range="A1:C1", target="https://example.com/c")) # another range: appended
print(links()) # ['https://example.com/b A1', 'https://example.com/c A1:C1']
print(ws.get_hyperlink("A1").target) # https://example.com/b
ws.remove_hyperlink("A1") # removes both
print(len(ws.list_hyperlinks())) # 0setHyperlink gives every cell of the range Excel's Hyperlink cell style: the font becomes the Normal font underlined in theme colour 10, while fill, number format, border, alignment and protection are kept. Blank and merge-covered cells are formatted too, and a whole-column range formats the column.
An empty top-left cell receives display, else the stored target, else location, as text; a cell that holds a value keeps it. With no display, a top-left cell holding other text gives the link the stored target as its display, and a location-only link gets its location.
removeHyperlink and clearHyperlinks reset every cell of each removed link's range to the default format (cell format 0), including cells another link still covers; values stay.
A range of more than 134,217,728 cells that is not a whole column or row makes all three throw StatusCode.SecurityResourceLimit (Python SecurityError) and change nothing; A1:Z100000, 2,600,000 cells, is formatted.
setHyperlink stores the target the way Excel does:
- an http, https or ftp URL without userinfo gets a lowercase scheme and host,
/for an empty path and decoded unreserved percent-escapes, and its#fragmentmoves tolocation(HTTPS://Example.COM#Topis stored ashttps://example.com/with the locationTop); - a
mailto:target has only its scheme lowercased; - a POSIX path becomes
file:///..., a drive pathfile:///C:/...and a UNC pathfile:////server/..., each with a#fragmentsplit off intolocation; a POSIX path holding%stays a plain path with only%20decoded to a space; - a relative path is resolved against the folder of
wb.getLocation()when one is set, and relative targets already stored are resolved when a folder is first set; - any other target is stored as written, and loading a file changes no target.
A save writes local targets relative to the location's folder where they share one; see Hyperlinks in a saved file.
To remove one of several overlapping links, find its position in listHyperlinks() and pass it to removeHyperlinkAt.
insertHyperlinkAt(index, link) replaces nothing, needs link.range and appends for an index equal to the count, so it puts a removed link back exactly where it was, which is what an undo needs.
Both edit the list only: insertHyperlinkAt stores the target verbatim and writes no cell text or format, and removeHyperlinkAt resets no format, so an undo of removeHyperlink restores the cell formats itself.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
using wb = formulon.Workbook.createDefault()
const ws = wb.sheet(0)
ws.setHyperlink({ target: 'https://example.com/b', range: 'A1' })
ws.setHyperlink({ target: 'https://example.com/c', range: 'A1:C1' })
const index = ws.listHyperlinks().findIndex((link) => link.target === 'https://example.com/c')
const removed = ws.listHyperlinks()[index]
ws.removeHyperlinkAt(index)
console.log(ws.listHyperlinks().map((link) => link.target)) // [ 'https://example.com/b' ]
ws.insertHyperlinkAt(index, removed)
console.log(ws.listHyperlinks().map((link) => link.target)) // [ 'https://example.com/b', 'https://example.com/c' ]from formulon import HyperlinkInput, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_hyperlink(HyperlinkInput(range="A1", target="https://example.com/b"))
ws.set_hyperlink(HyperlinkInput(range="A1:C1", target="https://example.com/c"))
index = [link.target for link in ws.list_hyperlinks()].index("https://example.com/c")
removed = ws.list_hyperlinks()[index]
ws.remove_hyperlink_at(index)
print([link.target for link in ws.list_hyperlinks()]) # ['https://example.com/b']
ws.insert_hyperlink_at(index, removed)
print([link.target for link in ws.list_hyperlinks()]) # ['https://example.com/b', 'https://example.com/c']