Skip to content

7. Styles

Styles control the visual formatting of cells. A style is registered once with add_style, which returns a numeric style ID. That ID is then applied to cells, rows, or columns.

add_style(style) / addStyle(style)

Register a style definition and return its style ID. Identical styles are deduplicated: registering the same style twice returns the same ID.

Rust:

rust
use sheetkit::style::*;

let style = Style {
    font: Some(FontStyle {
        name: Some("Arial".to_string()),
        size: Some(12.0),
        bold: true,
        color: Some(StyleColor::Rgb("#FF0000".to_string())),
        ..Default::default()
    }),
    fill: Some(FillStyle {
        pattern: PatternType::Solid,
        fg_color: Some(StyleColor::Rgb("#FFFF00".to_string())),
        bg_color: None,
    }),
    num_fmt: Some(NumFmtStyle::Custom("#,##0.00".to_string())),
    ..Default::default()
};
let style_id: u32 = wb.add_style(&style)?;

TypeScript:

typescript
const styleId = wb.addStyle({
    font: {
        name: "Arial",
        size: 12,
        bold: true,
        color: "#FF0000",
    },
    fill: {
        pattern: "solid",
        fgColor: "#FFFF00",
    },
    customNumFmt: "#,##0.00",
});

set_cell_style / get_cell_style

Apply a style ID to a single cell, or get the style ID currently applied to a cell. Returns None/null for the default style.

Rust:

rust
wb.set_cell_style("Sheet1", "A1", style_id)?;
let current: Option<u32> = wb.get_cell_style("Sheet1", "A1")?;

TypeScript:

typescript
wb.setCellStyle("Sheet1", "A1", styleId);
const current: number | null = wb.getCellStyle("Sheet1", "A1");

Style Components Reference

FontStyle

FieldRust TypeTS TypeDescription
nameOption<String>string?Font family (e.g., "Calibri")
sizeOption<f64>number?Font size in points
boldboolboolean?Bold text
italicboolboolean?Italic text
underlineboolboolean?Underline text
strikethroughboolboolean?Strikethrough text
colorOption<StyleColor>string?Font color

StyleColor (Rust): StyleColor::Rgb("#FF0000".into()), StyleColor::Theme(1), StyleColor::Indexed(8)

Color strings (TypeScript): "#FF0000" (RGB hex), "theme:1" (theme color), "indexed:8" (indexed color)

FillStyle

FieldRust TypeTS TypeDescription
patternPatternTypestring?Fill pattern type
fg_colorOption<StyleColor>string?Foreground color
bg_colorOption<StyleColor>string?Background color

PatternType values:

RustTypeScriptDescription
PatternType::None"none"No fill
PatternType::Solid"solid"Solid fill
PatternType::Gray125"gray125"12.5% gray
PatternType::DarkGray"darkGray"Dark gray
PatternType::MediumGray"mediumGray"Medium gray
PatternType::LightGray"lightGray"Light gray

BorderStyle

Each side (left, right, top, bottom, diagonal) accepts a BorderSideStyle:

FieldRust TypeTS TypeDescription
styleBorderLineStylestring?Line style
colorOption<StyleColor>string?Border color

BorderLineStyle values:

RustTypeScript
BorderLineStyle::Thin"thin"
BorderLineStyle::Medium"medium"
BorderLineStyle::Thick"thick"
BorderLineStyle::Dashed"dashed"
BorderLineStyle::Dotted"dotted"
BorderLineStyle::Double"double"
BorderLineStyle::Hair"hair"
BorderLineStyle::MediumDashed"mediumDashed"
BorderLineStyle::DashDot"dashDot"
BorderLineStyle::MediumDashDot"mediumDashDot"
BorderLineStyle::DashDotDot"dashDotDot"
BorderLineStyle::MediumDashDotDot"mediumDashDotDot"
BorderLineStyle::SlantDashDot"slantDashDot"

Rust example:

rust
use sheetkit::style::*;

let style = Style {
    border: Some(BorderStyle {
        top: Some(BorderSideStyle {
            style: BorderLineStyle::Thin,
            color: Some(StyleColor::Rgb("#000000".to_string())),
        }),
        bottom: Some(BorderSideStyle {
            style: BorderLineStyle::Double,
            color: Some(StyleColor::Rgb("#0000FF".to_string())),
        }),
        ..Default::default()
    }),
    ..Default::default()
};

TypeScript example:

typescript
const styleId = wb.addStyle({
    border: {
        top: { style: "thin", color: "#000000" },
        bottom: { style: "double", color: "#0000FF" },
    },
});

AlignmentStyle

FieldRust TypeTS TypeDescription
horizontalOption<HorizontalAlign>string?Horizontal alignment
verticalOption<VerticalAlign>string?Vertical alignment
wrap_textboolboolean?Enable text wrapping
text_rotationOption<u32>number?Rotation angle in degrees
indentOption<u32>number?Indentation level
shrink_to_fitboolboolean?Shrink text to fit cell width

HorizontalAlign values: General, Left, Center, Right, Fill, Justify, CenterContinuous, Distributed In TypeScript: "general", "left", "center", "right", "fill", "justify", "centerContinuous", "distributed"

VerticalAlign values: Top, Center, Bottom, Justify, Distributed In TypeScript: "top", "center", "bottom", "justify", "distributed"

NumFmtStyle

Number formats control how values are displayed.

Rust:

rust
use sheetkit::style::NumFmtStyle;

// Built-in format by ID
NumFmtStyle::Builtin(9)  // 0%

// Custom format string
NumFmtStyle::Custom("#,##0.00".to_string())

TypeScript:

Use numFmtId for built-in formats or customNumFmt for custom format strings on the style object:

typescript
// Built-in format
wb.addStyle({ numFmtId: 9 }); // 0%

// Custom format
wb.addStyle({ customNumFmt: "#,##0.00" });

Common built-in format IDs:

IDFormatDescription
0GeneralGeneral
10Integer
20.002 decimal places
3#,##0Thousands separator
4#,##0.00Thousands with 2 decimals
90%Percentage
100.00%Percentage with 2 decimals
110.00E+00Scientific notation
14m/d/yyyyDate
15d-mmm-yyDate
20h:mmTime
21h:mm:ssTime
22m/d/yyyy h:mmDate and time
49@Text

ProtectionStyle

FieldRust TypeTS TypeDescription
lockedboolboolean?Lock the cell (default: true)
hiddenboolboolean?Hide formulas in protected sheet view

Released under the MIT / Apache-2.0 License.