Skip to content

Commit 67b80fa

Browse files
committed
Add 2 new functions: SetColVisible and SetColWidth
- Fix SetColStyle function does not work - Update unit test
1 parent b01d492 commit 67b80fa

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

Excelize.Tests/UnitTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,8 @@ public void TestStyle()
11881188
{
11891189
f.SetCellStyle("Sheet1", "A1", "B2", styleId);
11901190
f.SetColStyle("Sheet1", "H", styleId);
1191+
f.SetColVisible("Sheet1", "D:F", false);
1192+
f.SetColWidth("Sheet1", "A", "A", 44.5);
11911193
f.UpdateLinkedValue();
11921194
f.SetCellInt("Sheet1", "A1", 100);
11931195
f.SetCellBool("Sheet1", "A11", true);
@@ -1358,6 +1360,10 @@ public void TestStyle()
13581360
Assert.Equal(expected, err.Message);
13591361
err = Assert.Throws<RuntimeError>(() => f.SetColStyle("Sheet1", "H", 1));
13601362
Assert.Equal(expected, err.Message);
1363+
err = Assert.Throws<RuntimeError>(() => f.SetColVisible("Sheet1", "H", false));
1364+
Assert.Equal(expected, err.Message);
1365+
err = Assert.Throws<RuntimeError>(() => f.SetColWidth("Sheet1", "A", "H", 20));
1366+
Assert.Equal(expected, err.Message);
13611367
err = Assert.Throws<RuntimeError>(() => f.SetSheetName("Sheet1", "Sheet2"));
13621368
Assert.Equal(expected, err.Message);
13631369
err = Assert.Throws<RuntimeError>(() =>

Excelize/Excelize.cs

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,23 @@ internal static extern IntPtr SetColStyle(
488488
long styleID
489489
);
490490

491+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
492+
internal static extern IntPtr SetColVisible(
493+
long fileIdx,
494+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
495+
[MarshalAs(UnmanagedType.LPUTF8Str)] string columns,
496+
bool visible
497+
);
498+
499+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
500+
internal static extern IntPtr SetColWidth(
501+
long fileIdx,
502+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
503+
[MarshalAs(UnmanagedType.LPUTF8Str)] string startCol,
504+
[MarshalAs(UnmanagedType.LPUTF8Str)] string endCol,
505+
double width
506+
);
507+
491508
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
492509
internal static extern IntPtr SetHeaderFooter(
493510
long fileIdx,
@@ -3664,7 +3681,7 @@ public void SetCellInt(string sheet, string cell, long value)
36643681
/// new RichTextRun
36653682
/// {
36663683
/// Text = " and ",
3667-
/// Font = new Font
3684+
/// Font = new Font
36683685
/// {
36693686
/// Family = "Times New Roman",
36703687
/// },
@@ -3693,7 +3710,7 @@ public void SetCellInt(string sheet, string cell, long value)
36933710
/// new RichTextRun
36943711
/// {
36953712
/// Text = "\r\nlarge text with ",
3696-
/// Font = new Font
3713+
/// Font = new Font
36973714
/// {
36983715
/// Size = 14,
36993716
/// Color = "AD23E8",
@@ -3702,7 +3719,7 @@ public void SetCellInt(string sheet, string cell, long value)
37023719
/// new RichTextRun
37033720
/// {
37043721
/// Text = "strike",
3705-
/// Font = new Font
3722+
/// Font = new Font
37063723
/// {
37073724
/// Color = "E89923",
37083725
/// Strike = true,
@@ -3711,7 +3728,7 @@ public void SetCellInt(string sheet, string cell, long value)
37113728
/// new RichTextRun
37123729
/// {
37133730
/// Text = " superscript",
3714-
/// Font = new Font
3731+
/// Font = new Font
37153732
/// {
37163733
/// Color = "DBC21F",
37173734
/// VertAlign = "superscript",
@@ -3748,7 +3765,7 @@ public void SetCellInt(string sheet, string cell, long value)
37483765
/// }
37493766
/// );
37503767
/// int style = f.NewStyle(
3751-
/// new Style
3768+
/// new Style
37523769
/// {
37533770
/// Alignment = new Alignment { WrapText = true },
37543771
/// }
@@ -3914,9 +3931,81 @@ public void SetColOutlineLevel(string sheet, string col, int level)
39143931
/// <exception cref="RuntimeError">Return None if no error occurred,
39153932
/// otherwise raise a RuntimeError with the message.</exception>
39163933
public void SetColStyle(string sheet, string columns, int styleID)
3934+
{
3935+
string err = Marshal.PtrToStringUTF8(Lib.SetColStyle(FileIdx, sheet, columns, styleID));
3936+
if (!string.IsNullOrEmpty(err))
3937+
throw new RuntimeError(err);
3938+
}
3939+
3940+
/// <summary>
3941+
/// SetColVisible provides a function to set visible columns by given
3942+
/// worksheet name, columns range and visibility.
3943+
/// <example>
3944+
/// For example hide column D on Sheet1:
3945+
/// <code><![CDATA[
3946+
/// try
3947+
/// {
3948+
/// f.SetColVisible("Sheet1", "D", false);
3949+
/// }
3950+
/// catch (RuntimeError err)
3951+
/// {
3952+
/// Console.WriteLine(err.Message);
3953+
/// }]]>
3954+
/// </code>
3955+
/// Hide the columns from D to F (included):
3956+
/// <code><![CDATA[
3957+
/// try
3958+
/// {
3959+
/// f.SetColVisible("Sheet1", "D:F", false);
3960+
/// }
3961+
/// catch (RuntimeError err)
3962+
/// {
3963+
/// Console.WriteLine(err.Message);
3964+
/// }]]>
3965+
/// </code>
3966+
/// </example>
3967+
/// </summary>
3968+
/// <param name="sheet">The worksheet name</param>
3969+
/// <param name="columns">The columns range</param>
3970+
/// <param name="visible">The column visible</param>
3971+
/// <exception cref="RuntimeError">Return None if no error occurred,
3972+
/// otherwise raise a RuntimeError with the message.</exception>
3973+
public void SetColVisible(string sheet, string columns, bool visible)
3974+
{
3975+
string err = Marshal.PtrToStringUTF8(
3976+
Lib.SetColVisible(FileIdx, sheet, columns, visible)
3977+
);
3978+
if (!string.IsNullOrEmpty(err))
3979+
throw new RuntimeError(err);
3980+
}
3981+
3982+
/// <summary>
3983+
/// SetColVisible provides a function to set visible columns by given
3984+
/// worksheet name, columns range and visibility.
3985+
/// <example>
3986+
/// For example set column width for column A to H on Sheet1:
3987+
/// <code><![CDATA[
3988+
/// try
3989+
/// {
3990+
/// f.SetColWidth("Sheet1", "A", "H", 20);
3991+
/// }
3992+
/// catch (RuntimeError err)
3993+
/// {
3994+
/// Console.WriteLine(err.Message);
3995+
/// }]]>
3996+
/// </code>
3997+
/// </example>
3998+
/// </summary>
3999+
/// <param name="sheet">The worksheet name</param>
4000+
/// <param name="startCol">The start column name</param>
4001+
/// <param name="endCol">The end column name</param>
4002+
/// <param name="width">The column width</param>
4003+
/// <exception cref="RuntimeError">Return None if no error occurred,
4004+
/// otherwise raise a RuntimeError with the message.</exception>
4005+
public void SetColWidth(string sheet, string startCol, string endCol, double width)
39174006
{
39184007
string err = Marshal.PtrToStringUTF8(
3919-
Lib.SetColOutlineLevel(FileIdx, sheet, columns, styleID)
4008+
Lib.SetColWidth(FileIdx, sheet, startCol, endCol, width)
39204009
);
39214010
if (!string.IsNullOrEmpty(err))
39224011
throw new RuntimeError(err);

0 commit comments

Comments
 (0)