Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugZilla 67646 - allow append rows to streaming workbooks #600

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ lib/

# Compiled module-info class-files
/poi*/src/*/java9/*.class

.DS_Store

.project

.classpath
1 change: 1 addition & 0 deletions poi-ooxml-lite-agent/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/main/
1 change: 1 addition & 0 deletions poi-ooxml/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -210,13 +211,32 @@ 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
*/
@Override
public SXSSFRow getRow(int rownum) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might break user code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 this cannot be changed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this is compatible with the interface

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok - we might be able to change this but it would need to be in a major release - eg POI 6.0.0

return _rows.get(rownum);
public Row getRow(int rownum) {
Row row = _rows.get(rownum);
// BugZilla 67646: allow reading all the content also from template sheet
if (row == null) {
row = _sh.getRow(rownum);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use spaces not tabs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change my IDE settings

}
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;
}

/**
Expand Down Expand Up @@ -249,7 +269,8 @@ public int getFirstRowNum() {
*/
@Override
public int getLastRowNum() {
return _rows.isEmpty() ? -1 : _rows.lastKey();
// BugZilla 67646 allow append
return _rows.isEmpty() ? _sh.getLastRowNum() : _rows.lastKey();
}

/**
Expand Down Expand Up @@ -1264,7 +1285,7 @@ public void setColumnGroupCollapsed(int columnNumber, boolean collapsed) {
*/
@Override
public void groupColumn(int fromColumn, int toColumn) {
_sh.groupColumn(fromColumn, toColumn);
_sh.groupColumn(fromColumn,toColumn);
}

/**
Expand Down Expand Up @@ -1317,14 +1338,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();
}

/**
Expand All @@ -1344,16 +1367,19 @@ 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);
}
}

Expand Down Expand Up @@ -1391,19 +1417,19 @@ 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 = getSXSSFRow(rowIndex);
if(row == null) {
throw new IllegalArgumentException("Invalid row number("+ rowIndex + "). Row does not exist.");
} else {
int startRow = findStartOfRowOutlineGroup(rowIndex);

// Hide all the columns until the end of the group
int lastRow = writeHidden(row, startRow);
SXSSFRow lastRowObj = getRow(lastRow);
SXSSFRow lastRowObj = getSXSSFRow(lastRow);
if (lastRowObj != null) {
lastRowObj.setCollapsed(true);
} else {
SXSSFRow newRow = createRow(lastRow);
SXSSFRow newRow = createRow(lastRow);
newRow.setCollapsed(true);
}
}
Expand All @@ -1429,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 = getRow(rowIndex);

SXSSFRow currRow = getSXSSFRow(rowIndex);
while (currRow != null && currRow.getOutlineLevel() >= level) {
currRow.setHidden(true);
rowIndex++;
currRow = getRow(rowIndex);
currRow = getSXSSFRow(rowIndex);
}
return rowIndex;
}
Expand Down Expand Up @@ -2172,6 +2197,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){
Expand Down
2 changes: 2 additions & 0 deletions poi-scratchpad/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/main/
/test/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these gitignore changes should be removed - you can update your own user .gitignore but we don't need these in svn - POI is not a Git project - it is an svn project

1 change: 1 addition & 0 deletions poi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/