Skip to content

API: StylePresets ​

Presets ​

NamenumFormatOther stylesUse for
header—Bold, size 12, 1F4E79 fill, white text, centeredHeaders (when applied manually)
currency#,##0.00Right-alignedAmounts
percent0.00%Right-alignedRatios, growth rates
dateyyyy-MM-ddCenteredDate columns
datetimeyyyy-MM-dd HH:mmCenteredDate-time columns
dataRow—Left-aligned, vertically centered, thin BFBFBF bottom borderData rows
bordered—Thin BFBFBF box on all four sidesTable-wide borders
danger—Bold red C00000, centeredRisk / anomalies

Full definitions ​

The presets are plain constant objects — the exact values shipped in src/style-presets.ts:

ts
export const StylePresets = {
  header: {
    font: { bold: true, size: 12, color: "FFFFFF" },
    fill: { pattern: "solid", fgColor: "1F4E79" },
    alignment: { horizontal: "center", vertical: "center" },
  },
  currency: { numFormat: "#,##0.00", alignment: { horizontal: "right" } },
  percent: { numFormat: "0.00%", alignment: { horizontal: "right" } },
  date: { numFormat: "yyyy-MM-dd", alignment: { horizontal: "center" } },
  datetime: {
    numFormat: "yyyy-MM-dd HH:mm",
    alignment: { horizontal: "center" },
  },
  dataRow: {
    alignment: { horizontal: "left", vertical: "center" },
    border: { bottom: { style: "thin", color: "BFBFBF" } },
  },
  bordered: {
    border: {
      top: { style: "thin", color: "BFBFBF" },
      bottom: { style: "thin", color: "BFBFBF" },
      left: { style: "thin", color: "BFBFBF" },
      right: { style: "thin", color: "BFBFBF" },
    },
  },
  danger: {
    font: { color: "C00000", bold: true },
    alignment: { horizontal: "center" },
  },
} as const;

Usage ​

ts
import { exportExcel, StylePresets } from "@marcusok/excel-exporter";

columns: [
  { prop: "amount", label: "Amount", width: 14, style: StylePresets.currency },
  { prop: "rate", label: "Growth", width: 12, style: StylePresets.percent },
  { prop: "date", label: "Date", width: 12, style: StylePresets.date },
  { prop: "flag", label: "Status", width: 10, style: StylePresets.danger },
];

Where to attach a style ​

TargetFieldMerge semantics
One column's data cellsColumnConfig.styledeep-merges over sheet dataStyle
One column's header (group headers included)ColumnConfig.headerStylereplaces sheet headerStyle wholesale
Every data cell (base layer)SheetConfig.dataStyleoverridable field by field by column style
Every header cell (default)SheetConfig.headerStylereplaced wholesale by column headerStyle
The injected index columnIndexColumnOptions.style / .headerStylesame rules as a regular column

Deriving variants ​

Presets are not factories and not frozen — derive variants with object spread:

ts
// Amounts without decimals
style: { ...StylePresets.currency, numFormat: "#,##0" }

// Only change one nested field: spread the nested object too,
// otherwise its siblings are lost (spread is a shallow merge)
style: { ...StylePresets.header, font: { ...StylePresets.header.font, size: 14 } }

The spread is your merge, done before the library sees the config — distinct from the engine's field-level deep merge between dataStyle and a column's style. Ready-made recipes (report template, header overrides, numFormat tweaks): see the Styling guide.

Type ​

ts
import type { StylePresetName } from "@marcusok/excel-exporter";

const name: StylePresetName = "currency"; // "header" | "currency" | "percent" | "date" | "datetime" | "dataRow" | "bordered" | "danger"

StylePresetName parameterizes preset selection:

ts
const columnPresets: Record<string, StylePresetName> = {
  amount: "currency",
  rate: "percent",
  orderDate: "date",
};

const columns = Object.entries(columnPresets).map(([prop, name]) => ({
  prop,
  label: prop,
  style: StylePresets[name],
}));

Column style applies to data cells, not headers. For header styling use the headerStyle field directly (sheet-level SheetConfig.headerStyle sets the default; column-level ColumnConfig.headerStyle overrides it), e.g. headerStyle: StylePresets.header.