Skip to content

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

Merged
michelebastione merged 8 commits into
mini-software:masterfrom
martin-vitous:fix/template-calcchain-formula-cells-v2
Aug 4, 2026
Merged

Fix template calcChain regeneration and $= formula cells (MiniExcel.OpenXml)#993
michelebastione merged 8 commits 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
Contributor

Ports the v1.x template-render fixes from #990 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 formula handling for sparse rows, dollar-prefixed formulas, and asynchronous template rendering.
  • Fixed generated formula cells and calculation-chain references to remain valid after inserting rows.
  • Prevented outdated, empty, or invalid calculation-chain data from appearing in generated workbooks.
  • Improved conversion of calculated strings and boolean cell values.
  • Improved compatibility with Excel OpenXML workbook formatting and metadata.

…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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 923fc883-bf48-43d8-8c14-08cdf035ff4d

📥 Commits

Reviewing files that changed from the base of the PR and between 5924ead and 28dfcdb.

📒 Files selected for processing (2)
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.ValueExtractorHook.cs
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.cs
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.cs
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.ValueExtractorHook.cs

📝 Walkthrough

Walkthrough

Template formula conversion now produces valid formula cells and derives calculation-chain references from cell addresses. Template saving conditionally regenerates calculation-chain metadata and rebuilds worksheet relationships. Synchronous and asynchronous tests cover stale chains and sparse-row formulas.

Changes

CalcChain Template Rendering

Layer / File(s) Summary
Formula cell conversion
src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
Formula cells remove obsolete inlineStr typing, use cell references for calculation-chain entries, and use ExcelDataTypes constants.
Template archive and relationship handling
src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.cs, src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.ValueExtractorHook.cs, src/MiniExcel.OpenXml/MiniExcel.OpenXml.csproj
Template saving excludes stale metadata, emits calcChain.xml only when valid content exists, rebuilds worksheet relationships, and removes the obsolete project folder inclusion.
Calculation-chain regression coverage
tests/MiniExcel.OpenXml.Tests/Templates/CalcChainTests.cs, tests/MiniExcel.OpenXml.Tests/Templates/CalcChainAsyncTests.cs
Tests cover stale-chain removal and sparse-row formula cells for synchronous and asynchronous template rendering.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant OpenXmlTemplate
  participant OpenXmlTemplateImpl
  participant ValueExtractorHook
  participant OutputArchive
  OpenXmlTemplate->>OpenXmlTemplateImpl: Convert formula cells and generate calcChain content
  OpenXmlTemplate->>ValueExtractorHook: Rebuild worksheet relationships
  ValueExtractorHook->>ValueExtractorHook: Remove calcChain relationship when requested
  OpenXmlTemplate->>OutputArchive: Write calcChain.xml when content is valid
  OpenXmlTemplate->>OutputArchive: Remove calcChain metadata when content is invalid
Loading

Possibly related PRs

🚥 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 identifies the main changes to template calcChain regeneration and $= formula cells.
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 thread src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs Outdated

@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.cs`:
- Around line 180-182: Normalize the Content Types calcChain PartName to the
archive-path form before matching in OpenXmlTemplate, so `/xl/calcChain.xml`
matches `xl/calcChain.xml`; in OpenXmlTemplate.ValueExtractorHook, remove the
calcChain relationship by relationship type or compare its target using the
relative `calcChain.xml` form. Extend the stale-chain tests to verify both the
content-type entry and workbook relationship are absent when `xl/calcChain.xml`
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: 11825d7a-4bfa-4b07-8d71-6183788e4cc2

📥 Commits

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

📒 Files selected for processing (6)
  • src/MiniExcel.OpenXml/MiniExcel.OpenXml.csproj
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.ValueExtractorHook.cs
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.cs
  • tests/MiniExcel.OpenXml.Tests/Templates/CalcChainAsyncTests.cs
  • tests/MiniExcel.OpenXml.Tests/Templates/CalcChainTests.cs
💤 Files with no reviewable changes (1)
  • src/MiniExcel.OpenXml/MiniExcel.OpenXml.csproj

Comment thread src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.cs
@michelebastione
michelebastione merged commit 1d22a55 into mini-software:master Aug 4, 2026
4 checks passed
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.

2 participants