Excel.RangeFormat class

A format object encapsulating the range's font, fill, borders, alignment, and other properties.

[ API set: ExcelApi 1.1 ]

Extends
OfficeExtension.ClientObject

Properties

borders

Collection of border objects that apply to the overall range. Read-only.

[ API set: ExcelApi 1.1 ]

columnWidth

Gets or sets the width of all colums within the range. If the column widths are not uniform, null will be returned.

[ API set: ExcelApi 1.2 ]

fill

Returns the fill object defined on the overall range. Read-only.

[ API set: ExcelApi 1.1 ]

font

Returns the font object defined on the overall range. Read-only.

[ API set: ExcelApi 1.1 ]

horizontalAlignment

Represents the horizontal alignment for the specified object. See Excel.HorizontalAlignment for details.

[ API set: ExcelApi 1.1 ]

protection

Returns the format protection object for a range. Read-only.

[ API set: ExcelApi 1.2 ]

rowHeight

Gets or sets the height of all rows in the range. If the row heights are not uniform, null will be returned.

[ API set: ExcelApi 1.2 ]

textOrientation

Gets or sets the text orientation of all the cells within the range. The text orientation should be an integer either from -90 to 90, or 180 for vertically-oriented text. If the orientation within a range are not uniform, then null will be returned.

[ API set: ExcelApi 1.7 ]

useStandardHeight

Determines if the row height of the Range object equals the standard height of the sheet. Returns True if the row height of the Range object equals the standard height of the sheet. Returns Null if the range contains more than one row and the rows aren't all the same height. Returns False otherwise.

[ API set: ExcelApi 1.7 ]

useStandardWidth

Indicates whether the column width of the Range object equals the standard width of the sheet. Returns True if the column width of the Range object equals the standard width of the sheet. Returns Null if the range contains more than one column and the columns aren't all the same height. Returns False otherwise.

[ API set: ExcelApi 1.7 ]

verticalAlignment

Represents the vertical alignment for the specified object. See Excel.VerticalAlignment for details.

[ API set: ExcelApi 1.1 ]

wrapText

Indicates if Excel wraps the text in the object. A null value indicates that the entire range doesn't have uniform wrap setting

[ API set: ExcelApi 1.1 ]

Methods

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 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 set: ExcelApi 1.2 ]

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

borders

Collection of border objects that apply to the overall range. Read-only.

[ API set: ExcelApi 1.1 ]

readonly borders: Excel.RangeBorderCollection;
Property Value

columnWidth

Gets or sets the width of all colums within the range. If the column widths are not uniform, null will be returned.

[ API set: ExcelApi 1.2 ]

columnWidth: number;
Property Value
number

fill

Returns the fill object defined on the overall range. Read-only.

[ API set: ExcelApi 1.1 ]

readonly fill: Excel.RangeFill;
Property Value

font

Returns the font object defined on the overall range. Read-only.

[ API set: ExcelApi 1.1 ]

readonly font: Excel.RangeFont;
Property Value

horizontalAlignment

Represents the horizontal alignment for the specified object. See Excel.HorizontalAlignment for details.

[ API set: ExcelApi 1.1 ]

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

protection

Returns the format protection object for a range. Read-only.

[ API set: ExcelApi 1.2 ]

readonly protection: Excel.FormatProtection;
Property Value

rowHeight

Gets or sets the height of all rows in the range. If the row heights are not uniform, null will be returned.

[ API set: ExcelApi 1.2 ]

rowHeight: number;
Property Value
number

textOrientation

Gets or sets the text orientation of all the cells within the range. The text orientation should be an integer either from -90 to 90, or 180 for vertically-oriented text. If the orientation within a range are not uniform, then null will be returned.

[ API set: ExcelApi 1.7 ]

textOrientation: number;
Property Value
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

Determines if the row height of the Range object equals the standard height of the sheet. Returns True if the row height of the Range object equals the standard height of the sheet. Returns Null if the range contains more than one row and the rows aren't all the same height. Returns False otherwise.

[ API set: ExcelApi 1.7 ]

useStandardHeight: boolean;
Property Value
boolean

useStandardWidth

Indicates whether the column width of the Range object equals the standard width of the sheet. Returns True if the column width of the Range object equals the standard width of the sheet. Returns Null if the range contains more than one column and the columns aren't all the same height. Returns False otherwise.

[ API set: ExcelApi 1.7 ]

useStandardWidth: boolean;
Property Value
boolean

verticalAlignment

Represents the vertical alignment for the specified object. See Excel.VerticalAlignment for details.

[ API set: ExcelApi 1.1 ]

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

wrapText

Indicates if Excel wraps the text in the object. A null value indicates that the entire range doesn't have uniform wrap setting

[ API set: ExcelApi 1.1 ]

wrapText: boolean;
Property Value
boolean

Method Details

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 set: ExcelApi 1.2 ]

autofitColumns(): void;
Returns
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 set: ExcelApi 1.2 ]

autofitRows(): void;
Returns
void

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.RangeFormat;
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.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 - 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

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

toJSON()

toJSON(): Excel.Interfaces.RangeFormatData;
Returns