Fix column width distortion in AutoFit with active AutoFilter#2387
Closed
lievendf wants to merge 1 commit into
Closed
Fix column width distortion in AutoFit with active AutoFilter#2387lievendf wants to merge 1 commit into
lievendf wants to merge 1 commit into
Conversation
Contributor
|
Hello @lievendf, Thanks for this — the fix is correct and we've merged it. While reviewing it I noticed that it also surfaced a separate, pre-existing issue: with the header now measured correctly, the autofilter dropdown arrow is no longer accounted for. I've opened a follow-up PR to handle that. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses a bug where performing an
AutoFit(orAutoFitColumns()) on a worksheet with an activeAutoFilterresults in distorted and bloated column widths (clamping column widths to at least ~16-17 points regardless of actual cell content).Root Cause
During the AutoFit calculation, EPPlus iterates over active autofilter addresses (
afAddr) to ensure the column headers are sized correctly.However, in
src/EPPlus/Core/AutofitHelper.cs(line 129), the code was fetching the entire row-range representing the AutoFilter's headers:For an AutoFilter range such as
A1:U1,af.Addressrepresents the multi-cell range"A1:U1". Consequently,cellbecomes a multi-cellExcelRangecontaining a 2D array of cells.When EPPlus attempts to measure the text width of this cell via
GetTextLength(),cell.TextForWidthevaluates the underlying value of the entire range. Since a multi-cell range evaluates to a 2D object array (object[,]), formatting it falls back to calling.ToString(), which returns the string"System.Object[,]"(exactly 16 characters).This 16-character string was then measured for every column covered by the AutoFilter, artificially inflating the column widths to a minimum of ~16.07 points (clamped to the length of the string
"System.Object[]"), even if the column's header was as short as"hour"(4 characters) or"minute"(6 characters).Solution
The fix surgically targets the specific column's header cell instead of the entire AutoFilter header range.
In
src/EPPlus/Core/AutofitHelper.cs(line 129), we replace:with:
This correctly resolves to the individual column cell for the column
colcurrently being processed, allowing the column to auto-fit to its true text length.Code Changes
src/EPPlus/Core/AutofitHelper.csUnit Test
We have added the following unit test to the
EPPlusTestsuite insrc/EPPlusTest/WorkSheetTests.cs:Verification
EPPlus.sln) with 0 errors.dotnet testtargeting the new unit test under both.NET 8.0and.NET Framework 4.8.1, passing successfully:Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: 574 ms - EPPlusTest.dll (net8.0)Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: 1 s - EPPlusTest.dll (net481)