@@ -17,7 +17,7 @@ public class Subtotal : IDisposable
17
17
private IXLWorksheet _tempSheet ;
18
18
private readonly List < SubtotalGroup > _groups = new List < SubtotalGroup > ( ) ;
19
19
public string TotalLabel { get ; set ; } = "Total" ;
20
- public string GrandLabel { get ; set ; } = "Grand" ;
20
+ public string GrandLabel { get ; set ; } = "Grand Total " ;
21
21
22
22
public Subtotal ( IXLRange range ) : this ( range , false )
23
23
{
@@ -40,7 +40,7 @@ public Subtotal(IXLRange range, bool summaryAbove)
40
40
41
41
public SubtotalGroup [ ] Groups => _groups . ToArray ( ) ;
42
42
43
- public SubtotalGroup AddGrandTotal ( SummaryFuncTag [ ] summaries )
43
+ public SubtotalGroup AddGrandTotal ( GroupTag [ ] groups , SummaryFuncTag [ ] summaries )
44
44
{
45
45
if ( Sheet . Row ( _range . Row ( 2 ) . RowNumber ( ) ) . OutlineLevel == 0 )
46
46
{
@@ -49,13 +49,13 @@ public SubtotalGroup AddGrandTotal(SummaryFuncTag[] summaries)
49
49
{
50
50
var rangeAddress = _range . ShiftRows ( 1 ) . RangeAddress ;
51
51
_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 ) ;
53
53
}
54
54
else
55
55
{
56
56
var rangeAddress = _range . RangeAddress ;
57
57
_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 ) ;
59
59
}
60
60
gr . Column = 0 ;
61
61
_groups . Add ( gr ) ;
@@ -64,7 +64,7 @@ public SubtotalGroup AddGrandTotal(SummaryFuncTag[] summaries)
64
64
else return null ;
65
65
}
66
66
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 )
68
68
{
69
69
_pageBreaks = pageBreaks ;
70
70
_getGroupLabel = getGroupLabel ;
@@ -87,7 +87,7 @@ public void GroupBy(int groupBy, SummaryFuncTag[] summaries, bool pageBreaks = f
87
87
foreach ( var moveData in grRanges )
88
88
{
89
89
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 ) ) ;
91
91
}
92
92
ArrangePageBreaks ( Groups , grRanges ) ;
93
93
}
@@ -252,7 +252,7 @@ private void MoveRange(MoveData moveData)
252
252
Sheet . Cell ( moveData . TargetAddress . FirstAddress ) . Value = _tempSheet . Range ( 1 , 1 , srcRng . RowCount ( ) , srcRng . ColumnCount ( ) ) ;
253
253
}
254
254
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 )
256
256
{
257
257
var firstRow = groupRng . RangeAddress . FirstAddress . RowNumber ;
258
258
var lastRow = groupRng . RangeAddress . LastAddress . RowNumber ;
@@ -272,7 +272,9 @@ private SubtotalGroup CreateGroup(IXLRange groupRng, int groupClmn, int level, s
272
272
}
273
273
274
274
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 ) ) ;
276
278
Sheet . Row ( summRow . RowNumber ( ) ) . OutlineLevel = level - 1 ;
277
279
foreach ( var item in summaries )
278
280
{
0 commit comments