Skip to content

Commit

Permalink
Merge pull request #1403 from PatriceDargenton/master
Browse files Browse the repository at this point in the history
maxRows is usefull for AutoSizeColumn and GetColumnWidth
  • Loading branch information
tonyqus authored Aug 31, 2024
2 parents 93b88c3 + c5523b8 commit 384f899
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 17 additions & 1 deletion main/HSSF/UserModel/HSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,23 @@ public void AutoSizeColumn(int column)
/// <param name="useMergedCells">whether to use the contents of merged cells when calculating the width of the column</param>
public void AutoSizeColumn(int column, bool useMergedCells)
{
double width = SheetUtil.GetColumnWidth(this, column, useMergedCells);
AutoSizeColumn(column, useMergedCells, maxRows: 0);
}

/// <summary>
/// Adjusts the column width to fit the contents.
/// This Process can be relatively slow on large sheets, so this should
/// normally only be called once per column, at the end of your
/// Processing.
/// You can specify whether the content of merged cells should be considered or ignored.
/// Default is to ignore merged cells.
/// </summary>
/// <param name="column">the column index</param>
/// <param name="useMergedCells">whether to use the contents of merged cells when calculating the width of the column</param>
/// <param name="maxRows">limit the scope to maxRows rows to speed up the function, or leave 0 (optional)</param>
public void AutoSizeColumn(int column, bool useMergedCells, int maxRows = 0)
{
double width = SheetUtil.GetColumnWidth(this, column, useMergedCells, maxRows);
if (width != -1)
{
width *= 256;
Expand Down
5 changes: 3 additions & 2 deletions main/SS/Util/SheetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,12 @@ private static double GetCellWidth(int defaultCharWidth, int colspan,
* @param sheet the sheet to calculate
* @param column 0-based index of the column
* @param useMergedCells whether to use merged cells
* @param maxRows limit the scope to maxRows rows to speed up the function, or leave 0 (optional)
* @return the width in pixels or -1 if all cells are empty
*/
public static double GetColumnWidth(ISheet sheet, int column, bool useMergedCells)
public static double GetColumnWidth(ISheet sheet, int column, bool useMergedCells, int maxRows = 0)
{
return GetColumnWidth(sheet, column, useMergedCells, sheet.FirstRowNum, sheet.LastRowNum);
return GetColumnWidth(sheet, column, useMergedCells, sheet.FirstRowNum, sheet.LastRowNum, maxRows);
}
/**
* Compute width of a column based on a subset of the rows and return the result
Expand Down

0 comments on commit 384f899

Please sign in to comment.