Skip to content

Commit

Permalink
reimplement SXSSFSheet arbitrary extra width support (5.4.0 code does…
Browse files Browse the repository at this point in the history
… not work)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923071 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Jan 11, 2025
1 parent 0ab1ccc commit 9eda246
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ private void implicitlyTrackColumnsInRow(Row row) {
*/
private void updateColumnWidth(final Cell cell, final ColumnWidthPair pair) {
final double unmergedWidth =
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false) + arbitraryExtraWidth;
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false);
final double mergedWidth =
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true) + arbitraryExtraWidth;
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true);
pair.setMaxColumnWidths(unmergedWidth, mergedWidth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,8 @@ public void autoSizeColumn(int column, boolean useMergedCells) {
}

// get the best-fit width of rows currently in the random access window
final int activeWidth = (int) (256 * SheetUtil.getColumnWidth(this, column, useMergedCells));
final double w1 = SheetUtil.getColumnWidth(this, column, useMergedCells);
final int activeWidth = (int) ((256 * w1) + getArbitraryExtraWidth());

// the best-fit width for both flushed rows and random access window rows
// flushedWidth or activeWidth may be negative if column contains only blank cells
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
Expand All @@ -45,7 +46,7 @@ public TestSXSSFSheet() {


@AfterEach
void tearDown(){
void tearDown() {
SXSSFITestDataProvider.instance.cleanup();
}

Expand Down Expand Up @@ -84,9 +85,9 @@ protected void shiftMerged() {
}

/**
* Bug 35084: cloning cells with formula
*
* The test is disabled because cloning of sheets is not supported in SXSSF
* Bug 35084: cloning cells with formula
* <p>
* The test is disabled because cloning of sheets is not supported in SXSSF
*/
@Override
@Test
Expand Down Expand Up @@ -193,9 +194,9 @@ void groupRow() throws IOException {
//one level
sheet.groupRow(9, 10);

try(UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get()) {
try (UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get()) {
workbook.write(bos);
try(XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bos.toInputStream())) {
try (XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bos.toInputStream())) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
CTRow ctrow = xssfSheet.getRow(9).getCTRow();

Expand Down Expand Up @@ -223,9 +224,9 @@ void groupRow2Levels() throws IOException {
//two level
sheet.groupRow(10, 13);

try(UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get()) {
try (UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get()) {
workbook.write(bos);
try(XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bos.toInputStream())) {
try (XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bos.toInputStream())) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
CTRow ctrow = xssfSheet.getRow(9).getCTRow();

Expand All @@ -242,4 +243,43 @@ void groupRow2Levels() throws IOException {
}
}
}

@Test
void autosizeWithArbitraryExtraWidth() throws IOException {
final int extra = 100;
final String longText =
"This is a very long text that will exceed default column width for sure.";
int width0, width1 = 0;
try (SXSSFWorkbook workbook0 = new SXSSFWorkbook()) {
SXSSFSheet sheet = workbook0.createSheet();
sheet.trackColumnForAutoSizing(0);

SXSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue(longText);
sheet.autoSizeColumn(0);

try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
workbook0.write(bos);
}
width0 = sheet.getColumnWidth(0);
}

try (SXSSFWorkbook workbook1 = new SXSSFWorkbook()) {
SXSSFSheet sheet = workbook1.createSheet();
sheet.setArbitraryExtraWidth(extra);
sheet.trackColumnForAutoSizing(0);

SXSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue(longText);
sheet.autoSizeColumn(0);

try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
workbook1.write(bos);
}
width1 = sheet.getColumnWidth(0);
}

assertEquals(width0 + extra, width1);
}

}

0 comments on commit 9eda246

Please sign in to comment.