Skip to content

Commit 8a957ba

Browse files
authored
Add multiple levels totallabel support (#205)
* Add summary tag with label in group feature * Add summary tag with label in group feature * Add multiple levels totallabel feature * Reformat
1 parent bdc7724 commit 8a957ba

12 files changed

+18
-15
lines changed

ClosedXML.Report/Excel/Subtotal.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Subtotal : IDisposable
1717
private IXLWorksheet _tempSheet;
1818
private readonly List<SubtotalGroup> _groups = new List<SubtotalGroup>();
1919
public string TotalLabel { get; set; } = "Total";
20-
public string GrandLabel { get; set; } = "Grand";
20+
public string GrandLabel { get; set; } = "Grand Total";
2121

2222
public Subtotal(IXLRange range) : this(range, false)
2323
{
@@ -40,7 +40,7 @@ public Subtotal(IXLRange range, bool summaryAbove)
4040

4141
public SubtotalGroup[] Groups => _groups.ToArray();
4242

43-
public SubtotalGroup AddGrandTotal(SummaryFuncTag[] summaries)
43+
public SubtotalGroup AddGrandTotal(GroupTag[] groups, SummaryFuncTag[] summaries)
4444
{
4545
if (Sheet.Row(_range.Row(2).RowNumber()).OutlineLevel == 0)
4646
{
@@ -49,13 +49,13 @@ public SubtotalGroup AddGrandTotal(SummaryFuncTag[] summaries)
4949
{
5050
var rangeAddress = _range.ShiftRows(1).RangeAddress;
5151
_range.InsertRowsAbove(1, true);
52-
gr = CreateGroup(Sheet.Range(rangeAddress), 1, 1, GrandLabel, summaries, false);
52+
gr = CreateGroup(Sheet.Range(rangeAddress), 1, 1, GrandLabel, groups, summaries, false,true);
5353
}
5454
else
5555
{
5656
var rangeAddress = _range.RangeAddress;
5757
_range.InsertRowsBelow(1, true);
58-
gr = CreateGroup(Sheet.Range(rangeAddress), 1, 1, GrandLabel, summaries, false);
58+
gr = CreateGroup(Sheet.Range(rangeAddress), 1, 1, GrandLabel, groups, summaries, false,true);
5959
}
6060
gr.Column = 0;
6161
_groups.Add(gr);
@@ -64,7 +64,7 @@ public SubtotalGroup AddGrandTotal(SummaryFuncTag[] summaries)
6464
else return null;
6565
}
6666

67-
public void GroupBy(int groupBy, SummaryFuncTag[] summaries, bool pageBreaks = false, Func<string, string> getGroupLabel = null)
67+
public void GroupBy(int groupBy, GroupTag[] groups, SummaryFuncTag[] summaries, bool pageBreaks = false, Func<string, string> getGroupLabel = null)
6868
{
6969
_pageBreaks = pageBreaks;
7070
_getGroupLabel = getGroupLabel;
@@ -87,7 +87,7 @@ public void GroupBy(int groupBy, SummaryFuncTag[] summaries, bool pageBreaks = f
8787
foreach (var moveData in grRanges)
8888
{
8989
if (moveData.Type == RangeType.DataRange)
90-
_groups.Add(CreateGroup(Sheet.Range(moveData.TargetAddress), groupBy, level, moveData.GroupTitle, summaries, _pageBreaks));
90+
_groups.Add(CreateGroup(Sheet.Range(moveData.TargetAddress), groupBy, level, moveData.GroupTitle, groups, summaries, _pageBreaks));
9191
}
9292
ArrangePageBreaks(Groups, grRanges);
9393
}
@@ -252,7 +252,7 @@ private void MoveRange(MoveData moveData)
252252
Sheet.Cell(moveData.TargetAddress.FirstAddress).Value = _tempSheet.Range(1, 1, srcRng.RowCount(), srcRng.ColumnCount());
253253
}
254254

255-
private SubtotalGroup CreateGroup(IXLRange groupRng, int groupClmn, int level, string title, SummaryFuncTag[] summaries, bool pageBreaks)
255+
private SubtotalGroup CreateGroup(IXLRange groupRng, int groupClmn, int level, string title, GroupTag[] groups, SummaryFuncTag[] summaries, bool pageBreaks,bool isGrandTotal=false)
256256
{
257257
var firstRow = groupRng.RangeAddress.FirstAddress.RowNumber;
258258
var lastRow = groupRng.RangeAddress.LastAddress.RowNumber;
@@ -272,7 +272,9 @@ private SubtotalGroup CreateGroup(IXLRange groupRng, int groupClmn, int level, s
272272
}
273273

274274
summRow.Clear(XLClearOptions.Contents | XLClearOptions.DataType); //TODO Check if the issue persists (ClosedXML issue 844)
275-
summRow.Cell(groupClmn).Value = _getGroupLabel != null ? _getGroupLabel(title) : title + " "+ TotalLabel;
275+
//TODO: Remove the extra space if we can change the existing gauge files
276+
//summRow.Cell(groupClmn).Value = _getGroupLabel != null ? _getGroupLabel(title) : title + (isGrandTotal?null: (string.IsNullOrWhiteSpace(groups?.Where(x => x.Column == groupClmn).Select(x => x.TotalLabel).FirstOrDefault())?null:" ") + (groups?.Where(x => x.Column == groupClmn).Select(x=>x.TotalLabel).FirstOrDefault()??TotalLabel));
277+
summRow.Cell(groupClmn).Value = _getGroupLabel != null ? _getGroupLabel(title) : title + (isGrandTotal ? null : " " + (groups?.Where(x => x.Column == groupClmn).Select(x => x.TotalLabel).FirstOrDefault() ?? TotalLabel));
276278
Sheet.Row(summRow.RowNumber()).OutlineLevel = level - 1;
277279
foreach (var item in summaries)
278280
{

ClosedXML.Report/Excel/XlExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ public static void Subtotal(this IXLRange range, int groupBy, string function, i
159159
if (replace)
160160
subtotal.Unsubtotal();
161161
var summaries = totalList.Select(x => new SummaryFuncTag {Name=function.ToLower(), Cell = new TemplateCell { Column = x } }).ToArray();
162-
subtotal.AddGrandTotal(summaries);
163-
subtotal.GroupBy(groupBy, summaries, pageBreaks);
162+
subtotal.AddGrandTotal(null, summaries);
163+
subtotal.GroupBy(groupBy, null, summaries, pageBreaks);
164164
}
165165
}
166166

ClosedXML.Report/Options/GroupTag.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private void Process(IXLRange root, GroupTag[] groups, bool summaryAbove,Summary
121121
if (GrandLabel != null) subtotal.GrandLabel = GrandLabel;
122122
if (!disableGrandTotal)
123123
{
124-
var total = subtotal.AddGrandTotal(summaries);
124+
var total = subtotal.AddGrandTotal(groups, summaries);
125125
total.SummaryRow.Cell(2).Value = total.SummaryRow.Cell(1).Value;
126126
total.SummaryRow.Cell(1).Value = null;
127127
level++;
@@ -136,7 +136,7 @@ private void Process(IXLRange root, GroupTag[] groups, bool summaryAbove,Summary
136136
if (g.MergeLabels == MergeMode.Merge2 && funcs.Length == 0)
137137
subtotal.ScanForGroups(g.Column);
138138
else
139-
subtotal.GroupBy(g.Column, g.DisableSubtotals ? new SummaryFuncTag[0] : summaries, g.PageBreaks, labFormat);
139+
subtotal.GroupBy(g.Column, groups, g.DisableSubtotals ? new SummaryFuncTag[0] : summaries, g.PageBreaks, labFormat);
140140

141141
g.Level = ++level;
142142
}

tests/ClosedXML.Report.Tests/GroupTagTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void EmptyDataSource(string templateFile)
7070
InlineData("GroupTagTests_FormulasInGroupRow.xlsx"),
7171
InlineData("GroupTagTests_MultiRanges.xlsx"),
7272
InlineData("GroupTagTests_FormulasWithTagsInGroupRow.xlsx"),
73+
InlineData("GroupTagTests_TotalLabel.xlsx"),
7374
]
7475
public void Customers(string templateFile)
7576
{

tests/ClosedXML.Report.Tests/SubtotalTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public void WithHeaders()
9191
using (var subtotal = new Subtotal(_rng))
9292
{
9393
var summaries = new[] { new SummaryFuncTag { Name = "sum", Cell=new TemplateCell { Column = 7 } } };//{new SubtotalSummaryFunc("sum", 7), };
94-
subtotal.AddGrandTotal(summaries);
95-
subtotal.GroupBy(2, summaries, true);
96-
subtotal.GroupBy(3, summaries, true);
94+
subtotal.AddGrandTotal(null, summaries);
95+
subtotal.GroupBy(2, null, summaries, true);
96+
subtotal.GroupBy(3, null, summaries, true);
9797
subtotal.AddHeaders(2);
9898
subtotal.AddHeaders(3);
9999
}
Binary file not shown.
333 Bytes
Binary file not shown.
Binary file not shown.
38.3 KB
Binary file not shown.
267 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)