Excel.RangeFormat class

一个格式对象,其中封装了区域的字体、填充、边框、对齐方式和其他属性。A format object encapsulating the range's font, fill, borders, alignment, and other properties.

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

Extends

属性

autoIndent

指示将文本对齐方式设为相等分布时文本是否会自动缩进。Indicates if text is automatically indented when text alignment is set to equal distribution.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

borders

应用于整个区域的 Border 对象的集合。Collection of border objects that apply to the overall range. 只读。Read-only.

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

columnWidth

获取或设置区域内的所有列的宽度。Gets or sets the width of all colums within the range. 如果列宽不统一,则返回 NULL。If the column widths are not uniform, null will be returned.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

context

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

fill

返回在整个区域内定义的 fill 对象。Returns the fill object defined on the overall range. 只读。Read-only.

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

font

返回在整个区域内定义的 Font 对象。Returns the font object defined on the overall range. 只读。Read-only.

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

horizontalAlignment

表示指定对象的水平对齐方式。Represents the horizontal alignment for the specified object. 有关详细信息, 请参阅 HorizontalAlignment。See Excel.HorizontalAlignment for details.

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

indentLevel

0 到 250 之间的一个整数,指示缩进水平。An integer from 0 to 250 that indicates the indent level.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

protection

返回某一区域的格式 protection 对象。Returns the format protection object for a range. 只读。Read-only.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

readingOrder

区域的读取顺序。The reading order for the range.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

rowHeight

获取或设置区域中所有行的高度。Gets or sets the height of all rows in the range. 如果行高不一致, 则将返回 null。If the row heights are not uniform, null will be returned.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

shrinkToFit

指示文本是否自动缩小以适合可用列宽。Indicates if text automatically shrinks to fit in the available column width.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

textOrientation

获取或设置区域内的所有单元格的文本方向。Gets or sets the text orientation of all the cells within the range. 文本方向应为-90 到90之间的整数, 或者为180垂直方向的文本。The text orientation should be an integer either from -90 to 90, or 180 for vertically-oriented text. 如果区域中的方向不一致, 则将返回 null。If the orientation within a range are not uniform, then null will be returned.

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

useStandardHeight

确定 Range 对象的行高是否等于工作表的标准行高。Determines if the row height of the Range object equals the standard height of the sheet. 如果 Range 对象的行高等于工作表的标准高度, 则返回 True。Returns True if the row height of the Range object equals the standard height of the sheet. 如果区域包含多行, 并且行不是都具有相同的高度, 则返回 Null。Returns Null if the range contains more than one row and the rows aren't all the same height. 否则返回 False。Returns False otherwise.

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

useStandardWidth

指示 Range 对象的列宽是否等于工作表的标准宽度。Indicates whether the column width of the Range object equals the standard width of the sheet. 如果 Range 对象的列宽等于工作表的标准宽度, 则返回 True。Returns True if the column width of the Range object equals the standard width of the sheet. 如果区域中包含多个列, 并且各列的高度不相同, 则返回 Null。Returns Null if the range contains more than one column and the columns aren't all the same height. 否则返回 False。Returns False otherwise.

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

verticalAlignment

表示指定对象的垂直对齐方式。Represents the vertical alignment for the specified object. 有关详细信息, 请参阅 VerticalAlignment。See Excel.VerticalAlignment for details.

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

wrapText

指示 Excel 是否将对象中的文本换行。Indicates if Excel wraps the text in the object. 指示整个区域不具有统一换行设置的空值A null value indicates that the entire range doesn't have uniform wrap setting

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

方法

autofitColumns()

根据列中的当前数据更改当前范围的列宽,以达到最佳宽度。Changes the width of the columns of the current range to achieve the best fit, based on the current data in the columns.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

autofitRows()

根据列中的当前数据,更改当前范围的行高以达到最佳高度。Changes the height of the rows of the current range to achieve the best fit, based on the current data in the columns.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

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给它的对象的方法。尽管原始 RangeFormat 对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.RangeFormatData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.RangeFormat object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.RangeFormatData), 其中包含原始对象中所有已加载的子属性的浅表副本。) that contains shallow copies of any loaded child properties from the original object.

属性详细信息

autoIndent

指示将文本对齐方式设为相等分布时文本是否会自动缩进。Indicates if text is automatically indented when text alignment is set to equal distribution.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

autoIndent: boolean;

属性值

boolean

borders

应用于整个区域的 Border 对象的集合。Collection of border objects that apply to the overall range. 只读。Read-only.

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

readonly borders: Excel.RangeBorderCollection;

属性值

columnWidth

获取或设置区域内的所有列的宽度。Gets or sets the width of all colums within the range. 如果列宽不统一,则返回 NULL。If the column widths are not uniform, null will be returned.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

columnWidth: number;

属性值

number

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

fill

返回在整个区域内定义的 fill 对象。Returns the fill object defined on the overall range. 只读。Read-only.

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

readonly fill: Excel.RangeFill;

属性值

font

返回在整个区域内定义的 Font 对象。Returns the font object defined on the overall range. 只读。Read-only.

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

readonly font: Excel.RangeFont;

属性值

horizontalAlignment

表示指定对象的水平对齐方式。Represents the horizontal alignment for the specified object. 有关详细信息, 请参阅 HorizontalAlignment。See Excel.HorizontalAlignment for details.

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

horizontalAlignment: Excel.HorizontalAlignment | "General" | "Left" | "Center" | "Right" | "Fill" | "Justify" | "CenterAcrossSelection" | "Distributed";

属性值

Excel.HorizontalAlignment | "General" | "Left" | "Center" | "Right" | "Fill" | "Justify" | "CenterAcrossSelection" | "Distributed"

indentLevel

0 到 250 之间的一个整数,指示缩进水平。An integer from 0 to 250 that indicates the indent level.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

indentLevel: number;

属性值

number

protection

返回某一区域的格式 protection 对象。Returns the format protection object for a range. 只读。Read-only.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

readonly protection: Excel.FormatProtection;

属性值

readingOrder

区域的读取顺序。The reading order for the range.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

readingOrder: Excel.ReadingOrder | "Context" | "LeftToRight" | "RightToLeft";

属性值

Excel.ReadingOrder | "Context" | "LeftToRight" | "RightToLeft"

rowHeight

获取或设置区域中所有行的高度。Gets or sets the height of all rows in the range. 如果行高不一致, 则将返回 null。If the row heights are not uniform, null will be returned.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

rowHeight: number;

属性值

number

shrinkToFit

指示文本是否自动缩小以适合可用列宽。Indicates if text automatically shrinks to fit in the available column width.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

shrinkToFit: boolean;

属性值

boolean

textOrientation

获取或设置区域内的所有单元格的文本方向。Gets or sets the text orientation of all the cells within the range. 文本方向应为-90 到90之间的整数, 或者为180垂直方向的文本。The text orientation should be an integer either from -90 to 90, or 180 for vertically-oriented text. 如果区域中的方向不一致, 则将返回 null。If the orientation within a range are not uniform, then null will be returned.

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

textOrientation: number;

属性值

number

示例Examples

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B2:E2");

    // Set textOrientation to either an integer between -90 and 90
    // or to 180 for vertically-oriented text.
    range.format.textOrientation = 90;

    await context.sync();
});

useStandardHeight

确定 Range 对象的行高是否等于工作表的标准行高。Determines if the row height of the Range object equals the standard height of the sheet. 如果 Range 对象的行高等于工作表的标准高度, 则返回 True。Returns True if the row height of the Range object equals the standard height of the sheet. 如果区域包含多行, 并且行不是都具有相同的高度, 则返回 Null。Returns Null if the range contains more than one row and the rows aren't all the same height. 否则返回 False。Returns False otherwise.

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

useStandardHeight: boolean;

属性值

boolean

useStandardWidth

指示 Range 对象的列宽是否等于工作表的标准宽度。Indicates whether the column width of the Range object equals the standard width of the sheet. 如果 Range 对象的列宽等于工作表的标准宽度, 则返回 True。Returns True if the column width of the Range object equals the standard width of the sheet. 如果区域中包含多个列, 并且各列的高度不相同, 则返回 Null。Returns Null if the range contains more than one column and the columns aren't all the same height. 否则返回 False。Returns False otherwise.

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

useStandardWidth: boolean;

属性值

boolean

verticalAlignment

表示指定对象的垂直对齐方式。Represents the vertical alignment for the specified object. 有关详细信息, 请参阅 VerticalAlignment。See Excel.VerticalAlignment for details.

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

verticalAlignment: Excel.VerticalAlignment | "Top" | "Center" | "Bottom" | "Justify" | "Distributed";

属性值

Excel.VerticalAlignment | "Top" | "Center" | "Bottom" | "Justify" | "Distributed"

wrapText

指示 Excel 是否将对象中的文本换行。Indicates if Excel wraps the text in the object. 指示整个区域不具有统一换行设置的空值A null value indicates that the entire range doesn't have uniform wrap setting

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

wrapText: boolean;

属性值

boolean

方法详细信息

autofitColumns()

根据列中的当前数据更改当前范围的列宽,以达到最佳宽度。Changes the width of the columns of the current range to achieve the best fit, based on the current data in the columns.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

autofitColumns(): void;

返回

void

autofitRows()

根据列中的当前数据,更改当前范围的行高以达到最佳高度。Changes the height of the rows of the current range to achieve the best fit, based on the current data in the columns.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

autofitRows(): void;

返回

void

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.RangeFormatLoadOptions): Excel.RangeFormat;

参数

返回

注解

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

load(option?: string | string[]): Excel.RangeFormat-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。load(option?: string | string[]): Excel.RangeFormat - 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.RangeFormat-Where 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。load(option?: { select?: string; expand?: string; }): Excel.RangeFormat - 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.RangeFormat-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.RangeFormat - 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.RangeFormat;

参数

propertyNames
string | string[]

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

返回

示例Examples

// Below example selects all of the Range's format properties. 
Excel.run(function (ctx) { 
    var sheetName = "Sheet1";
    var rangeAddress = "F:G";
    var worksheet = ctx.workbook.worksheets.getItem(sheetName);
    var range = worksheet.getRange(rangeAddress);
    range.load(["format/*", "format/fill", "format/borders", "format/font"]);
    return ctx.sync().then(function() {
        console.log(range.format.wrapText);
        console.log(range.format.fill.color);
        console.log(range.format.font.name);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
// The example below sets font name, fill color and wraps text. 
Excel.run(function (ctx) { 
    var sheetName = "Sheet1";
    var rangeAddress = "F:G";
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    range.format.wrapText = true;
    range.format.font.name = 'Times New Roman';
    range.format.fill.color = '0000FF';
    return ctx.sync(); 
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
// The example below adds grid border around the range.
Excel.run(function (ctx) { 
    var sheetName = "Sheet1";
    var rangeAddress = "F:G";
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    range.format.borders.getItem('InsideHorizontal').style = 'Continuous';
    range.format.borders.getItem('InsideVertical').style = 'Continuous';
    range.format.borders.getItem('EdgeBottom').style = 'Continuous';
    range.format.borders.getItem('EdgeLeft').style = 'Continuous';
    range.format.borders.getItem('EdgeRight').style = 'Continuous';
    range.format.borders.getItem('EdgeTop').style = 'Continuous';
    return ctx.sync(); 
}).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.RangeFormat;

参数

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.RangeFormatUpdateData, options?: OfficeExtension.UpdateOptions): void;

参数

properties
Interfaces.RangeFormatUpdateData

一个 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.RangeFormat): void

set(properties)

基于现有加载的对象同时设置该对象的多个属性。Sets multiple properties on the object at the same time, based on an existing loaded object.

set(properties: Excel.RangeFormat): void;

参数

properties
Excel.RangeFormat

返回

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给它的对象的方法。尽管原始 RangeFormat 对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.RangeFormatData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.RangeFormat object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.RangeFormatData), 其中包含原始对象中所有已加载的子属性的浅表副本。) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Excel.Interfaces.RangeFormatData;

返回