Skip to content

Commit

Permalink
Merge pull request #48 from TraxTechnologies/master
Browse files Browse the repository at this point in the history
Implemented the Row.getZeroHeight and Sheet.IsColumnHidden methods
  • Loading branch information
monitorjbl authored Aug 4, 2016
2 parents 5a56ea9 + e558e5d commit a49bb9a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
22 changes: 13 additions & 9 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

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

public StreamingRow(int rowIndex) {
public StreamingRow(int rowIndex, boolean isHidden) {
this.rowIndex = rowIndex;
this.isHidden = isHidden;
}

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

/**
* Get whether or not to display this row with 0 height
*
* @return - zHeight height is zero or not.
*/
@Override
public boolean getZeroHeight() {
return isHidden;
}

/* Not supported */

/**
Expand Down Expand Up @@ -151,14 +163,6 @@ public void setZeroHeight(boolean zHeight) {
throw new NotSupportedException();
}

/**
* Not supported
*/
@Override
public boolean getZeroHeight() {
throw new NotSupportedException();
}

/**
* Not supported
*/
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public String getSheetName() {
return name;
}

/**
* Get the hidden state for a given column
*
* @param columnIndex - the column to set (0-based)
* @return hidden - <code>false</code> if the column is visible
*/
@Override
public boolean isColumnHidden(int columnIndex) {
return reader.isColumnHidden(columnIndex);
}

/* Unsupported */

/**
Expand Down Expand Up @@ -119,14 +130,6 @@ public void setColumnHidden(int columnIndex, boolean hidden) {
throw new UnsupportedOperationException();
}

/**
* Not supported
*/
@Override
public boolean isColumnHidden(int columnIndex) {
throw new UnsupportedOperationException();
}

/**
* Not supported
*/
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.monitorjbl.xlsx.impl;

import com.monitorjbl.xlsx.exceptions.CloseException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
Expand Down Expand Up @@ -29,8 +28,10 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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

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

if("row".equals(tagLocalName)) {
Attribute rowIndex = startElement.getAttributeByName(new QName("r"));
currentRow = new StreamingRow(Integer.parseInt(rowIndex.getValue()) - 1);
Attribute rowNumAttr = startElement.getAttributeByName(new QName("r"));
Attribute isHiddenAttr = startElement.getAttributeByName(new QName("hidden"));
int rowIndex = Integer.parseInt(rowNumAttr.getValue()) - 1;
boolean isHidden = isHiddenAttr != null && "1".equals(isHiddenAttr.getValue());
currentRow = new StreamingRow(rowIndex, isHidden);
} else if("col".equals(tagLocalName)) {
Attribute isHiddenAttr = startElement.getAttributeByName(new QName("hidden"));
boolean isHidden = isHiddenAttr != null && "1".equals(isHiddenAttr.getValue());
if (isHidden) {
Attribute minAttr = startElement.getAttributeByName(new QName("min"));
Attribute maxAttr = startElement.getAttributeByName(new QName("max"));
int min = Integer.parseInt(minAttr.getValue()) - 1;
int max = Integer.parseInt(maxAttr.getValue()) - 1;
for (int columnIndex = min; columnIndex <= max; columnIndex++)
hiddenColumns.add(columnIndex);
}
} else if("c".equals(tagLocalName)) {
Attribute ref = startElement.getAttributeByName(new QName("r"));

Expand Down Expand Up @@ -135,6 +151,19 @@ private void handleEvent(XMLEvent event) throws SAXException {
}
}

/**
* Get the hidden state for a given column
*
* @param columnIndex - the column to set (0-based)
* @return hidden - <code>false</code> if the column is visible
*/
boolean isColumnHidden(int columnIndex) {
if(rowCacheIterator == null) {
getRow();
}
return hiddenColumns.contains(columnIndex);
}

/**
* Read the numeric format string out of the styles table for this cell. Stores
* the result in the Cell.
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Locale;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class StreamingWorkbookTest {
@BeforeClass
Expand Down Expand Up @@ -41,4 +43,22 @@ public void testIterateSheets() throws Exception {
}
}

@Test
public void testHiddenCells() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/hidden_cells.xlsx"));
Workbook workbook = StreamingReader.builder().open(is)
) {
assertEquals(1, workbook.getNumberOfSheets());
Sheet sheet = workbook.getSheetAt(0);

assertFalse("Column 0 should not be hidden", sheet.isColumnHidden(0));
assertTrue("Column 1 should be hidden", sheet.isColumnHidden(1));
assertFalse("Column 2 should not be hidden", sheet.isColumnHidden(2));

assertFalse("Row 0 should not be hidden", sheet.rowIterator().next().getZeroHeight());
assertTrue("Row 1 should be hidden", sheet.rowIterator().next().getZeroHeight());
assertFalse("Row 2 should not be hidden", sheet.rowIterator().next().getZeroHeight());
}
}
}
Binary file added src/test/resources/hidden_cells.xlsx
Binary file not shown.

0 comments on commit a49bb9a

Please sign in to comment.