Excel.NamedItem class

表示单元格区域或值的定义名称。Represents a defined name for a range of cells or value. 名称可以是命名为对象的基元 (如下面的类型中所示)、range 对象或对区域的引用。Names can be primitive named objects (as seen in the type below), range object, or a reference to a range. 此对象可用于获取与名称相关的 range 对象。This object can be used to obtain range object associated with names.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

Extends

属性

arrayValues

返回包含已命名项目的值和类型的对象。Returns an object containing values and types of the named item. 只读。Read-only.

[API 集: ExcelApi 1.7][ API set: ExcelApi 1.7 ]

comment

表示与此名称相关联的注释。Represents the comment associated with this name.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。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 集: ExcelApi 1.7][ API set: ExcelApi 1.7 ]

name

对象的名称。The name of the object. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

scope

指明是否将 name 限定到工作簿或特定工作表。Indicates whether the name is scoped to the workbook or to a specific worksheet. 可能的值为: 工作表、工作簿。Possible values are: Worksheet, Workbook. 只读。Read-only.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

type

指明 name 公式返回的值的类型。Indicates the type of the value returned by the name's formula. 有关详细信息, 请参阅 NamedItemType。See Excel.NamedItemType for details. 只读。Read-only.

[API 集: 字符串、整型、双精度、布尔值、范围、错误的 ExcelApi 1.1、数组的 1.7][ API set: ExcelApi 1.1 for String,Integer,Double,Boolean,Range,Error; 1.7 for Array ]

value

表示 name 公式计算出的值。Represents the value computed by the name's formula. 对于已命名区域,将返回区域地址。For a named range, will return the range address. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

visible

指定对象是否可见。Specifies whether the object is visible or not.

[API 集: ExcelApi 1.1][ 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 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

worksheetOrNullObject

返回已命名项限定到的工作表。Returns the worksheet on which the named item is scoped to. 如果项改为限定到工作簿,将返回 NULL 对象。Returns a null object if the item is scoped to the workbook instead.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

方法

delete()

删除给定的名称。Deletes the given name.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

getRange()

返回与名称相关的 range 对象。Returns the range object that is associated with the name. 如果已命名项的类型不是 Range,将引发错误。Throws an error if the named item's type is not a range.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getRangeOrNullObject()

返回与名称相关联的 Range 对象。Returns the range object that is associated with the name. 如果已命名项的类型不是 Range,将返回 NULL 对象。Returns a null object if the named item's type is not a range.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

set(properties, options)

同时设置一个对象的多个属性。Sets multiple properties of an object at the same time. 您可以传递具有相应属性的纯对象或相同类型的其他 API 对象。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()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始 NamedItem 对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.NamedItemData, 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.

属性详细信息

arrayValues

返回包含已命名项目的值和类型的对象。Returns an object containing values and types of the named item. 只读。Read-only.

[API 集: ExcelApi 1.7][ API set: ExcelApi 1.7 ]

readonly arrayValues: Excel.NamedItemArrayValues;

属性值

comment

表示与此名称相关联的注释。Represents the comment associated with this name.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

comment: string;

属性值

string

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

context: RequestContext;

属性值

RequestContext

formula

获取或设置的已命名项目的公式。Gets or sets the formula of the named item. 公式始终以等号 (=) 开头。Formula always starts with a '=' sign.

[API 集: ExcelApi 1.7][ API set: ExcelApi 1.7 ]

formula: any;

属性值

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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

readonly name: string;

属性值

string

scope

指明是否将 name 限定到工作簿或特定工作表。Indicates whether the name is scoped to the workbook or to a specific worksheet. 可能的值为: 工作表、工作簿。Possible values are: Worksheet, Workbook. 只读。Read-only.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

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

属性值

Excel.NamedItemScope | "Worksheet" | "Workbook"

type

指明 name 公式返回的值的类型。Indicates the type of the value returned by the name's formula. 有关详细信息, 请参阅 NamedItemType。See Excel.NamedItemType for details. 只读。Read-only.

[API 集: 字符串、整型、双精度、布尔值、范围、错误的 ExcelApi 1.1、数组的 1.7][ 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";

属性值

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

value

表示 name 公式计算出的值。Represents the value computed by the name's formula. 对于已命名区域,将返回区域地址。For a named range, will return the range address. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

readonly value: any;

属性值

any

visible

指定对象是否可见。Specifies whether the object is visible or not.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

visible: boolean;

属性值

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 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

readonly worksheet: Excel.Worksheet;

属性值

worksheetOrNullObject

返回已命名项限定到的工作表。Returns the worksheet on which the named item is scoped to. 如果项改为限定到工作簿,将返回 NULL 对象。Returns a null object if the item is scoped to the workbook instead.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

readonly worksheetOrNullObject: Excel.Worksheet;

属性值

方法详细信息

delete()

删除给定的名称。Deletes the given name.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

delete(): void;

返回

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

返回与名称相关的 range 对象。Returns the range object that is associated with the name. 如果已命名项的类型不是 Range,将引发错误。Throws an error if the named item's type is not a range.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getRange(): Excel.Range;

返回

示例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()

返回与名称相关联的 Range 对象。Returns the range object that is associated with the name. 如果已命名项的类型不是 Range,将返回 NULL 对象。Returns a null object if the named item's type is not a range.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

getRangeOrNullObject(): Excel.Range;

返回

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

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

参数

返回

注解

除了此签名之外, 此方法还具有以下签名:In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.NamedItem-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。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 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。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-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.NamedItem - Only available on collection types. 它类似于前面的签名。It is similar to the preceding signature. top 指定结果中可包含的集合项的最大数量。Option.top specifies the maximum number of collection items that can be included in the result. 选项。 skip 指定要跳过且不包含在结果中的项目数。Option.skip specifies the number of items that are to be skipped and not included in the result. 如果指定了 top 选项, 则在跳过指定数目的项目后将启动结果集。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. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

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

参数

propertyNames
string | string[]

一个以逗号分隔的字符串或字符串数组, 用于指定要加载的属性。A comma-delimited string or an array of strings that specify the properties to load.

返回

示例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. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNamesAndPaths?: { select?: string; expand?: string; }): Excel.NamedItem;

参数

propertyNamesAndPaths
{ select?: string; expand?: string; }

其中, propertyNamesAndPaths 是用逗号分隔的字符串, 用于指定要加载的属性和 propertyNamesAndPaths。扩展是一个用逗号分隔的字符串, 用于指定要加载的导航属性。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.

返回

set(properties, options)

同时设置一个对象的多个属性。Sets multiple properties of an object at the same time. 您可以传递具有相应属性的纯对象或相同类型的其他 API 对象。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;

参数

properties
Interfaces.NamedItemUpdateData

一个 JavaScript 对象, 其属性的结构 isomorphically 为在其上调用方法的对象的属性。A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.

options
OfficeExtension.UpdateOptions

提供一个选项, 以在 properties 对象尝试设置任何只读属性时禁止显示错误。Provides an option to suppress errors if the properties object tries to set any read-only properties.

返回

void

注解

此方法具有以下附加签名: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;

参数

properties
Excel.NamedItem

返回

void

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始 NamedItem 对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.NamedItemData, 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;

返回