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

[ API set: ExcelApi 1.1 ]

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

arrayValues

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

comment

Specifies the comment associated with this name.

context

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

formula

The formula of the named item. Formulas always start with an equal sign ("=").

name

The name of the object.

scope

Specifies if the name is scoped to the workbook or to a specific worksheet. Possible values are: Worksheet, Workbook.

type

Specifies the type of the value returned by the name's formula. See Excel.NamedItemType for details.

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.

valueAsJson

A JSON representation of the values in this named item. Unlike NamedItem.value, NamedItem.valueAsJson supports all data types which can be in a cell. Examples include formatted number values and web images, in addition to the standard boolean, number, and string values. Data returned from this API always aligns with the en-US locale. To retrieve data in the user's display locale, use NamedItem.valueAsJsonLocal.

valueAsJsonLocal

A JSON representation of the values in this named item. Unlike NamedItem.value, NamedItem.valueAsJsonLocal supports all data types which can be in a cell. Examples include formatted number values and web images, in addition to the standard boolean, number, and string values. Data returned from this API always aligns with the user's display locale. To retrieve data independent of locale, use NamedItem.valueAsJson.

visible

Specifies if the object is visible.

worksheet

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

worksheetOrNullObject

Returns the worksheet to which the named item is scoped. If the item is scoped to the workbook instead, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

Methods

delete()

Deletes the given name.

getRange()

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

getRangeOrNullObject()

Returns the range object that is associated with the name. If the named item's type is not a range, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

load(options)

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.

readonly arrayValues: Excel.NamedItemArrayValues;

Property Value

Remarks

[ API set: ExcelApi 1.7 ]

comment

Specifies the comment associated with this name.

comment: string;

Property Value

string

Remarks

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

context: RequestContext;

Property Value

formula

The formula of the named item. Formulas always start with an equal sign ("=").

formula: any;

Property Value

any

Remarks

[ API set: ExcelApi 1.7 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/34-named-item/update-named-item.yaml

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.

readonly name: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.1 ]

scope

Specifies if the name is scoped to the workbook or to a specific worksheet. Possible values are: Worksheet, Workbook.

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

Property Value

Excel.NamedItemScope | "Worksheet" | "Workbook"

Remarks

[ API set: ExcelApi 1.4 ]

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

[ API set: ExcelApi 1.1 ]

valueAsJson

A JSON representation of the values in this named item. Unlike NamedItem.value, NamedItem.valueAsJson supports all data types which can be in a cell. Examples include formatted number values and web images, in addition to the standard boolean, number, and string values. Data returned from this API always aligns with the en-US locale. To retrieve data in the user's display locale, use NamedItem.valueAsJsonLocal.

readonly valueAsJson: CellValue | string;

Property Value

Excel.CellValue | string

Remarks

[ API set: ExcelApi 1.16 ]

valueAsJsonLocal

A JSON representation of the values in this named item. Unlike NamedItem.value, NamedItem.valueAsJsonLocal supports all data types which can be in a cell. Examples include formatted number values and web images, in addition to the standard boolean, number, and string values. Data returned from this API always aligns with the user's display locale. To retrieve data independent of locale, use NamedItem.valueAsJson.

readonly valueAsJsonLocal: CellValue | string;

Property Value

Excel.CellValue | string

Remarks

[ API set: ExcelApi 1.16 ]

visible

Specifies if the object is visible.

visible: boolean;

Property Value

boolean

Remarks

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

readonly worksheet: Excel.Worksheet;

Property Value

Remarks

[ API set: ExcelApi 1.4 ]

worksheetOrNullObject

Returns the worksheet to which the named item is scoped. If the item is scoped to the workbook instead, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

readonly worksheetOrNullObject: Excel.Worksheet;

Property Value

Remarks

[ API set: ExcelApi 1.4 ]

Method Details

delete()

Deletes the given name.

delete(): void;

Returns

void

Remarks

[ API set: ExcelApi 1.4 ]

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) => {
    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.

getRange(): Excel.Range;

Returns

Remarks

[ API set: ExcelApi 1.1 ]

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

getRangeOrNullObject()

Returns the range object that is associated with the name. If the named item's type is not a range, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getRangeOrNullObject(): Excel.Range;

Returns

Remarks

[ API set: ExcelApi 1.4 ]

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

options
Excel.Interfaces.NamedItemLoadOptions

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