https://bz.apache.org/bugzilla/show_bug.cgi?id=53877

Dominik Stadler <dominik.stad...@gmx.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |WORKSFORME

--- Comment #4 from Dominik Stadler <dominik.stad...@gmx.at> ---
I tried it again at it does work for me, not sure where the difference is to
what you see. Please use the following unit test and show how it needs to be
adjusted to cause the problem if this is really still a problem for you.

This test verifies that "A", "D", "E" end up in the columns, but I also checked
with LibreOffice (sorry, no MS Office on this machine)

    private static final int ROWS = 10;
    private static final int COLS = 10;

    @Test
    public void test_deleteColumns() throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sh = wb.createSheet("sheet1");
        HSSFRow r = null;
        for (int row = 0; row < ROWS; row++) {
            r = sh.createRow(row);
            for (int col = 0; col < COLS; col++) {
                r.createCell(col);
            }
        }
        sh.getRow(2).getCell(0).setCellValue("A");
        sh.getRow(2).getCell(1).setCellValue("B");
        sh.getRow(2).getCell(2).setCellValue("C");
        sh.getRow(2).getCell(3).setCellValue("D");
        sh.getRow(2).getCell(4).setCellValue("E");
        // Put in ABCDE into consecutive cells;
        // deletion should have gotten ADE but got ACE instead.
        for (int deleteIndex = 0; deleteIndex < 2; deleteIndex++) {
            HSSFRow row = sh.getRow(2);
            if (null != row) {
                deleteColumnFromRow(row, 1);
            }
        }

        assertEquals("A", sh.getRow(2).getCell(0).getStringCellValue());
        assertEquals("D", sh.getRow(2).getCell(1).getStringCellValue());
        assertEquals("E", sh.getRow(2).getCell(2).getStringCellValue());

        wb.close();
    }


    private void deleteColumnFromRow(HSSFRow row, final int START_COL) {
        if (START_COL >= COLS) {
            return;
        }
        HSSFCell check = row.getCell(START_COL);
        if (null != check) {
            row.removeCell(check);
        }
        System.err.println(
                "        deleteColumnFromRow: row = " + row.getRowNum());
        HSSFCell cell = null;
        for (int col = START_COL + 1; col < COLS; col++) {
            int source = col;
            int destination = col - 1;
            cell = row.getCell(source);
            if (null != row.getCell(destination)) {
                row.removeCell(row.getCell(destination));
                // Because the API says the destination cannot exist,
                // the destination has been deleted. The API reference is
                //
poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFRow.html
            }
            if (null != cell) {
                // moveCell is most likely where the error occurs.
                row.moveCell(cell, (short) (destination));
            }
        }
    }

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to