Defined Names
A defined name assigns a custom name to a cell, a range, or a formula. Formulas can then refer to that name instead of the raw address, which makes them easier to read and easier to maintain.
- Formulas read closer to what they mean.
Amountexpresses the intent more clearly thanA1 + A2 + A3. - Several cells or a complex range can be represented by a single name. Define
SalesDataforA1:A10and useSalesDatain formulas. - A data area is maintained in one place. When the range changes, update the reference of the name instead of every formula that uses it.
Usage
The Name Manager entry is next to the name box in the upper left corner of the sheet. Clicking it displays every defined name and opens the Name Manager, where you can view, edit, and delete defined names.
In a formula, enter the name directly. If a range is named TotalSales, =TotalSales references that range.
Notes
- A defined name must be unique within the same worksheet, and it cannot conflict with another name or a built-in function.
- A name cannot begin with a number, and it cannot contain spaces or special characters such as
!,@, or#. If an entered name violates these rules, the system reports an error. - To change a name, reopen the "Define Name" edit panel, select the name, edit it, and click "OK".
- To delete a name, select it in the "Define Name" edit panel and click "Delete". Formulas that referenced the name become invalid.
- Names can be shared across different worksheets in the same workbook, but the uniqueness rule still applies: defining the same name on different worksheets can make references ambiguous.
- Use meaningful names and avoid very short or vague ones, so that other people can understand and maintain the sheet.
- If using a defined name causes problems, check that the name is correct, that the reference range is accurate, and that no other name conflicts with it.
- A name can be set at the workbook level or the worksheet level. Workbook-level names are available throughout the workbook, whereas worksheet-level names are available only on the specific worksheet.
Facade API
The complete Facade API type definitions are in the Facade API.
Create defined names
FWorkbook.newDefinedNameBuilder() creates a defined name builder and returns an FDefinedNameBuilder. Chain its methods to produce the ISetDefinedNameMutationParam object used to create the name.
These are some of the member methods on FDefinedNameBuilder:
| Method | Description |
|---|---|
| build | Builds the defined name object |
| setName | Sets the defined name |
| setFormula | Sets the formula for the defined name |
| setRef | Sets the reference range for the defined name |
| setComment | Sets the comment for the defined name |
| setScopeToWorksheet | Sets the defined name to be available for a specific worksheet |
| setScopeToWorkbook | Sets the defined name to be available for the entire workbook |
FWorkbook.insertDefinedNameBuilder() creates a defined name that is available for the entire workbook.
const fWorkbook = univerAPI.getActiveWorkbook()const definedNameParam = fWorkbook.newDefinedNameBuilder() .setName('MyDefinedName') .setRef('Sheet1!$A$1') .setComment('A reference to A1 cell in Sheet1') .build()fWorkbook.insertDefinedNameBuilder(definedNameParam)FWorkbook.insertDefinedName() is a shortcut for creating a defined name when the name and the reference are already known.
const fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.insertDefinedName('MyDefinedName', 'Sheet1!$A$1')Other methods:
FWorksheet.insertDefinedName(): Creates a defined name that is available for a specific worksheet.
Get defined names
FWorkbook.getDefinedName() gets one defined name from the workbook and returns an FDefinedName.
const fWorkbook = univerAPI.getActiveWorkbook()const definedName = fWorkbook.getDefinedName('MyDefinedName')console.log(definedName?.getFormulaOrRefString())if (definedName) { definedName.setName('NewDefinedName')}Other methods:
FWorkbook.getDefinedNames()returns every defined name in the workbook as anFDefinedName[]array.FWorksheet.getDefinedNames()returns every defined name available to this worksheet as anFDefinedName[]array.
Modify defined names
FWorkbook.updateDefinedNameBuilder() modifies a defined name in the workbook.
const fWorkbook = univerAPI.getActiveWorkbook()const definedName = fWorkbook.getDefinedName('MyDefinedName')console.log(definedName?.getFormulaOrRefString())// Modify the defined nameif (definedName) { const newDefinedNameParam = definedName.toBuilder() .setName('NewDefinedName') .setRef('Sheet1!$A$2') .build() fWorkbook.updateDefinedNameBuilder(newDefinedNameParam)}You can also change the configuration directly on the returned FDefinedName instance.
const fWorkbook = univerAPI.getActiveWorkbook()const definedName = fWorkbook.getDefinedName('MyDefinedName')console.log(definedName?.getFormulaOrRefString())// Modify the defined nameif (definedName) { definedName.setName('NewDefinedName') definedName.setRef('Sheet1!$A$2')}Delete defined names
FWorkbook.deleteDefinedName() deletes a defined name in the workbook.
const fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.deleteDefinedName('MyDefinedName')You can also delete the name through the returned FDefinedName instance.
const fWorkbook = univerAPI.getActiveWorkbook()const definedName = fWorkbook.getDefinedName('MyDefinedName')console.log(definedName?.getFormulaOrRefString())// Delete the defined nameif (definedName) { definedName.delete()}How is this guide?