modern-xlsx API Reference - v1.3.0
    Preparing search index...

    Class StyleBuilder

    Fluent builder for constructing cell styles (font, fill, border, alignment, etc.).

    Chain setter methods then call build to register the style in a StylesData object and obtain a reusable style index.

    const idx = new StyleBuilder()
    .font({ bold: true, color: 'FF0000' })
    .fill({ pattern: 'solid', fgColor: 'FFFF00' })
    .border({ bottom: { style: 'thin', color: '000000' } })
    .build(wb.styles);
    ws.cell('A1').styleIndex = idx;
    Index
    • Set border styles and colors for each side (left, right, top, bottom).

      Parameters

      • opts: {
            bottom?: { color?: string | null; style: BorderStyle };
            left?: { color?: string | null; style: BorderStyle };
            right?: { color?: string | null; style: BorderStyle };
            top?: { color?: string | null; style: BorderStyle };
        }

        Border definitions for each side.

      Returns this

      this for chaining.

      builder.border({
      bottom: { style: 'double', color: '000000' },
      top: { style: 'thin' },
      });
    • Build the style and register it in the given styles data object.

      Equal fonts, fills, borders and cell formats are reused, so identical builds return the same index; an entry that differs from every existing one is appended. A number format code already in styles.numFmts is reused; a new code gets the id after the highest existing one (at least 164). Without numberFormat the format is General (id 0).

      Parameters

      • styles: StylesData

        The workbook's shared StylesData (typically wb.styles).

      Returns number

      The zero-based style index to assign to cell.styleIndex.

      const idx = new StyleBuilder()
      .font({ bold: true })
      .build(wb.styles);
      ws.cell('A1').styleIndex = idx;
    • Set fill properties (pattern type, foreground color, background color).

      Parameters

      • opts: { bgColor?: string | null; fgColor?: string | null; pattern?: PatternType }

        Fill options with pattern type and colors as hex RGB (RRGGBB) or ARGB (AARRGGBB) strings, with or without a leading #, written as uppercase ARGB (FF alpha added for RRGGBB) and read back in that form.

      Returns this

      this for chaining.

      builder.fill({ pattern: 'solid', fgColor: '4472C4' });
      
    • Set font properties (name, size, bold, italic, color, etc.).

      build writes name, size, bold, italic, underline, strike and color, with 'Calibri', 11, false and no colour as the defaults; the other FontData members (underlineStyle, colorSpec, vertAlign, ...) are not carried into the built font.

      Parameters

      • opts: Partial<FontData>

        Partial font data to merge into the current font.

      Returns this

      this for chaining.

      builder.font({ name: 'Arial', size: 14, bold: true });