Skip to content

Commit 1c774e1

Browse files
authored
Merge pull request #571 from igorbolic/row-visibility
Keep track of row hidden/visible state
2 parents fbeba18 + 5e46a9f commit 1c774e1

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/Row.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ public class Row implements Iterable<Cell> {
2525
private final int rowNum;
2626
private final List<Cell> cells;
2727
private final int physicalCellCount;
28+
private final boolean isHidden;
2829

29-
Row(int rowNum, int physicalCellCount, List<Cell> cells) {
30+
Row(int rowNum, int physicalCellCount, List<Cell> cells, boolean isHidden) {
3031
this.rowNum = rowNum;
3132
this.physicalCellCount = physicalCellCount;
3233
this.cells = cells;
34+
this.isHidden = isHidden;
3335
}
3436

3537
/**
@@ -81,6 +83,15 @@ public int getPhysicalCellCount() {
8183
return physicalCellCount;
8284
}
8385

86+
/**
87+
* Indicates whether this row is hidden in the worksheet.
88+
*
89+
* @return {@code true} if the row is hidden; {@code false} otherwise
90+
*/
91+
public boolean isHidden() {
92+
return isHidden;
93+
}
94+
8495
@Override
8596
public String toString() {
8697
return "Row " + rowNum + ' ' + cells;

fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/RowSpliterator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private Row next() throws XMLStreamException {
8686

8787
int trackedColIndex = 0;
8888
int rowIndex = getRowIndexWithFallback(++trackedRowIndex);
89+
boolean isHidden = "1".equals(r.getAttribute("hidden"));
8990

9091
List<Cell> cells = new ArrayList<>(rowCapacity);
9192
int physicalCellCount = 0;
@@ -103,7 +104,7 @@ private Row next() throws XMLStreamException {
103104
physicalCellCount++;
104105
}
105106
rowCapacity = Math.max(rowCapacity, cells.size());
106-
return new Row(rowIndex, physicalCellCount, cells);
107+
return new Row(rowIndex, physicalCellCount, cells, isHidden);
107108
}
108109

109110
private int getRowIndexWithFallback(int fallbackRowIndex) {

fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/RowTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package org.dhatim.fastexcel.reader;
22

33
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
46

57
import java.io.IOException;
68
import java.io.InputStream;
9+
import java.math.BigDecimal;
710

11+
import static org.assertj.core.api.Assertions.assertThat;
812
import static org.dhatim.fastexcel.reader.Resources.open;
913
import static org.junit.jupiter.api.Assertions.assertEquals;
1014
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -25,4 +29,49 @@ public void testGetCellByAddress() throws IOException {
2529
assertEquals(cellIndex0, cellA1);
2630
}
2731
}
32+
33+
@ParameterizedTest
34+
@CsvSource({
35+
"0, false, 1, Lorem, 43101, 1+A1, true",
36+
"1, true, 2, ipsum, 43102, 1+A2, false",
37+
"2, true, 3, dolor, 43103, 1+A3, true",
38+
"3, false, 4, sit, 43104, 1+A4, false",
39+
"4, false, 5, amet, 43105, 1+A5, true",
40+
"5, false, 6, consectetur, 43106, 1+A6, false",
41+
"6, true, 7, adipiscing, 43107, 1+A7, true",
42+
"7, true, 8, elit, 43108, 1+A8, false",
43+
"8, true, 9, Ut, 43109, 1+A9, true",
44+
"9, false, 10, nec, 43110, 1+A10, false"
45+
})
46+
public void shouldGetVisibleAndHiddenRows(int index, boolean isHidden, String cellIndex0, String cellIndex1, int cellIndex2, String cellIndex3,
47+
boolean cellIndex4) throws IOException {
48+
try (InputStream inputStream = open("/xlsx/simple-with-hidden-rows.xlsx");
49+
ReadableWorkbook excel = new ReadableWorkbook(inputStream)) {
50+
Sheet firstSheet = excel.getFirstSheet();
51+
52+
Row row = firstSheet.read().get(index);
53+
54+
assertThat(row.isHidden()).isEqualTo(isHidden);
55+
assertThat(row.getCell(0)).satisfies(cell -> {
56+
assertThat(cell.getType()).isEqualTo(CellType.NUMBER);
57+
assertThat(cell.asNumber()).isEqualTo(new BigDecimal(cellIndex0));
58+
});
59+
assertThat(row.getCell(1)).satisfies(cell -> {
60+
assertThat(cell.getType()).isEqualTo(CellType.STRING);
61+
assertThat(cell.asString()).isEqualTo(cellIndex1);
62+
});
63+
assertThat(row.getCell(2)).satisfies(cell -> {
64+
assertThat(cell.getType()).isEqualTo(CellType.NUMBER);
65+
assertThat(cell.asNumber()).isEqualTo(new BigDecimal(cellIndex2));
66+
});
67+
assertThat(row.getCell(3)).satisfies(cell -> {
68+
assertThat(cell.getType()).isEqualTo(CellType.FORMULA);
69+
assertThat(cell.getFormula()).isEqualTo(cellIndex3);
70+
});
71+
assertThat(row.getCell(4)).satisfies(cell -> {
72+
assertThat(cell.getType()).isEqualTo(CellType.BOOLEAN);
73+
assertThat(cell.asBoolean()).isEqualTo(cellIndex4);
74+
});
75+
}
76+
}
2877
}
Binary file not shown.

0 commit comments

Comments
 (0)