diff --git a/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java b/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java index 96cb2a29..b1fca444 100644 --- a/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java +++ b/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java @@ -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")); diff --git a/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java b/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java index 1bcad3e1..3a53f5c3 100644 --- a/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java +++ b/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java @@ -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 */ /** @@ -38,7 +47,7 @@ public Iterator iterator() { */ @Override public String getSheetName(int sheet) { - return reader.findSheetNameByIndex(sheet); + return reader.getSheetProperties().get(sheet).get("name"); } /** @@ -46,7 +55,7 @@ public String getSheetName(int sheet) { */ @Override public int getSheetIndex(String name) { - return reader.findSheetByName(name); + return findSheetByName(name); } /** @@ -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"); } @@ -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} */ @@ -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 */ diff --git a/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbookReader.java b/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbookReader.java index e541ac6e..74571f90 100644 --- a/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbookReader.java +++ b/src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbookReader.java @@ -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; @@ -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; @@ -38,7 +41,7 @@ public class StreamingWorkbookReader implements Iterable, AutoCloseable { private static final Logger log = LoggerFactory.getLogger(StreamingWorkbookReader.class); private final List sheets; - private final List sheetNames = new ArrayList<>(); + private final List> sheetProperties = new ArrayList<>(); private final Builder builder; private File tmp; private OPCPackage pkg; @@ -48,7 +51,7 @@ public class StreamingWorkbookReader implements Iterable, 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 */ @@ -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 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 getSheets() { return sheets; } + public List> getSheetProperties() { + return sheetProperties; + } + @Override public Iterator iterator() { return new StreamingSheetIterator(sheets.iterator()); diff --git a/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java b/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java index 842dabe8..c0635c57 100644 --- a/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java +++ b/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java @@ -187,7 +187,7 @@ public void testGaps() throws Exception { obj.add(o); } - assertEquals(2, obj.size()); + assertEquals(3, obj.size()); List row; row = obj.get(0); diff --git a/src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java b/src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java index 80c6ab30..251a0020 100644 --- a/src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java +++ b/src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java @@ -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)); @@ -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( diff --git a/src/test/resources/hidden.xlsx b/src/test/resources/hidden.xlsx new file mode 100644 index 00000000..3a36d7a9 Binary files /dev/null and b/src/test/resources/hidden.xlsx differ diff --git a/src/test/resources/hidden_cells.xlsx b/src/test/resources/hidden_cells.xlsx deleted file mode 100644 index 7c9d6d88..00000000 Binary files a/src/test/resources/hidden_cells.xlsx and /dev/null differ