View | Details | Raw Unified | Return to bug 43937
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (+29 lines)
Lines 445-450 Link Here
445
        return workbook.getSheetName(sheet);
445
        return workbook.getSheetName(sheet);
446
    }
446
    }
447
447
448
    /**
449
     * check whether a sheet is hidden
450
     * @param sheet Number
451
     * @return True if sheet is hidden
452
     */
453
454
    public boolean isSheetHidden(int sheet) {
455
        if (sheet > (sheets.size() - 1))
456
        {
457
            throw new RuntimeException("Sheet out of bounds");
458
        }
459
        return workbook.isSheetHidden(sheet);
460
    }
461
462
    /**
463
     * Hide or unhide a sheet
464
     * 
465
     * @param sheetnum The sheet number
466
     * @param hidden True to mark the sheet as hidden, false otherwise
467
     */
468
469
    public void setSheetHidden(int sheet, boolean hidden) {
470
        if (sheet > (sheets.size() - 1))
471
        {
472
            throw new RuntimeException("Sheet out of bounds");
473
        }
474
        workbook.setSheetHidden(sheet,hidden);
475
    }
476
    
448
    /*
477
    /*
449
     * get the sheet's index
478
     * get the sheet's index
450
     * @param name  sheet name
479
     * @param name  sheet name
(-)src/java/org/apache/poi/hssf/model/Workbook.java (+23 lines)
Lines 543-548 Link Here
543
    }
543
    }
544
544
545
    /**
545
    /**
546
     * gets the hidden flag for a given sheet.
547
     *
548
     * @param sheetnum the sheet number (0 based)
549
     * @return True if sheet is hidden
550
     */
551
552
    public boolean isSheetHidden(int sheetnum) {
553
        BoundSheetRecord bsr = ( BoundSheetRecord ) boundsheets.get(sheetnum);
554
        return bsr.isHidden();
555
    }
556
557
    /**
558
     * Hide or unhide a sheet
559
     * 
560
     * @param sheetnum The sheet number
561
     * @param hidden True to mark the sheet as hidden, false otherwise
562
     */
563
    
564
    public void setSheetHidden(int sheetnum, boolean hidden) {
565
        BoundSheetRecord bsr = ( BoundSheetRecord ) boundsheets.get(sheetnum);
566
        bsr.setHidden(hidden);
567
    }
568
    /**
546
     * get the sheet's index
569
     * get the sheet's index
547
     * @param name  sheet name
570
     * @param name  sheet name
548
     * @return sheet index or -1 if it was not found.
571
     * @return sheet index or -1 if it was not found.
(-)src/java/org/apache/poi/hssf/record/BoundSheetRecord.java (+10 lines)
Lines 19-24 Link Here
19
19
20
package org.apache.poi.hssf.record;
20
package org.apache.poi.hssf.record;
21
21
22
import org.apache.poi.util.BitFieldFactory;
22
import org.apache.poi.util.LittleEndian;
23
import org.apache.poi.util.LittleEndian;
23
import org.apache.poi.util.StringUtil;
24
import org.apache.poi.util.StringUtil;
24
25
Lines 36-41 Link Here
36
public class BoundSheetRecord
37
public class BoundSheetRecord
37
        extends Record
38
        extends Record
38
{
39
{
40
    private static final short HIDDEN_FLAG_MASK = 0x01;
39
    public final static short sid = 0x85;
41
    public final static short sid = 0x85;
40
    private int field_1_position_of_BOF;
42
    private int field_1_position_of_BOF;
41
    private short field_2_option_flags;
43
    private short field_2_option_flags;
Lines 301-304 Link Here
301
    {
303
    {
302
        return sid;
304
        return sid;
303
    }
305
    }
306
307
    public boolean isHidden() {
308
	    return BitFieldFactory.getInstance(HIDDEN_FLAG_MASK).isSet(field_2_option_flags);
309
    }
310
311
    public void setHidden(boolean hidden) {
312
	    field_2_option_flags = BitFieldFactory.getInstance(HIDDEN_FLAG_MASK).setShortBoolean(field_2_option_flags, hidden);
313
    }
304
}
314
}
(-)src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java (-21 / +40 lines)
Lines 19-30 Link Here
19
import java.io.ByteArrayInputStream;
19
import java.io.ByteArrayInputStream;
20
import java.io.ByteArrayOutputStream;
20
import java.io.ByteArrayOutputStream;
21
import java.io.FileInputStream;
21
import java.io.FileInputStream;
22
import java.io.FileNotFoundException;
23
22
23
import junit.framework.TestCase;
24
24
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
25
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
25
26
26
import junit.framework.TestCase;
27
28
/**
27
/**
29
 * Tests for how HSSFWorkbook behaves with XLS files
28
 * Tests for how HSSFWorkbook behaves with XLS files
30
 *  with a WORKBOOK directory entry (instead of the more
29
 *  with a WORKBOOK directory entry (instead of the more
Lines 34-44 Link Here
34
	private String dirPath;
33
	private String dirPath;
35
	private String xlsHidden = "TwoSheetsOneHidden.xls";
34
	private String xlsHidden = "TwoSheetsOneHidden.xls";
36
	private String xlsShown  = "TwoSheetsNoneHidden.xls";
35
	private String xlsShown  = "TwoSheetsNoneHidden.xls";
36
	private HSSFWorkbook wbH;
37
	private HSSFWorkbook wbU;
37
38
38
	protected void setUp() throws Exception {
39
	protected void setUp() throws Exception {
39
		super.setUp();
40
		super.setUp();
40
		
41
		
41
        dirPath = System.getProperty("HSSF.testdata.path");
42
                dirPath = System.getProperty("HSSF.testdata.path");
43
                FileInputStream isH = new FileInputStream(dirPath + "/" + xlsHidden);
44
                POIFSFileSystem fsH = new POIFSFileSystem(isH);
45
                
46
                FileInputStream isU = new FileInputStream(dirPath + "/" + xlsShown);
47
                POIFSFileSystem fsU = new POIFSFileSystem(isU);
48
        
49
                wbH = new HSSFWorkbook(fsH);
50
                wbU = new HSSFWorkbook(fsU);
42
	}
51
	}
43
52
44
	/**
53
	/**
Lines 47-62 Link Here
47
	 *  the hidden flags are
56
	 *  the hidden flags are
48
	 */
57
	 */
49
	public void testTextSheets() throws Exception {
58
	public void testTextSheets() throws Exception {
50
		FileInputStream isH = new FileInputStream(dirPath + "/" + xlsHidden);
59
                // Both should have two sheets
51
		POIFSFileSystem fsH = new POIFSFileSystem(isH);
52
		
53
		FileInputStream isU = new FileInputStream(dirPath + "/" + xlsShown);
54
		POIFSFileSystem fsU = new POIFSFileSystem(isU);
55
56
		HSSFWorkbook wbH = new HSSFWorkbook(fsH);
57
		HSSFWorkbook wbU = new HSSFWorkbook(fsU);
58
		
59
		// Both should have two sheets
60
		assertEquals(2, wbH.sheets.size());
60
		assertEquals(2, wbH.sheets.size());
61
		assertEquals(2, wbU.sheets.size());
61
		assertEquals(2, wbU.sheets.size());
62
		
62
		
Lines 73-82 Link Here
73
		assertEquals(1, wbU.getSheetAt(1).getRow(0).getLastCellNum());
73
		assertEquals(1, wbU.getSheetAt(1).getRow(0).getLastCellNum());
74
		
74
		
75
		// Text should be sheet based
75
		// Text should be sheet based
76
		assertEquals("Sheet1A1", wbH.getSheetAt(0).getRow(0).getCell((short)0).getStringCellValue());
76
		assertEquals("Sheet1A1", wbH.getSheetAt(0).getRow(0).getCell((short)0).getRichStringCellValue().getString());
77
		assertEquals("Sheet2A1", wbH.getSheetAt(1).getRow(0).getCell((short)0).getStringCellValue());
77
		assertEquals("Sheet2A1", wbH.getSheetAt(1).getRow(0).getCell((short)0).getRichStringCellValue().getString());
78
		assertEquals("Sheet1A1", wbU.getSheetAt(0).getRow(0).getCell((short)0).getStringCellValue());
78
		assertEquals("Sheet1A1", wbU.getSheetAt(0).getRow(0).getCell((short)0).getRichStringCellValue().getString());
79
		assertEquals("Sheet2A1", wbU.getSheetAt(1).getRow(0).getCell((short)0).getStringCellValue());
79
		assertEquals("Sheet2A1", wbU.getSheetAt(1).getRow(0).getCell((short)0).getRichStringCellValue().getString());
80
	}
80
	}
81
81
82
	/**
82
	/**
Lines 84-90 Link Here
84
	 *  as expected
84
	 *  as expected
85
	 */
85
	 */
86
	public void testHideUnHideFlags() throws Exception {
86
	public void testHideUnHideFlags() throws Exception {
87
		// TODO
87
                assertTrue(wbH.isSheetHidden(0));
88
                assertFalse(wbH.isSheetHidden(1));
89
                assertFalse(wbU.isSheetHidden(0));
90
                assertFalse(wbU.isSheetHidden(1));
88
	}
91
	}
89
92
90
	/**
93
	/**
Lines 92-98 Link Here
92
	 *  one hidden
95
	 *  one hidden
93
	 */
96
	 */
94
	public void testHide() throws Exception {
97
	public void testHide() throws Exception {
95
		// TODO
98
	        wbU.setSheetHidden(0, true);
99
                assertTrue(wbU.isSheetHidden(0));
100
                assertFalse(wbU.isSheetHidden(1));
101
                ByteArrayOutputStream out = new ByteArrayOutputStream();
102
                wbU.write(out);
103
                out.close();
104
                HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
105
                assertTrue(wb2.isSheetHidden(0));
106
                assertFalse(wb2.isSheetHidden(1));
96
	}
107
	}
97
108
98
	/**
109
	/**
Lines 100-105 Link Here
100
	 *  none hidden
111
	 *  none hidden
101
	 */
112
	 */
102
	public void testUnHide() throws Exception {
113
	public void testUnHide() throws Exception {
103
		// TODO
114
                wbH.setSheetHidden(0, false);
115
                assertFalse(wbH.isSheetHidden(0));
116
                assertFalse(wbH.isSheetHidden(1));
117
                ByteArrayOutputStream out = new ByteArrayOutputStream();
118
                wbH.write(out);
119
                out.close();
120
                HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
121
                assertFalse(wb2.isSheetHidden(0));
122
                assertFalse(wb2.isSheetHidden(1));
104
	}
123
	}
105
}
124
}

Return to bug 43937