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

StreamingRow.getLastCellNum() off-by-one and blank cells #62

Closed
ms1111 opened this issue Nov 4, 2016 · 1 comment
Closed

StreamingRow.getLastCellNum() off-by-one and blank cells #62

ms1111 opened this issue Nov 4, 2016 · 1 comment

Comments

@ms1111
Copy link
Contributor

ms1111 commented Nov 4, 2016

Hi! I think getLastCellNum() may be returning a value 1 more than intended.

With two columns (A and B), it returns a value of 3. (see implementation below - it's returning size() + 1).

With a blank column - let's say we have A populated, B blank, C blank, and D populated - it's not looking for the highest valued column index, just the number of populated columns, so the result there is not correct either.

  /**
   * Gets the index of the last cell contained in this row <b>PLUS ONE</b>.
   * 
   * @return short representing the last logical cell in the row <b>PLUS ONE</b>,
   *   or -1 if the row does not contain any cells.
   */
  @Override
  public short getLastCellNum() {
    return (short) (cellMap.size() == 0 ? -1 : cellMap.size() + 1);
  }

Workaround, to get the number of cells in a row with either XLSX streaming or regular POI:

    private int numCellsInRow(Row row) {
        if (row instanceof StreamingRow) {
            List<Integer> colIndexes = new ArrayList<>(((StreamingRow) row).getCellMap().keySet());
            if (colIndexes.size() < 1) {
                return 0;
            }
            colIndexes.sort(null);
            return colIndexes.get(colIndexes.size()-1) + 1;
        }
        else {
            // HSSFRow.getLastCellNum() returns the 1-based index of the last cell, or -1
            return (row.getLastCellNum() < 1) ? 0 : row.getLastCellNum();
        }
    }
@ms1111
Copy link
Contributor Author

ms1111 commented Dec 14, 2016

Fixed by #61

@ms1111 ms1111 closed this as completed Dec 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant