forked from SpreadsheetGear/SpreadsheetGearExplorerSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorksheetProtectionSample.code.cs
69 lines (60 loc) · 3.11 KB
/
WorksheetProtectionSample.code.cs
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
namespace SamplesLibrary.Engine.Samples.Workbook.Worksheet
{
public class WorksheetProtectionSample : ISpreadsheetGearEngineSample
{
public SpreadsheetGear.IWorkbook Workbook { get; set; }
public void InitializeWorkbook()
{
// Get the full path to a workbook that will demonstrate how certain aspects of a worksheet can
// still be enabled when worksheet protection is enabled.
string workbookPath = Helpers.GetFullOutputFolderPath(@"Files\Engine\WorksheetProtection.xlsx");
// Open the workbook.
Workbook = SpreadsheetGear.Factory.GetWorkbook(workbookPath);
}
public void RunSample()
{
// Create a local variable to the active worksheet.
SpreadsheetGear.IWorksheet worksheet = Workbook.ActiveWorksheet;
// The Protect(...) method provides full control over the various options you can use
// to protect a worksheet. You can provide an optional first parameter for the
// worksheet password. All additional parameters are also optional, and are setup
// below using their default values.
worksheet.Protect(
password: "MyPassword1234",
protectDrawingObjects: true,
protectContents: true,
protectScenarios: true,
userInterfaceOnly: false,
allowFormattingCells: false,
allowFormattingColumns: false,
allowFormattingRows: false,
allowInsertingColumns: false,
allowInsertingRows: false,
allowInsertingHyperlinks: false,
allowDeletingColumns: false,
allowDeletingRows: false,
allowSorting: false,
allowFiltering: false,
allowUsingPivotTables: false
);
// Unprotect a worksheet with a password.
worksheet.Unprotect("MyPassword1234");
// Enables worksheet protection but without a password.
worksheet.ProtectContents = true;
// Modify the various aspects of a worksheet that should be protected with the
// IProtection interface.
SpreadsheetGear.IProtection protectOptions = worksheet.Protection;
protectOptions.AllowFormattingCells = false;
protectOptions.AllowFormattingColumns = false;
// ... See documentation or use IntelliSense for additional options...
// Prevent IRange.Locked == true cells from being selected.
worksheet.EnableSelection = SpreadsheetGear.EnableSelection.UnlockedCells;
// Enable this property to allow programmatic changes to still be made to a
// worksheet even when worksheet protection is enabled. UI protection is still
// enforced.
worksheet.ProtectionMode = true;
worksheet.Cells["A6"].Value = "This cell was programmatically modified while worksheet " +
"protection was enabled because IWorksheet.ProtectionMode was set to true.";
}
}
}