Skip to content

Fill in missing row and cell references in templates (#863) - #1015

Merged
michelebastione merged 1 commit into
mini-software:masterfrom
gdols:fix/template-missing-cell-references
Sep 23, 2026
Merged

michelebastione merged 1 commit into
mini-software:masterfrom
gdols:fix/template-missing-cell-references

Conversation

@gdols

@gdols gdols commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #863

The r attribute on rows and cells is optional in SpreadsheetML (ECMA-376 18.3.1.73 and 18.3.1.4). When it's missing, the row comes right after the previous row and the cell right after the previous cell, which is also how OpenXmlReader already reads these files. Some tools write xlsx files like that and Excel opens them fine, but the template code keys everything on r.

The screenshot in #863 is from v1: the debugger stops at //TODO: some xlsx without r with a FormatException from int.Parse(row.GetAttribute("r")). On master the same kind of template fails a bit earlier:

System.ArgumentNullException : Value cannot be null. (Parameter 'key')
   at OpenXmlTemplate.UpdateDimensionAndGetRowsInfo(...) in OpenXmlTemplate.Impl.cs:line 1101

I added FillMissingReferences, which fills in the missing r values when the template sheet is loaded. It runs in both the create and the update mode, so FillTemplate and MergeSameCells are both covered, and nothing else in the template code changes. References that are already there are left alone, so a malformed one still gets the existing NotSupportedException. I also removed the TODO about this.

Tests:

  • TestIssue863 takes TestTemplateComplex.xlsx, removes r from every cell (and from every row too, or keeps the row ones as a second case) and checks the result is the same as filling the original template. There's an async version as well.
  • TestIssue863_MergeSameCells does the same with TestMergeWithTag.xlsx.

All of them fail on master and pass with the fix, and the whole OpenXml test project passes on net8.0 and net10.0.

The same bug is on v1.x-maintenance, which is the version the reporter uses. I can port the fix there too if you want.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Excel template processing when row or cell references are missing.
    • Automatically restores valid row and cell references while preserving existing references.
    • Ensured generated worksheets maintain correct dimensions, row values, and merged-cell ranges in reference-free templates.

The "r" attribute on rows and cells is optional (ECMA-376 18.3.1.73 and
18.3.1.4): without it, a row follows the previous row and a cell follows the
previous cell, which is how OpenXmlReader already reads them. The template
code keys everything on "r", so a template written that way crashed in
UpdateDimensionAndGetRowsInfo with an ArgumentNullException.

FillMissingReferences fills in the missing ones when the template sheet is
loaded, in both the create and update modes, so FillTemplate and
MergeSameCells both work. References that are already there are left as they
are, so a malformed one still gets the existing NotSupportedException.
@coderabbitai

coderabbitai Bot commented Sep 22, 2026

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: ff4282c6-91c2-4000-90a6-b51e4c52e002

📥 Commits

Reviewing files that changed from the base of the PR and between 357b9de and 0851c7a.

📒 Files selected for processing (4)
  • src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
  • tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesAsyncTests.cs
  • tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
  • tests/MiniExcel.OpenXml.Tests/Utils/SheetHelper.cs

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Template generation now reconstructs missing row and cell references in update and create modes. Tests cover templates with removed references and verify filled values, dimensions, and merged ranges.

Changes

Reference reconstruction

Layer / File(s) Summary
Normalize missing worksheet references
src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs
Update and create modes call FillMissingReferences. The helper assigns row references and derives missing cell references while preserving explicit parseable references.
Validate reference-free templates
tests/MiniExcel.OpenXml.Tests/Utils/SheetHelper.cs, tests/MiniExcel.OpenXml.Tests/Issues/*
Tests remove worksheet references, compare filled output with the original template, verify the A1:C7 dimension, check Keaton, and validate merged ranges.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: michelebastione

Merge Risk: ⚪ Minimal · up to 0851c

Template processing now supports XLSX files with omitted row or cell references in both create and update paths. The supplied regression coverage indicates expected values and merged ranges are preserved, with no actionable merge-blocking risk identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.38% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding missing row and cell references in templates.
Linked Issues check ✅ Passed Issue [#863] requires SaveAsByTemplate to process valid XLSX files that omit row or cell r attributes. OpenXmlTemplate.Impl.cs now calls FillMissingReferences in both create and update templat…
Out of Scope Changes check ✅ Passed The changes stay within issue [#863]. The implementation fixes missing SpreadsheetML references. The tests and SheetHelper.CopyWithoutCellReferences create the affected input and verify template out…
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

@michelebastione

Copy link
Copy Markdown
Collaborator

Thank you for your contribution! I'll review it soon!

@michelebastione
michelebastione merged commit 2ecd46f into mini-software:master Sep 23, 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.

SaveAsByTemplate throw exception in some xlsx

2 participants