Skip to content

Fix template calcChain regeneration and $= formula cells (MiniExcel.OpenXml) - #993

Open
martin-vitous wants to merge 1 commit into
mini-software:masterfrom
martin-vitous:fix/template-calcchain-formula-cells-v2
Open

Fix template calcChain regeneration and $= formula cells (MiniExcel.OpenXml)#993
martin-vitous wants to merge 1 commit into
mini-software:masterfrom
martin-vitous:fix/template-calcchain-formula-cells-v2

Conversation

@martin-vitous

@martin-vitous martin-vitous commented Jul 28, 2026

Copy link
Copy Markdown

Ports the v1.x template-render fixes to the v2 MiniExcel.OpenXml line (the engine moved to src/MiniExcel.OpenXml/ with a LINQ-to-XML rewrite).

Two defects made Excel reject an otherwise-valid rendered package:

  • $= formula cells kept their source t="inlineStr" attribute after the inline string was replaced with an <f> child. A cell typed inlineStr with no <is> is schema-invalid. The attribute is now removed.
  • The regenerated calcChain entry took the cell address from the running child-index, which is wrong for sparse rows (cells with no content are not emitted, so position ≠ column). The address is now read from the cell's own r attribute.
  • The template's own calcChain was reused/copied and could be written empty. Row insertion shifts every formula cell, so copied entries point at stale addresses; and an empty calcChain (no rendered formulas) is schema-invalid. The chain is now regenerated only when formulas were rendered, and dropped entirely otherwise — Excel rebuilds it on open.

The <f> namespace fix from the v1 change is not needed here: v2's XElement-based serialization already emits it in the spreadsheetml namespace.

Existing MiniExcelTemplateTests / MiniExcelTemplateAsyncTests stay green on net8/9/10.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of formula cells when generating Excel files, including files with sparse rows.
    • Fixed formula conversion to produce valid OpenXML structures.
    • Prevented outdated calculation-chain data from being copied into generated workbooks.

…cel.OpenXml)

Porting the v1.x template-render fixes to the v2 OpenXml line, where the
engine moved to src/MiniExcel.OpenXml/ and a LINQ-to-XML rewrite.

Two defects made Excel reject an otherwise-valid rendered package:

- $= formula cells kept their source t="inlineStr" attribute after the
  inline string was replaced with an <f> child — a cell typed inlineStr
  with no <is> is schema-invalid. The attribute is now removed.

- The regenerated calcChain entry took the cell address from the running
  child-index, which is wrong for sparse rows (cells with no content are
  not emitted, so position != column). The address is now read from the
  cell's own r attribute.

- The template's own calcChain was reused/copied even though row insertion
  shifts every formula cell, leaving stale entries; and an empty calcChain
  (no rendered formulas) was written, which is schema-invalid. The chain is
  now regenerated only when formulas were rendered and dropped entirely
  otherwise — Excel rebuilds it on open.

The <f> namespace fix from the v1 change is not needed here: v2's
XElement-based serialization already emits it in the spreadsheetml namespace.
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Inline-string formula conversion now produces valid formula cells and derives references from cell addresses. Template saving no longer copies stale calcChain.xml; it regenerates the entry only when generated calcChain content exists.

Changes

CalcChain Formula Updates

Layer / File(s) Summary
Inline formula conversion
src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
Converted inline-string formulas remove t="inlineStr" and use the cell’s r attribute for calcChain references when available.
CalcChain output handling
src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.cs
calcChain.xml is regenerated only when an output entry exists and _calcChainContent is non-empty; the original template fallback is removed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: michelebastione

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main changes: calcChain regeneration and $= formula cell handling in MiniExcel.OpenXml.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs`:
- Around line 943-949: Update the cell reference assignment near cellRef so
calcChain entries use the complete c@r value directly whenever it is present,
rather than reconstructing it with rowIndex or the running index. Preserve the
existing coordinate fallback only when c@r is missing.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e4ade420-4e26-4481-b2b7-bd2137761a9f

📥 Commits

Reviewing files that changed from the base of the PR and between 5195330 and 12d7dc0.

📒 Files selected for processing (2)
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.cs

Comment on lines +943 to +949
// take the column from the cell's own reference — the running index is wrong for
// sparse rows (cells without content are not emitted, so position != column)
var cellRef = cell.Attribute("r")?.Value;
var columnLetters = cellRef is null ? "" : new string(cellRef.TakeWhile(char.IsLetter).ToArray());
var celRef = string.IsNullOrEmpty(columnLetters)
? CellReferenceConverter.GetCellFromCoordinates(index, rowIndex)
: $"{columnLetters}{rowIndex}";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Use the complete cell address for calcChain entries.

rowIndex can differ from the rendered cell’s row in enumerable/merged rows, so this produces a chain entry for the wrong cell. Use c@r directly when present.

Proposed fix
 var cellRef = cell.Attribute("r")?.Value;
-var columnLetters = cellRef is null ? "" : new string(cellRef.TakeWhile(char.IsLetter).ToArray());
-var celRef = string.IsNullOrEmpty(columnLetters)
+var celRef = string.IsNullOrEmpty(cellRef)
     ? CellReferenceConverter.GetCellFromCoordinates(index, rowIndex)
-    : $"{columnLetters}{rowIndex}";
+    : cellRef;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// take the column from the cell's own reference — the running index is wrong for
// sparse rows (cells without content are not emitted, so position != column)
var cellRef = cell.Attribute("r")?.Value;
var columnLetters = cellRef is null ? "" : new string(cellRef.TakeWhile(char.IsLetter).ToArray());
var celRef = string.IsNullOrEmpty(columnLetters)
? CellReferenceConverter.GetCellFromCoordinates(index, rowIndex)
: $"{columnLetters}{rowIndex}";
// take the column from the cell's own reference — the running index is wrong for
// sparse rows (cells without content are not emitted, so position != column)
var cellRef = cell.Attribute("r")?.Value;
var celRef = string.IsNullOrEmpty(cellRef)
? CellReferenceConverter.GetCellFromCoordinates(index, rowIndex)
: cellRef;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs` around lines 943 -
949, Update the cell reference assignment near cellRef so calcChain entries use
the complete c@r value directly whenever it is present, rather than
reconstructing it with rowIndex or the running index. Preserve the existing
coordinate fallback only when c@r is missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant