-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProtectionActions.vb
79 lines (67 loc) · 3.18 KB
/
ProtectionActions.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Imports System
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraPrinting.Control
Imports System.Drawing
Namespace SpreadsheetControl_API
Public NotInheritable Class ProtectionActions
Private Sub New()
End Sub
#Region "Actions"
Public Shared ProtectWorkbookAction As Action(Of IWorkbook) = AddressOf ProtectWorkbook
Public Shared ProtectWorksheetAction As Action(Of IWorkbook) = AddressOf ProtectWorksheet
Public Shared ProtectRangeAction As Action(Of IWorkbook) = AddressOf ProtectRange
#End Region
Private Shared Sub ProtectWorkbook(ByVal workbook As IWorkbook)
' #Region "#ProtectWorkbook"
' Protect workbook structure (prevents users from adding or deleting worksheets
' or from displaying hidden worksheets).
workbook.BeginUpdate()
If Not workbook.IsProtected Then
workbook.Protect("password", True, False)
End If
workbook.Worksheets(0).Visible = False
Dim worksheet As Worksheet = workbook.Worksheets(1)
worksheet("D5").Value = "You are not allowed to add or delete a worksheet."
worksheet("D6").Value = "Hidden worksheets cannot be displayed."
workbook.EndUpdate()
' #End Region ' #ProtectWorkbook
End Sub
Private Shared Sub ProtectWorksheet(ByVal workbook As IWorkbook)
' #Region "#ProtectWorksheet"
Dim worksheet As Worksheet = workbook.Worksheets(0)
' Protect a worksheet. Prevent users from making changes to worksheet elements.
If Not worksheet.IsProtected Then
worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If
workbook.BeginUpdate()
worksheet("C3:F8").Borders.SetOutsideBorders(Color.Red, BorderLineStyle.Thin)
worksheet("D5:E6").Merge()
worksheet("D5").Value = "Try to change me!"
worksheet("D5").Alignment.Vertical = SpreadsheetVerticalAlignment.Center
workbook.EndUpdate()
' #End Region ' #ProtectWorksheet
End Sub
Private Shared Sub ProtectRange(ByVal workbook As IWorkbook)
' #Region "#ProtectRange"
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()
' #End Region ' #ProtectRange
End Sub
End Class
End Namespace