add arbitrary extra width support to XSSFSheet

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1924335 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2025-03-12 14:53:23 +00:00
parent 502a0f756e
commit 410653681e
2 changed files with 46 additions and 0 deletions

View File

@ -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;
@ -507,6 +509,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.
*

View File

@ -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 {