[]
        
(Showing Draft Content)

WriteProtection

Class WriteProtection

java.lang.Object
com.grapecity.documents.excel.WriteProtection

public class WriteProtection extends Object
Provides access to the workbook write protection options.

Use this class to configure how a workbook is protected from modification, including recommending that the workbook be opened as read-only, specifying the user who has write permission, and setting a password required to modify the file. The write-protection takes effect when the workbook is opened in an Excel application.

Instances of this class are typically obtained from Workbook.getWriteProtection().


 WriteProtection protection = workbook.getWriteProtection();
 protection.setWriteReservedBy("User");
 protection.setReadOnlyRecommended(true);
 // Get the password from user input via getPasswordFromSecureSource().
 String password = getPasswordFromSecureSource();
 protection.setWritePassword(password);
 String fileName = "WriteProtection.xlsx";
 workbook.save(fileName);
 
  • Constructor Details

    • WriteProtection

      public WriteProtection()
  • Method Details

    • getWriteReservedBy

      public String getWriteReservedBy()
      Gets the write-reserved user name for the workbook.

      This property returns the write-reserved user name stored in the workbook write protection settings.

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setWriteReservedBy("User");
       String userName = protection.getWriteReservedBy();
       
      Returns:
      The write-reserved user name for the workbook.
    • setWriteReservedBy

      public void setWriteReservedBy(String value)
      Sets the write-reserved user name for the workbook.

      This property sets the write-reserved user name stored in the workbook write protection settings.

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setWriteReservedBy("User");
       String userName = protection.getWriteReservedBy();
       
      Parameters:
      value - The write-reserved user name for the workbook. Must not be null or an empty string.
      Throws:
      IllegalArgumentException - if value is null or an empty string.
    • getReadOnlyRecommended

      public boolean getReadOnlyRecommended()
      Gets whether the Read Only Recommended option is selected.

      When this option is enabled, applications that open the workbook can recommend opening it in read-only mode.

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setReadOnlyRecommended(true);
       boolean readOnlyRecommended = protection.getReadOnlyRecommended();
       
      Returns:
      true if the Read Only Recommended option is selected; otherwise, false.
    • setReadOnlyRecommended

      public void setReadOnlyRecommended(boolean value)
      Sets whether the Read Only Recommended option is selected.

      When this option is enabled, applications that open the workbook can recommend opening it in read-only mode.

      
       WriteProtection protection = workbook.getWriteProtection();
       protection.setReadOnlyRecommended(true);
       boolean readOnlyRecommended = protection.getReadOnlyRecommended();
       
      Parameters:
      value - true if the Read Only Recommended option is selected; otherwise, false.
    • getWriteReserved

      public boolean getWriteReserved()
      Gets whether this workbook is write protected.
      
       WriteProtection protection = workbook.getWriteProtection();
       String password = getPasswordFromSecureSource();
       protection.setWritePassword(password);
       boolean writeReserved = protection.getWriteReserved();
       
      Returns:
      true if this workbook is write protected; otherwise, false.
    • setWritePassword

      public void setWritePassword(String value)
      Sets the password used for workbook write protection.

      After a non-empty password is specified, the workbook is marked as write protected. This write protection is applied when the workbook is opened in an Excel application. Passing null or an empty string clears the write-protection password and disables workbook write protection.

      
       WriteProtection protection = workbook.getWriteProtection();
       // Get the password from user input via getPasswordFromSecureSource().
       String password = getPasswordFromSecureSource();
       protection.setWritePassword(password);
       boolean writeReserved = protection.getWriteReserved();
       boolean passwordMatches = protection.validatePassword(password);
       
      Parameters:
      value - The password used to protect the workbook from modification. Must not exceed 15 characters; null or an empty string clears the password and disables write protection.
      Throws:
      IllegalArgumentException - if value is longer than 15 characters.
    • validatePassword

      public boolean validatePassword(String password)
      Validates the specified password against the workbook write-protection password.

      Use this method to check whether a password matches the password set by setWritePassword(String). If the workbook is not write protected, this method returns true.

      
       WriteProtection protection = workbook.getWriteProtection();
       // Get the password from user input via getPasswordFromSecureSource().
       String password = getPasswordFromSecureSource();
       protection.setWritePassword(password);
       boolean isValid = protection.validatePassword(password);
       
      Parameters:
      password - The password to validate.
      Returns:
      true if the specified password matches the workbook write-protection password, or if the workbook is not write protected; otherwise, false.