Excel.NamedItemCollection class

属于工作簿或工作表一部分的所有 NamedItem 对象的集合,具体取决于访问方式。

扩展

属性

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

items

获取此集合中已加载的子项。

方法

add(name, reference, comment)

将新名称添加到给定范围的集合。

addFormulaLocal(name, formula, comment)

使用用户的公式区域设置,将新名称添加到给定范围的集合。

getCount()

获取集合中已命名项的数量。

getItem(name)

使用对象名称获取 NamedItem 对象。

getItemOrNullObject(name)

使用对象名称获取 NamedItem 对象。 如果对象不存在,则此方法返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.NamedItemCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.NamedItemCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。

属性详细信息

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

items

获取此集合中已加载的子项。

readonly items: Excel.NamedItem[];

属性值

方法详细信息

add(name, reference, comment)

将新名称添加到给定范围的集合。

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

参数

name

string

已命名项目的名称。

reference

Excel.Range | string

名称将引用的公式或区域。

comment

string

可选。 与命名项关联的注释。

返回

注解

API 集:ExcelApi 1.4

示例

// 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 headerRange = sheet.getRange("A1:D1");

    sheet.names.add("ExpensesHeader", headerRange);
    const namedItems = sheet.names.load("name, type");

    await context.sync();
});

addFormulaLocal(name, formula, comment)

使用用户的公式区域设置,将新名称添加到给定范围的集合。

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

参数

name

string

已命名项目的名称。

formula

string

名称将引用的采用用户区域设置的公式。

comment

string

可选。 与命名项关联的注释。

返回

注解

API 集:ExcelApi 1.4

getCount()

获取集合中已命名项的数量。

getCount(): OfficeExtension.ClientResult<number>;

返回

注解

API 集:ExcelApi 1.4

getItem(name)

使用对象名称获取 NamedItem 对象。

getItem(name: string): Excel.NamedItem;

参数

name

string

命名项名称。

返回

注解

API 集:ExcelApi 1.1

示例

await Excel.run(async (context) => { 
    const sheetName = 'Sheet1';
    const nameditem = context.workbook.names.getItem(sheetName);
    nameditem.load('type');
    await context.sync();
    
    console.log(nameditem.type);
});

getItemOrNullObject(name)

使用对象名称获取 NamedItem 对象。 如果对象不存在,则此方法返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getItemOrNullObject(name: string): Excel.NamedItem;

参数

name

string

命名项名称。

返回

注解

API 集:ExcelApi 1.4

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: Excel.Interfaces.NamedItemCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.NamedItemCollection;

参数

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): Excel.NamedItemCollection;

参数

propertyNames

string | string[]

指定要加载的属性的逗号分隔的字符串或字符串数组。

返回

示例

await Excel.run(async (context) => { 
    const nameditems = context.workbook.names;
    nameditems.load('items');
    await context.sync();

    for (let i = 0; i < nameditems.items.length; i++) {
        console.log(nameditems.items[i].name);
    }
});

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Excel.NamedItemCollection;

参数

propertyNamesAndPaths
OfficeExtension.LoadOption

propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。

返回

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.NamedItemCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.NamedItemCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。

toJSON(): Excel.Interfaces.NamedItemCollectionData;

返回