Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Protect Worksheet Ranges

    • 7 minutes to read

    A protected range specifies cells that users can edit in a protected worksheet. If the range has a password, spreadsheet applications (for example, Microsoft Excel) ask users to enter the range password before they edit cells in the range.

    Note

    Protected ranges have no effect until you protect the worksheet. In an unprotected worksheet, users can edit all cells.

    Spreadsheet applications (for example, Microsoft Excel) ask users for range passwords. The Spreadsheet Document API does not. Your code can modify cells in a protected range without the range password.

    You can allow users to edit cells in a protected worksheet in the following ways:

    Cells Behavior in Spreadsheet Applications API
    Unlocked cells Users edit cells without a password. CellRange.ApplyProtection(protection)
    Protected range Users enter the range password before they edit cells. If the range has no password, users edit cells without a password. Worksheet.ProtectedRanges

    Add a Protected Range

    Use the Worksheet.ProtectedRanges property to access the collection of protected ranges in a worksheet. Call the ProtectedRangeCollection.Add(name, range, password) method to create a protected range and add the range to the collection. If you do not specify a password, users can edit the range without a password.

    The cell range must be on the worksheet that contains the collection. The cell range can contain multiple areas (for example, a range returned by the CellRange.Union(other) method).

    A protected range name must start with a letter or an underscore. The name can contain letters, digits, underscores, periods, and spaces. Protected range names are case-sensitive (Range1 and range1 are different names). A worksheet cannot contain two protected ranges with the same name.

    The following code snippet adds a password-protected range for input cells and protects the worksheet:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Cells["A1"].Value = "Region";
    worksheet.Cells["B1"].Value = "Budget";
    worksheet.Cells["A2"].Value = "North";
    worksheet.Cells["A3"].Value = "South";
    
    // Allow users to edit budget values after they enter the range password.
    worksheet.ProtectedRanges.Add("Budget", worksheet["B2:B3"], "range-password");
    
    // Protect the worksheet.
    worksheet.Protect(new WorksheetProtectionOptions("sheet-password"));
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Find and Modify a Protected Range

    Use the following members to find protected ranges in the collection:

    Member Description
    ProtectedRangeCollection[name] Gets the protected range with the specified name.
    ProtectedRangeCollection[index] Gets or sets the protected range at the specified position in the collection.
    ProtectedRangeCollection.Contains(name) Returns whether the collection contains a protected range with the specified name.
    ProtectedRangeCollection.IndexOf(name) Returns the index of the protected range with the specified name.
    ProtectedRangeCollection.Count Gets the number of protected ranges in the collection.

    ProtectedRange is an immutable structure. To modify a protected range, call one of the following methods and assign the result to the ProtectedRangeCollection[index] indexer:

    Method Description
    ProtectedRange.WithName(value) Returns a copy of the protected range with the specified name.
    ProtectedRange.WithRange(value) Returns a copy of the protected range with the specified cell range.
    ProtectedRange.WithPassword(value) Returns a copy of the protected range with the specified password.
    ProtectedRange.WithSecurityDescriptor(value) Returns a copy of the protected range with the specified security descriptor.

    The API saves a password hash to the document. You cannot get the range password. Use the ProtectedRange.HasPassword property to check whether a protected range has a password.

    The following code snippet extends a protected range, removes the range password, and displays the range name and password status in the console:

    using System;
    using DevExpress.Docs.Spreadsheet;
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.ProtectedRanges.Add("Budget", worksheet["B2:B3"], "range-password");
    
    // ProtectedRange is immutable. Assign a modified copy to the collection.
    int index = worksheet.ProtectedRanges.IndexOf("Budget");
    worksheet.ProtectedRanges[index] = worksheet.ProtectedRanges[index]
        .WithRange(worksheet["B2:B10"])
        .WithPassword(null);
    
    ProtectedRange budget = worksheet.ProtectedRanges["Budget"];
    Console.WriteLine($"{budget.Name}: HasPassword = {budget.HasPassword}");
    

    Permissions for Windows User Accounts

    Microsoft Excel can allow specific Windows user accounts to edit a protected range without a password. Excel saves these permissions to the document as a security descriptor. Use the ProtectedRange.SecurityDescriptor property to get the security descriptor of a protected range.

    The Spreadsheet Document API loads and saves security descriptors and does not check the specified user accounts.

    To specify permissions, use the SecurityDescriptorStringBuilder class:

    1. Call SecurityDescriptorStringBuilder.Allow(sid) and SecurityDescriptorStringBuilder.Deny(sid) methods to add permissions for Windows user accounts and groups. Allow and Deny methods accept security identifiers (SIDs) in string format (for example, S-1-5-32-545).
    2. Call the SecurityDescriptorStringBuilder.ToString() method to get the security descriptor.
    3. Pass the security descriptor to the ProtectedRange.WithSecurityDescriptor(value) method and assign the result to the ProtectedRangeCollection[index] indexer.

    The following code snippet allows members of the built-in Users group to edit a protected range without a password, denies this permission to the built-in Guests group, and displays the security descriptor in the console:

    using System;
    using DevExpress.Docs.Spreadsheet;
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.ProtectedRanges.Add("Budget", worksheet["B2:B10"], "range-password");
    
    // Create a security descriptor for the BUILTIN\Users and BUILTIN\Guests groups.
    string securityDescriptor = new SecurityDescriptorStringBuilder()
        .Allow("S-1-5-32-545")
        .Deny("S-1-5-32-546")
        .ToString();
    
    int index = worksheet.ProtectedRanges.IndexOf("Budget");
    worksheet.ProtectedRanges[index] = worksheet.ProtectedRanges[index].WithSecurityDescriptor(securityDescriptor);
    
    Console.WriteLine(worksheet.ProtectedRanges["Budget"].SecurityDescriptor);
    

    Note

    SecurityDescriptorStringBuilder.Allow(sid) and SecurityDescriptorStringBuilder.Deny(sid) methods do not resolve account names. On Windows, call the NTAccount.Translate(targetType) method to get the SID of a user account or group from the account name. If Windows cannot find the account, the Translate method throws an IdentityNotMappedException.

    The following code snippet gets SIDs of the CORP\jsmith user account and the CORP\Contractors group, allows the user account to edit a protected range without a password, and denies this permission to the group:

    using System;
    using System.Security.Principal;
    using DevExpress.Docs.Spreadsheet;
    
    using var workbook = new Workbook();
    var worksheet = workbook.Worksheets[0];
    worksheet.ProtectedRanges.Add("Budget", worksheet["B2:B10"], "range-password");
    
    // Get SIDs of Windows accounts. NTAccount.Translate is available on Windows only.
    var userSid = new NTAccount("CORP", "jsmith").Translate(typeof(SecurityIdentifier)).Value;
    var groupSid = new NTAccount("CORP", "Contractors").Translate(typeof(SecurityIdentifier)).Value;
    
    var securityDescriptor = new SecurityDescriptorStringBuilder()
        .Allow(userSid)
        .Deny(groupSid)
        .ToString();
    
    var index = worksheet.ProtectedRanges.IndexOf("Budget");
    worksheet.ProtectedRanges[index] = worksheet.ProtectedRanges[index].WithSecurityDescriptor(securityDescriptor);
    
    Console.WriteLine(worksheet.ProtectedRanges["Budget"].SecurityDescriptor);
    

    Remove Protected Ranges

    Call one of the following methods to remove protected ranges from a worksheet:

    Method Description
    ProtectedRangeCollection.Remove(name) Removes the protected range with the specified name from the collection.
    ProtectedRangeCollection.RemoveAt(index) Removes the protected range at the specified position from the collection.
    ProtectedRangeCollection.Clear() Removes all protected ranges from the collection.

    Protected Ranges and Worksheet Modifications

    When you insert or delete rows and columns, protected ranges move with the cells. If you delete all cells in a protected range, the API removes the protected range from the collection.

    Note

    If you delete rows, columns, or cells that contain locked cells on a protected worksheet, the API throws an InvalidOperationException (“The affected range contains locked cells on a protected worksheet.”). This restriction also applies to locked cells in a protected range.

    Next Steps