StyleManager
Manages a workbook’s styles array with efficient deduplication.
This class stores complete styles including number format properties. Cell objects can override these formats using their userZ/formulaZ properties.
The cache uses property-based keys (sorted properties + values) rather than JSON.stringify to ensure deterministic hashing regardless of property insertion order.
A separate collection of NamedStyles is exposed via named.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new StyleManager(initialStyles?, initialNamedStyles?): StyleManager;Create a new StyleManager.
Styles index 0 is the workbook DEFAULT style (XLSX cellXf 0, ECMA-376
18.8.10): what every cell without an explicit s reference displays as.
When no initial styles are given (a workbook built from scratch), slot 0
is seeded with the empty default style {} so that findOrCreate
can never hand index 0 to the first non-default style anything creates.
When initial styles are given (a loaded workbook), styles[0] is the
file’s own default and is left untouched.
Parameters
Section titled “Parameters”initialStyles?
Section titled “initialStyles?”StyleRelaxed[] = []
Optional array of styles to initialize the anonymous pool with. The dedup cache is pre-populated.
initialNamedStyles?
Section titled “initialNamedStyles?”Record<string, NamedStyle> = {}
Optional map of named styles keyed by name. Each Record key must
match its entry’s ns.name (case-insensitively) and must be unique case-insensitively;
mismatches and duplicates are rejected. Keys are normalised to lowercase internally; the
original casing is preserved on each NamedStyle.name.
Returns
Section titled “Returns”StyleManager
Properties
Section titled “Properties”readonly named: NamedStyles;Named-style CRUD. See NamedStyles.
Accessors
Section titled “Accessors”defaultStyle
Section titled “defaultStyle”Get Signature
Section titled “Get Signature”get defaultStyle(): StyleRelaxed;The workbook default style: the style at index 0, which every cell without
an explicit s reference displays as (XLSX cellXf 0, ECMA-376 18.8.10),
including the workbook default font.
Returns
Section titled “Returns”StyleRelaxed
length
Section titled “length”Get Signature
Section titled “Get Signature”get length(): number;Get the number of styles in the array.
Returns
Section titled “Returns”number
Methods
Section titled “Methods”findOrCreate()
Section titled “findOrCreate()”findOrCreate(style): number;Find an existing style or create a new one.
This method first checks the cache for an exact match. If found, returns the existing style’s index. If not found, appends the style to the array, updates the cache, and returns the new index.
style.extendsStyle is canonicalized to the casing registered on the
named style before the cache lookup, so two references that differ only
in case dedupe to the same list entry.
Parameters
Section titled “Parameters”StyleRelaxed
The style object to find or create
Returns
Section titled “Returns”number
The index of the style in the styles array
Throws
Section titled “Throws”if style.extendsStyle references a named style that does not exist.
get(index): StyleRelaxed | null;Get a style by its index.
Parameters
Section titled “Parameters”number
Zero-based index into the styles array
Returns
Section titled “Returns”StyleRelaxed | null
The style object, or null if the index is out of bounds
setDefaultStyle()
Section titled “setDefaultStyle()”setDefaultStyle(style): void;Replace the workbook default style (index 0) in place.
This is workbook-wide: it changes the appearance of every cell without an
explicit style reference. The dedup cache is rebuilt so findOrCreate of
the new default returns 0 and the old default no longer resolves to it.
The default style may not carry extendsStyle; a workbook default cannot
inherit from a named style.
Parameters
Section titled “Parameters”StyleRelaxed
The new default style.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”if style carries extendsStyle.
toArray()
Section titled “toArray()”toArray(): readonly StyleRelaxed[];Get a list of all defined styles.
Returns the internal array, typed as readonly to prohibit external code from modifying the array without updating the cache.
Returns
Section titled “Returns”readonly StyleRelaxed[]
Readonly array of styles