Skip to content

Commit 1e36909

Browse files
committed
Add new function: AddTable
- Update unit test
1 parent afc7240 commit 1e36909

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

Excelize.Tests/UnitTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,61 @@ public void TestPivotTable()
789789
public void TestAddSlicer()
790790
{
791791
File f = Excelize.NewFile();
792+
Assert.Null(
793+
Record.Exception(() =>
794+
{
795+
f.NewSheet("Sheet2");
796+
f.AddTable("Sheet1", new Table { Name = "Table1", Range = "A1:D5" });
797+
f.AddTable(
798+
"Sheet2",
799+
new Table
800+
{
801+
Range = "F2:H6",
802+
Name = "table",
803+
StyleName = "TableStyleMedium2",
804+
ShowFirstColumn = true,
805+
ShowLastColumn = true,
806+
ShowRowStripes = false,
807+
ShowColumnStripes = true,
808+
}
809+
);
810+
f.AddSlicer(
811+
"Sheet1",
812+
new SlicerOptions
813+
{
814+
Name = "Column1",
815+
Cell = "E1",
816+
TableSheet = "Sheet1",
817+
TableName = "Table1",
818+
Caption = "Column1",
819+
}
820+
);
821+
f.AddSlicer(
822+
"Sheet1",
823+
new SlicerOptions
824+
{
825+
Name = "Column1",
826+
Cell = "I1",
827+
TableSheet = "Sheet2",
828+
TableName = "table",
829+
Caption = "Column1",
830+
}
831+
);
832+
})
833+
);
792834
RuntimeError err = Assert.Throws<RuntimeError>(() =>
793835
f.AddSlicer("Sheet1", new SlicerOptions { })
794836
);
795837
Assert.Equal("parameter is invalid", err.Message);
838+
err = Assert.Throws<RuntimeError>(() => f.AddTable("Sheet1", new Table { }));
839+
Assert.Equal("parameter is invalid", err.Message);
840+
Assert.Null(
841+
Record.Exception(() =>
842+
{
843+
f.SaveAs("TestAddSlicer.xlsx");
844+
})
845+
);
846+
Assert.Empty(f.Close());
796847
}
797848

798849
[Fact]

Excelize/Excelize.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ internal static extern IntPtr AddSparkline(
156156
ref TypesC.SparklineOptions opts
157157
);
158158

159+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
160+
internal static extern IntPtr AddTable(
161+
long fileIdx,
162+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
163+
ref TypesC.Table opts
164+
);
165+
159166
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
160167
internal static extern IntPtr AddVBAProject(long fileIdx, byte[] b, int bLen);
161168

@@ -2109,6 +2116,61 @@ public void AddSparkline(string sheet, SparklineOptions options)
21092116
throw new RuntimeError(err);
21102117
}
21112118

2119+
/// <summary>
2120+
/// Adds a table to a worksheet using the specified worksheet name,
2121+
/// range reference, and format settings.
2122+
/// <example>
2123+
/// Create a table on Sheet1 for the range A1:D5:
2124+
/// <code>
2125+
/// f.AddTable("Sheet1", new Table { Range = "A1:D5" });
2126+
/// </code>
2127+
/// Create a table on Sheet2 for the range F2:H6 with formatting:
2128+
/// <code>
2129+
/// f.AddTable(
2130+
/// "Sheet2",
2131+
/// new Table
2132+
/// {
2133+
/// Range = "F2:H6",
2134+
/// Name = "table",
2135+
/// StyleName = "TableStyleMedium2",
2136+
/// ShowFirstColumn = true,
2137+
/// ShowLastColumn = true,
2138+
/// ShowRowStripes = false,
2139+
/// ShowColumnStripes = true,
2140+
/// }
2141+
/// );
2142+
/// </code>
2143+
/// </example>
2144+
/// </summary>
2145+
/// <remarks>
2146+
/// The table must include at least two rows, including a header row.
2147+
/// Header cells must contain unique string values.
2148+
/// Set the header row data before calling <c>AddTable</c>.
2149+
/// Table ranges within the same worksheet must not intersect.
2150+
///
2151+
/// <para><b>Name:</b> The name of the table must be unique within the
2152+
/// worksheet, begin with a letter or underscore, contain no spaces or
2153+
/// special characters, and be no more than 255 characters long.</para>
2154+
///
2155+
/// <para><b>StyleName:</b> Valid built-in table style names include:</para>
2156+
/// <list type="bullet">
2157+
/// <item><description>TableStyleLight1 – TableStyleLight21</description></item>
2158+
/// <item><description>TableStyleMedium1 – TableStyleMedium28</description></item>
2159+
/// <item><description>TableStyleDark1 – TableStyleDark11</description></item>
2160+
/// </list>
2161+
/// </remarks>
2162+
/// <param name="sheet">The worksheet name</param>
2163+
/// <param name="options">The table options</param>
2164+
/// <exception cref="RuntimeError">Return None if no error occurred,
2165+
/// otherwise raise a RuntimeError with the message.</exception>
2166+
public void AddTable(string sheet, Table options)
2167+
{
2168+
var opts = (TypesC.Table)Lib.CsToC(options, new TypesC.Table());
2169+
string err = Marshal.PtrToStringUTF8(Lib.AddTable(FileIdx, sheet, ref opts));
2170+
if (!string.IsNullOrEmpty(err))
2171+
throw new RuntimeError(err);
2172+
}
2173+
21122174
/// <summary>
21132175
/// Add vbaProject.bin file which contains functions and/or macros. The
21142176
/// file extension should be XLSM or XLTM.

Excelize/TypesC.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,25 @@ public unsafe struct SparklineOptions
605605
public sbyte* EmptyCells;
606606
}
607607

608+
[StructLayout(LayoutKind.Sequential)]
609+
public unsafe struct Table
610+
{
611+
public sbyte* Range;
612+
public sbyte* Name;
613+
public sbyte* StyleName;
614+
615+
[MarshalAs(UnmanagedType.I1)]
616+
public bool ShowColumnStripes;
617+
618+
[MarshalAs(UnmanagedType.I1)]
619+
public bool ShowFirstColumn;
620+
public bool* ShowHeaderRow;
621+
622+
[MarshalAs(UnmanagedType.I1)]
623+
public bool ShowLastColumn;
624+
public bool* ShowRowStripes;
625+
}
626+
608627
[StructLayout(LayoutKind.Sequential)]
609628
public unsafe struct StringErrorResult
610629
{

Excelize/TypesCs.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,21 @@ public struct SparklineOptions
762762
public string EmptyCells;
763763
}
764764

765+
/// <summary>
766+
/// Table directly maps the format settings of the table.
767+
/// </summary>
768+
public struct Table
769+
{
770+
public string Range;
771+
public string Name;
772+
public string StyleName;
773+
public bool ShowColumnStripes;
774+
public bool ShowFirstColumn;
775+
public bool? ShowHeaderRow;
776+
public bool ShowLastColumn;
777+
public bool? ShowRowStripes;
778+
}
779+
765780
public struct Row
766781
{
767782
public string[]? Cell;

0 commit comments

Comments
 (0)