Excel.NamedItem class
Represents a defined name for a range of cells or value. Names can be primitive named objects (as seen in the type below), range object, or a reference to a range. This object can be used to obtain range object associated with names.
- Extends
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/34-named-item/create-and-remove-named-item.yaml
await Excel.run(async (context) => {
// Log all the named items in the active worksheet.
const namedItems = context.workbook.worksheets.getActiveWorksheet().names.load();
await context.sync();
console.log("This worksheet contains " + namedItems.items.length + " named items.");
for (let i = 0; i < namedItems.items.length; i++) {
const namedItem : Excel.NamedItem = namedItems.items[i];
console.log(JSON.stringify(namedItem)) + "\n";
}
await context.sync();
});
Properties
| context | The request context associated with the object. This connects the add-in's process to the Office host application's process. |
| name | The name of the object. |
| type | Specifies the type of the value returned by the name's formula. See |
| value | Represents the value computed by the name's formula. For a named range, it will return the range address. This API returns the #VALUE! error in the Excel UI if it refers to a user-defined function. |
| visible | Specifies if the object is visible. |
Methods
| get |
Returns the range object that is associated with the name. Throws an error if the named item's type is not a range. |
| load(options) | Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| set(properties, options) | Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type. |
| set(properties) | Sets multiple properties on the object at the same time, based on an existing loaded object. |
| toJSON() | Overrides the JavaScript |
Property Details
context
The request context associated with the object. This connects the add-in's process to the Office host application's process.
context: RequestContext;
Property Value
name
The name of the object.
readonly name: string;
Property Value
string
Remarks
type
Specifies the type of the value returned by the name's formula. See Excel.NamedItemType for details.
readonly type: Excel.NamedItemType | "String" | "Integer" | "Double" | "Boolean" | "Range" | "Error" | "Array";
Property Value
Excel.NamedItemType | "String" | "Integer" | "Double" | "Boolean" | "Range" | "Error" | "Array"
Remarks
[ API set: ExcelApi 1.1 for String,Integer,Double,Boolean,Range,Error; 1.7 for Array ]
value
Represents the value computed by the name's formula. For a named range, it will return the range address. This API returns the #VALUE! error in the Excel UI if it refers to a user-defined function.
readonly value: any;
Property Value
any
Remarks
visible
Specifies if the object is visible.
visible: boolean;
Property Value
boolean
Remarks
Method Details
getRange()
Returns the range object that is associated with the name. Throws an error if the named item's type is not a range.
getRange(): Excel.Range;
Returns
Remarks
Examples
// Returns the Range object that is associated with the name.
// Returns `null` if the name is not of type Range.
// Note: This API currently supports only the Workbook scoped items.
await Excel.run(async (context) => {
const names = context.workbook.names;
const range = names.getItem('MyRange').getRange();
range.load('address');
await context.sync();
console.log(range.address);
});
load(options)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(options?: Excel.Interfaces.NamedItemLoadOptions): Excel.NamedItem;
Parameters
Provides options for which properties of the object to load.
Returns
load(propertyNames)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNames?: string | string[]): Excel.NamedItem;
Parameters
- propertyNames
-
string | string[]
A comma-delimited string or an array of strings that specify the properties to load.
Returns
Examples
await Excel.run(async (context) => {
const names = context.workbook.names;
const namedItem = names.getItem('MyRange');
namedItem.load('type');
await context.sync();
console.log(namedItem.type);
});
load(propertyNamesAndPaths)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Excel.NamedItem;
Parameters
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.
Returns
set(properties, options)
Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.
set(properties: Interfaces.NamedItemUpdateData, options?: OfficeExtension.UpdateOptions): void;
Parameters
- properties
- Excel.Interfaces.NamedItemUpdateData
A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.
- options
- OfficeExtension.UpdateOptions
Provides an option to suppress errors if the properties object tries to set any read-only properties.
Returns
void
set(properties)
Sets multiple properties on the object at the same time, based on an existing loaded object.
set(properties: Excel.NamedItem): void;
Parameters
- properties
- Excel.NamedItem
Returns
void
toJSON()
Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Excel.NamedItem object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.NamedItemData) that contains shallow copies of any loaded child properties from the original object.
toJSON(): Excel.Interfaces.NamedItemData;