dhatim / fastexcel

Generate and read big Excel files quickly
Other
677 stars 122 forks source link

Inconsistency between row-number and row-index #294

Closed d22 closed 1 year ago

d22 commented 1 year ago

An exception is thrown, when getting a Cell from a Row by CellAddress, although it should work.

Consider the follwoing case:

public void testCellAddress() {
    URL inputFile = getClass().getClassLoader().getResource("xlsx/input-00.xlsx");
    assert inputFile != null;
    final File xlsxFile = new File(inputFile.getFile());
    try (ReadableWorkbook wb = new ReadableWorkbook(xlsxFile)) {
        Sheet firstSheet = wb.getFirstSheet();
        Optional<Row> optionalRow = firstSheet.openStream().findFirst();
        assert optionalRow.isPresent();
        Row row = optionalRow.get();
        System.out.println("rowNum: " + row.getRowNum());
        Cell cellA1 = row.getCell(0);
        System.out.println("cellAddress: " + cellA1.getAddress());
        row.getCell(new CellAddress("A1"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

which produces the following output:

rowNum: 1
cellAddress: A1

java.lang.IllegalArgumentException: The given address A1 concerns another row (1)
    at org.dhatim.fastexcel.reader.Row.getCell(Row.java:47)
    [...]

The issue is, that rowNum, the row number in Row, is not a 0 based index, while the CellAddress::getRow returns a 0 based index. See Row.java:46.

Thanks for looking into this. Jonas

d22 commented 1 year ago

I created a PR with a suggestion for a fix and an according unit test.