Skip to content

Commit

Permalink
Adding support for Workbook.isSheetHidden() and .isSheetVeryHidden() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Jan 17, 2017
1 parent ad652a0 commit 9b381af
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ && isSpreadsheetTag(event.asStartElement().getName())) {
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());
boolean isHidden = isHiddenAttr != null && ("1".equals(isHiddenAttr.getValue()) || "true".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());
boolean isHidden = isHiddenAttr != null && ("1".equals(isHiddenAttr.getValue()) || "true".equals(isHiddenAttr.getValue()));
if(isHidden) {
Attribute minAttr = startElement.getAttributeByName(new QName("min"));
Attribute maxAttr = startElement.getAttributeByName(new QName("max"));
Expand Down
47 changes: 28 additions & 19 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public StreamingWorkbook(StreamingWorkbookReader reader) {
this.reader = reader;
}

int findSheetByName(String name) {
for(int i = 0; i < reader.getSheetProperties().size(); i++) {
if(reader.getSheetProperties().get(i).get("name").equals(name)) {
return i;
}
}
return -1;
}

/* Supported */

/**
Expand All @@ -38,15 +47,15 @@ public Iterator<Sheet> iterator() {
*/
@Override
public String getSheetName(int sheet) {
return reader.findSheetNameByIndex(sheet);
return reader.getSheetProperties().get(sheet).get("name");
}

/**
* {@inheritDoc}
*/
@Override
public int getSheetIndex(String name) {
return reader.findSheetByName(name);
return findSheetByName(name);
}

/**
Expand All @@ -55,7 +64,7 @@ public int getSheetIndex(String name) {
@Override
public int getSheetIndex(Sheet sheet) {
if(sheet instanceof StreamingSheet) {
return reader.findSheetByName(sheet.getSheetName());
return findSheetByName(sheet.getSheetName());
} else {
throw new UnsupportedOperationException("Cannot use non-StreamingSheet sheets");
}
Expand Down Expand Up @@ -85,6 +94,22 @@ public Sheet getSheet(String name) {
return reader.getSheets().get(getSheetIndex(name));
}

/**
* {@inheritDoc}
*/
@Override
public boolean isSheetHidden(int sheetIx) {
return "hidden".equals(reader.getSheetProperties().get(sheetIx).get("state"));
}

/**
* {@inheritDoc}
*/
@Override
public boolean isSheetVeryHidden(int sheetIx) {
return "veryHidden".equals(reader.getSheetProperties().get(sheetIx).get("state"));
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -423,22 +448,6 @@ public void setHidden(boolean hiddenFlag) {
throw new UnsupportedOperationException();
}

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

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

/**
* Not supported
*/
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbookReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.poi.xssf.model.StylesTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.stream.XMLEventReader;
Expand All @@ -27,8 +28,10 @@
import java.nio.file.Files;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.monitorjbl.xlsx.XmlUtils.document;
import static com.monitorjbl.xlsx.XmlUtils.searchForNodeList;
Expand All @@ -38,7 +41,7 @@ public class StreamingWorkbookReader implements Iterable<Sheet>, AutoCloseable {
private static final Logger log = LoggerFactory.getLogger(StreamingWorkbookReader.class);

private final List<StreamingSheet> sheets;
private final List<String> sheetNames = new ArrayList<>();
private final List<Map<String, String>> sheetProperties = new ArrayList<>();
private final Builder builder;
private File tmp;
private OPCPackage pkg;
Expand All @@ -48,7 +51,7 @@ public class StreamingWorkbookReader implements Iterable<Sheet>, AutoCloseable {
* a StreamingWorkbook using its own reader implementation. Do not use
* going forward.
*
* @param pkg The POI package that should be closed when this workbook is closed
* @param pkg The POI package that should be closed when this workbook is closed
* @param reader A single streaming reader instance
* @param builder The builder containing all options
*/
Expand Down Expand Up @@ -118,30 +121,31 @@ void loadSheets(XSSFReader reader, SharedStringsTable sst, StylesTable stylesTab
int i = 0;
while(iter.hasNext()) {
XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(iter.next());
sheets.add(new StreamingSheet(sheetNames.get(i++), new StreamingSheetReader(sst, stylesTable, parser, rowCacheSize)));
sheets.add(new StreamingSheet(sheetProperties.get(i++).get("name"), new StreamingSheetReader(sst, stylesTable, parser, rowCacheSize)));
}
}

void lookupSheetNames(XSSFReader reader) throws IOException, InvalidFormatException {
sheetNames.clear();
sheetProperties.clear();
NodeList nl = searchForNodeList(document(reader.getWorkbookData()), "/workbook/sheets/sheet");
for(int i = 0; i < nl.getLength(); i++) {
sheetNames.add(nl.item(i).getAttributes().getNamedItem("name").getTextContent());
}
}

int findSheetByName(String name) {
return sheetNames.indexOf(name);
}
Map<String, String> props = new HashMap<>();
props.put("name", nl.item(i).getAttributes().getNamedItem("name").getTextContent());

String findSheetNameByIndex(int index) {
return sheetNames.get(index);
Node state = nl.item(i).getAttributes().getNamedItem("state");
props.put("state", state == null ? "visible" : state.getTextContent());
sheetProperties.add(props);
}
}

List<? extends Sheet> getSheets() {
return sheets;
}

public List<Map<String, String>> getSheetProperties() {
return sheetProperties;
}

@Override
public Iterator<Sheet> iterator() {
return new StreamingSheetIterator(sheets.iterator());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void testGaps() throws Exception {
obj.add(o);
}

assertEquals(2, obj.size());
assertEquals(3, obj.size());
List<Cell> row;

row = obj.get(0);
Expand Down
21 changes: 19 additions & 2 deletions src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public void testIterateSheets() throws Exception {
@Test
public void testHiddenCells() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/hidden_cells.xlsx"));
InputStream is = new FileInputStream(new File("src/test/resources/hidden.xlsx"));
Workbook workbook = StreamingReader.builder().open(is)
) {
assertEquals(1, workbook.getNumberOfSheets());
assertEquals(3, workbook.getNumberOfSheets());
Sheet sheet = workbook.getSheetAt(0);

assertFalse("Column 0 should not be hidden", sheet.isColumnHidden(0));
Expand All @@ -66,6 +66,23 @@ public void testHiddenCells() throws Exception {
}
}

@Test
public void testHiddenSheets() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/hidden.xlsx"));
Workbook workbook = StreamingReader.builder().open(is)
) {
assertEquals(3, workbook.getNumberOfSheets());
assertFalse(workbook.isSheetHidden(0));

assertTrue(workbook.isSheetHidden(1));
assertFalse(workbook.isSheetVeryHidden(1));

assertFalse(workbook.isSheetHidden(2));
assertTrue(workbook.isSheetVeryHidden(2));
}
}

@Test
public void testFormulaCells() throws Exception {
try(
Expand Down
Binary file added src/test/resources/hidden.xlsx
Binary file not shown.
Binary file removed src/test/resources/hidden_cells.xlsx
Binary file not shown.

0 comments on commit 9b381af

Please sign in to comment.