Skip to content

Commit 6fceb2f

Browse files
Add new function: UnmergeCell (#13)
- Update unit test
1 parent de5421e commit 6fceb2f

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Excelize.Tests/UnitTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,14 @@ public void TestMergeCells()
977977
);
978978
RuntimeError err = Assert.Throws<RuntimeError>(() => f.MergeCell("SheetN", "A1", "B2"));
979979
Assert.Equal("sheet SheetN does not exist", err.Message);
980+
Assert.Null(
981+
Record.Exception(() =>
982+
{
983+
f.UnmergeCell("Sheet1", "D3", "E9");
984+
})
985+
);
986+
err = Assert.Throws<RuntimeError>(() => f.UnmergeCell("SheetN", "A1", "B2"));
987+
Assert.Equal("sheet SheetN does not exist", err.Message);
980988
Assert.Null(Record.Exception(() => f.SaveAs("TestMergeCells.xlsx")));
981989
Assert.Empty(f.Close());
982990
}

Excelize/Excelize.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,14 @@ internal static extern IntPtr MergeCell(
421421
[MarshalAs(UnmanagedType.LPUTF8Str)] string bottomRightCell
422422
);
423423

424+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
425+
internal static extern IntPtr UnmergeCell(
426+
long fileIdx,
427+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
428+
[MarshalAs(UnmanagedType.LPUTF8Str)] string topLeftCell,
429+
[MarshalAs(UnmanagedType.LPUTF8Str)] string bottomRightCell
430+
);
431+
424432
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
425433
internal static extern IntPtr MoveSheet(
426434
long fileIdx,
@@ -6338,6 +6346,37 @@ public void UngroupSheets()
63386346
throw new RuntimeError(err);
63396347
}
63406348

6349+
/// <summary>
6350+
/// UnmergeCell provides a function to unmerge a given range reference.
6351+
/// <example>
6352+
/// For example unmerge range reference D3:E9 on Sheet1:
6353+
/// <code>
6354+
/// try
6355+
/// {
6356+
/// f.UnmergeCell("Sheet1", "D3", "E9");
6357+
/// }
6358+
/// catch (RuntimeError err)
6359+
/// {
6360+
/// Console.WriteLine(err.Message);
6361+
/// }
6362+
/// </code>
6363+
/// </example>
6364+
/// </summary>
6365+
/// <param name="sheet">The worksheet name</param>
6366+
/// <param name="topLeftCell">The top-left cell reference</param>
6367+
/// <param name="bottomRightCell">The bottom-right cell reference</param>
6368+
/// <remarks>Attention: overlapped range will also be unmerged</remarks>
6369+
/// <exception cref="RuntimeError">Return None if no error occurred,
6370+
/// otherwise raise a RuntimeError with the message.</exception>
6371+
public void UnmergeCell(string sheet, string topLeftCell, string bottomRightCell)
6372+
{
6373+
string err = Marshal.PtrToStringUTF8(
6374+
Lib.UnmergeCell(FileIdx, sheet, topLeftCell, bottomRightCell)
6375+
);
6376+
if (!string.IsNullOrEmpty(err))
6377+
throw new RuntimeError(err);
6378+
}
6379+
63416380
/// <summary>
63426381
/// UnsetConditionalFormat provides a function to unset the conditional
63436382
/// format by given worksheet name and range reference.

0 commit comments

Comments
 (0)