Skip to content

Commit de5421e

Browse files
authored
Add new function: DeleteDataValidation (#12)
- Update unit tests
1 parent 26b929a commit de5421e

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Excelize.Tests/UnitTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,18 @@ public void TestCustomProps()
830830
Assert.Empty(f.Close());
831831
}
832832

833+
[Fact]
834+
public void TestDeleteDataValidation()
835+
{
836+
File f = Excelize.NewFile();
837+
Assert.Null(Record.Exception(() => f.DeleteDataValidation("Sheet1")));
838+
Assert.Null(Record.Exception(() => f.DeleteDataValidation("Sheet1", "A1:B2")));
839+
Assert.Null(Record.Exception(() => f.DeleteDataValidation("Sheet1", "A1:B2", "D3")));
840+
RuntimeError err = Assert.Throws<RuntimeError>(() => f.DeleteDataValidation("SheetN"));
841+
Assert.Equal("sheet SheetN does not exist", err.Message);
842+
Assert.Empty(f.Close());
843+
}
844+
833845
[Fact]
834846
public void TestDllImportResolver()
835847
{

Excelize/Excelize.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ internal static extern IntPtr DeleteComment(
256256
[MarshalAs(UnmanagedType.LPUTF8Str)] string cell
257257
);
258258

259+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
260+
internal static extern IntPtr DeleteDataValidation(
261+
long fileIdx,
262+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
263+
[In] IntPtr[] sqref,
264+
long length
265+
);
266+
259267
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
260268
internal static extern IntPtr DeleteFormControl(
261269
long fileIdx,
@@ -3633,6 +3641,90 @@ public void DeleteComment(string sheet, string cell)
36333641
throw new RuntimeError(err);
36343642
}
36353643

3644+
/// <summary>
3645+
/// Delete data validation by given worksheet name and reference
3646+
/// sequence. All data validations in the worksheet will be deleted if
3647+
/// not specify reference sequence parameter.
3648+
/// <example>
3649+
/// Example 1, delete data validation on <c>Sheet1!A1:B2</c>:
3650+
/// <code>
3651+
/// try
3652+
/// {
3653+
/// f.DeleteDataValidation("Sheet1", "A1:B2");
3654+
/// }
3655+
/// catch (RuntimeError err)
3656+
/// {
3657+
/// Console.WriteLine(err.Message);
3658+
/// }
3659+
/// </code>
3660+
/// Example 2, delete data validations on Sheet1 with multiple cell
3661+
/// ranges <c>A1:B2</c> and <c>C1:C3</c> with reference sequence slice:
3662+
/// <code>
3663+
/// try
3664+
/// {
3665+
/// f.DeleteDataValidation("Sheet1", "A1:B2", "C1:C3");
3666+
/// }
3667+
/// catch (RuntimeError err)
3668+
/// {
3669+
/// Console.WriteLine(err.Message);
3670+
/// }
3671+
/// </code>
3672+
/// Example 3, delete data validations on Sheet1 with multiple cell
3673+
/// ranges <c>A1:B2</c> and <c>C1:C3</c> with blank separated reference
3674+
/// sequence string, the result same as example 2:
3675+
/// <code>
3676+
/// try
3677+
/// {
3678+
/// f.DeleteDataValidation("Sheet1", "A1:B2 C1:C3");
3679+
/// }
3680+
/// catch (RuntimeError err)
3681+
/// {
3682+
/// Console.WriteLine(err.Message);
3683+
/// }
3684+
/// </code>
3685+
/// Example 4, delete all data validations on Sheet1:
3686+
/// <code>
3687+
/// try
3688+
/// {
3689+
/// f.DeleteDataValidation("Sheet1");
3690+
/// }
3691+
/// catch (RuntimeError err)
3692+
/// {
3693+
/// Console.WriteLine(err.Message);
3694+
/// }
3695+
/// </code>
3696+
/// </example>
3697+
/// </summary>
3698+
/// <param name="sheet">The worksheet name</param>
3699+
/// <param name="sqref">The reference sequence</param>
3700+
/// <exception cref="RuntimeError">Return None if no error occurred,
3701+
/// otherwise raise a RuntimeError with the message.</exception>
3702+
public void DeleteDataValidation(string sheet, params string[] sqref)
3703+
{
3704+
var l = 0;
3705+
if (sqref != null)
3706+
{
3707+
l = sqref.Length;
3708+
}
3709+
var ptrs = new IntPtr[l];
3710+
try
3711+
{
3712+
for (int i = 0; i < l; i++)
3713+
ptrs[i] = Marshal.StringToCoTaskMemUTF8(sqref[i]);
3714+
string err = Marshal.PtrToStringUTF8(
3715+
Lib.DeleteDataValidation(FileIdx, sheet, ptrs, l)
3716+
);
3717+
if (!string.IsNullOrEmpty(err))
3718+
throw new RuntimeError(err);
3719+
}
3720+
finally
3721+
{
3722+
foreach (var ptr in ptrs)
3723+
if (ptr != IntPtr.Zero)
3724+
Marshal.FreeCoTaskMem(ptr);
3725+
}
3726+
}
3727+
36363728
/// <summary>
36373729
/// DeleteFormControl provides the method to delete form control in a
36383730
/// worksheet by given worksheet name and cell reference.

0 commit comments

Comments
 (0)