Skip to content

Commit d06c32f

Browse files
committed
Add 2 new functions: ProtectWorkbook and ProtectSheet
- Update unit test
1 parent c2a779c commit d06c32f

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

Excelize.Tests/UnitTest.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,46 @@ public void TestPivotTable()
10501050
Assert.Empty(f.Close());
10511051
}
10521052

1053+
[Fact]
1054+
public void TestProtectSheet()
1055+
{
1056+
File f = Excelize.NewFile();
1057+
Assert.Null(
1058+
Record.Exception(() =>
1059+
{
1060+
f.ProtectSheet(
1061+
"Sheet1",
1062+
new SheetProtectionOptions
1063+
{
1064+
AlgorithmName = "SHA-512",
1065+
Password = "password",
1066+
SelectLockedCells = true,
1067+
SelectUnlockedCells = true,
1068+
EditScenarios = true,
1069+
}
1070+
);
1071+
})
1072+
);
1073+
RuntimeError err = Assert.Throws<RuntimeError>(() =>
1074+
f.ProtectSheet("SheetN", new SheetProtectionOptions { })
1075+
);
1076+
Assert.Equal("sheet SheetN does not exist", err.Message);
1077+
}
1078+
1079+
[Fact]
1080+
public void TestProtectWorkbook()
1081+
{
1082+
File f = Excelize.NewFile();
1083+
Assert.Null(
1084+
Record.Exception(() =>
1085+
{
1086+
f.ProtectWorkbook(
1087+
new WorkbookProtectionOptions { Password = "password", LockStructure = true }
1088+
);
1089+
})
1090+
);
1091+
}
1092+
10531093
[Fact]
10541094
public void TestSetSheetName()
10551095
{
@@ -1380,6 +1420,14 @@ public void TestStyle()
13801420
Assert.Equal(expected, err.Message);
13811421
err = Assert.Throws<RuntimeError>(() => f.NewStyle(s));
13821422
Assert.Equal(expected, err.Message);
1423+
err = Assert.Throws<RuntimeError>(() =>
1424+
f.ProtectSheet("Sheet1", new SheetProtectionOptions { })
1425+
);
1426+
Assert.Equal(expected, err.Message);
1427+
err = Assert.Throws<RuntimeError>(() =>
1428+
f.ProtectWorkbook(new WorkbookProtectionOptions { })
1429+
);
1430+
Assert.Equal(expected, err.Message);
13831431
err = Assert.Throws<RuntimeError>(() => f.Save());
13841432
Assert.Equal(expected, err.Message);
13851433
err = Assert.Throws<RuntimeError>(() => f.SetActiveSheet(1));

Excelize/Excelize.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,19 @@ internal static extern TypesC.IntErrorResult NewStreamWriter(
363363
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
364364
internal static extern TypesC.IntErrorResult NewStyle(long fileIdx, ref TypesC.Style style);
365365

366+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
367+
internal static extern IntPtr ProtectSheet(
368+
long fileIdx,
369+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
370+
ref TypesC.SheetProtectionOptions opts
371+
);
372+
373+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
374+
internal static extern IntPtr ProtectWorkbook(
375+
long fileIdx,
376+
ref TypesC.WorkbookProtectionOptions opts
377+
);
378+
366379
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
367380
internal static extern TypesC.IntErrorResult OpenFile(
368381
[MarshalAs(UnmanagedType.LPUTF8Str)] string filename,
@@ -3232,6 +3245,90 @@ public unsafe int NewStyle(Style style)
32323245
return res.val;
32333246
}
32343247

3248+
/// <summary>
3249+
/// ProtectSheet provides a function to prevent other users from
3250+
/// accidentally or deliberately changing, moving, or deleting data in a
3251+
/// worksheet. The optional field AlgorithmName specified hash
3252+
/// algorithm, support XOR, MD4, MD5, SHA-1, SHA2-56, SHA-384, and
3253+
/// SHA-512 currently, if no hash algorithm specified, will be using the
3254+
/// XOR algorithm as default.
3255+
/// <example>
3256+
/// For example, protect Sheet1 with protection settings:
3257+
/// <code>
3258+
/// try
3259+
/// {
3260+
/// f.ProtectSheet(
3261+
/// "Sheet1",
3262+
/// new SheetProtectionOptions
3263+
/// {
3264+
/// AlgorithmName = "SHA-512",
3265+
/// Password = "password",
3266+
/// SelectLockedCells = true,
3267+
/// SelectUnlockedCells = true,
3268+
/// EditScenarios = true,
3269+
/// }
3270+
/// );
3271+
/// }
3272+
/// catch (RuntimeError err)
3273+
/// {
3274+
/// Console.WriteLine(err.Message);
3275+
/// }
3276+
/// </code>
3277+
/// </example>
3278+
/// </summary>
3279+
/// <param name="sheet">The worksheet name</param>
3280+
/// <param name="opts">The sheet protection options</param>
3281+
/// <exception cref="RuntimeError">Return None if no error occurred,
3282+
/// otherwise raise a RuntimeError with the message.</exception>
3283+
public void ProtectSheet(string sheet, SheetProtectionOptions opts)
3284+
{
3285+
var options = (TypesC.SheetProtectionOptions)
3286+
Lib.CsToC(opts, new TypesC.SheetProtectionOptions());
3287+
string err = Marshal.PtrToStringUTF8(Lib.ProtectSheet(FileIdx, sheet, ref options));
3288+
if (!string.IsNullOrEmpty(err))
3289+
throw new RuntimeError(err);
3290+
}
3291+
3292+
/// <summary>
3293+
/// ProtectWorkbook provides a function to prevent other users from
3294+
/// viewing hidden worksheets, adding, moving, deleting, or hiding
3295+
/// worksheets, and renaming worksheets in a workbook. The optional
3296+
/// field AlgorithmName specified hash algorithm, support XOR, MD4, MD5,
3297+
/// SHA-1, SHA2-56, SHA-384, and SHA-512 currently, if no hash algorithm
3298+
/// specified, will be using the XOR algorithm as default. The generated
3299+
/// workbook only works on Microsoft Office 2007 and later.
3300+
/// <example>
3301+
/// For example, protect workbook with protection settings:
3302+
/// <code>
3303+
/// try
3304+
/// {
3305+
/// f.ProtectWorkbook(
3306+
/// new WorkbookProtectionOptions
3307+
/// {
3308+
/// Password = "password",
3309+
/// LockStructure = true,
3310+
/// }
3311+
/// );
3312+
/// }
3313+
/// catch (RuntimeError err)
3314+
/// {
3315+
/// Console.WriteLine(err.Message);
3316+
/// }
3317+
/// </code>
3318+
/// </example>
3319+
/// </summary>
3320+
/// <param name="opts">The workbook protection options</param>
3321+
/// <exception cref="RuntimeError">Return None if no error occurred,
3322+
/// otherwise raise a RuntimeError with the message.</exception>
3323+
public void ProtectWorkbook(WorkbookProtectionOptions opts)
3324+
{
3325+
var options = (TypesC.WorkbookProtectionOptions)
3326+
Lib.CsToC(opts, new TypesC.WorkbookProtectionOptions());
3327+
string err = Marshal.PtrToStringUTF8(Lib.ProtectWorkbook(FileIdx, ref options));
3328+
if (!string.IsNullOrEmpty(err))
3329+
throw new RuntimeError(err);
3330+
}
3331+
32353332
/// <summary>
32363333
/// Override the spreadsheet with origin path.
32373334
/// </summary>

Excelize/TypesC.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,58 @@ public unsafe struct SheetPropsOptions
667667
public bool* ThickBottom;
668668
}
669669

670+
[StructLayout(LayoutKind.Sequential)]
671+
public unsafe struct SheetProtectionOptions
672+
{
673+
public sbyte* AlgorithmName;
674+
675+
[MarshalAs(UnmanagedType.I1)]
676+
public bool AutoFilter;
677+
678+
[MarshalAs(UnmanagedType.I1)]
679+
public bool DeleteColumns;
680+
681+
[MarshalAs(UnmanagedType.I1)]
682+
public bool DeleteRows;
683+
684+
[MarshalAs(UnmanagedType.I1)]
685+
public bool EditObjects;
686+
687+
[MarshalAs(UnmanagedType.I1)]
688+
public bool EditScenarios;
689+
690+
[MarshalAs(UnmanagedType.I1)]
691+
public bool FormatCells;
692+
693+
[MarshalAs(UnmanagedType.I1)]
694+
public bool FormatColumns;
695+
696+
[MarshalAs(UnmanagedType.I1)]
697+
public bool FormatRows;
698+
699+
[MarshalAs(UnmanagedType.I1)]
700+
public bool InsertColumns;
701+
702+
[MarshalAs(UnmanagedType.I1)]
703+
public bool InsertHyperlinks;
704+
705+
[MarshalAs(UnmanagedType.I1)]
706+
public bool InsertRows;
707+
public sbyte* Password;
708+
709+
[MarshalAs(UnmanagedType.I1)]
710+
public bool PivotTables;
711+
712+
[MarshalAs(UnmanagedType.I1)]
713+
public bool SelectLockedCells;
714+
715+
[MarshalAs(UnmanagedType.I1)]
716+
public bool SelectUnlockedCells;
717+
718+
[MarshalAs(UnmanagedType.I1)]
719+
public bool Sort;
720+
}
721+
670722
[StructLayout(LayoutKind.Sequential)]
671723
public unsafe struct SlicerOptions
672724
{
@@ -781,6 +833,19 @@ public unsafe struct WorkbookPropsOptions
781833
public sbyte** CodeName;
782834
}
783835

836+
[StructLayout(LayoutKind.Sequential)]
837+
public unsafe struct WorkbookProtectionOptions
838+
{
839+
public sbyte* AlgorithmName;
840+
public sbyte* Password;
841+
842+
[MarshalAs(UnmanagedType.I1)]
843+
public bool LockStructure;
844+
845+
[MarshalAs(UnmanagedType.I1)]
846+
public bool LockWindows;
847+
}
848+
784849
[StructLayout(LayoutKind.Sequential)]
785850
public unsafe struct StringErrorResult
786851
{

Excelize/TypesCs.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,31 @@ public struct SheetPropsOptions
835835
public bool? ThickBottom;
836836
}
837837

838+
/// <summary>
839+
/// SheetProtectionOptions directly maps the settings of worksheet
840+
/// protection.
841+
/// </summary>
842+
public struct SheetProtectionOptions
843+
{
844+
public string AlgorithmName;
845+
public bool AutoFilter;
846+
public bool DeleteColumns;
847+
public bool DeleteRows;
848+
public bool EditObjects;
849+
public bool EditScenarios;
850+
public bool FormatCells;
851+
public bool FormatColumns;
852+
public bool FormatRows;
853+
public bool InsertColumns;
854+
public bool InsertHyperlinks;
855+
public bool InsertRows;
856+
public string Password;
857+
public bool PivotTables;
858+
public bool SelectLockedCells;
859+
public bool SelectUnlockedCells;
860+
public bool Sort;
861+
}
862+
838863
/// <summary>
839864
/// Represents the settings of the slicer.
840865
/// </summary>
@@ -956,6 +981,18 @@ public struct WorkbookPropsOptions
956981
public string? CodeName;
957982
}
958983

984+
/// <summary>
985+
/// WorkbookProtectionOptions directly maps the settings of workbook
986+
/// protection.
987+
/// </summary>
988+
public struct WorkbookProtectionOptions
989+
{
990+
public string AlgorithmName;
991+
public string Password;
992+
public bool LockStructure;
993+
public bool LockWindows;
994+
}
995+
959996
public struct Cells
960997
{
961998
public string[]? Cell;

0 commit comments

Comments
 (0)