Skip to content

Commit 945b83e

Browse files
Ensured showTotal is inserted correctly (#1164)
* Ensured showTotal is inserted correctly * Insert delete alternative * Functional fix * Cleanup comments and unused functions --------- Co-authored-by: Jan Källman <[email protected]>
1 parent 1c18912 commit 945b83e

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

src/EPPlus/Table/ExcelTable.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Date Author Change
2424
using OfficeOpenXml.Export.ToDataTable;
2525
using System.IO;
2626
using OfficeOpenXml.Style.Dxf;
27-
using OfficeOpenXml.Export.HtmlExport;
2827
using System.Globalization;
2928
using OfficeOpenXml.Sorting;
3029
using OfficeOpenXml.Export.HtmlExport.Interfaces;
@@ -788,7 +787,8 @@ public bool ShowTotal
788787
{
789788
if (value)
790789
{
791-
Address=new ExcelAddress(WorkSheet.Name, ExcelAddressBase.GetAddress(Address.Start.Row, Address.Start.Column, Address.End.Row+1, Address.End.Column));
790+
WorkSheet.Cells[Address._toRow + 1, Address._fromCol, Address._toRow + 1, Address._toCol].Clear();
791+
Address =new ExcelAddress(WorkSheet.Name, ExcelAddressBase.GetAddress(Address.Start.Row, Address.Start.Column, Address.End.Row+1, Address.End.Column));
792792
}
793793
else
794794
{
@@ -806,10 +806,12 @@ public bool ShowTotal
806806
}
807807
else
808808
{
809+
WorkSheet.Cells[Address._toRow + 1, Address._fromCol, Address._toRow + 1, Address._toCol].Clear();
809810
DeleteNode(TOTALSROWCOUNT_PATH);
810811
DataStyle.SetStyle();
811812
}
812813
WriteAutoFilter(value);
814+
813815
}
814816
}
815817
}
@@ -1044,12 +1046,12 @@ public ExcelRangeBase InsertRow(int position, int rows=1, bool copyStyles=true)
10441046
}
10451047
var firstRow = _address._fromRow;
10461048
var isFirstRow = position == 0;
1047-
var subtact = ShowTotal ? 2 : 1;
1048-
if (position>=ExcelPackage.MaxRows || position > _address._fromRow + position + rows - subtact)
1049+
var subtract = ShowTotal ? 2 : 1;
1050+
if (position>=ExcelPackage.MaxRows || position > _address._fromRow + position + rows - subtract)
10491051
{
1050-
position = _address.Rows - subtact;
1052+
position = _address.Rows - subtract;
10511053
}
1052-
if (_address._fromRow+position+rows>ExcelPackage.MaxRows)
1054+
if (_address._fromRow + position + rows>ExcelPackage.MaxRows)
10531055
{
10541056
throw new InvalidOperationException("Insert will exceed the maximum number of rows in the worksheet");
10551057
}

src/EPPlusTest/Issues.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Date Author Change
3636
using OfficeOpenXml.Drawing.Chart.Style;
3737
using OfficeOpenXml.Drawing.Slicer;
3838
using OfficeOpenXml.Drawing.Style.Coloring;
39+
using OfficeOpenXml.Export.HtmlExport;
3940
using OfficeOpenXml.Filter;
4041
using OfficeOpenXml.FormulaParsing;
4142
using OfficeOpenXml.Sparkline;

src/EPPlusTest/Table/TableInsertTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public void TableInsertRowBottomWithTotal()
107107
Assert.AreEqual("Don't Shift Me", ws.Cells["F5"].Value);
108108
Assert.AreEqual("Shift Me Down", ws.Cells["B106"].Value);
109109
}
110+
110111
[TestMethod]
111112
public void TableInsertRowInside()
112113
{

src/EPPlusTest/Table/TableTests.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ Date Author Change
3333
using System;
3434
using System.Drawing;
3535
using System.Globalization;
36-
using System.IO;
3736

3837
namespace EPPlusTest.Table
3938
{
@@ -697,5 +696,79 @@ public void CreateTableAfterDeletingAMergedCell()
697696
var table = worksheet.Tables.Add(tableCells, "table"); // --> This triggers a NullReferenceException
698697
}
699698
}
699+
700+
[TestMethod]
701+
public void ShowTotalWhenValueBelowIt()
702+
{
703+
using (var package = OpenPackage("ShowTotalInsert.xlsx", true))
704+
{
705+
var sheet = package.Workbook.Worksheets.Add("Tables");
706+
707+
//Cause of issue. (Specifically sheet.cells[11,1]
708+
for (int i = 1; i < 25; i++)
709+
{
710+
sheet.Cells[1 + i, 1].Formula = $"\"Number:{i}\"";
711+
}
712+
713+
sheet.Cells["A1"].Value = "Month";
714+
sheet.Cells["B1"].Value = "Sales";
715+
sheet.Cells["C1"].Value = "VAT";
716+
sheet.Cells["D1"].Value = "Total";
717+
718+
var table = sheet.Tables.Add(new ExcelAddress("A1:E10"), "testTable");
719+
table.ShowHeader = true;
720+
table.ShowFirstColumn = true;
721+
table.TableStyle = TableStyles.Dark2;
722+
723+
sheet.Cells["A1"].Value = "testStuff";
724+
725+
sheet.Cells["C3:C5"].Value = 3;
726+
sheet.Cells["D3:E3"].Value = 4;
727+
sheet.Cells["E10"].Value = 5;
728+
729+
sheet.Cells["F11"].Value = "Don't clear me";
730+
var noHeader = package.Workbook.Worksheets.Add("noHeader", sheet);
731+
var stackTest = package.Workbook.Worksheets.Add("stackTest", sheet);
732+
733+
table.ShowTotal = true;
734+
table.Columns[2].TotalsRowFunction = RowFunctions.Sum;
735+
736+
sheet.Calculate();
737+
738+
Assert.AreEqual(sheet.Cells["A10"].Value, "Number:9");
739+
Assert.AreEqual(sheet.Cells["A11"].Value, null);
740+
Assert.AreEqual(sheet.Cells["A12"].Value, "Number:11");
741+
Assert.AreEqual(sheet.Cells["F11"].Value, "Don't clear me");
742+
743+
noHeader.Tables[0].ShowHeader = false;
744+
noHeader.Tables[0].ShowTotal = true;
745+
noHeader.Tables[0].Columns[2].TotalsRowFunction = RowFunctions.Sum;
746+
747+
noHeader.Calculate();
748+
749+
Assert.AreEqual(noHeader.Cells["A10"].Value, "Number:9");
750+
Assert.AreEqual(noHeader.Cells["A11"].Value, null);
751+
Assert.AreEqual(noHeader.Cells["A12"].Value, "Number:11");
752+
Assert.AreEqual(noHeader.Cells["F11"].Value, "Don't clear me");
753+
754+
stackTest.Tables[0].ShowTotal = true;
755+
stackTest.Tables[0].ShowTotal = false;
756+
stackTest.Tables[0].ShowTotal = true;
757+
stackTest.Tables[0].ShowTotal = false;
758+
stackTest.Tables[0].ShowTotal = true;
759+
stackTest.Tables[0].ShowTotal = false;
760+
stackTest.Tables[0].ShowTotal = true;
761+
stackTest.Tables[0].ShowTotal = false;
762+
763+
stackTest.Calculate();
764+
765+
Assert.AreEqual(stackTest.Cells["A10"].Value, "Number:9");
766+
Assert.AreEqual(stackTest.Cells["A11"].Value, null);
767+
Assert.AreEqual(stackTest.Cells["A12"].Value, "Number:11");
768+
Assert.AreEqual(stackTest.Cells["F11"].Value, "Don't clear me");
769+
770+
SaveAndCleanup(package);
771+
}
772+
}
700773
}
701774
}

0 commit comments

Comments
 (0)