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 ]

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.

toJSON()

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

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) {
        OfficeHelpers.UI.notify(`There is no named item with the name "MyRange".`);
    } 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();

        OfficeHelpers.UI.notify(`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

try {
    await Excel.run(async (context) => {

        const sheet = context.workbook.worksheets.getItem("Sample");

        sheet.names.getItem("TotalAmount").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])"]];

        await context.sync();

    });
}
catch (error) {
    OfficeHelpers.UI.notify(error);
    OfficeHelpers.Utilities.log(error);
}

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?: string | string[]): Excel.NamedItem;
Parameters
option
string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns
Remarks

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

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.

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

toJSON()

toJSON(): Excel.Interfaces.NamedItemData;
Returns