Skip to content

Commit a49bb9a

Browse files
authored
Merge pull request #48 from TraxTechnologies/master
Implemented the Row.getZeroHeight and Sheet.IsColumnHidden methods
2 parents 5a56ea9 + e558e5d commit a49bb9a

File tree

5 files changed

+76
-20
lines changed

5 files changed

+76
-20
lines changed

src/main/java/com/monitorjbl/xlsx/impl/StreamingRow.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
public class StreamingRow implements Row {
1414
private int rowIndex;
15+
private boolean isHidden;
1516
private Map<Integer, Cell> cellMap = new TreeMap<>();
1617

17-
public StreamingRow(int rowIndex) {
18+
public StreamingRow(int rowIndex, boolean isHidden) {
1819
this.rowIndex = rowIndex;
20+
this.isHidden = isHidden;
1921
}
2022

2123
public Map<Integer, Cell> getCellMap() {
@@ -77,6 +79,16 @@ public short getLastCellNum() {
7779
return (short) (cellMap.size() == 0 ? -1 : cellMap.size() + 1);
7880
}
7981

82+
/**
83+
* Get whether or not to display this row with 0 height
84+
*
85+
* @return - zHeight height is zero or not.
86+
*/
87+
@Override
88+
public boolean getZeroHeight() {
89+
return isHidden;
90+
}
91+
8092
/* Not supported */
8193

8294
/**
@@ -151,14 +163,6 @@ public void setZeroHeight(boolean zHeight) {
151163
throw new NotSupportedException();
152164
}
153165

154-
/**
155-
* Not supported
156-
*/
157-
@Override
158-
public boolean getZeroHeight() {
159-
throw new NotSupportedException();
160-
}
161-
162166
/**
163167
* Not supported
164168
*/

src/main/java/com/monitorjbl/xlsx/impl/StreamingSheet.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ public String getSheetName() {
6161
return name;
6262
}
6363

64+
/**
65+
* Get the hidden state for a given column
66+
*
67+
* @param columnIndex - the column to set (0-based)
68+
* @return hidden - <code>false</code> if the column is visible
69+
*/
70+
@Override
71+
public boolean isColumnHidden(int columnIndex) {
72+
return reader.isColumnHidden(columnIndex);
73+
}
74+
6475
/* Unsupported */
6576

6677
/**
@@ -119,14 +130,6 @@ public void setColumnHidden(int columnIndex, boolean hidden) {
119130
throw new UnsupportedOperationException();
120131
}
121132

122-
/**
123-
* Not supported
124-
*/
125-
@Override
126-
public boolean isColumnHidden(int columnIndex) {
127-
throw new UnsupportedOperationException();
128-
}
129-
130133
/**
131134
* Not supported
132135
*/

src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.monitorjbl.xlsx.impl;
22

33
import com.monitorjbl.xlsx.exceptions.CloseException;
4-
import org.apache.poi.openxml4j.opc.OPCPackage;
54
import org.apache.poi.ss.usermodel.BuiltinFormats;
65
import org.apache.poi.ss.usermodel.DataFormatter;
76
import org.apache.poi.ss.usermodel.Row;
@@ -29,8 +28,10 @@
2928
import java.io.InputStream;
3029
import java.nio.file.Files;
3130
import java.util.ArrayList;
31+
import java.util.HashSet;
3232
import java.util.Iterator;
3333
import java.util.List;
34+
import java.util.Set;
3435

3536
public class StreamingSheetReader implements Iterable<Row> {
3637
private static final Logger log = LoggerFactory.getLogger(StreamingSheetReader.class);
@@ -39,6 +40,7 @@ public class StreamingSheetReader implements Iterable<Row> {
3940
private final StylesTable stylesTable;
4041
private final XMLEventReader parser;
4142
private final DataFormatter dataFormatter = new DataFormatter();
43+
private final Set<Integer> hiddenColumns = new HashSet<>();
4244

4345
private int rowCacheSize;
4446
private List<Row> rowCache = new ArrayList<>();
@@ -89,8 +91,22 @@ private void handleEvent(XMLEvent event) throws SAXException {
8991
String tagLocalName = startElement.getName().getLocalPart();
9092

9193
if("row".equals(tagLocalName)) {
92-
Attribute rowIndex = startElement.getAttributeByName(new QName("r"));
93-
currentRow = new StreamingRow(Integer.parseInt(rowIndex.getValue()) - 1);
94+
Attribute rowNumAttr = startElement.getAttributeByName(new QName("r"));
95+
Attribute isHiddenAttr = startElement.getAttributeByName(new QName("hidden"));
96+
int rowIndex = Integer.parseInt(rowNumAttr.getValue()) - 1;
97+
boolean isHidden = isHiddenAttr != null && "1".equals(isHiddenAttr.getValue());
98+
currentRow = new StreamingRow(rowIndex, isHidden);
99+
} else if("col".equals(tagLocalName)) {
100+
Attribute isHiddenAttr = startElement.getAttributeByName(new QName("hidden"));
101+
boolean isHidden = isHiddenAttr != null && "1".equals(isHiddenAttr.getValue());
102+
if (isHidden) {
103+
Attribute minAttr = startElement.getAttributeByName(new QName("min"));
104+
Attribute maxAttr = startElement.getAttributeByName(new QName("max"));
105+
int min = Integer.parseInt(minAttr.getValue()) - 1;
106+
int max = Integer.parseInt(maxAttr.getValue()) - 1;
107+
for (int columnIndex = min; columnIndex <= max; columnIndex++)
108+
hiddenColumns.add(columnIndex);
109+
}
94110
} else if("c".equals(tagLocalName)) {
95111
Attribute ref = startElement.getAttributeByName(new QName("r"));
96112

@@ -135,6 +151,19 @@ private void handleEvent(XMLEvent event) throws SAXException {
135151
}
136152
}
137153

154+
/**
155+
* Get the hidden state for a given column
156+
*
157+
* @param columnIndex - the column to set (0-based)
158+
* @return hidden - <code>false</code> if the column is visible
159+
*/
160+
boolean isColumnHidden(int columnIndex) {
161+
if(rowCacheIterator == null) {
162+
getRow();
163+
}
164+
return hiddenColumns.contains(columnIndex);
165+
}
166+
138167
/**
139168
* Read the numeric format string out of the styles table for this cell. Stores
140169
* the result in the Cell.

src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.Locale;
1313

1414
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertFalse;
16+
import static org.junit.Assert.assertTrue;
1517

1618
public class StreamingWorkbookTest {
1719
@BeforeClass
@@ -41,4 +43,22 @@ public void testIterateSheets() throws Exception {
4143
}
4244
}
4345

46+
@Test
47+
public void testHiddenCells() throws Exception {
48+
try(
49+
InputStream is = new FileInputStream(new File("src/test/resources/hidden_cells.xlsx"));
50+
Workbook workbook = StreamingReader.builder().open(is)
51+
) {
52+
assertEquals(1, workbook.getNumberOfSheets());
53+
Sheet sheet = workbook.getSheetAt(0);
54+
55+
assertFalse("Column 0 should not be hidden", sheet.isColumnHidden(0));
56+
assertTrue("Column 1 should be hidden", sheet.isColumnHidden(1));
57+
assertFalse("Column 2 should not be hidden", sheet.isColumnHidden(2));
58+
59+
assertFalse("Row 0 should not be hidden", sheet.rowIterator().next().getZeroHeight());
60+
assertTrue("Row 1 should be hidden", sheet.rowIterator().next().getZeroHeight());
61+
assertFalse("Row 2 should not be hidden", sheet.rowIterator().next().getZeroHeight());
62+
}
63+
}
4464
}

src/test/resources/hidden_cells.xlsx

27.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)