diff options
author | PJ Fanning <fanningpj@apache.org> | 2025-03-12 14:53:23 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2025-03-12 14:53:23 +0000 |
commit | 410653681ea2cb5e9dee03f4ec15be90e5a645f5 (patch) | |
tree | 79ab20c4b33008e6bfb9e809c4e9f3b50af1624b | |
parent | 502a0f756e48e8862ecb7d74a53acff27c0b5e73 (diff) | |
download | poi-410653681ea2cb5e9dee03f4ec15be90e5a645f5.tar.gz poi-410653681ea2cb5e9dee03f4ec15be90e5a645f5.zip |
add arbitrary extra width support to XSSFSheet
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1924335 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java | 26 | ||||
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java | 20 |
2 files changed, 46 insertions, 0 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index ed45e1208d..b30883966a 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -104,6 +104,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx private final XSSFDataValidationHelper dataValidationHelper; private XSSFVMLDrawing xssfvmlDrawing; private CellRangeAddress dimensionOverride; + private double arbitraryExtraWidth = 0.0; /** * Creates new XSSFSheet - called by XSSFWorkbook to create a sheet from scratch. @@ -498,6 +499,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx if (width != -1) { width *= 256; + width += arbitraryExtraWidth; int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters if (width > maxColumnWidth) { width = maxColumnWidth; @@ -508,6 +510,30 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx } /** + * Set the extra width added to the best-fit column width (default 0.0). + * <p> + * Only applied to auto-sized columns. + * </p> + * @param arbitraryExtraWidth the extra width added to the best-fit column width + * @since 5.4.1 + */ + public void setArbitraryExtraWidth(final double arbitraryExtraWidth) { + this.arbitraryExtraWidth = arbitraryExtraWidth; + } + + /** + * Get the extra width added to the best-fit column width. + * <p> + * Only applied to auto-sized columns. + * </p> + * @return the extra width added to the best-fit column width + * @since 5.4.0 + */ + public double getArbitraryExtraWidth() { + return arbitraryExtraWidth; + } + + /** * Return the sheet's existing drawing, or null if there isn't yet one. * * Use {@link #createDrawingPatriarch()} to get or create diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 762ef821a7..cca87501c4 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -241,6 +241,26 @@ public final class TestXSSFSheet extends BaseTestXSheet { } } + @Test + void autoSizeColumnWithArbitraryExtraWidth() throws IOException { + try (XSSFWorkbook workbook = new XSSFWorkbook()) { + XSSFSheet sheet = workbook.createSheet("Sheet 1"); + XSSFCell cell = sheet.createRow(0).createCell(13); + cell.setCellValue("test"); + sheet.autoSizeColumn(13); + final int size1 = sheet.getColumnWidth(13); + + sheet.setArbitraryExtraWidth(10.0); + sheet.autoSizeColumn(13); + final int size2 = sheet.getColumnWidth(13); + + assertEquals(size1 + 10, size2); + + ColumnHelper columnHelper = sheet.getColumnHelper(); + CTCol col = columnHelper.getColumn(13, false); + assertTrue(col.getBestFit()); + } + } @Test void setCellComment() throws IOException { |