diff options
author | PJ Fanning <fanningpj@apache.org> | 2024-08-21 11:35:20 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2024-08-21 11:35:20 +0000 |
commit | 41453f3916fefd469cfa554dcb5ca069d221b331 (patch) | |
tree | 46340826445e00357c60a560a8c3a68d6257f1b1 | |
parent | f33b2cbd51a12084bf4365521b53a6cf2eccb0f1 (diff) | |
download | poi-41453f3916fefd469cfa554dcb5ca069d221b331.tar.gz poi-41453f3916fefd469cfa554dcb5ca069d221b331.zip |
[github-657] SXSSF: support setting an arbitrary extra width value for column widths
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1920107 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java | 30 | ||||
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java | 28 |
2 files changed, 54 insertions, 4 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java index 0891b300a7..965f23a03e 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java @@ -60,6 +60,8 @@ import org.apache.poi.util.Internal; // 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 @@ -116,7 +118,27 @@ import org.apache.poi.util.Internal; // 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, @@ -369,8 +391,10 @@ import org.apache.poi.util.Internal; * @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); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index f32ac9bb2c..bd0f7c3dc1 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -379,7 +379,6 @@ public class SXSSFSheet implements Sheet, OoxmlSheetExtensions { _sh.setDefaultRowHeightInPoints(height); } - /** * Get VML drawing for this sheet (aka 'legacy' drawing). * @@ -1452,6 +1451,33 @@ public class SXSSFSheet implements Sheet, OoxmlSheetExtensions { _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. |