Skip to content

Formula cells should inherit number format from referenced cells #184

@arcaputo3

Description

@arcaputo3

Problem

When a formula references cells with number formatting (e.g., currency), Excel automatically inherits that formatting for the result cell. Currently, xl does not do this - formula cells get General format regardless of their inputs.

Example:

[
  {"op": "put", "ref": "B2", "value": 1000000, "format": "currency"},
  {"op": "put", "ref": "B3", "value": 600000, "format": "currency"},
  {"op": "putf", "ref": "B4", "value": "=B2-B3"}
]

Current behavior: B4 shows 400000 (General format)
Expected behavior: B4 shows $400,000.00 (inherited Currency format)

Workaround

Explicitly style formula cells:

{"op": "putf", "ref": "B4", "value": "=B2-B3"},
{"op": "style", "range": "B4", "numFormat": "currency"}

Proposed Solution

  1. Parse the formula to extract cell references
  2. Look up styles of referenced cells
  3. Determine "dominant" format using Excel's precedence rules:
    • Currency > Percent > Date > Decimal > General
    • If mixed incompatible formats, use General
  4. Apply the dominant format to the formula cell

Scope

This would affect:

  • BatchParser.applyPutFormula()
  • WriteCommands.putFormula()
  • Potentially the streaming writer

Priority

Nice-to-have for Excel parity, but the explicit styling workaround is reasonable for now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions