officefileapi-devexpress-dot-spreadsheet-dot-protectedrangecollection-dot-add-x28-system-dot-string-system-dot-collections-dot-generic-dot-ilist-devexpress-dot-spreadsheet-dot-cellrange-x29.md
Creates a protected range and adds it to a worksheet collection of protected ranges.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
ProtectedRange Add(
string name,
IList<CellRange> ranges
)
Function Add(
name As String,
ranges As IList(Of CellRange)
) As ProtectedRange
| Name | Type | Description |
|---|---|---|
| name | String |
A string that is the name of the newly created range.
| | ranges | IList<CellRange> |
A list of worksheet CellRange objects to include in a protected range.
|
| Type | Description |
|---|---|
| ProtectedRange |
A ProtectedRange object that combines ranges with identical protection options.
|
The entire worksheet is protected with a “password” password. End-users can only select cells.
On an attempt to edit range “C3:E8” on a protected sheet, the end-user will be prompted for a password (“123”) unless the application which loaded the worksheet is not run under the same account as the application which has executed the following code snippet. If it is so, a password is not required to edit the range “C3:E8”.
workbook.BeginUpdate()
Dim worksheet As Worksheet = workbook.Worksheets(0)
worksheet("C3:E8").Borders.SetAllBorders(Color.Black, BorderLineStyle.Thin)
' Give specific user permission to edit a range in a protected worksheet.
Dim protectedRange As ProtectedRange = worksheet.ProtectedRanges.Add("My Range", worksheet("C3:E8"))
Dim permission As New EditRangePermission()
permission.UserName = Environment.UserName
permission.DomainName = Environment.UserDomainName
permission.Deny = False
protectedRange.SecurityDescriptor = protectedRange.CreateSecurityDescriptor(New EditRangePermission() { permission })
protectedRange.SetPassword("123")
' Protect a worksheet.
If Not worksheet.IsProtected Then
worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If
worksheet.ActiveView.ShowGridlines = False
workbook.EndUpdate()
workbook.BeginUpdate();
Worksheet worksheet = workbook.Worksheets[0];
worksheet["C3:E8"].Borders.SetAllBorders(Color.Black, BorderLineStyle.Thin);
// Give specific user permission to edit a range in a protected worksheet.
ProtectedRange protectedRange = worksheet.ProtectedRanges.Add("My Range", worksheet["C3:E8"]);
EditRangePermission permission = new EditRangePermission();
permission.UserName = Environment.UserName;
permission.DomainName = Environment.UserDomainName;
permission.Deny = false;
protectedRange.SecurityDescriptor = protectedRange.CreateSecurityDescriptor(new EditRangePermission[] { permission });
protectedRange.SetPassword("123");
// Protect a worksheet.
if (!worksheet.IsProtected)
worksheet.Protect("password", WorksheetProtectionPermissions.Default);
worksheet.ActiveView.ShowGridlines = false;
workbook.EndUpdate();
See Also
ProtectedRangeCollection Interface