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

(-)src/java/org/apache/poi/ss/usermodel/Sheet.java (-1 / +7 lines)
Lines 455-462 Link Here
455
     * @return true => protection enabled; false => protection disabled
455
     * @return true => protection enabled; false => protection disabled
456
     */
456
     */
457
    boolean getProtect();
457
    boolean getProtect();
458
458
    
459
    /**
459
    /**
460
     * Sets the protection enabled as well as the password
461
     * @param password to set for protection. Pass <code>null</code> to remove protection
462
     */
463
    public void protectSheet(String password);
464
    
465
    /**
460
     * Answer whether scenario protection is enabled or disabled
466
     * Answer whether scenario protection is enabled or disabled
461
     *
467
     *
462
     * @return true => protection enabled; false => protection disabled
468
     * @return true => protection enabled; false => protection disabled
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (+36 lines)
Lines 32-37 Link Here
32
32
33
import org.apache.poi.POIXMLDocumentPart;
33
import org.apache.poi.POIXMLDocumentPart;
34
import org.apache.poi.POIXMLException;
34
import org.apache.poi.POIXMLException;
35
import org.apache.poi.hssf.record.PasswordRecord;
35
import org.apache.poi.hssf.record.formula.FormulaShifter;
36
import org.apache.poi.hssf.record.formula.FormulaShifter;
36
import org.apache.poi.hssf.util.PaneInformation;
37
import org.apache.poi.hssf.util.PaneInformation;
37
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
38
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
Lines 52-57 Link Here
52
import org.apache.poi.ss.util.CellRangeAddressList;
53
import org.apache.poi.ss.util.CellRangeAddressList;
53
import org.apache.poi.ss.util.CellReference;
54
import org.apache.poi.ss.util.CellReference;
54
import org.apache.poi.ss.util.SSCellRange;
55
import org.apache.poi.ss.util.SSCellRange;
56
import org.apache.poi.util.HexDump;
55
import org.apache.poi.util.Internal;
57
import org.apache.poi.util.Internal;
56
import org.apache.poi.util.POILogFactory;
58
import org.apache.poi.util.POILogFactory;
57
import org.apache.poi.util.POILogger;
59
import org.apache.poi.util.POILogger;
Lines 941-947 Link Here
941
    public boolean getProtect() {
943
    public boolean getProtect() {
942
        return worksheet.isSetSheetProtection() && sheetProtectionEnabled();
944
        return worksheet.isSetSheetProtection() && sheetProtectionEnabled();
943
    }
945
    }
946
 
947
    /**
948
     * Enables sheet protection and sets the password for the sheet.
949
     * Also sets some attributes on the {@link CTSheetProtection} that correspond to
950
     * the default values used by Excel
951
     * 
952
     * @param password to set for protection. Pass <code>null</code> to remove protection
953
     */
954
    @Override
955
    public void protectSheet(String password) {
956
        	
957
    	if(password != null) {
958
    		CTSheetProtection sheetProtection = worksheet.addNewSheetProtection();
959
    		sheetProtection.xsetPassword(stringToExcelPassword(password));
960
    		sheetProtection.setSheet(true);
961
    		sheetProtection.setScenarios(true);
962
    		sheetProtection.setObjects(true);
963
    	} else {
964
    		worksheet.unsetSheetProtection();
965
    	}
966
    }
944
967
968
	/**
969
	 * Converts a String to a {@link STUnsignedShortHex} value that contains the {@link PasswordRecord#hashPassword(String)}
970
	 * value in hexadecimal format
971
	 *  
972
	 * @param password the password string you wish convert to an {@link STUnsignedShortHex}
973
	 * @return {@link STUnsignedShortHex} that contains Excel hashed password in Hex format
974
	 */
975
	private STUnsignedShortHex stringToExcelPassword(String password) {
976
		STUnsignedShortHex hexPassword = STUnsignedShortHex.Factory.newInstance();
977
		hexPassword.setStringValue(String.valueOf(HexDump.shortToHex(PasswordRecord.hashPassword(password))).substring(2));
978
		return hexPassword;
979
	}
980
	
945
    /**
981
    /**
946
     * Returns the logical row ( 0-based).  If you ask for a row that is not
982
     * Returns the logical row ( 0-based).  If you ask for a row that is not
947
     * defined you get a null.  This is to say row 4 represents the fifth row on a sheet.
983
     * defined you get a null.  This is to say row 4 represents the fifth row on a sheet.
(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java (+12 lines)
Lines 979-982 Link Here
979
979
980
        assertEquals("A1:D100", sheet.getCTWorksheet().getAutoFilter().getRef());
980
        assertEquals("A1:D100", sheet.getCTWorksheet().getAutoFilter().getRef());
981
    }
981
    }
982
    
983
    public void testProtectSheet() {
984
    	
985
    	XSSFWorkbook wb = new XSSFWorkbook();
986
    	XSSFSheet sheet = wb.createSheet();
987
    	sheet.protectSheet("Test");  
988
    	assertTrue(sheet.getProtect());
989
    	sheet.protectSheet(null);  
990
    	assertFalse(sheet.getProtect());
991
992
    }
993
    
982
}
994
}

Return to bug 48900