Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SXSSF: support setting an arbitrary extra width value for column widths #657

Closed
wants to merge 1 commit into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
// Using a HashSet instead of a TreeSet because we don't care about order.
private final Set<Integer> untrackedColumns = new HashSet<>();
private boolean trackAllColumns;
// arbitraryExtraWidth is the extra width added to the best-fit column width (since POI 5.3.1)
private double arbitraryExtraWidth = 0.0d;

/**
* Tuple to store the column widths considering and not considering merged cells
Expand Down Expand Up @@ -116,7 +118,27 @@ public AutoSizeColumnTracker(final Sheet sheet) {
// If sheet needs to be saved, use a java.lang.ref.WeakReference to avoid garbage collector gridlock.
defaultCharWidth = SheetUtil.getDefaultCharWidthAsFloat(sheet.getWorkbook());
}


/**
* Set the extra width added to the best-fit column width (default 0.0).
*
* @param arbitraryExtraWidth the extra width added to the best-fit column width
* @since 5.3.1
*/
public void setArbitraryExtraWidth(final double arbitraryExtraWidth) {
this.arbitraryExtraWidth = arbitraryExtraWidth;
}

/**
* Get the extra width added to the best-fit column width.
*
* @return the extra width added to the best-fit column width
* @since 5.3.1
*/
public double getArbitraryExtraWidth() {
return arbitraryExtraWidth;
}

/**
* Get the currently tracked columns, naturally ordered.
* Note if all columns are tracked, this will only return the columns that have been explicitly or implicitly tracked,
Expand Down Expand Up @@ -369,8 +391,10 @@ private void implicitlyTrackColumnsInRow(Row row) {
* @since 3.14beta1
*/
private void updateColumnWidth(final Cell cell, final ColumnWidthPair pair) {
final double unmergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false);
final double mergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true);
final double unmergedWidth =
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false) + arbitraryExtraWidth;
final double mergedWidth =
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true) + arbitraryExtraWidth;
pair.setMaxColumnWidths(unmergedWidth, mergedWidth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ public void setDefaultRowHeightInPoints(float height) {
_sh.setDefaultRowHeightInPoints(height);
}


/**
* Get VML drawing for this sheet (aka 'legacy' drawing).
*
Expand Down Expand Up @@ -1452,6 +1451,33 @@ public void setDefaultColumnStyle(int column, CellStyle style) {
_sh.setDefaultColumnStyle(column, style);
}

/**
* Set the extra width added to the best-fit column width (default 0.0).
*
* @param arbitraryExtraWidth the extra width added to the best-fit column width
* @throws IllegalStateException if autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)
* @since 5.3.1
*/
public void setArbitraryExtraWidth(final double arbitraryExtraWidth) {
if (_autoSizeColumnTracker == null) {
throw new IllegalStateException("Cannot trackColumnForAutoSizing because autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)");
}
_autoSizeColumnTracker.setArbitraryExtraWidth(arbitraryExtraWidth);
}

/**
* Get the extra width added to the best-fit column width.
*
* @return the extra width added to the best-fit column width
* @throws IllegalStateException if autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)
* @since 5.3.1
*/
public double getArbitraryExtraWidth() {
if (_autoSizeColumnTracker == null) {
throw new IllegalStateException("Cannot trackColumnForAutoSizing because autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)");
}
return _autoSizeColumnTracker.getArbitraryExtraWidth();
}

/**
* Track a column in the sheet for auto-sizing.
Expand Down