View | Details | Raw Unified | Return to bug 51635
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (-36 / +16 lines)
Lines 570-576 Link Here
570
            ctRow = prev.getCTRow();
570
            ctRow = prev.getCTRow();
571
            ctRow.set(CTRow.Factory.newInstance());
571
            ctRow.set(CTRow.Factory.newInstance());
572
        } else {
572
        } else {
573
            ctRow = worksheet.getSheetData().addNewRow();
573
        	if(_rows.isEmpty() || rownum > _rows.lastKey()) {
574
        		// we can append the new row at the end
575
        		ctRow = worksheet.getSheetData().addNewRow();
576
        	} else {
577
        		// get number of rows where row index < rownum
578
        		// --> this tells us where our row should go
579
        		int idx = _rows.headMap(rownum).size();
580
        		ctRow = worksheet.getSheetData().insertNewRow(idx);
581
        	}
574
        }
582
        }
575
        XSSFRow r = new XSSFRow(ctRow, this);
583
        XSSFRow r = new XSSFRow(ctRow, this);
576
        r.setRowNum(rownum);
584
        r.setRowNum(rownum);
Lines 1496-1502 Link Here
1496
1504
1497
        for(XSSFCell cell : cellsToDelete) row.removeCell(cell);
1505
        for(XSSFCell cell : cellsToDelete) row.removeCell(cell);
1498
1506
1507
        int idx = _rows.headMap(row.getRowNum()).size();
1499
        _rows.remove(row.getRowNum());
1508
        _rows.remove(row.getRowNum());
1509
        worksheet.getSheetData().removeRow(idx);
1500
    }
1510
    }
1501
1511
1502
    /**
1512
    /**
Lines 2355-2361 Link Here
2355
     */
2365
     */
2356
    @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
2366
    @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
2357
    public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) {
2367
    public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) {
2358
        for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
2368
    	for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
2359
            XSSFRow row = (XSSFRow)it.next();
2369
            XSSFRow row = (XSSFRow)it.next();
2360
            int rownum = row.getRowNum();
2370
            int rownum = row.getRowNum();
2361
            if(rownum < startRow) continue;
2371
            if(rownum < startRow) continue;
Lines 2365-2370 Link Here
2365
            }
2375
            }
2366
2376
2367
            if (removeRow(startRow, endRow, n, rownum)) {
2377
            if (removeRow(startRow, endRow, n, rownum)) {
2378
            	// remove row from worksheet.getSheetData row array
2379
            	int idx = _rows.headMap(row.getRowNum()).size();
2380
                worksheet.getSheetData().removeRow(idx);
2381
                // remove row from _rows
2368
                it.remove();
2382
                it.remove();
2369
            }
2383
            }
2370
            else if (rownum >= startRow && rownum <= endRow) {
2384
            else if (rownum >= startRow && rownum <= endRow) {
Lines 2689-2695 Link Here
2689
        for(XSSFRow row : _rows.values()){
2703
        for(XSSFRow row : _rows.values()){
2690
            row.onDocumentWrite();
2704
            row.onDocumentWrite();
2691
        }
2705
        }
2692
        ensureRowOrdering();
2693
2706
2694
        XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
2707
        XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
2695
        xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
2708
        xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
Lines 2701-2739 Link Here
2701
    }
2714
    }
2702
2715
2703
    /**
2716
    /**
2704
     * ensure that the array of CTRow written to CTSheetData is ordered by row index
2705
     */
2706
    private void ensureRowOrdering(){
2707
        CTSheetData sheetData = worksheet.getSheetData();
2708
        // check if row indexes in CTSheetData match the internal model:
2709
        // rows in the internal model (_rows) are always ordered while
2710
        // CTRow beans held by CTSheetData may be not, for example, user can
2711
        // insert rows in random order, shift rows after insertion, etc.
2712
        boolean isOrdered = true;
2713
        if(sheetData.sizeOfRowArray() != _rows.size()) isOrdered = false;
2714
        else {
2715
            int i = 0;
2716
            for (XSSFRow row : _rows.values()) {
2717
                CTRow c1 = row.getCTRow();
2718
                CTRow c2 = sheetData.getRowArray(i++); 
2719
                if (c1.getR() != c2.getR()){
2720
                    isOrdered = false;
2721
                    break;
2722
                }
2723
            }
2724
        }
2725
        
2726
        if(!isOrdered){
2727
            CTRow[] cArray = new CTRow[_rows.size()];
2728
            int i = 0;
2729
            for(XSSFRow row : _rows.values()){
2730
                cArray[i++] = row.getCTRow();
2731
            }
2732
            sheetData.setRowArray(cArray);
2733
        }
2734
    }
2735
2736
    /**
2737
     * @return true when Autofilters are locked and the sheet is protected.
2717
     * @return true when Autofilters are locked and the sheet is protected.
2738
     */
2718
     */
2739
    public boolean isAutoFilterLocked() {
2719
    public boolean isAutoFilterLocked() {
(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java (-9 / +9 lines)
Lines 901-907 Link Here
901
901
902
    /**
902
    /**
903
     * Rows and cells can be created in random order,
903
     * Rows and cells can be created in random order,
904
     * but serialization forces strict ascending order of the CTRow and CTCell xml beans
904
     * but CTRows are kept in ascending order
905
     */
905
     */
906
    public void testCreateRow() {
906
    public void testCreateRow() {
907
        XSSFWorkbook workbook = new XSSFWorkbook();
907
        XSSFWorkbook workbook = new XSSFWorkbook();
Lines 929-948 Link Here
929
        CTRow[] xrow = sheetData.getRowArray();
929
        CTRow[] xrow = sheetData.getRowArray();
930
        assertEquals(3, xrow.length);
930
        assertEquals(3, xrow.length);
931
931
932
        //rows are unsorted: {2, 1, 0}
932
        //rows are sorted: {0, 1, 2}
933
        assertEquals(2, xrow[0].sizeOfCArray());
933
        assertEquals(4, xrow[0].sizeOfCArray());
934
        assertEquals(3, xrow[0].getR());
934
        assertEquals(1, xrow[0].getR());
935
        assertTrue(xrow[0].equals(row1.getCTRow()));
935
        assertTrue(xrow[0].equals(row3.getCTRow()));
936
936
937
        assertEquals(3, xrow[1].sizeOfCArray());
937
        assertEquals(3, xrow[1].sizeOfCArray());
938
        assertEquals(2, xrow[1].getR());
938
        assertEquals(2, xrow[1].getR());
939
        assertTrue(xrow[1].equals(row2.getCTRow()));
939
        assertTrue(xrow[1].equals(row2.getCTRow()));
940
940
941
        assertEquals(4, xrow[2].sizeOfCArray());
941
        assertEquals(2, xrow[2].sizeOfCArray());
942
        assertEquals(1, xrow[2].getR());
942
        assertEquals(3, xrow[2].getR());
943
        assertTrue(xrow[2].equals(row3.getCTRow()));
943
        assertTrue(xrow[2].equals(row1.getCTRow()));
944
944
945
        CTCell[] xcell = xrow[2].getCArray();
945
        CTCell[] xcell = xrow[0].getCArray();
946
        assertEquals("D1", xcell[0].getR());
946
        assertEquals("D1", xcell[0].getR());
947
        assertEquals("A1", xcell[1].getR());
947
        assertEquals("A1", xcell[1].getR());
948
        assertEquals("C1", xcell[2].getR());
948
        assertEquals("C1", xcell[2].getR());

Return to bug 51635