aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2008-01-08 11:59:26 +0000
committerNick Burch <nick@apache.org>2008-01-08 11:59:26 +0000
commitf52c41539d3055c46ff61daa91067e66c8b9dd55 (patch)
treea569c7f2e200b4b2cd283c84e16b5a9dd90c4da4 /src
parent7b8ac5e4c63cfaf3bd004789974fa53934c73b82 (diff)
downloadpoi-f52c41539d3055c46ff61daa91067e66c8b9dd55.tar.gz
poi-f52c41539d3055c46ff61daa91067e66c8b9dd55.zip
Patch from Ugo to get and set the sheet hidden flag (bug #43937)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@609942 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/documentation/content/xdocs/changes.xml1
-rw-r--r--src/documentation/content/xdocs/status.xml1
-rw-r--r--src/java/org/apache/poi/hssf/model/Workbook.java23
-rw-r--r--src/java/org/apache/poi/hssf/record/BoundSheetRecord.java10
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java29
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java67
6 files changed, 107 insertions, 24 deletions
diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml
index ccf962dae8..06c9b0c071 100644
--- a/src/documentation/content/xdocs/changes.xml
+++ b/src/documentation/content/xdocs/changes.xml
@@ -36,6 +36,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.0.2-FINAL" date="2008-??-??">
+ <action dev="POI-DEVELOPERS" type="add">43937 - Add support for hiding and un-hiding sheets, and checking their current hidden status</action>
<action dev="POI-DEVELOPERS" type="fix">44167 - Fix for non-contiguous named ranges</action>
<action dev="POI-DEVELOPERS" type="fix">44070 - Fix for shifting comments when shifting rows</action>
</release>
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml
index d3af731337..c7bf2de8d9 100644
--- a/src/documentation/content/xdocs/status.xml
+++ b/src/documentation/content/xdocs/status.xml
@@ -33,6 +33,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.0.2-FINAL" date="2008-??-??">
+ <action dev="POI-DEVELOPERS" type="add">43937 - Add support for hiding and un-hiding sheets, and checking their current hidden status</action>
<action dev="POI-DEVELOPERS" type="fix">44167 - Fix for non-contiguous named ranges</action>
<action dev="POI-DEVELOPERS" type="fix">44070 - Fix for shifting comments when shifting rows</action>
</release>
diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java
index 972aa09015..2af77a7814 100644
--- a/src/java/org/apache/poi/hssf/model/Workbook.java
+++ b/src/java/org/apache/poi/hssf/model/Workbook.java
@@ -543,6 +543,29 @@ public class Workbook implements Model
}
/**
+ * gets the hidden flag for a given sheet.
+ *
+ * @param sheetnum the sheet number (0 based)
+ * @return True if sheet is hidden
+ */
+
+ public boolean isSheetHidden(int sheetnum) {
+ BoundSheetRecord bsr = ( BoundSheetRecord ) boundsheets.get(sheetnum);
+ return bsr.isHidden();
+ }
+
+ /**
+ * Hide or unhide a sheet
+ *
+ * @param sheetnum The sheet number
+ * @param hidden True to mark the sheet as hidden, false otherwise
+ */
+
+ public void setSheetHidden(int sheetnum, boolean hidden) {
+ BoundSheetRecord bsr = ( BoundSheetRecord ) boundsheets.get(sheetnum);
+ bsr.setHidden(hidden);
+ }
+ /**
* get the sheet's index
* @param name sheet name
* @return sheet index or -1 if it was not found.
diff --git a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
index bf264bf02a..78daff2ebb 100644
--- a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
+++ b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
@@ -19,6 +19,7 @@
package org.apache.poi.hssf.record;
+import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;
@@ -36,6 +37,7 @@ import org.apache.poi.util.StringUtil;
public class BoundSheetRecord
extends Record
{
+ private static final short HIDDEN_FLAG_MASK = 0x01;
public final static short sid = 0x85;
private int field_1_position_of_BOF;
private short field_2_option_flags;
@@ -301,4 +303,12 @@ public class BoundSheetRecord
{
return sid;
}
+
+ public boolean isHidden() {
+ return BitFieldFactory.getInstance(HIDDEN_FLAG_MASK).isSet(field_2_option_flags);
+ }
+
+ public void setHidden(boolean hidden) {
+ field_2_option_flags = BitFieldFactory.getInstance(HIDDEN_FLAG_MASK).setShortBoolean(field_2_option_flags, hidden);
+ }
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
index 487f9c6b22..e237ed75db 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
@@ -445,6 +445,35 @@ public class HSSFWorkbook extends POIDocument
return workbook.getSheetName(sheet);
}
+ /**
+ * check whether a sheet is hidden
+ * @param sheet Number
+ * @return True if sheet is hidden
+ */
+
+ public boolean isSheetHidden(int sheet) {
+ if (sheet > (sheets.size() - 1))
+ {
+ throw new RuntimeException("Sheet out of bounds");
+ }
+ return workbook.isSheetHidden(sheet);
+ }
+
+ /**
+ * Hide or unhide a sheet
+ *
+ * @param sheetnum The sheet number
+ * @param hidden True to mark the sheet as hidden, false otherwise
+ */
+
+ public void setSheetHidden(int sheet, boolean hidden) {
+ if (sheet > (sheets.size() - 1))
+ {
+ throw new RuntimeException("Sheet out of bounds");
+ }
+ workbook.setSheetHidden(sheet,hidden);
+ }
+
/*
* get the sheet's index
* @param name sheet name
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java b/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java
index 9628ab23e4..038b6cce55 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java
@@ -19,12 +19,11 @@ package org.apache.poi.hssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import junit.framework.TestCase;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+
/**
* Tests for how HSSFWorkbook behaves with XLS files
* with a WORKBOOK directory entry (instead of the more
@@ -34,11 +33,21 @@ public class TestSheetHiding extends TestCase {
private String dirPath;
private String xlsHidden = "TwoSheetsOneHidden.xls";
private String xlsShown = "TwoSheetsNoneHidden.xls";
+ private HSSFWorkbook wbH;
+ private HSSFWorkbook wbU;
protected void setUp() throws Exception {
super.setUp();
-
- dirPath = System.getProperty("HSSF.testdata.path");
+
+ dirPath = System.getProperty("HSSF.testdata.path");
+ FileInputStream isH = new FileInputStream(dirPath + "/" + xlsHidden);
+ POIFSFileSystem fsH = new POIFSFileSystem(isH);
+
+ FileInputStream isU = new FileInputStream(dirPath + "/" + xlsShown);
+ POIFSFileSystem fsU = new POIFSFileSystem(isU);
+
+ wbH = new HSSFWorkbook(fsH);
+ wbU = new HSSFWorkbook(fsU);
}
/**
@@ -47,36 +56,27 @@ public class TestSheetHiding extends TestCase {
* the hidden flags are
*/
public void testTextSheets() throws Exception {
- FileInputStream isH = new FileInputStream(dirPath + "/" + xlsHidden);
- POIFSFileSystem fsH = new POIFSFileSystem(isH);
-
- FileInputStream isU = new FileInputStream(dirPath + "/" + xlsShown);
- POIFSFileSystem fsU = new POIFSFileSystem(isU);
-
- HSSFWorkbook wbH = new HSSFWorkbook(fsH);
- HSSFWorkbook wbU = new HSSFWorkbook(fsU);
-
// Both should have two sheets
assertEquals(2, wbH.sheets.size());
assertEquals(2, wbU.sheets.size());
-
+
// All sheets should have one row
assertEquals(0, wbH.getSheetAt(0).getLastRowNum());
assertEquals(0, wbH.getSheetAt(1).getLastRowNum());
assertEquals(0, wbU.getSheetAt(0).getLastRowNum());
assertEquals(0, wbU.getSheetAt(1).getLastRowNum());
-
+
// All rows should have one column
assertEquals(1, wbH.getSheetAt(0).getRow(0).getLastCellNum());
assertEquals(1, wbH.getSheetAt(1).getRow(0).getLastCellNum());
assertEquals(1, wbU.getSheetAt(0).getRow(0).getLastCellNum());
assertEquals(1, wbU.getSheetAt(1).getRow(0).getLastCellNum());
-
+
// Text should be sheet based
- assertEquals("Sheet1A1", wbH.getSheetAt(0).getRow(0).getCell((short)0).getStringCellValue());
- assertEquals("Sheet2A1", wbH.getSheetAt(1).getRow(0).getCell((short)0).getStringCellValue());
- assertEquals("Sheet1A1", wbU.getSheetAt(0).getRow(0).getCell((short)0).getStringCellValue());
- assertEquals("Sheet2A1", wbU.getSheetAt(1).getRow(0).getCell((short)0).getStringCellValue());
+ assertEquals("Sheet1A1", wbH.getSheetAt(0).getRow(0).getCell((short)0).getRichStringCellValue().getString());
+ assertEquals("Sheet2A1", wbH.getSheetAt(1).getRow(0).getCell((short)0).getRichStringCellValue().getString());
+ assertEquals("Sheet1A1", wbU.getSheetAt(0).getRow(0).getCell((short)0).getRichStringCellValue().getString());
+ assertEquals("Sheet2A1", wbU.getSheetAt(1).getRow(0).getCell((short)0).getRichStringCellValue().getString());
}
/**
@@ -84,7 +84,10 @@ public class TestSheetHiding extends TestCase {
* as expected
*/
public void testHideUnHideFlags() throws Exception {
- // TODO
+ assertTrue(wbH.isSheetHidden(0));
+ assertFalse(wbH.isSheetHidden(1));
+ assertFalse(wbU.isSheetHidden(0));
+ assertFalse(wbU.isSheetHidden(1));
}
/**
@@ -92,7 +95,15 @@ public class TestSheetHiding extends TestCase {
* one hidden
*/
public void testHide() throws Exception {
- // TODO
+ wbU.setSheetHidden(0, true);
+ assertTrue(wbU.isSheetHidden(0));
+ assertFalse(wbU.isSheetHidden(1));
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ wbU.write(out);
+ out.close();
+ HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+ assertTrue(wb2.isSheetHidden(0));
+ assertFalse(wb2.isSheetHidden(1));
}
/**
@@ -100,6 +111,14 @@ public class TestSheetHiding extends TestCase {
* none hidden
*/
public void testUnHide() throws Exception {
- // TODO
+ wbH.setSheetHidden(0, false);
+ assertFalse(wbH.isSheetHidden(0));
+ assertFalse(wbH.isSheetHidden(1));
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ wbH.write(out);
+ out.close();
+ HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+ assertFalse(wb2.isSheetHidden(0));
+ assertFalse(wb2.isSheetHidden(1));
}
}