ExVEx.OOXML.Styles (ExVEx v0.2.0)

Copy Markdown View Source

Parser for xl/styles.xml.

The OOXML style model is an indirection graph: a cell carries a style index s="N" into cellXfs; each <xf> there points at a numFmt, font, fill, and border by further indices.

ExVEx parses the indirection arrays (numFmts, fonts, fills, borders, cellXfs) and exposes resolve/2 to flatten an <xf> index into a user-facing %ExVEx.Style{} record.

Built-in number format IDs (0..163) are not listed in numFmts at all; workbook-specific custom formats start at ID 164.

Summary

Functions

Decides whether a cell format represents a date (or date-time / time).

The format code string for a given numFmt id, resolving built-ins.

Dereferences a cell's style index into a flat %ExVEx.Style{} record. Returns a default style for nil (an unstyled cell).

Re-serialises the stylesheet into its original XML container, rewriting the <numFmts>, <fonts>, <fills>, <borders>, and <cellXfs> sections from the in-memory struct. Elements we don't model (<cellStyles>, <dxfs>, <tableStyles>, extension lists, etc.) are preserved verbatim.

Finds-or-adds a border. Returns {index, updated_styles}.

Finds-or-adds a full <xf> record. Returns {index, styles}.

Adds (or finds) an <xf> with the given numFmtId sitting on top of the default font / fill / border. Returns {index, updated_styles} — the index is the value that goes into a cell's s attribute.

Finds-or-adds a fill. Returns {index, updated_styles}.

Finds-or-adds a font. Returns {index, updated_styles}.

Finds-or-adds a number format code and returns {numFmtId, styles}. Recognises built-in codes and returns the built-in id without adding anything to the custom list.

Types

t()

@type t() :: %ExVEx.OOXML.Styles{
  borders: [ExVEx.Style.Border.t()],
  cell_formats: [ExVEx.OOXML.Styles.CellFormat.t()],
  fills: [ExVEx.Style.Fill.t()],
  fonts: [ExVEx.Style.Font.t()],
  num_fmts: [ExVEx.OOXML.Styles.NumFmt.t()]
}

Functions

cell_format(styles, index)

@spec cell_format(t(), non_neg_integer()) ::
  {:ok, ExVEx.OOXML.Styles.CellFormat.t()} | :error

date_format?(styles, cell_format)

@spec date_format?(t(), ExVEx.OOXML.Styles.CellFormat.t()) :: boolean()

Decides whether a cell format represents a date (or date-time / time).

format_code(styles, id)

@spec format_code(t(), non_neg_integer()) :: String.t()

The format code string for a given numFmt id, resolving built-ins.

parse(xml)

@spec parse(binary()) :: {:ok, t()} | {:error, term()}

resolve(styles, index)

@spec resolve(t(), non_neg_integer() | nil) :: ExVEx.Style.t()

Dereferences a cell's style index into a flat %ExVEx.Style{} record. Returns a default style for nil (an unstyled cell).

serialize_into(styles, original_xml)

@spec serialize_into(t(), binary()) :: binary()

Re-serialises the stylesheet into its original XML container, rewriting the <numFmts>, <fonts>, <fills>, <borders>, and <cellXfs> sections from the in-memory struct. Elements we don't model (<cellStyles>, <dxfs>, <tableStyles>, extension lists, etc.) are preserved verbatim.

upsert_border(styles, border)

@spec upsert_border(t(), ExVEx.Style.Border.t()) :: {non_neg_integer(), t()}

Finds-or-adds a border. Returns {index, updated_styles}.

upsert_cell_format(styles, xf)

@spec upsert_cell_format(t(), ExVEx.OOXML.Styles.CellFormat.t()) ::
  {non_neg_integer(), t()}

Finds-or-adds a full <xf> record. Returns {index, styles}.

upsert_date_format(styles, num_fmt_id)

@spec upsert_date_format(t(), non_neg_integer()) :: {non_neg_integer(), t()}

Adds (or finds) an <xf> with the given numFmtId sitting on top of the default font / fill / border. Returns {index, updated_styles} — the index is the value that goes into a cell's s attribute.

upsert_fill(styles, fill)

@spec upsert_fill(t(), ExVEx.Style.Fill.t()) :: {non_neg_integer(), t()}

Finds-or-adds a fill. Returns {index, updated_styles}.

upsert_font(styles, font)

@spec upsert_font(t(), ExVEx.Style.Font.t()) :: {non_neg_integer(), t()}

Finds-or-adds a font. Returns {index, updated_styles}.

upsert_num_fmt(styles, format_code)

@spec upsert_num_fmt(t(), String.t()) :: {non_neg_integer(), t()}

Finds-or-adds a number format code and returns {numFmtId, styles}. Recognises built-in codes and returns the built-in id without adding anything to the custom list.