Excel.NamedItemCollection class

A collection of all the NamedItem objects that are part of the workbook or worksheet, depending on how it was reached.

[ API set: ExcelApi 1.1 ]

Extends
OfficeExtension.ClientObject

Properties

items

Gets the loaded child items in this collection.

Methods

add(name, reference, comment)

Adds a new name to the collection of the given scope.

[ API set: ExcelApi 1.4 ]

addFormulaLocal(name, formula, comment)

Adds a new name to the collection of the given scope using the user's locale for the formula.

[ API set: ExcelApi 1.4 ]

getCount()

Gets the number of named items in the collection.

[ API set: ExcelApi 1.4 ]

getItem(name)

Gets a NamedItem object using its name.

[ API set: ExcelApi 1.1 ]

getItemOrNullObject(name)

Gets a NamedItem object using its name. If the nameditem object does not exist, will return a null object.

[ 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

items

Gets the loaded child items in this collection.

readonly items: Excel.NamedItem[];
Property Value
Excel.NamedItem[]

Method Details

add(name, reference, comment)

Adds a new name to the collection of the given scope.

[ API set: ExcelApi 1.4 ]

add(name: string, reference: Range | string, comment?: string): Excel.NamedItem;
Parameters
name
string

The name of the named item.

reference
Range | string

The formula or the range that the name will refer to.

comment
string

Optional. The comment associated with the named item.

Returns

Examples

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const headerRange = sheet.getRange("A1:E1");
    sheet.names.add("ExpensesHeader", headerRange);
    const namedItems = sheet.names.load("name, type");

    await context.sync();

    let namedItemsList = `This workbook contains ${namedItems.items.length} named item(s):`;
    for (let i = 0; i < namedItems.items.length; i++) {
        namedItemsList += JSON.stringify(namedItems.items[i]);
    }
    OfficeHelpers.UI.notify(namedItemsList);

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

        await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sample");

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

        let expensesTable = sheet.tables.add("A1:D1", true);
        expensesTable.name = "ExpensesTable";

        expensesTable.getHeaderRowRange().values = [["DATE", "MERCHANT", "CATEGORY", "AMOUNT"]];

        let newData = transactions.map(item =>
            [item.DATE, item.MERCHANT, item.CATEGORY, item.AMOUNT]);

        expensesTable.rows.add(null, newData);

        sheet.names.add("TotalAmount", "=SUM(ExpensesTable[AMOUNT])");

        sheet.getRange("D11").values = [["=TotalAmount"]];

        if (Office.context.requirements.isSetSupported("ExcelApi", 1.2)) {
            sheet.getUsedRange().format.autofitColumns();
            sheet.getUsedRange().format.autofitRows();
        }

        sheet.activate();

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

addFormulaLocal(name, formula, comment)

Adds a new name to the collection of the given scope using the user's locale for the formula.

[ API set: ExcelApi 1.4 ]

addFormulaLocal(name: string, formula: string, comment?: string): Excel.NamedItem;
Parameters
name
string

The "name" of the named item.

formula
string

The formula in the user's locale that the name will refer to.

comment
string

Optional. The comment associated with the named item.

Returns

getCount()

Gets the number of named items in the collection.

[ API set: ExcelApi 1.4 ]

getCount(): OfficeExtension.ClientResult<number>;
Returns
OfficeExtension.ClientResult<number>

getItem(name)

Gets a NamedItem object using its name.

[ API set: ExcelApi 1.1 ]

getItem(name: string): Excel.NamedItem;
Parameters
name
string

Nameditem name.

Returns

Examples

Excel.run(function (ctx) { 
    var sheetName = 'Sheet1';
    var nameditem = ctx.workbook.names.getItem(sheetName);
    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));
    }
});

getItemOrNullObject(name)

Gets a NamedItem object using its name. If the nameditem object does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

getItemOrNullObject(name: string): Excel.NamedItem;
Parameters
name
string

Nameditem name.

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.NamedItemCollection;
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.NamedItemCollection - 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.NamedItemCollection - 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 nameditems = ctx.workbook.names;
    nameditems.load('items');
    return ctx.sync().then(function() {
        for (var i = 0; i < nameditems.items.length; i++)
        {
            console.log(nameditems.items[i].name);
            console.log(nameditems.items[i].index);
        }
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

toJSON()

toJSON(): Excel.Interfaces.NamedItemCollectionData;
Returns