Excel.Style class
An object encapsulating a style's format and other properties.
- Extends
Remarks
Properties
| auto |
Specifies if text is automatically indented when the text alignment in a cell is set to equal distribution. |
| borders | A collection of four border objects that represent the style of the four borders. |
| built |
Specifies if the style is a built-in style. |
| context | The request context associated with the object. This connects the add-in's process to the Office host application's process. |
| fill | The fill of the style. |
| font | A |
| formula |
Specifies if the formula will be hidden when the worksheet is protected. |
| horizontal |
Represents the horizontal alignment for the style. See |
| include |
Specifies if the style includes the auto indent, horizontal alignment, vertical alignment, wrap text, indent level, and text orientation properties. |
| include |
Specifies if the style includes the color, color index, line style, and weight border properties. |
| include |
Specifies if the style includes the background, bold, color, color index, font style, italic, name, size, strikethrough, subscript, superscript, and underline font properties. |
| include |
Specifies if the style includes the number format property. |
| include |
Specifies if the style includes the color, color index, invert if negative, pattern, pattern color, and pattern color index interior properties. |
| include |
Specifies if the style includes the formula hidden and locked protection properties. |
| indent |
An integer from 0 to 250 that indicates the indent level for the style. |
| locked | Specifies if the object is locked when the worksheet is protected. |
| name | The name of the style. |
| number |
The format code of the number format for the style. |
| number |
The localized format code of the number format for the style. |
| reading |
The reading order for the style. |
| shrink |
Specifies if text automatically shrinks to fit in the available column width. |
| text |
The text orientation for the style. |
| vertical |
Specifies the vertical alignment for the style. See |
| wrap |
Specifies if Excel wraps the text in the object. |
Methods
| delete() | Deletes this style. |
| load(options) | Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| set(properties, options) | Sets multiple properties of an object at the same time. 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() | Overrides the JavaScript |
Property Details
autoIndent
Specifies if text is automatically indented when the text alignment in a cell is set to equal distribution.
autoIndent: boolean;
Property Value
boolean
Remarks
borders
A collection of four border objects that represent the style of the four borders.
readonly borders: Excel.RangeBorderCollection;
Property Value
Remarks
builtIn
Specifies if the style is a built-in style.
readonly builtIn: boolean;
Property Value
boolean
Remarks
context
The request context associated with the object. This connects the add-in's process to the Office host application's process.
context: RequestContext;
Property Value
fill
The fill of the style.
readonly fill: Excel.RangeFill;
Property Value
Remarks
font
A Font object that represents the font of the style.
readonly font: Excel.RangeFont;
Property Value
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/style.yaml
await Excel.run(async (context) => {
let style = context.workbook.styles.getItem("Normal");
style.font.load("bold, color, italic, name, size");
style.fill.load("color");
await context.sync();
console.log("Bold: " + style.font.bold);
console.log("Font color: " + style.font.color);
console.log("Italic: " + style.font.italic);
console.log("Name: " + style.font.name);
console.log("Size: " + style.font.size);
console.log("Fill color: " + style.fill.color);
});
formulaHidden
Specifies if the formula will be hidden when the worksheet is protected.
formulaHidden: boolean;
Property Value
boolean
Remarks
horizontalAlignment
Represents the horizontal alignment for the style. See Excel.HorizontalAlignment for details.
horizontalAlignment: Excel.HorizontalAlignment | "General" | "Left" | "Center" | "Right" | "Fill" | "Justify" | "CenterAcrossSelection" | "Distributed";
Property Value
Excel.HorizontalAlignment | "General" | "Left" | "Center" | "Right" | "Fill" | "Justify" | "CenterAcrossSelection" | "Distributed"
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/style.yaml
await Excel.run(async (context) => {
let worksheet = context.workbook.worksheets.getItem("Sample");
let range = worksheet.getRange("A1:E1");
// Apply built-in style.
// Styles are in the Home tab ribbon.
range.style = Excel.BuiltInStyle.neutral;
range.format.horizontalAlignment = "Right";
await context.sync();
});
includeAlignment
Specifies if the style includes the auto indent, horizontal alignment, vertical alignment, wrap text, indent level, and text orientation properties.
includeAlignment: boolean;
Property Value
boolean
Remarks
includeBorder
Specifies if the style includes the color, color index, line style, and weight border properties.
includeBorder: boolean;
Property Value
boolean
Remarks
includeFont
Specifies if the style includes the background, bold, color, color index, font style, italic, name, size, strikethrough, subscript, superscript, and underline font properties.
includeFont: boolean;
Property Value
boolean
Remarks
includeNumber
Specifies if the style includes the number format property.
includeNumber: boolean;
Property Value
boolean
Remarks
includePatterns
Specifies if the style includes the color, color index, invert if negative, pattern, pattern color, and pattern color index interior properties.
includePatterns: boolean;
Property Value
boolean
Remarks
includeProtection
Specifies if the style includes the formula hidden and locked protection properties.
includeProtection: boolean;
Property Value
boolean
Remarks
indentLevel
An integer from 0 to 250 that indicates the indent level for the style.
indentLevel: number;
Property Value
number
Remarks
locked
Specifies if the object is locked when the worksheet is protected.
locked: boolean;
Property Value
boolean
Remarks
name
The name of the style.
readonly name: string;
Property Value
string
Remarks
numberFormat
The format code of the number format for the style.
numberFormat: string;
Property Value
string
Remarks
numberFormatLocal
The localized format code of the number format for the style.
numberFormatLocal: string;
Property Value
string
Remarks
readingOrder
The reading order for the style.
readingOrder: Excel.ReadingOrder | "Context" | "LeftToRight" | "RightToLeft";
Property Value
Excel.ReadingOrder | "Context" | "LeftToRight" | "RightToLeft"
Remarks
shrinkToFit
Specifies if text automatically shrinks to fit in the available column width.
shrinkToFit: boolean;
Property Value
boolean
Remarks
textOrientation
The text orientation for the style.
textOrientation: number;
Property Value
number
Remarks
verticalAlignment
Specifies the vertical alignment for the style. See Excel.VerticalAlignment for details.
verticalAlignment: Excel.VerticalAlignment | "Top" | "Center" | "Bottom" | "Justify" | "Distributed";
Property Value
Excel.VerticalAlignment | "Top" | "Center" | "Bottom" | "Justify" | "Distributed"
Remarks
wrapText
Specifies if Excel wraps the text in the object.
wrapText: boolean;
Property Value
boolean
Remarks
Method Details
delete()
Deletes this style.
delete(): void;
Returns
void
Remarks
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/style.yaml
await Excel.run(async (context) => {
let style = context.workbook.styles.getItem("Diagonal Orientation Style");
// Delete the diagonal orientation style from the style collection.
// Styles are in the Home tab ribbon.
style.delete();
await context.sync();
console.log("Successfully deleted the diagonal orientation style from the Home tab ribbon.");
});
load(options)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(options?: Excel.Interfaces.StyleLoadOptions): Excel.Style;
Parameters
Provides options for which properties of the object to load.
Returns
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/style.yaml
await Excel.run(async (context) => {
let style = context.workbook.styles.getItem("Diagonal Orientation Style");
style.load("textOrientation, horizontalAlignment, autoIndent, readingOrder, wrapText, includeProtection, shrinkToFit, locked");
await context.sync();
console.log("Orientation: " + style.textOrientation);
console.log("Horizontal alignment: " + style.horizontalAlignment);
console.log("Add indent: " + style.autoIndent);
console.log("Reading order: " + style.readingOrder);
console.log("Wrap text: " + style.wrapText);
console.log("Include protection: " + style.includeProtection);
console.log("Shrink to fit: " + style.shrinkToFit);
console.log("Style locked: " + style.locked);
});
load(propertyNames)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNames?: string | string[]): Excel.Style;
Parameters
- propertyNames
-
string | string[]
A comma-delimited string or an array of strings that specify the properties to load.
Returns
load(propertyNamesAndPaths)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Excel.Style;
Parameters
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
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.
Returns
set(properties, options)
Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.
set(properties: Interfaces.StyleUpdateData, options?: OfficeExtension.UpdateOptions): void;
Parameters
- properties
- Excel.Interfaces.StyleUpdateData
A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.
- options
- OfficeExtension.UpdateOptions
Provides an option to suppress errors if the properties object tries to set any read-only properties.
Returns
void
set(properties)
Sets multiple properties on the object at the same time, based on an existing loaded object.
set(properties: Excel.Style): void;
Parameters
- properties
- Excel.Style
Returns
void
toJSON()
Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Excel.Style object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.StyleData) that contains shallow copies of any loaded child properties from the original object.
toJSON(): Excel.Interfaces.StyleData;