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

    Class Cell

    Represents a single cell within a worksheet row. Mutations are applied directly to the underlying data.

    Obtained via Worksheet.cell(). Setting value auto-detects the cell type from the JS type (string, number, boolean).

    const cell = ws.cell('A1');
    cell.value = 'Hello';
    cell.formula = 'UPPER(A2)';
    cell.styleIndex = boldStyleIdx;
    Index
    • get dateValue(): Date | null

      Returns a Date if this cell contains a date-formatted number, or null otherwise. Uses the cell's number format to determine whether the numeric value represents a date, and the workbook's date system (1900 or 1904) for the epoch.

      Returns Date | null

    • get formula(): string | null

      Returns the cell's formula text as stored, or null if no formula is set. A formula read from a file has no leading =; text given to the setter is kept as given, and the writer drops one leading = when it writes <f>.

      Returns string | null

    • set formula(f: string | null): void

      Sets or clears the cell's formula. A formula cell takes the type of its cached value (see Cell.value): "formulaStr" for a string, "number" for a number or no value. Clearing the formula (null, or an empty string, which is stored as null so the getter returns null) removes formulaType, formulaRef and sharedIndex with it, and turns a "formulaStr" cell into "sharedString", or "number" when it holds no value. Formula text set on a shared-formula child (formulaType "shared" or a sharedIndex, and no formulaRef, whether or not it carries formula text of its own) gives that child a plain formula of its own: its formulaType and sharedIndex are removed. New text on any other cell, a shared-formula master (one with a formulaRef) included, keeps those keys. The children of a master follow its text: a new text is theirs on write, and once the master's formula is cleared, a child without text of its own is written with its cached value alone (empty when it holds none) and a child with text of its own as a plain formula.

      Parameters

      • f: string | null

      Returns void

    • get numberFormat(): string | null

      Returns the resolved number format code (e.g., "#,##0.00", "yyyy-mm-dd"), or null if the cell uses the default General format.

      The workbook's own numFmts win, then the built-in table. A built-in date id the table has no code for (27-36, 50-55, 57 and 58) resolves to the code of id 14. Other ids without a code (5-8, 23-26 and 41-44) return null unless the file defines them.

      Returns string | null

    • get richText(): readonly RichTextRun[] | undefined

      Returns the rich text runs for this cell, or undefined if the cell has no rich text formatting. Each run contains a text segment and optional formatting properties (bold, italic, underline, strike, fontName, fontSize, color).

      Returns readonly RichTextRun[] | undefined

    • set richText(runs: readonly RichTextRun[] | undefined): void

      Sets rich text runs on this cell, enabling mixed formatting within a single cell. The cell type is automatically set to "sharedString" and the plain-text value is updated to the concatenation of all run texts. On a shared-formula child (no formula text, but formulaType "shared" or a sharedIndex), the rich text replaces the formula, as a value does: formulaType, formulaRef and sharedIndex are removed.

      Pass undefined to clear rich text.

      Parameters

      Returns void

    • get value(): string | number | boolean | null

      Returns the cell value, coerced to the appropriate JS type based on cellType.

      Returns string | number | boolean | null

    • set value(val: string | number | boolean | null): void

      Sets the cell value and auto-detects the cell type from the JS type. Strings become shared strings, numbers stay numeric, booleans become "1"/"0". Any rich text on the cell is cleared. With a formula set, the value is the cached result and the type follows it: "formulaStr" for a string, "number" for a number or null, "boolean" for a boolean; an "error" formula cell keeps its type. A null value on a cell without a formula makes it a "number" cell.

      On a shared-formula child (no formula text, but formulaType "shared" or a sharedIndex), any value, null included, replaces the formula: formulaType, formulaRef and sharedIndex are removed. A cell with formula text keeps them.

      Parameters

      • val: string | number | boolean | null

      Returns void

      ModernXlsxError with code INVALID_CELL_VALUE for NaN or an infinity.