Skip to content

Remove [Platform("Win")] from TestVBAMacroReader; fix AutoSizeColumn on Linux#1715

Merged
tonyqus merged 5 commits into
masterfrom
copilot/fix-autosize-column-issue
Mar 6, 2026
Merged

Remove [Platform("Win")] from TestVBAMacroReader; fix AutoSizeColumn on Linux#1715
tonyqus merged 5 commits into
masterfrom
copilot/fix-autosize-column-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 5, 2026

TestVBAMacroReader was gated to Windows-only and AutoSizeColumn/AutoSizeRow failed on Linux due to font rendering errors and three correctness bugs. This PR makes both work cross-platform.

AutoSizeColumn correctness bugs (XSSFSheet)

Three independent bugs prevented correct column sizing in existing workbooks:

  • CT_Cols.BreakUpCtCol off-by-one: lastColumn is 0-based, ctCol.min/ctCol.max are 1-based — fix: use lastColumn + 1
  • AutoSizeColumnTracker.SetMaxColumnWidths self-reference: withSkipMergedCells was compared against withUseMergedCells instead of itself, so unmerged width was never tracked independently
  • XSSFSheet.AutoSizeColumn wrong column updated: SetColBestFit() inserted a new <col> element instead of updating the one just resized — replaced with GetColumn(column)?.IsBestFit = true

AutoSizeColumn on Linux (SheetUtil.cs)

All TextMeasurer calls now catch FontException | InvalidFontFileException (thrown on Linux when font tables are missing) with graceful fallbacks:

  • Width: estimated as charCount × (fontHeightPx × 0.5) — preserves relative differences across fonts/rotations
  • Height: size × dpi / 72
  • Default char width: returns 7 (≈ pixel width of '0' in Calibri 11pt @ 96 DPI)

VBAMacroReader cross-platform (TestVBAMacroReader.cs)

SimpleMacro.vba test files have Unix \n endings but VBAMacroReader returns CRLF from Office binaries — Contains comparisons always failed on Linux. Fixed by normalizing before comparison:

// Normalize \n → \r\n to match what VBAMacroReader extracts from Office files
string testMacroContents = Regex.Replace(Encoding.UTF8.GetString(bytes), @"\r?\n", "\r\n");

Also changed FileAccess.ReadWriteFileAccess.Read (file is only read), and removed the [Platform("Win")] class attribute.

EscherGraphics font fallback

GetFontFamilyOrFallback now iterates system fonts and skips any that throw InvalidFontFileException (e.g. the APT-installed NotoColorEmoji.ttf which lacks the required loca table), rather than crashing on SystemFonts.Families.First().

Original prompt

This section details on the original issue you should resolve

<issue_title>AutoSizeColumn only resizes the second column</issue_title>
<issue_description>### NPOI Version
2.7.5

File Type

  • XLSX
  • XLS
  • DOCX
  • XLSM
  • OTHER

Upload the Excel File

Doesn't matter

Reproduce Steps

  1. Open a workbook using .NET Framework 4.8 and NPOI version 2.7.5.
  2. Call void AutoSizeColumn(int column) on all columns.
  3. Save workbook using void Write(Stream stream, bool leaveOpen = false);
  4. Open Excel file

Issue Description

Only the second column is resized; all other columns in all tested files remain unchanged.

Image</issue_description>

Comments on the Issue (you are @copilot in this section)

@tonyqus Can you upload the Excel file to help reproduce the issue?

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: tonyqus <772561+tonyqus@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix AutoSizeColumn to resize all columns correctly Fix AutoSizeColumn only resizing one column on existing workbooks Mar 5, 2026
…lbacks

Co-authored-by: tonyqus <772561+tonyqus@users.noreply.github.com>
Copilot AI changed the title Fix AutoSizeColumn only resizing one column on existing workbooks Fix AutoSizeColumn persisting only first column; make font measurement work cross-platform Mar 6, 2026
…atform

Co-authored-by: tonyqus <772561+tonyqus@users.noreply.github.com>
Copilot AI changed the title Fix AutoSizeColumn persisting only first column; make font measurement work cross-platform Remove [Platform("Win")] from font-related tests — make them work on Linux Mar 6, 2026
… font fallback

Co-authored-by: tonyqus <772561+tonyqus@users.noreply.github.com>
Copilot AI changed the title Remove [Platform("Win")] from font-related tests — make them work on Linux Make all tests run on Linux by fixing font handling and VBA macro line endings Mar 6, 2026
Copilot AI changed the title Make all tests run on Linux by fixing font handling and VBA macro line endings Remove [Platform("Win")] from TestVBAMacroReader; fix AutoSizeColumn on Linux Mar 6, 2026
@tonyqus tonyqus marked this pull request as ready for review March 6, 2026 08:11
@tonyqus tonyqus added this to the NPOI 2.8.0 milestone Mar 6, 2026
@tonyqus tonyqus merged commit 78f608a into master Mar 6, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoSizeColumn only resizes the second column

2 participants