aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2023-01-28 22:18:33 +0000
committerPJ Fanning <fanningpj@apache.org>2023-01-28 22:18:33 +0000
commit4944aa4c2e38430edb9b2c8616615062da166b30 (patch)
tree4180c3110133c46b115da8000ff9a5baea1b553f /poi-ooxml
parentcde87ab3c61d68baea10e1d77c2331dd8631f2fa (diff)
downloadpoi-4944aa4c2e38430edb9b2c8616615062da166b30.tar.gz
poi-4944aa4c2e38430edb9b2c8616615062da166b30.zip
rework SXSSF groupRow and add test
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1907068 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java24
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java4
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFSheet.java65
3 files changed, 76 insertions, 17 deletions
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 dab721b525..161580eb4d 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
@@ -51,7 +51,6 @@ public class SXSSFSheet implements Sheet, OoxmlSheetExtensions {
protected SheetDataWriter _writer;
private int _randomAccessWindowSize = SXSSFWorkbook.DEFAULT_WINDOW_SIZE;
protected AutoSizeColumnTracker _autoSizeColumnTracker;
- private int outlineLevelRow;
private int lastFlushedRowNumber = -1;
private boolean allFlushed;
private int leftMostColumn = SpreadsheetVersion.EXCEL2007.getLastColumnIndex();
@@ -1241,7 +1240,7 @@ public class SXSSFSheet implements Sheet, OoxmlSheetExtensions {
*/
@Override
public void groupColumn(int fromColumn, int toColumn) {
- _sh.groupColumn(fromColumn,toColumn);
+ _sh.groupColumn(fromColumn, toColumn);
}
/**
@@ -1294,16 +1293,14 @@ public class SXSSFSheet implements Sheet, OoxmlSheetExtensions {
*/
@Override
public void groupRow(int fromRow, int toRow) {
+ int maxLevelRow = -1;
for(SXSSFRow row : _rows.subMap(fromRow, toRow + 1).values()){
- int level = row.getOutlineLevel() + 1;
+ final int level = row.getOutlineLevel() + 1;
row.setOutlineLevel(level);
-
- if(level > outlineLevelRow) {
- outlineLevelRow = level;
- }
+ maxLevelRow = Math.max(maxLevelRow, level);
}
- setWorksheetOutlineLevelRow();
+ setWorksheetOutlineLevelRowIfNecessary((short) Math.min(Short.MAX_VALUE, maxLevelRow));
}
/**
@@ -1323,19 +1320,16 @@ public class SXSSFSheet implements Sheet, OoxmlSheetExtensions {
public void setRowOutlineLevel(int rownum, int level) {
SXSSFRow row = _rows.get(rownum);
row.setOutlineLevel(level);
- if(level > 0 && level > outlineLevelRow) {
- outlineLevelRow = level;
- setWorksheetOutlineLevelRow();
- }
+ setWorksheetOutlineLevelRowIfNecessary((short) Math.min(Short.MAX_VALUE, level));
}
- private void setWorksheetOutlineLevelRow() {
+ private void setWorksheetOutlineLevelRowIfNecessary(final short levelRow) {
CTWorksheet ct = _sh.getCTWorksheet();
CTSheetFormatPr pr = ct.isSetSheetFormatPr() ?
ct.getSheetFormatPr() :
ct.addNewSheetFormatPr();
- if(outlineLevelRow > 0) {
- pr.setOutlineLevelRow((short)outlineLevelRow);
+ if(levelRow > _sh.getSheetFormatPrOutlineLevelRow()) {
+ pr.setOutlineLevelRow(levelRow);
}
}
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 2613e80e64..63b3dbc9b9 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
@@ -3462,11 +3462,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx
getSheetTypeSheetFormatPr().setOutlineLevelRow(maxLevelRow);
}
- private short getSheetFormatPrOutlineLevelRow() {
+ public short getSheetFormatPrOutlineLevelRow() {
return getSheetTypeSheetFormatPr().getOutlineLevelRow();
}
- private short getSheetFormatPrOutlineLevelCol() {
+ public short getSheetFormatPrOutlineLevelCol() {
return getSheetTypeSheetFormatPr().getOutlineLevelCol();
}
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFSheet.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFSheet.java
index 6a56854ecc..41df3b3384 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFSheet.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFSheet.java
@@ -20,6 +20,7 @@
package org.apache.poi.xssf.streaming;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
@@ -33,6 +34,7 @@ import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
public final class TestSXSSFSheet extends BaseTestXSheet {
@@ -177,4 +179,67 @@ public final class TestSXSSFSheet extends BaseTestXSheet {
wb.close();
}
+
+ @Test
+ void groupRow() throws IOException {
+ try (SXSSFWorkbook workbook = new SXSSFWorkbook()) {
+ SXSSFSheet sheet = workbook.createSheet();
+
+ // XSSF code can group rows even if there are no XSSFRows yet, SXSSFWorkbook needs the rows to exist first
+ for (int i = 0; i < 20; i++) {
+ sheet.createRow(i);
+ }
+
+ //one level
+ sheet.groupRow(9, 10);
+
+ try(UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
+ workbook.write(bos);
+ try(XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bos.toInputStream())) {
+ XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
+ CTRow ctrow = xssfSheet.getRow(9).getCTRow();
+
+ assertNotNull(ctrow);
+ assertEquals(10, ctrow.getR());
+ assertEquals(1, ctrow.getOutlineLevel());
+ assertEquals(1, xssfSheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
+ }
+ }
+ }
+ }
+
+ @Test
+ void groupRow2Levels() throws IOException {
+ try (SXSSFWorkbook workbook = new SXSSFWorkbook()) {
+ SXSSFSheet sheet = workbook.createSheet();
+
+ // XSSF code can group rows even if there are no XSSFRows yet, SXSSFWorkbook needs the rows to exist first
+ for (int i = 0; i < 20; i++) {
+ sheet.createRow(i);
+ }
+
+ //one level
+ sheet.groupRow(9, 10);
+ //two level
+ sheet.groupRow(10, 13);
+
+ try(UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
+ workbook.write(bos);
+ try(XSSFWorkbook xssfWorkbook = new XSSFWorkbook(bos.toInputStream())) {
+ XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
+ CTRow ctrow = xssfSheet.getRow(9).getCTRow();
+
+ assertNotNull(ctrow);
+ assertEquals(10, ctrow.getR());
+ assertEquals(1, ctrow.getOutlineLevel());
+
+ ctrow = xssfSheet.getRow(10).getCTRow();
+ assertNotNull(ctrow);
+ assertEquals(11, ctrow.getR());
+ assertEquals(2, ctrow.getOutlineLevel());
+ assertEquals(2, xssfSheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
+ }
+ }
+ }
+ }
}