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
This commit is contained in:
Nick Burch 2008-01-08 11:59:26 +00:00
parent 033dfb4f0c
commit 32e42753ab
6 changed files with 107 additions and 24 deletions

View File

@ -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>

View File

@ -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>

View File

@ -542,6 +542,29 @@ public class Workbook implements Model
.getSheetname();
}
/**
* 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

View File

@ -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);
}
}

View File

@ -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

View File

@ -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,15 +56,6 @@ 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());
@ -73,10 +73,10 @@ public class TestSheetHiding extends TestCase {
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));
}
}