Skip to content

Commit 0f89f75

Browse files
committed
Add new function: AddPivotTable
- Bump v0.0.4 - Fix example code block can not display in doc blocks - Update unit test - Using no-redirect image URL to fix image can not display in NuGet package home page
1 parent 4a3bb64 commit 0f89f75

File tree

10 files changed

+453
-67
lines changed

10 files changed

+453
-67
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ body:
5656
label: Excelize version or commit ID
5757
description: |
5858
Which version of Excelize are you using?
59-
placeholder: e.g. 0.0.3
59+
placeholder: e.g. 0.0.4
6060
validations:
6161
required: true
6262

Excelize.Tests/UnitTest.cs

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright 2025 The excelize Authors. All rights reserved. Use of this source
2-
// code is governed by a BSD-style license that can be found in the LICENSE
3-
// file.
1+
// Copyright 2025 - 2026 The excelize Authors. All rights reserved. Use of this
2+
// source code is governed by a BSD-style license that can be found in the
3+
// LICENSE file.
44
//
55
// Package excelize-cs is a C# port of Go Excelize library, providing a set of
66
// functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM /
@@ -470,6 +470,113 @@ public void TestOpenFile()
470470
Assert.Equal("zip: not a valid zip file", err.Message);
471471
}
472472

473+
[Fact]
474+
public void TestPivotTable()
475+
{
476+
File f = Excelize.NewFile();
477+
List<string> months = new List<string>
478+
{
479+
"Jan",
480+
"Feb",
481+
"Mar",
482+
"Apr",
483+
"May",
484+
"Jun",
485+
"Jul",
486+
"Aug",
487+
"Sep",
488+
"Oct",
489+
"Nov",
490+
"Dec",
491+
};
492+
List<int> year = new List<int> { 2017, 2018, 2019 };
493+
List<string> types = new List<string> { "Meat", "Dairy", "Beverages", "Produce" };
494+
List<string> region = new List<string> { "East", "West", "North", "South" };
495+
Random random = new Random();
496+
Assert.Null(
497+
Record.Exception(() =>
498+
{
499+
f.SetSheetRow(
500+
"Sheet1",
501+
"A1",
502+
new List<object> { "Month", "Year", "Type", "Sales", "Region" }
503+
);
504+
for (int row = 2; row < 32; row++)
505+
{
506+
f.SetCellValue(
507+
"Sheet1",
508+
Excelize.CoordinatesToCellName(1, row),
509+
months[random.Next(12)]
510+
);
511+
f.SetCellValue(
512+
"Sheet1",
513+
Excelize.CoordinatesToCellName(2, row),
514+
year[random.Next(3)]
515+
);
516+
f.SetCellValue(
517+
"Sheet1",
518+
Excelize.CoordinatesToCellName(3, row),
519+
types[random.Next(4)]
520+
);
521+
f.SetCellValue(
522+
"Sheet1",
523+
Excelize.CoordinatesToCellName(4, row),
524+
random.Next(5000)
525+
);
526+
f.SetCellValue(
527+
"Sheet1",
528+
Excelize.CoordinatesToCellName(5, row),
529+
region[random.Next(4)]
530+
);
531+
}
532+
f.AddPivotTable(
533+
new PivotTableOptions
534+
{
535+
DataRange = "Sheet1!A1:E31",
536+
PivotTableRange = "Sheet1!G2:M34",
537+
Rows = new PivotTableField[]
538+
{
539+
new() { Data = "Month", DefaultSubtotal = true },
540+
new() { Data = "Year" },
541+
},
542+
Filter = new PivotTableField[] { new() { Data = "Region" } },
543+
Columns = new PivotTableField[]
544+
{
545+
new() { Data = "Type", DefaultSubtotal = true },
546+
},
547+
Data = new PivotTableField[]
548+
{
549+
new()
550+
{
551+
Data = "Sales",
552+
Name = "Summarize",
553+
Subtotal = "sum",
554+
},
555+
},
556+
RowGrandTotals = true,
557+
ColGrandTotals = true,
558+
ShowDrill = true,
559+
ShowRowHeaders = true,
560+
ShowColHeaders = true,
561+
ShowLastColumn = true,
562+
}
563+
);
564+
})
565+
);
566+
var err = Assert.Throws<RuntimeError>(() => f.AddPivotTable(new PivotTableOptions { }));
567+
Assert.Equal(
568+
"parameter 'PivotTableRange' parsing error: parameter is required",
569+
err.Message
570+
);
571+
Assert.Null(
572+
Record.Exception(() =>
573+
{
574+
f.SaveAs("TestAddPivotTable.xlsx");
575+
})
576+
);
577+
Assert.Empty(f.Close());
578+
}
579+
473580
[StructLayout(LayoutKind.Sequential)]
474581
public unsafe struct C1
475582
{

0 commit comments

Comments
 (0)