From cb9e1bf66a423f0f6139c7836ad97a3b8f4f3c10 Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Mon, 9 Oct 2023 11:27:06 +0200 Subject: [PATCH 1/7] Allow append rows in Streaming workbook --- .../apache/poi/xssf/streaming/SXSSFSheet.java | 82 +++++++++++-------- 1 file changed, 47 insertions(+), 35 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 ea1c7e1bfb5..d8cbb7487d3 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,6 +51,7 @@ 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(); @@ -61,11 +62,7 @@ protected SXSSFSheet(SXSSFWorkbook workbook, XSSFSheet xSheet, int randomAccessW _sh = xSheet; calculateLeftAndRightMostColumns(xSheet); setRandomAccessWindowSize(randomAccessWindowSize); - try { - _autoSizeColumnTracker = new AutoSizeColumnTracker(this); - } catch (UnsatisfiedLinkError | InternalError e) { - LOG.atWarn().log("Failed to create AutoSizeColumnTracker, possibly due to fonts not being installed in your OS", e); - } + _autoSizeColumnTracker = new AutoSizeColumnTracker(this); } private void calculateLeftAndRightMostColumns(XSSFSheet xssfSheet) { @@ -96,7 +93,7 @@ public SXSSFSheet(SXSSFWorkbook workbook, XSSFSheet xSheet) throws IOException { setRandomAccessWindowSize(_workbook.getRandomAccessWindowSize()); try { _autoSizeColumnTracker = new AutoSizeColumnTracker(this); - } catch (UnsatisfiedLinkError | InternalError e) { + } catch (Exception e) { LOG.atWarn().log("Failed to create AutoSizeColumnTracker, possibly due to fonts not being installed in your OS", e); } } @@ -142,11 +139,11 @@ public SXSSFRow createRow(int rownum) { "in the range [0," + _writer.getLastFlushedRow() + "] that is already written to disk."); } - // attempt to overwrite an existing row in the input template + // attempt to overwrite a existing row in the input template if(_sh.getPhysicalNumberOfRows() > 0 && rownum <= _sh.getLastRowNum() ) { throw new IllegalArgumentException( "Attempting to write a row["+rownum+"] " + - "in the range [0," + _sh.getLastRowNum() + "] that is already written to disk."); + "in the range [0," + _sh.getLastRowNum() + "] that is already written to disk. Eventually already existing rows are ignored?"); } SXSSFRow newRow = new SXSSFRow(this); @@ -157,7 +154,7 @@ public SXSSFRow createRow(int rownum) { try { flushRows(_randomAccessWindowSize); } catch (IOException ioe) { - throw new IllegalStateException(ioe); + throw new RuntimeException(ioe); } } return newRow; @@ -181,6 +178,10 @@ public void removeRow(Row row) { return; } } + // jlolling: allow reading all the content + if (row.getSheet() == _sh) { + _sh.removeRow(row); + } } /** @@ -191,8 +192,13 @@ public void removeRow(Row row) { * @return Row representing the rownumber or null if its not defined on the sheet */ @Override - public SXSSFRow getRow(int rownum) { - return _rows.get(rownum); + public Row getRow(int rownum) { + Row row = _rows.get(rownum); + // jlolling: allow reading all the content + if (row == null) { + row = _sh.getRow(rownum); + } + return row; } /** @@ -225,7 +231,8 @@ public int getFirstRowNum() { */ @Override public int getLastRowNum() { - return _rows.isEmpty() ? -1 : _rows.lastKey(); + // jlolling allow append + return _rows.isEmpty() ? _sh.getLastRowNum() : _rows.lastKey(); } /** @@ -984,7 +991,7 @@ public boolean getForceFormulaRecalculation() { @NotImplemented @Override public void shiftRows(int startRow, int endRow, int n) { - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } /** @@ -1008,7 +1015,7 @@ public void shiftRows(int startRow, int endRow, int n) { @NotImplemented @Override public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) { - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } /** @@ -1145,7 +1152,7 @@ public boolean isDisplayRowColHeadings() { * Breaks occur above the specified row and left of the specified column inclusive. * * For example, {@code sheet.setColumnBreak(2);} breaks the sheet into two parts - * with columns A,B,C in the first and D,E,... in the second. Similar, {@code sheet.setRowBreak(2);} + * with columns A,B,C in the first and D,E,... in the second. Simuilar, {@code sheet.setRowBreak(2);} * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part * and rows starting with rownum=4 in the second. * @@ -1240,11 +1247,11 @@ public void setColumnGroupCollapsed(int columnNumber, boolean collapsed) { */ @Override public void groupColumn(int fromColumn, int toColumn) { - _sh.groupColumn(fromColumn, toColumn); + _sh.groupColumn(fromColumn,toColumn); } /** - * Ungroup a range of columns that were previously grouped + * Ungroup a range of columns that were previously groupped * * @param fromColumn start column (0-based) * @param toColumn end column (0-based) @@ -1293,14 +1300,16 @@ public void ungroupColumn(int fromColumn, int toColumn) { */ @Override public void groupRow(int fromRow, int toRow) { - int maxLevelRow = -1; for(SXSSFRow row : _rows.subMap(fromRow, toRow + 1).values()){ - final int level = row.getOutlineLevel() + 1; + int level = row.getOutlineLevel() + 1; row.setOutlineLevel(level); - maxLevelRow = Math.max(maxLevelRow, level); + + if(level > outlineLevelRow) { + outlineLevelRow = level; + } } - setWorksheetOutlineLevelRowIfNecessary((short) Math.min(Short.MAX_VALUE, maxLevelRow)); + setWorksheetOutlineLevelRow(); } /** @@ -1320,21 +1329,24 @@ public void groupRow(int fromRow, int toRow) { public void setRowOutlineLevel(int rownum, int level) { SXSSFRow row = _rows.get(rownum); row.setOutlineLevel(level); - setWorksheetOutlineLevelRowIfNecessary((short) Math.min(Short.MAX_VALUE, level)); + if(level > 0 && level > outlineLevelRow) { + outlineLevelRow = level; + setWorksheetOutlineLevelRow(); + } } - private void setWorksheetOutlineLevelRowIfNecessary(final short levelRow) { + private void setWorksheetOutlineLevelRow() { CTWorksheet ct = _sh.getCTWorksheet(); CTSheetFormatPr pr = ct.isSetSheetFormatPr() ? ct.getSheetFormatPr() : ct.addNewSheetFormatPr(); - if(levelRow > _sh.getSheetFormatPrOutlineLevelRow()) { - pr.setOutlineLevelRow(levelRow); + if(outlineLevelRow > 0) { + pr.setOutlineLevelRow((short)outlineLevelRow); } } /** - * Ungroup a range of rows that were previously grouped + * Ungroup a range of rows that were previously groupped * * @param fromRow start row (0-based) * @param toRow end row (0-based) @@ -1349,9 +1361,9 @@ public void ungroupRow(int fromRow, int toRow) { * * Not implemented for expanding (i.e. collapse == false) * - * @param row start row of a grouped range of rows (0-based) + * @param row start row of a groupped range of rows (0-based) * @param collapse whether to expand/collapse the detail rows - * @throws IllegalStateException if collapse is false as this is not implemented for SXSSF. + * @throws RuntimeException if collapse is false as this is not implemented for SXSSF. */ @Override public void setRowGroupCollapsed(int row, boolean collapse) { @@ -1359,7 +1371,7 @@ public void setRowGroupCollapsed(int row, boolean collapse) { collapseRow(row); } else { //expandRow(rowIndex); - throw new IllegalStateException("Unable to expand row: Not Implemented"); + throw new RuntimeException("Unable to expand row: Not Implemented"); } } @@ -1367,7 +1379,7 @@ public void setRowGroupCollapsed(int row, boolean collapse) { * @param rowIndex the zero based row index to collapse */ private void collapseRow(int rowIndex) { - SXSSFRow row = getRow(rowIndex); + SXSSFRow row = (SXSSFRow) getRow(rowIndex); if(row == null) { throw new IllegalArgumentException("Invalid row number("+ rowIndex + "). Row does not exist."); } else { @@ -1375,7 +1387,7 @@ private void collapseRow(int rowIndex) { // Hide all the columns until the end of the group int lastRow = writeHidden(row, startRow); - SXSSFRow lastRowObj = getRow(lastRow); + SXSSFRow lastRowObj = (SXSSFRow) getRow(lastRow); if (lastRowObj != null) { lastRowObj.setCollapsed(true); } else { @@ -1407,12 +1419,12 @@ private int findStartOfRowOutlineGroup(int rowIndex) { private int writeHidden(SXSSFRow xRow, int rowIndex) { int level = xRow.getOutlineLevel(); - SXSSFRow currRow = getRow(rowIndex); + SXSSFRow currRow = (SXSSFRow) getRow(rowIndex); while (currRow != null && currRow.getOutlineLevel() >= level) { currRow.setHidden(true); rowIndex++; - currRow = getRow(rowIndex); + currRow = (SXSSFRow) getRow(rowIndex); } return rowIndex; } @@ -1759,7 +1771,7 @@ public CellRange setArrayFormula(String formula, CellRangeAddres // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files // return _sh.setArrayFormula(formula, range); - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } /** @@ -1774,7 +1786,7 @@ public CellRange removeArrayFormula(Cell cell) { // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files // return _sh.removeArrayFormula(cell); - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } @Override From 335e274d1e994119270a40417be79b92138a1b13 Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Mon, 9 Oct 2023 11:27:06 +0200 Subject: [PATCH 2/7] Allow append rows in Streaming workbook --- .../apache/poi/xssf/streaming/SXSSFSheet.java | 74 +++++++++++-------- 1 file changed, 45 insertions(+), 29 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 73d05fa187a..a8282d9f2be 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,6 +51,7 @@ 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(); @@ -166,11 +167,11 @@ public SXSSFRow createRow(int rownum) { "in the range [0," + _writer.getLastFlushedRow() + "] that is already written to disk."); } - // attempt to overwrite an existing row in the input template + // attempt to overwrite a existing row in the input template if(_sh.getPhysicalNumberOfRows() > 0 && rownum <= _sh.getLastRowNum() ) { throw new IllegalArgumentException( "Attempting to write a row["+rownum+"] " + - "in the range [0," + _sh.getLastRowNum() + "] that is already written to disk."); + "in the range [0," + _sh.getLastRowNum() + "] that is already written to disk. Eventually already existing rows are ignored?"); } SXSSFRow newRow = new SXSSFRow(this); @@ -181,7 +182,7 @@ public SXSSFRow createRow(int rownum) { try { flushRows(_randomAccessWindowSize); } catch (IOException ioe) { - throw new IllegalStateException(ioe); + throw new RuntimeException(ioe); } } return newRow; @@ -205,6 +206,10 @@ public void removeRow(Row row) { return; } } + // jlolling: allow reading all the content + if (row.getSheet() == _sh) { + _sh.removeRow(row); + } } /** @@ -215,8 +220,13 @@ public void removeRow(Row row) { * @return Row representing the rownumber or null if its not defined on the sheet */ @Override - public SXSSFRow getRow(int rownum) { - return _rows.get(rownum); + public Row getRow(int rownum) { + Row row = _rows.get(rownum); + // jlolling: allow reading all the content + if (row == null) { + row = _sh.getRow(rownum); + } + return row; } /** @@ -249,7 +259,8 @@ public int getFirstRowNum() { */ @Override public int getLastRowNum() { - return _rows.isEmpty() ? -1 : _rows.lastKey(); + // jlolling allow append + return _rows.isEmpty() ? _sh.getLastRowNum() : _rows.lastKey(); } /** @@ -1008,7 +1019,7 @@ public boolean getForceFormulaRecalculation() { @NotImplemented @Override public void shiftRows(int startRow, int endRow, int n) { - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } /** @@ -1032,7 +1043,7 @@ public void shiftRows(int startRow, int endRow, int n) { @NotImplemented @Override public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) { - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } /** @@ -1169,7 +1180,7 @@ public boolean isDisplayRowColHeadings() { * Breaks occur above the specified row and left of the specified column inclusive. * * For example, {@code sheet.setColumnBreak(2);} breaks the sheet into two parts - * with columns A,B,C in the first and D,E,... in the second. Similar, {@code sheet.setRowBreak(2);} + * with columns A,B,C in the first and D,E,... in the second. Simuilar, {@code sheet.setRowBreak(2);} * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part * and rows starting with rownum=4 in the second. * @@ -1264,11 +1275,11 @@ public void setColumnGroupCollapsed(int columnNumber, boolean collapsed) { */ @Override public void groupColumn(int fromColumn, int toColumn) { - _sh.groupColumn(fromColumn, toColumn); + _sh.groupColumn(fromColumn,toColumn); } /** - * Ungroup a range of columns that were previously grouped + * Ungroup a range of columns that were previously groupped * * @param fromColumn start column (0-based) * @param toColumn end column (0-based) @@ -1317,14 +1328,16 @@ public void ungroupColumn(int fromColumn, int toColumn) { */ @Override public void groupRow(int fromRow, int toRow) { - int maxLevelRow = -1; for(SXSSFRow row : _rows.subMap(fromRow, toRow + 1).values()){ - final int level = row.getOutlineLevel() + 1; + int level = row.getOutlineLevel() + 1; row.setOutlineLevel(level); - maxLevelRow = Math.max(maxLevelRow, level); + + if(level > outlineLevelRow) { + outlineLevelRow = level; + } } - setWorksheetOutlineLevelRowIfNecessary((short) Math.min(Short.MAX_VALUE, maxLevelRow)); + setWorksheetOutlineLevelRow(); } /** @@ -1344,21 +1357,24 @@ public void groupRow(int fromRow, int toRow) { public void setRowOutlineLevel(int rownum, int level) { SXSSFRow row = _rows.get(rownum); row.setOutlineLevel(level); - setWorksheetOutlineLevelRowIfNecessary((short) Math.min(Short.MAX_VALUE, level)); + if(level > 0 && level > outlineLevelRow) { + outlineLevelRow = level; + setWorksheetOutlineLevelRow(); + } } - private void setWorksheetOutlineLevelRowIfNecessary(final short levelRow) { + private void setWorksheetOutlineLevelRow() { CTWorksheet ct = _sh.getCTWorksheet(); CTSheetFormatPr pr = ct.isSetSheetFormatPr() ? ct.getSheetFormatPr() : ct.addNewSheetFormatPr(); - if(levelRow > _sh.getSheetFormatPrOutlineLevelRow()) { - pr.setOutlineLevelRow(levelRow); + if(outlineLevelRow > 0) { + pr.setOutlineLevelRow((short)outlineLevelRow); } } /** - * Ungroup a range of rows that were previously grouped + * Ungroup a range of rows that were previously groupped * * @param fromRow start row (0-based) * @param toRow end row (0-based) @@ -1373,9 +1389,9 @@ public void ungroupRow(int fromRow, int toRow) { * * Not implemented for expanding (i.e. collapse == false) * - * @param row start row of a grouped range of rows (0-based) + * @param row start row of a groupped range of rows (0-based) * @param collapse whether to expand/collapse the detail rows - * @throws IllegalStateException if collapse is false as this is not implemented for SXSSF. + * @throws RuntimeException if collapse is false as this is not implemented for SXSSF. */ @Override public void setRowGroupCollapsed(int row, boolean collapse) { @@ -1383,7 +1399,7 @@ public void setRowGroupCollapsed(int row, boolean collapse) { collapseRow(row); } else { //expandRow(rowIndex); - throw new IllegalStateException("Unable to expand row: Not Implemented"); + throw new RuntimeException("Unable to expand row: Not Implemented"); } } @@ -1391,7 +1407,7 @@ public void setRowGroupCollapsed(int row, boolean collapse) { * @param rowIndex the zero based row index to collapse */ private void collapseRow(int rowIndex) { - SXSSFRow row = getRow(rowIndex); + SXSSFRow row = (SXSSFRow) getRow(rowIndex); if(row == null) { throw new IllegalArgumentException("Invalid row number("+ rowIndex + "). Row does not exist."); } else { @@ -1399,7 +1415,7 @@ private void collapseRow(int rowIndex) { // Hide all the columns until the end of the group int lastRow = writeHidden(row, startRow); - SXSSFRow lastRowObj = getRow(lastRow); + SXSSFRow lastRowObj = (SXSSFRow) getRow(lastRow); if (lastRowObj != null) { lastRowObj.setCollapsed(true); } else { @@ -1431,12 +1447,12 @@ private int findStartOfRowOutlineGroup(int rowIndex) { private int writeHidden(SXSSFRow xRow, int rowIndex) { int level = xRow.getOutlineLevel(); - SXSSFRow currRow = getRow(rowIndex); + SXSSFRow currRow = (SXSSFRow) getRow(rowIndex); while (currRow != null && currRow.getOutlineLevel() >= level) { currRow.setHidden(true); rowIndex++; - currRow = getRow(rowIndex); + currRow = (SXSSFRow) getRow(rowIndex); } return rowIndex; } @@ -1783,7 +1799,7 @@ public CellRange setArrayFormula(String formula, CellRangeAddres // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files // return _sh.setArrayFormula(formula, range); - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } /** @@ -1798,7 +1814,7 @@ public CellRange removeArrayFormula(Cell cell) { // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files // return _sh.removeArrayFormula(cell); - throw new IllegalStateException("Not Implemented"); + throw new RuntimeException("Not Implemented"); } @Override From 5f1adc0d70ae83ad807f2f120a21b89e79e4afe9 Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Mon, 9 Oct 2023 14:09:59 +0200 Subject: [PATCH 3/7] become conform with ASF rules --- .../java/org/apache/poi/xssf/streaming/SXSSFSheet.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 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 a8282d9f2be..727a1e56703 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 @@ -206,7 +206,7 @@ public void removeRow(Row row) { return; } } - // jlolling: allow reading all the content + // BugZilla 67646: allow reading all the content if (row.getSheet() == _sh) { _sh.removeRow(row); } @@ -222,7 +222,7 @@ public void removeRow(Row row) { @Override public Row getRow(int rownum) { Row row = _rows.get(rownum); - // jlolling: allow reading all the content + // BugZilla 67646: allow reading all the content if (row == null) { row = _sh.getRow(rownum); } @@ -259,7 +259,7 @@ public int getFirstRowNum() { */ @Override public int getLastRowNum() { - // jlolling allow append + // BugZilla 67646 allow append return _rows.isEmpty() ? _sh.getLastRowNum() : _rows.lastKey(); } @@ -1180,7 +1180,7 @@ public boolean isDisplayRowColHeadings() { * Breaks occur above the specified row and left of the specified column inclusive. * * For example, {@code sheet.setColumnBreak(2);} breaks the sheet into two parts - * with columns A,B,C in the first and D,E,... in the second. Simuilar, {@code sheet.setRowBreak(2);} + * with columns A,B,C in the first and D,E,... in the second. Similar, {@code sheet.setRowBreak(2);} * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part * and rows starting with rownum=4 in the second. * @@ -1279,7 +1279,7 @@ public void groupColumn(int fromColumn, int toColumn) { } /** - * Ungroup a range of columns that were previously groupped + * Ungroup a range of columns that were previously grouped * * @param fromColumn start column (0-based) * @param toColumn end column (0-based) From cf215817a28dfef111c44d49de69b979a9cf5094 Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Mon, 9 Oct 2023 14:11:50 +0200 Subject: [PATCH 4/7] another spell error fixed --- .../src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 727a1e56703..3803fea3bbb 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 @@ -167,7 +167,7 @@ public SXSSFRow createRow(int rownum) { "in the range [0," + _writer.getLastFlushedRow() + "] that is already written to disk."); } - // attempt to overwrite a existing row in the input template + // attempt to overwrite an existing row in the input template if(_sh.getPhysicalNumberOfRows() > 0 && rownum <= _sh.getLastRowNum() ) { throw new IllegalArgumentException( "Attempting to write a row["+rownum+"] " + From ad0b8fce09f40d1e7b31c6fcf1a51a976d1081b5 Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Tue, 27 Feb 2024 14:37:03 +0100 Subject: [PATCH 5/7] Fix compile problems and removed not necessary changes --- .gitignore | 6 ++++ build.gradle | 2 +- poi-ooxml-lite-agent/bin/.gitignore | 1 + poi-ooxml/.gitignore | 1 + .../xssf/streaming/SXSSFEvaluationSheet.java | 4 +-- .../apache/poi/xssf/streaming/SXSSFSheet.java | 33 ++++++++++--------- poi-scratchpad/bin/.gitignore | 2 ++ poi/.gitignore | 1 + 8 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 poi-ooxml-lite-agent/bin/.gitignore create mode 100644 poi-ooxml/.gitignore create mode 100644 poi-scratchpad/bin/.gitignore create mode 100644 poi/.gitignore diff --git a/.gitignore b/.gitignore index 0a69847dcbf..9771aa78cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,9 @@ lib/ # Compiled module-info class-files /poi*/src/*/java9/*.class + +.DS_Store + +.project + +.classpath diff --git a/build.gradle b/build.gradle index abcc6b9b451..6c3cd00f01d 100644 --- a/build.gradle +++ b/build.gradle @@ -100,7 +100,7 @@ allprojects { // apply plugin: 'eclipse' apply plugin: 'idea' - version = '5.2.5-SNAPSHOT' + version = '5.2.5-patched' } /** diff --git a/poi-ooxml-lite-agent/bin/.gitignore b/poi-ooxml-lite-agent/bin/.gitignore new file mode 100644 index 00000000000..ddf9c65631a --- /dev/null +++ b/poi-ooxml-lite-agent/bin/.gitignore @@ -0,0 +1 @@ +/main/ diff --git a/poi-ooxml/.gitignore b/poi-ooxml/.gitignore new file mode 100644 index 00000000000..ae3c1726048 --- /dev/null +++ b/poi-ooxml/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFEvaluationSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFEvaluationSheet.java index 3f19893da84..93530de5071 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFEvaluationSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFEvaluationSheet.java @@ -51,14 +51,14 @@ public int getLastRowNum() { */ @Override public boolean isRowHidden(int rowIndex) { - SXSSFRow row = _xs.getRow(rowIndex); + SXSSFRow row = (SXSSFRow) _xs.getRow(rowIndex); if (row == null) return false; return row.getZeroHeight(); } @Override public EvaluationCell getCell(int rowIndex, int columnIndex) { - SXSSFRow row = _xs.getRow(rowIndex); + SXSSFRow row = (SXSSFRow) _xs.getRow(rowIndex); if (row == null) { if (rowIndex <= _xs.getLastFlushedRowNum()) { throw new SXSSFFormulaEvaluator.RowFlushedException(rowIndex, _xs.getLastFlushedRowNum()); 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 6f50d8c112c..ae99fdae4a9 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 @@ -167,11 +167,11 @@ public SXSSFRow createRow(int rownum) { "in the range [0," + _writer.getLastFlushedRow() + "] that is already written to disk."); } - // attempt to overwrite a existing row in the input template + // attempt to overwrite an existing row in the input template if(_sh.getPhysicalNumberOfRows() > 0 && rownum <= _sh.getLastRowNum() ) { throw new IllegalArgumentException( "Attempting to write a row["+rownum+"] " + - "in the range [0," + _sh.getLastRowNum() + "] that is already written to disk. Eventually already existing rows are ignored?"); + "in the range [0," + _sh.getLastRowNum() + "] that is already written to disk."); } SXSSFRow newRow = new SXSSFRow(this); @@ -182,7 +182,7 @@ public SXSSFRow createRow(int rownum) { try { flushRows(_randomAccessWindowSize); } catch (IOException ioe) { - throw new RuntimeException(ioe); + throw new IllegalStateException(ioe); } } return newRow; @@ -206,10 +206,6 @@ public void removeRow(Row row) { return; } } - // BugZilla 67646: allow reading all the content - if (row.getSheet() == _sh) { - _sh.removeRow(row); - } } /** @@ -1019,7 +1015,7 @@ public boolean getForceFormulaRecalculation() { @NotImplemented @Override public void shiftRows(int startRow, int endRow, int n) { - throw new RuntimeException("Not Implemented"); + throw new IllegalStateException("Not Implemented"); } /** @@ -1043,7 +1039,7 @@ public void shiftRows(int startRow, int endRow, int n) { @NotImplemented @Override public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) { - throw new RuntimeException("Not Implemented"); + throw new IllegalStateException("Not Implemented"); } /** @@ -1180,7 +1176,7 @@ public boolean isDisplayRowColHeadings() { * Breaks occur above the specified row and left of the specified column inclusive. * * For example, {@code sheet.setColumnBreak(2);} breaks the sheet into two parts - * with columns A,B,C in the first and D,E,... in the second. Simuilar, {@code sheet.setRowBreak(2);} + * with columns A,B,C in the first and D,E,... in the second. Similar, {@code sheet.setRowBreak(2);} * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part * and rows starting with rownum=4 in the second. * @@ -1279,7 +1275,7 @@ public void groupColumn(int fromColumn, int toColumn) { } /** - * Ungroup a range of columns that were previously groupped + * Ungroup a range of columns that were previously grouped * * @param fromColumn start column (0-based) * @param toColumn end column (0-based) @@ -1374,7 +1370,7 @@ private void setWorksheetOutlineLevelRow() { } /** - * Ungroup a range of rows that were previously groupped + * Ungroup a range of rows that were previously grouped * * @param fromRow start row (0-based) * @param toRow end row (0-based) @@ -1389,7 +1385,7 @@ public void ungroupRow(int fromRow, int toRow) { * * Not implemented for expanding (i.e. collapse == false) * - * @param row start row of a groupped range of rows (0-based) + * @param row start row of a grouped range of rows (0-based) * @param collapse whether to expand/collapse the detail rows * @throws RuntimeException if collapse is false as this is not implemented for SXSSF. */ @@ -1399,7 +1395,7 @@ public void setRowGroupCollapsed(int row, boolean collapse) { collapseRow(row); } else { //expandRow(rowIndex); - throw new RuntimeException("Unable to expand row: Not Implemented"); + throw new IllegalStateException("Unable to expand row: Not Implemented"); } } @@ -1799,7 +1795,7 @@ public CellRange setArrayFormula(String formula, CellRangeAddres // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files // return _sh.setArrayFormula(formula, range); - throw new RuntimeException("Not Implemented"); + throw new IllegalStateException("Not Implemented"); } /** @@ -1814,7 +1810,7 @@ public CellRange removeArrayFormula(Cell cell) { // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files // return _sh.removeArrayFormula(cell); - throw new RuntimeException("Not Implemented"); + throw new IllegalStateException("Not Implemented"); } @Override @@ -2188,6 +2184,11 @@ public void setTabColor(int colorIndex){ pr.setTabColor(color); } + /** + * This method is not yet supported. + * + * @throws UnsupportedOperationException this method is not yet supported + */ @NotImplemented @Override public void shiftColumns(int startColumn, int endColumn, int n){ diff --git a/poi-scratchpad/bin/.gitignore b/poi-scratchpad/bin/.gitignore new file mode 100644 index 00000000000..7eed456bec8 --- /dev/null +++ b/poi-scratchpad/bin/.gitignore @@ -0,0 +1,2 @@ +/main/ +/test/ diff --git a/poi/.gitignore b/poi/.gitignore new file mode 100644 index 00000000000..ae3c1726048 --- /dev/null +++ b/poi/.gitignore @@ -0,0 +1 @@ +/bin/ From 5c356b47739e5d73a19f7d76e4d96a77277070b0 Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Tue, 27 Feb 2024 14:40:28 +0100 Subject: [PATCH 6/7] changed version number back --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 6c3cd00f01d..abcc6b9b451 100644 --- a/build.gradle +++ b/build.gradle @@ -100,7 +100,7 @@ allprojects { // apply plugin: 'eclipse' apply plugin: 'idea' - version = '5.2.5-patched' + version = '5.2.5-SNAPSHOT' } /** From b192eb5cafa14c82dd82d29c8da94f06a1c520de Mon Sep 17 00:00:00 2001 From: Jan Lolling Date: Tue, 27 Feb 2024 15:31:23 +0100 Subject: [PATCH 7/7] Solved the internal usage of getRow() by replacing the calls to getSXSSFRow() --- .../apache/poi/xssf/streaming/SXSSFSheet.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 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 ae99fdae4a9..42ddd9111ce 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 @@ -211,6 +211,8 @@ public void removeRow(Row row) { /** * Returns the logical row (not physical) 0-based. If you ask for a row that is not * defined you get a null. This is to say row 4 represents the fifth row on a sheet. + * If the row is not created in this streaming sheet, instead is part of the XSSFSheet + * then this method takes the row from the XSSFSheet. * * @param rownum row to get (0-based) * @return Row representing the rownumber or null if its not defined on the sheet @@ -218,13 +220,25 @@ public void removeRow(Row row) { @Override public Row getRow(int rownum) { Row row = _rows.get(rownum); - // BugZilla 67646: allow reading all the content + // BugZilla 67646: allow reading all the content also from template sheet if (row == null) { row = _sh.getRow(rownum); } return row; } + /** + * Returns the logical row (not physical) 0-based. If you ask for a row that is not + * defined you get a null. This is to say row 4 represents the fifth row on a sheet. + * + * @param rownum row to get (0-based) + * @return Row representing the rownumber or null if its not defined on the sheet + */ + private SXSSFRow getSXSSFRow(int rownum) { + SXSSFRow row = _rows.get(rownum); + return row; + } + /** * Returns the number of physically defined rows (NOT the number of rows in the sheet) * @@ -1387,7 +1401,7 @@ public void ungroupRow(int fromRow, int toRow) { * * @param row start row of a grouped range of rows (0-based) * @param collapse whether to expand/collapse the detail rows - * @throws RuntimeException if collapse is false as this is not implemented for SXSSF. + * @throws IllegalStateException if collapse is false as this is not implemented for SXSSF. */ @Override public void setRowGroupCollapsed(int row, boolean collapse) { @@ -1403,7 +1417,7 @@ public void setRowGroupCollapsed(int row, boolean collapse) { * @param rowIndex the zero based row index to collapse */ private void collapseRow(int rowIndex) { - SXSSFRow row = (SXSSFRow) getRow(rowIndex); + SXSSFRow row = getSXSSFRow(rowIndex); if(row == null) { throw new IllegalArgumentException("Invalid row number("+ rowIndex + "). Row does not exist."); } else { @@ -1411,11 +1425,11 @@ private void collapseRow(int rowIndex) { // Hide all the columns until the end of the group int lastRow = writeHidden(row, startRow); - SXSSFRow lastRowObj = (SXSSFRow) getRow(lastRow); + SXSSFRow lastRowObj = getSXSSFRow(lastRow); if (lastRowObj != null) { lastRowObj.setCollapsed(true); } else { - SXSSFRow newRow = createRow(lastRow); + SXSSFRow newRow = createRow(lastRow); newRow.setCollapsed(true); } } @@ -1441,14 +1455,13 @@ private int findStartOfRowOutlineGroup(int rowIndex) { return currentRow + 1; } - private int writeHidden(SXSSFRow xRow, int rowIndex) { + private int writeHidden(Row xRow, int rowIndex) { int level = xRow.getOutlineLevel(); - SXSSFRow currRow = (SXSSFRow) getRow(rowIndex); - + SXSSFRow currRow = getSXSSFRow(rowIndex); while (currRow != null && currRow.getOutlineLevel() >= level) { currRow.setHidden(true); rowIndex++; - currRow = (SXSSFRow) getRow(rowIndex); + currRow = getSXSSFRow(rowIndex); } return rowIndex; }