Skip to content

Commit dc4c3a9

Browse files
committed
Add new function: AddSlicer
- Update unit test and fix AddShape test case failed
1 parent 592a6e0 commit dc4c3a9

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

Excelize.Tests/UnitTest.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,9 @@ public void TestAddShape()
550550
);
551551
})
552552
);
553-
RuntimeError err = Assert.Throws<RuntimeError>(() => f.AddShape("SheetN", new Shape { }));
553+
RuntimeError err = Assert.Throws<RuntimeError>(() =>
554+
f.AddShape("SheetN", new Shape { Cell = "G6", Type = "rect" })
555+
);
554556
Assert.Equal("sheet SheetN does not exist", err.Message);
555557
Assert.Null(
556558
Record.Exception(() =>
@@ -710,6 +712,7 @@ public void TestPivotTable()
710712
{
711713
DataRange = "Sheet1!A1:E31",
712714
PivotTableRange = "Sheet1!G2:M34",
715+
Name = "PivotTable1",
713716
Rows = new PivotTableField[]
714717
{
715718
new() { Data = "Month", DefaultSubtotal = true },
@@ -737,6 +740,17 @@ public void TestPivotTable()
737740
ShowLastColumn = true,
738741
}
739742
);
743+
f.AddSlicer(
744+
"Sheet1",
745+
new SlicerOptions
746+
{
747+
Name = "Month",
748+
Cell = "O2",
749+
TableSheet = "Sheet1",
750+
TableName = "PivotTable1",
751+
Caption = "Month",
752+
}
753+
);
740754
})
741755
);
742756
RuntimeError err = Assert.Throws<RuntimeError>(() =>
@@ -746,6 +760,22 @@ public void TestPivotTable()
746760
"parameter 'PivotTableRange' parsing error: parameter is required",
747761
err.Message
748762
);
763+
764+
err = Assert.Throws<RuntimeError>(() =>
765+
f.AddSlicer(
766+
"SheetN",
767+
new SlicerOptions
768+
{
769+
Name = "Year",
770+
Cell = "S2",
771+
TableSheet = "Sheet1",
772+
TableName = "PivotTable1",
773+
Caption = "Year",
774+
}
775+
)
776+
);
777+
Assert.Equal("sheet SheetN does not exist", err.Message);
778+
749779
Assert.Null(
750780
Record.Exception(() =>
751781
{
@@ -755,6 +785,16 @@ public void TestPivotTable()
755785
Assert.Empty(f.Close());
756786
}
757787

788+
[Fact]
789+
public void TestAddSlicer()
790+
{
791+
File f = Excelize.NewFile();
792+
RuntimeError err = Assert.Throws<RuntimeError>(() =>
793+
f.AddSlicer("Sheet1", new SlicerOptions { })
794+
);
795+
Assert.Equal("parameter is invalid", err.Message);
796+
}
797+
758798
[StructLayout(LayoutKind.Sequential)]
759799
public unsafe struct C1
760800
{

Excelize/Excelize.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ internal static extern IntPtr AddShape(
142142
ref TypesC.Shape opts
143143
);
144144

145+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
146+
internal static extern IntPtr AddSlicer(
147+
long fileIdx,
148+
[MarshalAs(UnmanagedType.LPUTF8Str)] string sheet,
149+
ref TypesC.SlicerOptions opts
150+
);
151+
145152
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
146153
internal static extern IntPtr AddVBAProject(long fileIdx, byte[] b, int bLen);
147154

@@ -1963,6 +1970,41 @@ public void AddShape(string sheet, Shape options)
19631970
throw new RuntimeError(err);
19641971
}
19651972

1973+
/// <summary>
1974+
/// AddSlicer function inserts a slicer by giving the worksheet name and
1975+
/// slicer settings.
1976+
/// <example>
1977+
/// For example, insert a slicer on the <c>Sheet1!E1</c> with field
1978+
/// <c>Column1</c> for the table named <c>Table1</c>:
1979+
/// <code>
1980+
/// f.AddSlicer(
1981+
/// "Sheet1",
1982+
/// new SlicerOptions
1983+
/// {
1984+
/// Name = "Column1",
1985+
/// Cell = "E1",
1986+
/// TableSheet = "Sheet1",
1987+
/// TableName = "Table1",
1988+
/// Caption = "Column1",
1989+
/// Width = 200,
1990+
/// Height = 200,
1991+
/// }
1992+
/// );
1993+
/// </code>
1994+
/// </example>
1995+
/// </summary>
1996+
/// <param name="sheet">The worksheet name</param>
1997+
/// <param name="options">The slicer options</param>
1998+
/// <exception cref="RuntimeError">Return None if no error occurred,
1999+
/// otherwise raise a RuntimeError with the message.</exception>
2000+
public void AddSlicer(string sheet, SlicerOptions options)
2001+
{
2002+
var opts = (TypesC.SlicerOptions)Lib.CsToC(options, new TypesC.SlicerOptions());
2003+
string err = Marshal.PtrToStringUTF8(Lib.AddSlicer(FileIdx, sheet, ref opts));
2004+
if (!string.IsNullOrEmpty(err))
2005+
throw new RuntimeError(err);
2006+
}
2007+
19662008
/// <summary>
19672009
/// Add vbaProject.bin file which contains functions and/or macros. The
19682010
/// file extension should be XLSM or XLTM.

Excelize/TypesC.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,24 @@ public unsafe struct SheetPropsOptions
533533
public bool* ThickBottom;
534534
}
535535

536+
[StructLayout(LayoutKind.Sequential)]
537+
public unsafe struct SlicerOptions
538+
{
539+
public sbyte* Name;
540+
public sbyte* Cell;
541+
public sbyte* TableSheet;
542+
public sbyte* TableName;
543+
public sbyte* Caption;
544+
public sbyte* Macro;
545+
public uint Width;
546+
public uint Height;
547+
public bool* DisplayHeader;
548+
549+
[MarshalAs(UnmanagedType.I1)]
550+
public bool ItemDesc;
551+
public GraphicOptions Format;
552+
}
553+
536554
[StructLayout(LayoutKind.Sequential)]
537555
public unsafe struct StringErrorResult
538556
{

Excelize/TypesCs.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,51 @@ public struct SheetPropsOptions
683683
public bool? ThickBottom;
684684
}
685685

686+
/// <summary>
687+
/// Represents the settings of the slicer.
688+
/// </summary>
689+
/// <remarks>
690+
/// <para><b>Name</b> specifies the slicer name and should be an existing
691+
/// field name of the given table or pivot table. This setting is
692+
/// required.</para>
693+
/// <para><b>Cell</b> specifies the top-left cell coordinates for inserting
694+
/// the slicer. This setting is required.</para>
695+
/// <para><b>TableSheet</b> specifies the worksheet name of the table or
696+
/// pivot table. This setting is required.</para>
697+
/// <para><b>TableName</b> specifies the name of the table or pivot table.
698+
/// This setting is required.</para>
699+
/// <para><b>Caption</b> specifies the caption of the slicer. This setting
700+
/// is optional.</para>
701+
/// <para><b>Macro</b> specifies the macro to assign to the slicer. The
702+
/// workbook extension should be XLSM or XLTM.</para>
703+
/// <para><b>Width</b> specifies the width of the slicer. This setting is
704+
/// optional.</para>
705+
/// <para><b>Height</b> specifies the height of the slicer. This setting is
706+
/// optional.</para>
707+
/// <para><b>DisplayHeader</b> specifies whether the slicer header is
708+
/// displayed. This setting is optional; the default is to display
709+
/// it.</para>
710+
/// <para><b>ItemDesc</b> specifies whether to sort items in descending
711+
/// (Z-A) order. This setting is optional; the default is false
712+
/// (ascending).</para>
713+
/// <para><b>Format</b> specifies the format of the slicer. This setting is
714+
/// optional.</para>
715+
/// </remarks>
716+
public struct SlicerOptions
717+
{
718+
public string Name;
719+
public string Cell;
720+
public string TableSheet;
721+
public string TableName;
722+
public string Caption;
723+
public string Macro;
724+
public uint Width;
725+
public uint Height;
726+
public bool? DisplayHeader;
727+
public bool ItemDesc;
728+
public GraphicOptions Format;
729+
}
730+
686731
public struct Row
687732
{
688733
public string[]? Cell;

0 commit comments

Comments
 (0)