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:
parent
85575f32f5
commit
15be6fb336
@ -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;
|
||||
@ -716,6 +717,27 @@ public final class InternalWorkbook {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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.
|
||||
@ -749,18 +750,45 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
|
||||
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
|
||||
|
46
src/java/org/apache/poi/ss/usermodel/SheetVisibility.java
Normal file
46
src/java/org/apache/poi/ss/usermodel/SheetVisibility.java
Normal 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;
|
||||
}
|
@ -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<Sheet> {
|
||||
* 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<Sheet> {
|
||||
* </p>
|
||||
*
|
||||
* @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<Sheet> {
|
||||
* </p>
|
||||
* @param sheetIx Number
|
||||
* @return <code>true</code> if sheet is hidden
|
||||
* @see #getSheetVisibility(int)
|
||||
*/
|
||||
boolean isSheetHidden(int sheetIx);
|
||||
|
||||
@ -561,6 +572,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
|
||||
* </p>
|
||||
* @param sheetIx sheet index to check
|
||||
* @return <code>true</code> if sheet is very hidden
|
||||
* @see #getSheetVisibility(int)
|
||||
*/
|
||||
boolean isSheetVeryHidden(int sheetIx);
|
||||
|
||||
@ -572,6 +584,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
|
||||
*
|
||||
* @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,9 +606,32 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
|
||||
* <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
|
||||
* @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.
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 <code>false</code> 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 <code>false</code> 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.
|
||||
* <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
|
||||
public boolean isSheetHidden(int 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
|
||||
public boolean isSheetVeryHidden(int sheetIx)
|
||||
{
|
||||
return _wb.isSheetVeryHidden(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 SheetVisibility getSheetVisibility(int sheetIx) {
|
||||
return _wb.getSheetVisibility(sheetIx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSheetHidden(int sheetIx, boolean hidden)
|
||||
{
|
||||
_wb.setSheetHidden(sheetIx,hidden);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide or unhide a sheet.
|
||||
*
|
||||
* <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
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* <i>Not implemented for SXSSFWorkbook</i>
|
||||
*
|
||||
|
@ -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.
|
||||
* <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
|
||||
public boolean isSheetHidden(int sheetIx) {
|
||||
validateSheetIndex(sheetIx);
|
||||
@ -1956,15 +1951,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||
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
|
||||
public boolean isSheetVeryHidden(int sheetIx) {
|
||||
validateSheetIndex(sheetIx);
|
||||
@ -1972,55 +1958,63 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
||||
return ctSheet.getState() == STSheetState.VERY_HIDDEN;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
public void setSheetHidden(int sheetIx, boolean hidden) {
|
||||
setSheetHidden(sheetIx, hidden ? SHEET_STATE_HIDDEN : SHEET_STATE_VISIBLE);
|
||||
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");
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide or unhide a sheet.
|
||||
*
|
||||
* <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
|
||||
public void setSheetHidden(int sheetIx, boolean hidden) {
|
||||
setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
|
||||
}
|
||||
|
||||
@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,
|
||||
* for example when calling cell.setCellFormula(null)
|
||||
|
@ -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");
|
||||
|
||||
@ -95,6 +104,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,
|
||||
* with the right text on them, no matter what
|
||||
|
Loading…
Reference in New Issue
Block a user