Define names
Use this when formulas should refer to a constant, a range or a calculation by name instead of by address, as Excel's Name Manager does.
Define a workbook-scoped name
wb.setDefinedName({ name, formula }) (Python set_defined_name(DefinedNameInput(...))) writes one name. The formula is a reference, a constant or any formula, with or without a leading =. Without a scope, the name is visible from every sheet.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [[10], [20], [30]])
wb.setDefinedName({ name: 'Sales', formula: 'Sheet1!$A$1:$A$3' })
wb.setDefinedName({ name: 'TaxRate', formula: '0.1' })
ws.setFormula('B1', '=SUM(Sales)*(1+TaxRate)')
console.log(ws.getValue('B1')) // 66
console.log(wb.getDefinedName('Sales').formula) // =Sheet1!$A$1:$A$3
} finally {
wb.dispose()
}from formulon import DefinedNameInput, 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"))
wb.set_defined_name(DefinedNameInput("TaxRate", "0.1"))
ws.set_formula("B1", "=SUM(Sales)*(1+TaxRate)")
print(ws["B1"]) # 66.0
print(wb.get_defined_name("Sales").formula) # =Sheet1!$A$1:$A$3A name follows Excel's rules: a name that looks like a cell address, such as A1, or that contains a space is refused with StatusCode.InvalidArgument.
Define a name local to one sheet
A scope (a sheet index, a sheet name or a Sheet) makes the name local to that sheet. Two sheets can then hold the same name with different formulas, and a formula on each sheet reads its own. A formula on another sheet reaches a local name through the sheet qualifier, Sheet1!Rate.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const east = wb.sheet(0)
const west = wb.addSheet('West')
wb.setDefinedName({ name: 'Rate', formula: '0.1', scope: east })
wb.setDefinedName({ name: 'Rate', formula: '0.2', scope: 'West' })
east.setFormula('A1', '=Rate')
west.setFormula('A1', '=Rate')
west.setFormula('A2', '=Sheet1!Rate')
console.log(east.getValue('A1'), west.getValue('A1'), west.getValue('A2')) // 0.1 0.2 0.1
console.log(wb.getDefinedName('Rate')) // null
console.log(wb.getDefinedName('Rate', 'West').scope) // 1
} finally {
wb.dispose()
}from formulon import DefinedNameInput, Workbook
with Workbook.create_default() as wb:
east = wb[0]
west = wb.add_sheet("West")
wb.set_defined_name(DefinedNameInput("Rate", "0.1", scope=east))
wb.set_defined_name(DefinedNameInput("Rate", "0.2", scope="West"))
east.set_formula("A1", "=Rate")
west.set_formula("A1", "=Rate")
west.set_formula("A2", "=Sheet1!Rate")
print(east["A1"], west["A1"], west["A2"]) # 0.1 0.2 0.1
print(wb.get_defined_name("Rate")) # None
print(wb.get_defined_name("Rate", scope="West").scope) # 1getDefinedName(name, scope?) looks in one scope only, so a lookup without scope finds workbook-scoped names and returns null here. A read reports scope as the sheet index, or null for workbook scope.
Set the comment and hidden flag
comment is the note Excel's Name Manager shows, and hidden: true hides the name from it. Both belong to the same record as the formula. listDefinedNames() returns every name with its attributes.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
wb.setDefinedName({ name: 'TaxRate', formula: '0.1', comment: 'Standard rate' })
wb.setDefinedName({ name: 'Helper', formula: '42', hidden: true })
for (const n of wb.listDefinedNames()) {
console.log(n.name, n.formula, n.comment, n.hidden)
}
} finally {
wb.dispose()
}
// TaxRate =0.1 Standard rate false
// Helper =42 null truefrom formulon import DefinedNameInput, Workbook
with Workbook.create_default() as wb:
wb.set_defined_name(DefinedNameInput("TaxRate", "0.1", comment="Standard rate"))
wb.set_defined_name(DefinedNameInput("Helper", "42", hidden=True))
for n in wb.list_defined_names():
print(n.name, n.formula, n.comment, n.hidden)
# TaxRate =0.1 Standard rate False
# Helper =42 None TrueReplace or remove a name
Writing a name that already exists in that scope replaces the whole record, so an omitted comment or hidden returns to its default. Pass the attributes again when only the formula changes. removeDefinedName(name, scope?) removes a name, and throws StatusCode.NotFound when that scope has no such name. A formula that still uses a removed name evaluates to #NAME?.
import createFormulon, { StatusCode } from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
wb.setDefinedName({ name: 'TaxRate', formula: '0.1', comment: 'Standard rate' })
ws.setFormula('A1', '=100*TaxRate')
const old = wb.getDefinedName('TaxRate')
wb.setDefinedName({ ...old, formula: '0.2' })
console.log(ws.getValue('A1'), wb.getDefinedName('TaxRate').comment) // 20 Standard rate
wb.removeDefinedName('TaxRate')
console.log(String(ws.getValue('A1'))) // #NAME?
try {
wb.removeDefinedName('TaxRate')
} catch (e) {
console.log(e.code === StatusCode.NotFound) // true
}
} finally {
wb.dispose()
}import dataclasses
from formulon import DefinedNameInput, NotFoundError, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
wb.set_defined_name(DefinedNameInput("TaxRate", "0.1", comment="Standard rate"))
ws.set_formula("A1", "=100*TaxRate")
old = wb.get_defined_name("TaxRate")
wb.set_defined_name(dataclasses.replace(old, formula="0.2"))
print(ws["A1"], wb.get_defined_name("TaxRate").comment) # 20.0 Standard rate
wb.remove_defined_name("TaxRate")
print(ws["A1"]) # #NAME?
try:
wb.remove_defined_name("TaxRate")
except NotFoundError:
print("not found") # not foundThere is no rename call for a defined name. Defining the new name and removing the old one leaves formulas spelling the old name, which then evaluate to #NAME?.
Match names regardless of case
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 an exact match first, then the one entry that differs only in letter case (Unicode simple case folding), then the single entry equal under the full comparison; two such entries with no closer match resolve nothing. A formula's =ä finds a name Ä. Tables, styles, pivots and sheet names follow the same rule.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
wb.setDefinedName({ name: 'Budget', formula: '500' })
wb.setDefinedName({ name: 'Ä', formula: '5' })
ws.setFormula('A1', '=budget+ä')
console.log(ws.getValue('A1')) // 505
console.log(wb.getDefinedName('BUDGET').name) // Budget
wb.setDefinedName({ name: 'BUDGET', formula: '600' })
console.log(wb.listDefinedNames().length, ws.getValue('A1')) // 2 605
} finally {
wb.dispose()
}from formulon import DefinedNameInput, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
wb.set_defined_name(DefinedNameInput("Budget", "500"))
wb.set_defined_name(DefinedNameInput("Ä", "5"))
ws.set_formula("A1", "=budget+ä")
print(ws["A1"]) # 505.0
print(wb.get_defined_name("BUDGET").name) # Budget
wb.set_defined_name(DefinedNameInput("BUDGET", "600"))
print(len(wb.list_defined_names()), ws["A1"]) # 2 605.0The last write replaces the existing name rather than adding a second one, and the stored spelling becomes the one written last.
Read next
- Names, merges, hyperlinks and outlines: the defined-name record in full.
- Work with tables: structured references, the other named reference.
- WASM API and Python API: the defined-name members.