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.

[ API set: ExcelApi 1.1 ]

Extends
OfficeExtension.ClientObject

Properties

arrayValues

Returns an object containing values and types of the named item. Read-only.

[ API set: ExcelApi 1.7 ]

comment

Represents the comment associated with this name.

[ API set: ExcelApi 1.4 ]

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

formula

Gets or sets the formula of the named item. Formula always starts with a '=' sign.

[ API set: ExcelApi 1.7 ]

name

The name of the object. Read-only.

[ API set: ExcelApi 1.1 ]

scope

Indicates whether the name is scoped to the workbook or to a specific worksheet. Possible values are: Worksheet, Workbook. Read-only.

[ API set: ExcelApi 1.4 ]

type

Indicates the type of the value returned by the name's formula. See Excel.NamedItemType for details. Read-only.

[ 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, will return the range address. Read-only.

[ API set: ExcelApi 1.1 ]

visible

Specifies whether the object is visible or not.

[ API set: ExcelApi 1.1 ]

worksheet

Returns the worksheet on which the named item is scoped to. Throws an error if the item is scoped to the workbook instead.

[ API set: ExcelApi 1.4 ]

worksheetOrNullObject

Returns the worksheet on which the named item is scoped to. Returns a null object if the item is scoped to the workbook instead.

[ API set: ExcelApi 1.4 ]

Methods

delete()

Deletes the given name.

[ API set: ExcelApi 1.4 ]

getRange()

Returns the range object that is associated with the name. Throws an error if the named item's type is not a range.

[ API set: ExcelApi 1.1 ]

getRangeOrNullObject()

Returns the range object that is associated with the name. Returns a null object if the named item's type is not a range.

[ API set: ExcelApi 1.4 ]

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

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 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 is 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.

Property Details

arrayValues

Returns an object containing values and types of the named item. Read-only.

[ API set: ExcelApi 1.7 ]

readonly arrayValues: Excel.NamedItemArrayValues;

Property Value

comment

Represents the comment associated with this name.

[ API set: ExcelApi 1.4 ]

comment: string;

Property Value

string

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

formula

Gets or sets the formula of the named item. Formula always starts with a '=' sign.

[ API set: ExcelApi 1.7 ]

formula: any;

Property Value

any

Examples

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");

    // Get the named item
    const myNamedItem = sheet.names.getItemOrNullObject("MyRange");
    myNamedItem.load("name, formula");
    await context.sync();

    if (myNamedItem.isNullObject) {
        console.log(`There is no named item. Create it with "Add named item for a range" first.`);
    } else {                    
        // Update named item to point to the second range
        myNamedItem.formula = "=Sample!$B$10:$D$14";
        sheet.getRange("B10:D14").select();
        await context.sync();

        console.log(`Just updated the named item "${myNamedItem.name}" -- it's now located here: ${myNamedItem.formula}`);
    }
});

name

The name of the object. Read-only.

[ API set: ExcelApi 1.1 ]

readonly name: string;

Property Value

string

scope

Indicates whether the name is scoped to the workbook or to a specific worksheet. Possible values are: Worksheet, Workbook. Read-only.

[ API set: ExcelApi 1.4 ]

readonly scope: Excel.NamedItemScope | "Worksheet" | "Workbook";

Property Value

Excel.NamedItemScope | "Worksheet" | "Workbook"

type

Indicates the type of the value returned by the name's formula. See Excel.NamedItemType for details. Read-only.

[ API set: ExcelApi 1.1 for String,Integer,Double,Boolean,Range,Error; 1.7 for Array ]

readonly type: Excel.NamedItemType | "String" | "Integer" | "Double" | "Boolean" | "Range" | "Error" | "Array";

Property Value

Excel.NamedItemType | "String" | "Integer" | "Double" | "Boolean" | "Range" | "Error" | "Array"

value

Represents the value computed by the name's formula. For a named range, will return the range address. Read-only.

[ API set: ExcelApi 1.1 ]

readonly value: any;

Property Value

any

visible

Specifies whether the object is visible or not.

[ API set: ExcelApi 1.1 ]

visible: boolean;

Property Value

boolean

worksheet

Returns the worksheet on which the named item is scoped to. Throws an error if the item is scoped to the workbook instead.

[ API set: ExcelApi 1.4 ]

readonly worksheet: Excel.Worksheet;

Property Value

worksheetOrNullObject

Returns the worksheet on which the named item is scoped to. Returns a null object if the item is scoped to the workbook instead.

[ API set: ExcelApi 1.4 ]

readonly worksheetOrNullObject: Excel.Worksheet;

Property Value

Method Details

delete()

Deletes the given name.

[ API set: ExcelApi 1.4 ]

delete(): void;

Returns

void

Examples

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const totalName = sheet.names.getItemOrNullObject("TotalAmount");
    totalName.load();
    await context.sync();

    if (totalName.value) {
        totalName.delete();

        // Replace the named item (TotalAmount) with the actual formula for TotalAmount to avoid displaying #NAME in the cell.
        sheet.getRange("D11").values = [["=SUM(ExpensesTable[AMOUNT])"]];
    } else {
        console.log("No named item created for the formula.");
    }

    await context.sync();
});

getRange()

Returns the range object that is associated with the name. Throws an error if the named item's type is not a range.

[ API set: ExcelApi 1.1 ]

getRange(): Excel.Range;

Returns

Examples

// Returns the Range object that is associated with the name. 
// null if the name is not of the type Range.
// Note: This API currently supports only the Workbook scoped items.
Excel.run(function (ctx) { 
    var names = ctx.workbook.names;
    var range = names.getItem('MyRange').getRange();
    range.load('address');
    return ctx.sync().then(function() {
        console.log(range.address);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getRangeOrNullObject()

Returns the range object that is associated with the name. Returns a null object if the named item's type is not a range.

[ API set: ExcelApi 1.4 ]

getRangeOrNullObject(): Excel.Range;

Returns

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(option?: Excel.Interfaces.NamedItemLoadOptions): Excel.NamedItem;

Parameters

Returns

Remarks

In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.NamedItem - Where option is a comma-delimited string or an array of strings that specify the properties to load.

load(option?: { select?: string; expand?: string; }): Excel.NamedItem - Where option.select is a comma-delimited string that specifies the properties to load, and options.expand is a comma-delimited string that specifies the navigation properties to load.

load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.NamedItem - Only available on collection types. It is similar to the preceding signature. Option.top specifies the maximum number of collection items that can be included in the result. Option.skip specifies the number of items that are to be skipped and not included in the result. If option.top is specified, the result set will start after skipping the specified number of items.

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

Excel.run(function (ctx) { 
    var names = ctx.workbook.names;
    var namedItem = names.getItem('MyRange');
    namedItem.load('type');
    return ctx.sync().then(function() {
            console.log(namedItem.type);
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});

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; }

Where 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
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

Remarks

This method has the following additional signature:

set(properties: Excel.NamedItem): 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 is 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;

Returns