ctRow = prev.getCTRow();
ctRow.set(CTRow.Factory.newInstance());
} else {
- ctRow = worksheet.getSheetData().addNewRow();
+ if(_rows.isEmpty() || rownum > _rows.lastKey()) {
+ // we can append the new row at the end
+ ctRow = worksheet.getSheetData().addNewRow();
+ } else {
+ // get number of rows where row index < rownum
+ // --> this tells us where our row should go
+ int idx = _rows.headMap(rownum).size();
+ ctRow = worksheet.getSheetData().insertNewRow(idx);
+ }
}
XSSFRow r = new XSSFRow(ctRow, this);
r.setRowNum(rownum);
for(XSSFCell cell : cellsToDelete) row.removeCell(cell);
+ int idx = _rows.headMap(row.getRowNum()).size();
_rows.remove(row.getRowNum());
+ worksheet.getSheetData().removeRow(idx);
}
/**
*/
@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) {
- for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
+ for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
XSSFRow row = (XSSFRow)it.next();
int rownum = row.getRowNum();
if(rownum < startRow) continue;
}
if (removeRow(startRow, endRow, n, rownum)) {
+ // remove row from worksheet.getSheetData row array
+ int idx = _rows.headMap(row.getRowNum()).size();
+ worksheet.getSheetData().removeRow(idx);
+ // remove row from _rows
it.remove();
}
else if (rownum >= startRow && rownum <= endRow) {
for(XSSFRow row : _rows.values()){
row.onDocumentWrite();
}
- ensureRowOrdering();
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
worksheet.save(out, xmlOptions);
}
- /**
- * ensure that the array of CTRow written to CTSheetData is ordered by row index
- */
- private void ensureRowOrdering(){
- CTSheetData sheetData = worksheet.getSheetData();
- // check if row indexes in CTSheetData match the internal model:
- // rows in the internal model (_rows) are always ordered while
- // CTRow beans held by CTSheetData may be not, for example, user can
- // insert rows in random order, shift rows after insertion, etc.
- boolean isOrdered = true;
- if(sheetData.sizeOfRowArray() != _rows.size()) isOrdered = false;
- else {
- int i = 0;
- for (XSSFRow row : _rows.values()) {
- CTRow c1 = row.getCTRow();
- CTRow c2 = sheetData.getRowArray(i++);
- if (c1.getR() != c2.getR()){
- isOrdered = false;
- break;
- }
- }
- }
-
- if(!isOrdered){
- CTRow[] cArray = new CTRow[_rows.size()];
- int i = 0;
- for(XSSFRow row : _rows.values()){
- cArray[i++] = row.getCTRow();
- }
- sheetData.setRowArray(cArray);
- }
- }
-
/**
* @return true when Autofilters are locked and the sheet is protected.
*/
/**
* Rows and cells can be created in random order,
- * but serialization forces strict ascending order of the CTRow and CTCell xml beans
+ * but CTRows are kept in ascending order
*/
public void testCreateRow() {
XSSFWorkbook workbook = new XSSFWorkbook();
CTRow[] xrow = sheetData.getRowArray();
assertEquals(3, xrow.length);
- //rows are unsorted: {2, 1, 0}
- assertEquals(2, xrow[0].sizeOfCArray());
- assertEquals(3, xrow[0].getR());
- assertTrue(xrow[0].equals(row1.getCTRow()));
+ //rows are sorted: {0, 1, 2}
+ assertEquals(4, xrow[0].sizeOfCArray());
+ assertEquals(1, xrow[0].getR());
+ assertTrue(xrow[0].equals(row3.getCTRow()));
assertEquals(3, xrow[1].sizeOfCArray());
assertEquals(2, xrow[1].getR());
assertTrue(xrow[1].equals(row2.getCTRow()));
- assertEquals(4, xrow[2].sizeOfCArray());
- assertEquals(1, xrow[2].getR());
- assertTrue(xrow[2].equals(row3.getCTRow()));
+ assertEquals(2, xrow[2].sizeOfCArray());
+ assertEquals(3, xrow[2].getR());
+ assertTrue(xrow[2].equals(row1.getCTRow()));
- CTCell[] xcell = xrow[2].getCArray();
+ CTCell[] xcell = xrow[0].getCArray();
assertEquals("D1", xcell[0].getR());
assertEquals("A1", xcell[1].getR());
assertEquals("C1", xcell[2].getR());