bug 60605: convert Workbook.SHEET_STATE_* to SheetVisibility enum

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1779558 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-01-20 04:23:42 +00:00
parent 85575f32f5
commit 15be6fb336
8 changed files with 314 additions and 140 deletions

View File

@ -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.ptg.Ref3DPtg;
import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.BuiltinFormats; 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.ss.usermodel.Workbook;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
@ -715,6 +716,27 @@ public final class InternalWorkbook {
public boolean isSheetVeryHidden(int sheetnum) { public boolean isSheetVeryHidden(int sheetnum) {
return getBoundSheetRec(sheetnum).isVeryHidden(); 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 * Hide or unhide a sheet
@ -723,32 +745,20 @@ public final class InternalWorkbook {
* @param hidden True to mark the sheet as hidden, false otherwise * @param hidden True to mark the sheet as hidden, false otherwise
*/ */
public void setSheetHidden(int sheetnum, boolean hidden) { public void setSheetHidden(int sheetnum, boolean hidden) {
getBoundSheetRec(sheetnum).setHidden(hidden); setSheetHidden(sheetnum, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
} }
/** /**
* Hide or unhide a sheet. * Hide or unhide a sheet.
* 0 = not hidden
* 1 = hidden
* 2 = very hidden.
* *
* @param sheetnum The sheet number * @param sheetnum The sheet number
* @param hidden 0 for not hidden, 1 for hidden, 2 for very hidden * @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); BoundSheetRecord bsr = getBoundSheetRec(sheetnum);
boolean h = false; bsr.setHidden(visibility == SheetVisibility.HIDDEN);
boolean vh = false; bsr.setVeryHidden(visibility == SheetVisibility.VERY_HIDDEN);
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);
} }

View File

@ -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.Name;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet; 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.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil; import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.Configurator; 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 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. * The locator of user-defined functions.
@ -748,19 +749,46 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
validateSheetIndex(sheetIx); validateSheetIndex(sheetIx);
return workbook.isSheetVeryHidden(sheetIx); return workbook.isSheetVeryHidden(sheetIx);
} }
@Override @Override
public void setSheetHidden(int sheetIx, boolean hidden) { public SheetVisibility getSheetVisibility(int sheetIx) {
validateSheetIndex(sheetIx); return workbook.getSheetVisibility(sheetIx);
workbook.setSheetHidden(sheetIx, hidden);
} }
@Override
public void setSheetHidden(int sheetIx, boolean hidden) {
setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
}
@Removal(version="3.18")
@Deprecated
@Override @Override
public void setSheetHidden(int sheetIx, int hidden) { 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); 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 /** Returns the index of the sheet by his name

View File

@ -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).
*
* <p>
* In Excel this state is only available programmatically in VBA:
* <code>ThisWorkbook.Sheets("MySheetName").Visible = xlSheetVeryHidden </code>
* </p>
*/
VERY_HIDDEN;
}

View File

@ -26,6 +26,7 @@ import java.util.List;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; 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 * High level representation of a Excel workbook. This is the first object most users
@ -57,14 +58,20 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* Indicates the sheet is visible. * Indicates the sheet is visible.
* *
* @see #setSheetHidden(int, int) * @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; int SHEET_STATE_VISIBLE = 0;
/** /**
* Indicates the book window is hidden, but can be shown by the user via the user interface. * Indicates the book window is hidden, but can be shown by the user via the user interface.
* *
* @see #setSheetHidden(int, int) * @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; int SHEET_STATE_HIDDEN = 1;
/** /**
@ -76,7 +83,10 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* </p> * </p>
* *
* @see #setSheetHidden(int, int) * @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; int SHEET_STATE_VERY_HIDDEN = 2;
/** /**
@ -550,6 +560,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* </p> * </p>
* @param sheetIx Number * @param sheetIx Number
* @return <code>true</code> if sheet is hidden * @return <code>true</code> if sheet is hidden
* @see #getSheetVisibility(int)
*/ */
boolean isSheetHidden(int sheetIx); boolean isSheetHidden(int sheetIx);
@ -561,6 +572,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* </p> * </p>
* @param sheetIx sheet index to check * @param sheetIx sheet index to check
* @return <code>true</code> if sheet is very hidden * @return <code>true</code> if sheet is very hidden
* @see #getSheetVisibility(int)
*/ */
boolean isSheetVeryHidden(int sheetIx); boolean isSheetVeryHidden(int sheetIx);
@ -572,6 +584,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* *
* @param sheetIx the sheet index (0-based) * @param sheetIx the sheet index (0-based)
* @param hidden True to mark the sheet as hidden, false otherwise * @param hidden True to mark the sheet as hidden, false otherwise
* @see #setSheetVisibility(int, SheetVisibility)
*/ */
void setSheetHidden(int sheetIx, boolean hidden); void setSheetHidden(int sheetIx, boolean hidden);
@ -593,8 +606,31 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* <code>Workbook.SHEET_STATE_HIDDEN</code>, or * <code>Workbook.SHEET_STATE_HIDDEN</code>, or
* <code>Workbook.SHEET_STATE_VERY_HIDDEN</code>. * <code>Workbook.SHEET_STATE_VERY_HIDDEN</code>.
* @throws IllegalArgumentException if the supplied sheet index or state is invalid * @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); 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. * Register a new toolpack in this workbook.

View File

@ -17,7 +17,10 @@
package org.apache.poi.ss.util; package org.apache.poi.ss.util;
import org.apache.poi.ss.usermodel.SheetVisibility;
import org.apache.poi.ss.usermodel.Workbook; 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_VISIBLE},
* {@link Workbook#SHEET_STATE_HIDDEN} or * {@link Workbook#SHEET_STATE_HIDDEN} or
* {@link Workbook#SHEET_STATE_VERY_HIDDEN} * {@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) { public static void validateSheetState(int state) {
switch(state){ switch(state){
case Workbook.SHEET_STATE_VISIBLE: break; case Workbook.SHEET_STATE_VISIBLE: break;
case Workbook.SHEET_STATE_HIDDEN: break; case Workbook.SHEET_STATE_HIDDEN: break;
case Workbook.SHEET_STATE_VERY_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"); "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;
}
}
} }

View File

@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet; 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.usermodel.Workbook;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
@ -1239,94 +1240,56 @@ public class SXSSFWorkbook implements Workbook {
return _wb.isDate1904(); return _wb.isDate1904();
} }
/**
* @return <code>false</code> if this workbook is not visible in the GUI
*/
@Override @Override
@NotImplemented("XSSFWorkbook#isHidden is not implemented")
public boolean isHidden() public boolean isHidden()
{ {
return _wb.isHidden(); return _wb.isHidden();
} }
/**
* @param hiddenFlag pass <code>false</code> to make the workbook visible in the GUI
*/
@Override @Override
@NotImplemented("XSSFWorkbook#setHidden is not implemented")
public void setHidden(boolean hiddenFlag) public void setHidden(boolean hiddenFlag)
{ {
_wb.setHidden(hiddenFlag); _wb.setHidden(hiddenFlag);
} }
/**
* Check whether a sheet is hidden.
* <p>
* Note that a sheet could instead be set to be very hidden, which is different
* ({@link #isSheetVeryHidden(int)})
* </p>
* @param sheetIx Number
* @return <code>true</code> if sheet is hidden
*/
@Override @Override
public boolean isSheetHidden(int sheetIx) public boolean isSheetHidden(int sheetIx)
{ {
return _wb.isSheetHidden(sheetIx); return _wb.isSheetHidden(sheetIx);
} }
/**
* Check whether a sheet is very hidden.
* <p>
* This is different from the normal hidden status
* ({@link #isSheetHidden(int)})
* </p>
* @param sheetIx sheet index to check
* @return <code>true</code> if sheet is very hidden
*/
@Override @Override
public boolean isSheetVeryHidden(int sheetIx) public boolean isSheetVeryHidden(int sheetIx)
{ {
return _wb.isSheetVeryHidden(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 @Override
public void setSheetHidden(int sheetIx, boolean hidden) public void setSheetHidden(int sheetIx, boolean hidden)
{ {
_wb.setSheetHidden(sheetIx,hidden); _wb.setSheetHidden(sheetIx,hidden);
} }
/** @Removal(version="3.18")
* Hide or unhide a sheet. @Deprecated
*
* <ul>
* <li>0 - visible. </li>
* <li>1 - hidden. </li>
* <li>2 - very hidden.</li>
* </ul>
*
* 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 <code>Workbook</code> constants:
* <code>Workbook.SHEET_STATE_VISIBLE</code>,
* <code>Workbook.SHEET_STATE_HIDDEN</code>, or
* <code>Workbook.SHEET_STATE_VERY_HIDDEN</code>.
* @throws IllegalArgumentException if the supplied sheet index or state is invalid
*/
@Override @Override
public void setSheetHidden(int sheetIx, int hidden) public void setSheetHidden(int sheetIx, int hidden)
{ {
_wb.setSheetHidden(sheetIx,hidden); _wb.setSheetHidden(sheetIx,hidden);
} }
@Override
public void setSheetVisibility(int sheetIx, SheetVisibility visibility) {
_wb.setSheetVisibility(sheetIx, visibility);
}
/** /**
* <i>Not implemented for SXSSFWorkbook</i> * <i>Not implemented for SXSSFWorkbook</i>
* *

View File

@ -37,12 +37,14 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import org.apache.commons.collections4.ListValuedMap; import org.apache.commons.collections4.ListValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.apache.commons.logging.Log;
import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException; 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;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet; 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.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.WorkbookUtil; 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.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.apache.poi.util.PackageHelper; import org.apache.poi.util.PackageHelper;
import org.apache.poi.util.Removal;
import org.apache.poi.xssf.XLSBUnsupportedException; import org.apache.poi.xssf.XLSBUnsupportedException;
import org.apache.poi.xssf.model.CalculationChain; import org.apache.poi.xssf.model.CalculationChain;
import org.apache.poi.xssf.model.ExternalLinksTable; import org.apache.poi.xssf.model.ExternalLinksTable;
@ -1940,15 +1944,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
throw new RuntimeException("Not implemented yet"); throw new RuntimeException("Not implemented yet");
} }
/**
* Check whether a sheet is hidden.
* <p>
* Note that a sheet could instead be set to be very hidden, which is different
* ({@link #isSheetVeryHidden(int)})
* </p>
* @param sheetIx Number
* @return <code>true</code> if sheet is hidden
*/
@Override @Override
public boolean isSheetHidden(int sheetIx) { public boolean isSheetHidden(int sheetIx) {
validateSheetIndex(sheetIx); validateSheetIndex(sheetIx);
@ -1956,70 +1951,69 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return ctSheet.getState() == STSheetState.HIDDEN; return ctSheet.getState() == STSheetState.HIDDEN;
} }
/**
* Check whether a sheet is very hidden.
* <p>
* This is different from the normal hidden status
* ({@link #isSheetHidden(int)})
* </p>
* @param sheetIx sheet index to check
* @return <code>true</code> if sheet is very hidden
*/
@Override @Override
public boolean isSheetVeryHidden(int sheetIx) { public boolean isSheetVeryHidden(int sheetIx) {
validateSheetIndex(sheetIx); validateSheetIndex(sheetIx);
CTSheet ctSheet = sheets.get(sheetIx).sheet; CTSheet ctSheet = sheets.get(sheetIx).sheet;
return ctSheet.getState() == STSheetState.VERY_HIDDEN; 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.
* <p>
* Calling <code>setSheetHidden(sheetIndex, true)</code> is equivalent to
* <code>setSheetHidden(sheetIndex, Workbook.SHEET_STATE_HIDDEN)</code>.
* <br/>
* Calling <code>setSheetHidden(sheetIndex, false)</code> is equivalent to
* <code>setSheetHidden(sheetIndex, Workbook.SHEET_STATE_VISIBLE)</code>.
* </p>
*
* 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 @Override
public void setSheetHidden(int sheetIx, boolean hidden) { public void setSheetHidden(int sheetIx, boolean hidden) {
setSheetHidden(sheetIx, hidden ? SHEET_STATE_HIDDEN : SHEET_STATE_VISIBLE); setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
} }
/** @Deprecated
* Hide or unhide a sheet. @Removal(version="3.18")
*
* <ul>
* <li>0 - visible. </li>
* <li>1 - hidden. </li>
* <li>2 - very hidden.</li>
* </ul>
*
* 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 <code>Workbook</code> constants:
* <code>Workbook.SHEET_STATE_VISIBLE</code>,
* <code>Workbook.SHEET_STATE_HIDDEN</code>, or
* <code>Workbook.SHEET_STATE_VERY_HIDDEN</code>.
* @throws IllegalArgumentException if the supplied sheet index or state is invalid
*/
@Override @Override
public void setSheetHidden(int sheetIx, int state) { public void setSheetHidden(int sheetIx, int state) {
validateSheetIndex(sheetIx);
WorkbookUtil.validateSheetState(state); WorkbookUtil.validateSheetState(state);
CTSheet ctSheet = sheets.get(sheetIx).sheet; setSheetVisibility(sheetIx, SheetVisibility.values()[state]);
ctSheet.setState(STSheetState.Enum.forInt(state + 1));
} }
@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, * Fired when a formula is deleted from this workbook,

View File

@ -25,8 +25,10 @@ import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.util.Removal;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public abstract class BaseTestSheetHiding { public abstract class BaseTestSheetHiding {
@ -59,8 +61,15 @@ public abstract class BaseTestSheetHiding {
wbU.close(); wbU.close();
} }
/**
* @deprecated 3.16 beta 2. Use {@link #testSheetVisibility()} instead.
*
* @throws IOException
*/
@Removal(version="3.18")
@Deprecated
@Test @Test
public final void testSheetHidden() throws IOException { public final void testSheetHiddenOld() throws IOException {
Workbook wb = _testDataProvider.createWorkbook(); Workbook wb = _testDataProvider.createWorkbook();
wb.createSheet("MySheet"); wb.createSheet("MySheet");
@ -94,6 +103,59 @@ public abstract class BaseTestSheetHiding {
wb.close(); 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, * Test that we get the right number of sheets,