diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index 843f142289..023007d365 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -105,6 +105,7 @@ import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ref3DPtg; import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.ss.usermodel.SheetVisibility; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.Internal; import org.apache.poi.util.LocaleUtil; @@ -715,6 +716,27 @@ public final class InternalWorkbook { public boolean isSheetVeryHidden(int sheetnum) { return getBoundSheetRec(sheetnum).isVeryHidden(); } + + /** + * Gets the hidden flag for a given sheet. + * Note that a sheet could instead be + * set to be very hidden, which is different + * ({@link #isSheetVeryHidden(int)}) + * + * @param sheetnum the sheet number (0 based) + * @return True if sheet is hidden + * @since 3.16 beta 2 + */ + public SheetVisibility getSheetVisibility(int sheetnum) { + final BoundSheetRecord bsr = getBoundSheetRec(sheetnum); + if (bsr.isVeryHidden()) { + return SheetVisibility.VERY_HIDDEN; + } + if (bsr.isHidden()) { + return SheetVisibility.HIDDEN; + } + return SheetVisibility.VISIBLE; + } /** * Hide or unhide a sheet @@ -723,32 +745,20 @@ public final class InternalWorkbook { * @param hidden True to mark the sheet as hidden, false otherwise */ public void setSheetHidden(int sheetnum, boolean hidden) { - getBoundSheetRec(sheetnum).setHidden(hidden); + setSheetHidden(sheetnum, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE); } /** * Hide or unhide a sheet. - * 0 = not hidden - * 1 = hidden - * 2 = very hidden. * - * @param sheetnum The sheet number - * @param hidden 0 for not hidden, 1 for hidden, 2 for very hidden + * @param sheetnum The sheet number + * @param visibility the sheet visibility to set (visible, hidden, very hidden) + * @since 3.16 beta 2 */ - public void setSheetHidden(int sheetnum, int hidden) { + public void setSheetHidden(int sheetnum, SheetVisibility visibility) { BoundSheetRecord bsr = getBoundSheetRec(sheetnum); - boolean h = false; - boolean vh = false; - if(hidden == 0) { - } else if(hidden == 1) { - h = true; - } else if(hidden == 2) { - vh = true; - } else { - throw new IllegalArgumentException("Invalid hidden flag " + hidden + " given, must be 0, 1 or 2"); - } - bsr.setHidden(h); - bsr.setVeryHidden(vh); + bsr.setHidden(visibility == SheetVisibility.HIDDEN); + bsr.setVeryHidden(visibility == SheetVisibility.VERY_HIDDEN); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 55382236e0..08504c3a6d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -100,6 +100,7 @@ import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.SheetVisibility; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.WorkbookUtil; import org.apache.poi.util.Configurator; @@ -188,7 +189,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss */ private MissingCellPolicy missingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK; - private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class); + private static final POILogger log = POILogFactory.getLogger(HSSFWorkbook.class); /** * The locator of user-defined functions. @@ -748,19 +749,46 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss validateSheetIndex(sheetIx); return workbook.isSheetVeryHidden(sheetIx); } - - + @Override - public void setSheetHidden(int sheetIx, boolean hidden) { - validateSheetIndex(sheetIx); - workbook.setSheetHidden(sheetIx, hidden); + public SheetVisibility getSheetVisibility(int sheetIx) { + return workbook.getSheetVisibility(sheetIx); } + @Override + public void setSheetHidden(int sheetIx, boolean hidden) { + setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE); + } + + @Removal(version="3.18") + @Deprecated @Override public void setSheetHidden(int sheetIx, int hidden) { + switch (hidden) { + case Workbook.SHEET_STATE_VISIBLE: + setSheetVisibility(sheetIx, SheetVisibility.VISIBLE); + break; + case Workbook.SHEET_STATE_HIDDEN: + setSheetVisibility(sheetIx, SheetVisibility.HIDDEN); + break; + case Workbook.SHEET_STATE_VERY_HIDDEN: + setSheetVisibility(sheetIx, SheetVisibility.VERY_HIDDEN); + break; + default: + throw new IllegalArgumentException("Invalid sheet state : " + hidden + "\n" + + "Sheet state must beone of the Workbook.SHEET_STATE_* constants"); + } + } + + @Override + public void setSheetVisibility(int sheetIx, SheetVisibility visibility) { validateSheetIndex(sheetIx); - WorkbookUtil.validateSheetState(hidden); - workbook.setSheetHidden(sheetIx, hidden); + + /*if (visibility != SheetVisibility.VISIBLE && sheetIx == getActiveSheetIndex()) { + throw new IllegalStateException("Cannot hide the active sheet. Change active sheet before hiding."); + }*/ + + workbook.setSheetHidden(sheetIx, visibility); } /** Returns the index of the sheet by his name diff --git a/src/java/org/apache/poi/ss/usermodel/SheetVisibility.java b/src/java/org/apache/poi/ss/usermodel/SheetVisibility.java new file mode 100644 index 0000000000..68123dd9f7 --- /dev/null +++ b/src/java/org/apache/poi/ss/usermodel/SheetVisibility.java @@ -0,0 +1,46 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel; + +/** + * Specifies sheet visibility + * + * @see Workbook#getSheetVisibility(int) + * @see Workbook#setSheetVisibility(int, SheetVisibility) + */ +public enum SheetVisibility { + + /** + * Indicates the sheet is visible. + */ + VISIBLE, + /** + * Indicates the book window is hidden, but can be shown by the user via the user interface. + */ + HIDDEN, + + /** + * Indicates the sheet is hidden and cannot be shown in the user interface (UI). + * + *

+ * In Excel this state is only available programmatically in VBA: + * ThisWorkbook.Sheets("MySheetName").Visible = xlSheetVeryHidden + *

+ */ + VERY_HIDDEN; +} diff --git a/src/java/org/apache/poi/ss/usermodel/Workbook.java b/src/java/org/apache/poi/ss/usermodel/Workbook.java index 5273423de0..49082536f0 100644 --- a/src/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/java/org/apache/poi/ss/usermodel/Workbook.java @@ -26,6 +26,7 @@ import java.util.List; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; +import org.apache.poi.util.Removal; /** * High level representation of a Excel workbook. This is the first object most users @@ -57,14 +58,20 @@ public interface Workbook extends Closeable, Iterable { * Indicates the sheet is visible. * * @see #setSheetHidden(int, int) + * @deprecated POI 3.16 beta 2. Use {@link SheetVisibility#VISIBLE} instead. */ + @Deprecated + @Removal(version="3.18") int SHEET_STATE_VISIBLE = 0; /** * Indicates the book window is hidden, but can be shown by the user via the user interface. * * @see #setSheetHidden(int, int) + * @deprecated POI 3.16 beta 2. Use {@link SheetVisibility#HIDDEN} instead. */ + @Deprecated + @Removal(version="3.18") int SHEET_STATE_HIDDEN = 1; /** @@ -76,7 +83,10 @@ public interface Workbook extends Closeable, Iterable { *

* * @see #setSheetHidden(int, int) + * @deprecated POI 3.16 beta 2. Use {@link SheetVisibility#VERY_HIDDEN} instead. */ + @Deprecated + @Removal(version="3.18") int SHEET_STATE_VERY_HIDDEN = 2; /** @@ -550,6 +560,7 @@ public interface Workbook extends Closeable, Iterable { *

* @param sheetIx Number * @return true if sheet is hidden + * @see #getSheetVisibility(int) */ boolean isSheetHidden(int sheetIx); @@ -561,6 +572,7 @@ public interface Workbook extends Closeable, Iterable { *

* @param sheetIx sheet index to check * @return true if sheet is very hidden + * @see #getSheetVisibility(int) */ boolean isSheetVeryHidden(int sheetIx); @@ -572,6 +584,7 @@ public interface Workbook extends Closeable, Iterable { * * @param sheetIx the sheet index (0-based) * @param hidden True to mark the sheet as hidden, false otherwise + * @see #setSheetVisibility(int, SheetVisibility) */ void setSheetHidden(int sheetIx, boolean hidden); @@ -593,8 +606,31 @@ public interface Workbook extends Closeable, Iterable { * Workbook.SHEET_STATE_HIDDEN, or * Workbook.SHEET_STATE_VERY_HIDDEN. * @throws IllegalArgumentException if the supplied sheet index or state is invalid + * @deprecated POI 3.16 beta 2. Use {@link #setSheetVisibility(int, SheetVisibility)} instead. */ + @Removal(version="3.18") void setSheetHidden(int sheetIx, int hidden); + + /** + * Get the visibility (visible, hidden, very hidden) of a sheet in this workbook + * + * @param sheetIx the index of the sheet + * @return the sheet visibility + * @since POI 3.16 beta 2 + */ + SheetVisibility getSheetVisibility(int sheetIx); + + /** + * Hide or unhide a sheet. + * + * Please note that the sheet currently set as active sheet (sheet 0 in a newly + * created workbook or the one set via setActiveSheet()) cannot be hidden. + * + * @param sheetIx the sheet index (0-based) + * @param visibility the sheet visibility to set + * @since POI 3.16 beta 2 + */ + void setSheetVisibility(int sheetIx, SheetVisibility visibility); /** * Register a new toolpack in this workbook. diff --git a/src/java/org/apache/poi/ss/util/WorkbookUtil.java b/src/java/org/apache/poi/ss/util/WorkbookUtil.java index 684434823c..e14b8e0c58 100644 --- a/src/java/org/apache/poi/ss/util/WorkbookUtil.java +++ b/src/java/org/apache/poi/ss/util/WorkbookUtil.java @@ -17,7 +17,10 @@ package org.apache.poi.ss.util; +import org.apache.poi.ss.usermodel.SheetVisibility; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.Internal; +import org.apache.poi.util.Removal; /** @@ -169,14 +172,46 @@ public class WorkbookUtil { * {@link Workbook#SHEET_STATE_VISIBLE}, * {@link Workbook#SHEET_STATE_HIDDEN} or * {@link Workbook#SHEET_STATE_VERY_HIDDEN} + * @deprecated POI 3.16 beta 2. Use {@link org.apache.poi.ss.usermodel.SheetVisibility} instead. */ + @Removal(version="3.18") + @Deprecated public static void validateSheetState(int state) { switch(state){ case Workbook.SHEET_STATE_VISIBLE: break; case Workbook.SHEET_STATE_HIDDEN: break; case Workbook.SHEET_STATE_VERY_HIDDEN: break; - default: throw new IllegalArgumentException("Ivalid sheet state : " + state + "\n" + + default: throw new IllegalArgumentException("Invalid sheet state : " + state + "\n" + "Sheet state must beone of the Workbook.SHEET_STATE_* constants"); } - } + } + + @Internal(since="3.16 beta 2") + public static int getNextActiveSheetDueToSheetHiding(Workbook wb, int sheetIx) { + if (sheetIx == wb.getActiveSheetIndex()) { + // activate next sheet + // if last sheet in workbook, the previous visible sheet should be activated + final int count = wb.getNumberOfSheets(); + for (int i=sheetIx+1; i < count; i++) { + // get the next visible sheet in this workbook + if (SheetVisibility.VISIBLE == wb.getSheetVisibility(i)) { + return i; + } + } + + // if there are no sheets to the right or all sheets to the right are hidden, activate a sheet to the left + for (int i=sheetIx-1; i < count; i--) { + if (SheetVisibility.VISIBLE == wb.getSheetVisibility(i)) { + return i; + } + } + + // there are no other visible sheets in this workbook + return -1; + //throw new IllegalStateException("Cannot hide sheet " + sheetIx + ". Workbook must contain at least 1 other visible sheet."); + } + else { + return sheetIx; + } + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index 611f16bc0c..026a45be7a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.SheetVisibility; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; @@ -1239,94 +1240,56 @@ public class SXSSFWorkbook implements Workbook { return _wb.isDate1904(); } - /** - * @return false if this workbook is not visible in the GUI - */ @Override + @NotImplemented("XSSFWorkbook#isHidden is not implemented") public boolean isHidden() { return _wb.isHidden(); } - /** - * @param hiddenFlag pass false to make the workbook visible in the GUI - */ @Override + @NotImplemented("XSSFWorkbook#setHidden is not implemented") public void setHidden(boolean hiddenFlag) { _wb.setHidden(hiddenFlag); } - /** - * Check whether a sheet is hidden. - *

- * Note that a sheet could instead be set to be very hidden, which is different - * ({@link #isSheetVeryHidden(int)}) - *

- * @param sheetIx Number - * @return true if sheet is hidden - */ @Override public boolean isSheetHidden(int sheetIx) { return _wb.isSheetHidden(sheetIx); } - /** - * Check whether a sheet is very hidden. - *

- * This is different from the normal hidden status - * ({@link #isSheetHidden(int)}) - *

- * @param sheetIx sheet index to check - * @return true if sheet is very hidden - */ @Override public boolean isSheetVeryHidden(int sheetIx) { return _wb.isSheetVeryHidden(sheetIx); } + + @Override + public SheetVisibility getSheetVisibility(int sheetIx) { + return _wb.getSheetVisibility(sheetIx); + } - /** - * Hide or unhide a sheet - * - * Please note that the sheet currently set as active sheet (sheet 0 in a newly - * created workbook or the one set via setActiveSheet()) cannot be hidden. - * - * @param sheetIx the sheet index (0-based) - * @param hidden True to mark the sheet as hidden, false otherwise - */ @Override public void setSheetHidden(int sheetIx, boolean hidden) { _wb.setSheetHidden(sheetIx,hidden); } - /** - * Hide or unhide a sheet. - * - *
    - *
  • 0 - visible.
  • - *
  • 1 - hidden.
  • - *
  • 2 - very hidden.
  • - *
- * - * Please note that the sheet currently set as active sheet (sheet 0 in a newly - * created workbook or the one set via setActiveSheet()) cannot be hidden. - * - * @param sheetIx the sheet index (0-based) - * @param hidden one of the following Workbook constants: - * Workbook.SHEET_STATE_VISIBLE, - * Workbook.SHEET_STATE_HIDDEN, or - * Workbook.SHEET_STATE_VERY_HIDDEN. - * @throws IllegalArgumentException if the supplied sheet index or state is invalid - */ + @Removal(version="3.18") + @Deprecated @Override public void setSheetHidden(int sheetIx, int hidden) { _wb.setSheetHidden(sheetIx,hidden); } + @Override + public void setSheetVisibility(int sheetIx, SheetVisibility visibility) { + _wb.setSheetVisibility(sheetIx, visibility); + } + /** * Not implemented for SXSSFWorkbook * diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 6aa2064c38..a64681222f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -37,12 +37,14 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.NoSuchElementException; +import java.util.logging.Logger; import java.util.regex.Pattern; import javax.xml.namespace.QName; import org.apache.commons.collections4.ListValuedMap; import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; +import org.apache.commons.logging.Log; import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLException; @@ -71,6 +73,7 @@ import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.SheetVisibility; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.WorkbookUtil; @@ -81,6 +84,7 @@ import org.apache.poi.util.NotImplemented; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.apache.poi.util.PackageHelper; +import org.apache.poi.util.Removal; import org.apache.poi.xssf.XLSBUnsupportedException; import org.apache.poi.xssf.model.CalculationChain; import org.apache.poi.xssf.model.ExternalLinksTable; @@ -1940,15 +1944,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { throw new RuntimeException("Not implemented yet"); } - /** - * Check whether a sheet is hidden. - *

- * Note that a sheet could instead be set to be very hidden, which is different - * ({@link #isSheetVeryHidden(int)}) - *

- * @param sheetIx Number - * @return true if sheet is hidden - */ @Override public boolean isSheetHidden(int sheetIx) { validateSheetIndex(sheetIx); @@ -1956,70 +1951,69 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return ctSheet.getState() == STSheetState.HIDDEN; } - /** - * Check whether a sheet is very hidden. - *

- * This is different from the normal hidden status - * ({@link #isSheetHidden(int)}) - *

- * @param sheetIx sheet index to check - * @return true if sheet is very hidden - */ @Override public boolean isSheetVeryHidden(int sheetIx) { validateSheetIndex(sheetIx); CTSheet ctSheet = sheets.get(sheetIx).sheet; return ctSheet.getState() == STSheetState.VERY_HIDDEN; } + + @Override + public SheetVisibility getSheetVisibility(int sheetIx) { + validateSheetIndex(sheetIx); + final CTSheet ctSheet = sheets.get(sheetIx).sheet; + final STSheetState.Enum state = ctSheet.getState(); + if (state == STSheetState.VISIBLE) { + return SheetVisibility.VISIBLE; + } + if (state == STSheetState.HIDDEN) { + return SheetVisibility.HIDDEN; + } + if (state == STSheetState.VERY_HIDDEN) { + return SheetVisibility.VERY_HIDDEN; + } + throw new IllegalArgumentException("This should never happen"); + } - /** - * Sets the visible state of this sheet. - *

- * Calling setSheetHidden(sheetIndex, true) is equivalent to - * setSheetHidden(sheetIndex, Workbook.SHEET_STATE_HIDDEN). - *
- * Calling setSheetHidden(sheetIndex, false) is equivalent to - * setSheetHidden(sheetIndex, Workbook.SHEET_STATE_VISIBLE). - *

- * - * Please note that the sheet currently set as active sheet (sheet 0 in a newly - * created workbook or the one set via setActiveSheet()) cannot be hidden. - * - * @param sheetIx the 0-based index of the sheet - * @param hidden whether this sheet is hidden - * @see #setSheetHidden(int, int) - */ @Override public void setSheetHidden(int sheetIx, boolean hidden) { - setSheetHidden(sheetIx, hidden ? SHEET_STATE_HIDDEN : SHEET_STATE_VISIBLE); + setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE); } - /** - * Hide or unhide a sheet. - * - *
    - *
  • 0 - visible.
  • - *
  • 1 - hidden.
  • - *
  • 2 - very hidden.
  • - *
- * - * Please note that the sheet currently set as active sheet (sheet 0 in a newly - * created workbook or the one set via setActiveSheet()) cannot be hidden. - * - * @param sheetIx the sheet index (0-based) - * @param state one of the following Workbook constants: - * Workbook.SHEET_STATE_VISIBLE, - * Workbook.SHEET_STATE_HIDDEN, or - * Workbook.SHEET_STATE_VERY_HIDDEN. - * @throws IllegalArgumentException if the supplied sheet index or state is invalid - */ + @Deprecated + @Removal(version="3.18") @Override public void setSheetHidden(int sheetIx, int state) { - validateSheetIndex(sheetIx); WorkbookUtil.validateSheetState(state); - CTSheet ctSheet = sheets.get(sheetIx).sheet; - ctSheet.setState(STSheetState.Enum.forInt(state + 1)); + setSheetVisibility(sheetIx, SheetVisibility.values()[state]); } + + @Override + public void setSheetVisibility(int sheetIx, SheetVisibility visibility) { + validateSheetIndex(sheetIx); + + /*if (visibility != SheetVisibility.VISIBLE && sheetIx == getActiveSheetIndex()) { + throw new IllegalStateException("Cannot hide the active sheet. Change active sheet before hiding."); + }*/ + + final CTSheet ctSheet = sheets.get(sheetIx).sheet; + switch (visibility) { + case VISIBLE: + ctSheet.setState(STSheetState.VISIBLE); + break; + case HIDDEN: + ctSheet.setState(STSheetState.HIDDEN); + break; + case VERY_HIDDEN: + ctSheet.setState(STSheetState.VERY_HIDDEN); + break; + default: + throw new IllegalArgumentException("This should never happen"); + } + } + + + /** * Fired when a formula is deleted from this workbook, diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java index 6ce1e13d42..5a35b3b4b5 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java @@ -25,8 +25,10 @@ import static org.junit.Assert.fail; import java.io.IOException; import org.apache.poi.ss.ITestDataProvider; +import org.apache.poi.util.Removal; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; public abstract class BaseTestSheetHiding { @@ -59,8 +61,15 @@ public abstract class BaseTestSheetHiding { wbU.close(); } + /** + * @deprecated 3.16 beta 2. Use {@link #testSheetVisibility()} instead. + * + * @throws IOException + */ + @Removal(version="3.18") + @Deprecated @Test - public final void testSheetHidden() throws IOException { + public final void testSheetHiddenOld() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); wb.createSheet("MySheet"); @@ -94,6 +103,59 @@ public abstract class BaseTestSheetHiding { wb.close(); } + + @Test + public final void testSheetVisibility() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + wb.createSheet("MySheet"); + + assertFalse(wb.isSheetHidden(0)); + assertFalse(wb.isSheetVeryHidden(0)); + assertEquals(SheetVisibility.VISIBLE, wb.getSheetVisibility(0)); + + wb.setSheetVisibility(0, SheetVisibility.HIDDEN); + assertTrue(wb.isSheetHidden(0)); + assertFalse(wb.isSheetVeryHidden(0)); + assertEquals(SheetVisibility.HIDDEN, wb.getSheetVisibility(0)); + + wb.setSheetVisibility(0, SheetVisibility.VERY_HIDDEN); + assertFalse(wb.isSheetHidden(0)); + assertTrue(wb.isSheetVeryHidden(0)); + assertEquals(SheetVisibility.VERY_HIDDEN, wb.getSheetVisibility(0)); + + wb.setSheetVisibility(0, SheetVisibility.VISIBLE); + assertFalse(wb.isSheetHidden(0)); + assertFalse(wb.isSheetVeryHidden(0)); + assertEquals(SheetVisibility.VISIBLE, wb.getSheetVisibility(0)); + + wb.close(); + } + + @Ignore + @Test + public void testCannotHideActiveSheet() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + wb.createSheet("Active Sheet"); + wb.createSheet("Inactive Sheet"); + wb.setActiveSheet(0); + assertEquals(0, wb.getActiveSheetIndex()); + + try { + wb.setSheetVisibility(0, SheetVisibility.VERY_HIDDEN); + fail("Should not be able to hide an active sheet"); + } catch (final IllegalStateException e) { + // expected + } + + try { + wb.setSheetVisibility(0, SheetVisibility.HIDDEN); + fail("Should not be able to hide an active sheet"); + } catch (final IllegalStateException e) { + // expected + } + + wb.close(); + } /** * Test that we get the right number of sheets,