xlsx/cell-formula

A workbook carries a formula in cell A3 as an <f> child of the cell element in the worksheet part; cells A1 and A2 supply the operand values.

Library verdicts: python-xlsx: — xlsxjs: pass

Metadata

Feature idxlsx/cell-formula
Formatxlsx
Categorycell-data
Spececma-376-5-part-1 § 18.3.1.40 f

XPath assertions

ID / partPredicateXPathpython-xlsxxlsxjs
formula-cell-expression
xl/worksheets/sheet1.xml
exist
The A3 cell in the first worksheet must have an <f> child whose text is the formula expression 'A1+A2'.
//x:c[@r='A3']/x:f[normalize-space()='A1+A2']

Render assertions

IDPredicateSelectorpython-xlsxxlsxjs
cell-formula-text-presentcss_selector/ match-text = (?:^3$|=A1\+A2)
The rendered sheet must show either the cached computed value '3' or the literal formula text '=A1+A2' (depending on the renderer's showFormulas mode).
section.xlsx td, section.xlsx thpass
sheet-countcss_selector/ equal-count
Exactly one rendered sheet section. Catches renderers that emit zero sheets (hard failure) or duplicate a sheet.
section.xlsx

Generator source

scripts/gen_cell_formula.py

#!/usr/bin/env python3
"""Generate ``fixtures/xlsx/cell-formula.xlsx``.

A minimal one-sheet workbook with cells A1 and A2 supplying operand values
and A3 containing the formula ``=A1+A2``. Designed to satisfy the assertions
in ``features/xlsx/cell-formula.json``.
"""

from __future__ import annotations

from pathlib import Path

from xlsx import Workbook

import datetime as _dt
_REPRODUCIBLE_DT = _dt.datetime(2000, 1, 1, 0, 0, 0)

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "xlsx" / "cell-formula.xlsx"


def main() -> int:
    wb = Workbook()
    ws = wb.active
    ws["A1"] = 1
    ws["A2"] = 2
    ws["A3"] = "=A1+A2"
    _OUT.parent.mkdir(parents=True, exist_ok=True)
    wb.save(_OUT, zip_date_time=_REPRODUCIBLE_DT)
    print(_OUT)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Manifest (expanded)

{
  "$schema": "../manifest.schema.json",
  "id": "xlsx/cell-formula",
  "title": "Formula cell value",
  "format": "xlsx",
  "category": "cell-data",
  "summary": "A workbook carries a formula in cell A3 as an <f> child of the cell element in the worksheet part; cells A1 and A2 supply the operand values.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "18.3.1.40",
    "element": "f",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/SpreadsheetML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/sml.xsd",
    "notes": "Formulas in SpreadsheetML live inside a <c> element as an <f> child whose text is the formula expression (without the leading '='). An Excel-computed cached value may accompany the formula as a sibling <v> child, but authoring libraries typically can't compute the result without a formula engine — python-xlsx emits an empty <v/> placeholder in that slot, Excel fills it on open. The assertion therefore targets only the <f> content. SpreadsheetML parts use a default namespace with no prefix; XPath therefore needs an explicit binding (here 'x')."
  },
  "fixtures": {
    "machine": "xlsx/cell-formula",
    "office": "xlsx/cell-formula"
  },
  "generator": {
    "python": "scripts/gen_cell_formula.py"
  },
  "assertions": [
    {
      "id": "formula-cell-expression",
      "part": "xl/worksheets/sheet1.xml",
      "namespaces": {
        "x": "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
      },
      "xpath": "//x:c[@r='A3']/x:f[normalize-space()='A1+A2']",
      "must": "exist",
      "description": "The A3 cell in the first worksheet must have an <f> child whose text is the formula expression 'A1+A2'."
    }
  ],
  "render_assertions": [
    {
      "id": "cell-formula-text-present",
      "kind": "css_selector",
      "selector": "section.xlsx td, section.xlsx th",
      "must": "match-text",
      "value": "(?:^3$|=A1\\+A2)",
      "description": "The rendered sheet must show either the cached computed value '3' or the literal formula text '=A1+A2' (depending on the renderer's showFormulas mode)."
    },
    {
      "id": "sheet-count",
      "kind": "css_selector",
      "selector": "section.xlsx",
      "must": "equal-count",
      "count": 1,
      "description": "Exactly one rendered sheet section. Catches renderers that emit zero sheets (hard failure) or duplicate a sheet."
    }
  ]
}

Fixture

Download cell-formula.xlsx (4.7 KB)

Reference preview

Reference (machine, page 1 PNG)

xlsx/cell-formula page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

Formulas in SpreadsheetML live inside a <c> element as an <f> child whose text is the formula expression (without the leading '='). An Excel-computed cached value may accompany the formula as a sibling <v> child, but authoring libraries typically can't compute the result without a formula engine — python-xlsx emits an empty <v/> placeholder in that slot, Excel fills it on open. The assertion therefore targets only the <f> content. SpreadsheetML parts use a default namespace with no prefix; XPath therefore needs an explicit binding (here 'x').