diff --git a/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/Row.java b/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/Row.java index 9c9a66ee..0e3ae4fb 100644 --- a/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/Row.java +++ b/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/Row.java @@ -25,11 +25,13 @@ public class Row implements Iterable { private final int rowNum; private final List cells; private final int physicalCellCount; + private final boolean isHidden; - Row(int rowNum, int physicalCellCount, List cells) { + Row(int rowNum, int physicalCellCount, List cells, boolean isHidden) { this.rowNum = rowNum; this.physicalCellCount = physicalCellCount; this.cells = cells; + this.isHidden = isHidden; } /** @@ -81,6 +83,15 @@ public int getPhysicalCellCount() { return physicalCellCount; } + /** + * Indicates whether this row is hidden in the worksheet. + * + * @return {@code true} if the row is hidden; {@code false} otherwise + */ + public boolean isHidden() { + return isHidden; + } + @Override public String toString() { return "Row " + rowNum + ' ' + cells; diff --git a/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/RowSpliterator.java b/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/RowSpliterator.java index 18ed4aad..6be28bac 100644 --- a/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/RowSpliterator.java +++ b/fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/RowSpliterator.java @@ -86,6 +86,7 @@ private Row next() throws XMLStreamException { int trackedColIndex = 0; int rowIndex = getRowIndexWithFallback(++trackedRowIndex); + boolean isHidden = "1".equals(r.getAttribute("hidden")); List cells = new ArrayList<>(rowCapacity); int physicalCellCount = 0; @@ -103,7 +104,7 @@ private Row next() throws XMLStreamException { physicalCellCount++; } rowCapacity = Math.max(rowCapacity, cells.size()); - return new Row(rowIndex, physicalCellCount, cells); + return new Row(rowIndex, physicalCellCount, cells, isHidden); } private int getRowIndexWithFallback(int fallbackRowIndex) { diff --git a/fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/RowTest.java b/fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/RowTest.java index e5c694ce..e49cf898 100644 --- a/fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/RowTest.java +++ b/fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/RowTest.java @@ -1,10 +1,14 @@ package org.dhatim.fastexcel.reader; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import java.io.IOException; import java.io.InputStream; +import java.math.BigDecimal; +import static org.assertj.core.api.Assertions.assertThat; import static org.dhatim.fastexcel.reader.Resources.open; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -25,4 +29,49 @@ public void testGetCellByAddress() throws IOException { assertEquals(cellIndex0, cellA1); } } + + @ParameterizedTest + @CsvSource({ + "0, false, 1, Lorem, 43101, 1+A1, true", + "1, true, 2, ipsum, 43102, 1+A2, false", + "2, true, 3, dolor, 43103, 1+A3, true", + "3, false, 4, sit, 43104, 1+A4, false", + "4, false, 5, amet, 43105, 1+A5, true", + "5, false, 6, consectetur, 43106, 1+A6, false", + "6, true, 7, adipiscing, 43107, 1+A7, true", + "7, true, 8, elit, 43108, 1+A8, false", + "8, true, 9, Ut, 43109, 1+A9, true", + "9, false, 10, nec, 43110, 1+A10, false" + }) + public void shouldGetVisibleAndHiddenRows(int index, boolean isHidden, String cellIndex0, String cellIndex1, int cellIndex2, String cellIndex3, + boolean cellIndex4) throws IOException { + try (InputStream inputStream = open("/xlsx/simple-with-hidden-rows.xlsx"); + ReadableWorkbook excel = new ReadableWorkbook(inputStream)) { + Sheet firstSheet = excel.getFirstSheet(); + + Row row = firstSheet.read().get(index); + + assertThat(row.isHidden()).isEqualTo(isHidden); + assertThat(row.getCell(0)).satisfies(cell -> { + assertThat(cell.getType()).isEqualTo(CellType.NUMBER); + assertThat(cell.asNumber()).isEqualTo(new BigDecimal(cellIndex0)); + }); + assertThat(row.getCell(1)).satisfies(cell -> { + assertThat(cell.getType()).isEqualTo(CellType.STRING); + assertThat(cell.asString()).isEqualTo(cellIndex1); + }); + assertThat(row.getCell(2)).satisfies(cell -> { + assertThat(cell.getType()).isEqualTo(CellType.NUMBER); + assertThat(cell.asNumber()).isEqualTo(new BigDecimal(cellIndex2)); + }); + assertThat(row.getCell(3)).satisfies(cell -> { + assertThat(cell.getType()).isEqualTo(CellType.FORMULA); + assertThat(cell.getFormula()).isEqualTo(cellIndex3); + }); + assertThat(row.getCell(4)).satisfies(cell -> { + assertThat(cell.getType()).isEqualTo(CellType.BOOLEAN); + assertThat(cell.asBoolean()).isEqualTo(cellIndex4); + }); + } + } } diff --git a/fastexcel-reader/src/test/resources/xlsx/simple-with-hidden-rows.xlsx b/fastexcel-reader/src/test/resources/xlsx/simple-with-hidden-rows.xlsx new file mode 100644 index 00000000..97000fd5 Binary files /dev/null and b/fastexcel-reader/src/test/resources/xlsx/simple-with-hidden-rows.xlsx differ