Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EPPlus/Core/AutofitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ internal void AutofitColumn(double MinimumWidth, double MaximumWidth)
{
if (af.Collide(fromRow, col, toRow, col) != eAddressCollition.No)
{
var cell = worksheet.Cells[af.Address];
var cell = worksheet.Cells[af._fromRow, col];
var cellStyleId = styles.CellXfs[cell.StyleID];
currentMaxWidth = GetTextLength(cell, textLengthCache, styles, cellStyleId, normalSize, MaximumWidth, currentMaxWidth);
}
Expand Down
21 changes: 21 additions & 0 deletions src/EPPlusTest/WorkSheetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,27 @@ public void AutoFitColumnTest()
SaveAndCleanup(p);
}
[TestMethod]
public void AutoFitColumnsWithAutoFilter()
{
var ws = _pck.Workbook.Worksheets.Add("AutofitAutoFilter");
ws.Cells["A1"].Value = "hour";
ws.Cells["B1"].Value = "minute";
ws.Cells["A2"].Value = 12;
ws.Cells["B2"].Value = 30;

ws.Cells["A1:B2"].AutoFilter = true;

ws.Cells["A1:B2"].AutoFitColumns();

// Without the fix, the AutoFilter header row range (A1:B1) is measured as a whole.
// Under the hood, worksheet.Cells["A1:B1"].TextForWidth evaluated to "System.Object[,]" (16 chars),
// which forced a minimum width of ~16.07 points.
// With the fix, the specific cell for each column in the AutoFilter is measured,
// resulting in a narrow width matching "hour" / "minute".
Assert.IsTrue(ws.Column(1).Width < 12d, $"Column 1 width should be small but was {ws.Column(1).Width}");
Assert.IsTrue(ws.Column(2).Width < 12d, $"Column 2 width should be small but was {ws.Column(2).Width}");
}
[TestMethod]
public void CopyOverwrite()
{
var ws = _pck.Workbook.Worksheets.Add("CopyOverwrite");
Expand Down
Loading