Skip to content

Commit 34b94e9

Browse files
committed
Fixes issue #393
1 parent 668adf5 commit 34b94e9

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/EPPlus/Table/ExcelTableColumn.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private void SetTableFormula()
262262
{
263263
int fromRow = _tbl.ShowHeader ? _tbl.Address._fromRow + 1 : _tbl.Address._fromRow;
264264
int toRow = _tbl.ShowTotal ? _tbl.Address._toRow - 1 : _tbl.Address._toRow;
265-
var colNum = _tbl.Address._fromRow + Position;
265+
var colNum = _tbl.Address._fromCol + Position;
266266
string r1c1Formula = ExcelCellBase.TranslateToR1C1(CalculatedColumnFormula, fromRow, colNum);
267267
bool needsTranslation = r1c1Formula != CalculatedColumnFormula;
268268

src/EPPlusTest/Table/TableTests.cs

+45-5
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void ValidateEncodingForTableColumnNames()
125125
ws.Cells["A1"].Value = "Col1>";
126126
ws.Cells["B1"].Value = "Col1>";
127127
var tbl = ws.Tables.Add(ws.Cells["A1:C2"], "TableValColNames");
128-
Assert.AreEqual("Col1>",tbl.Columns[0].Name);
128+
Assert.AreEqual("Col1>", tbl.Columns[0].Name);
129129
Assert.AreEqual("Col1>", tbl.Columns[1].Name);
130130
Assert.AreEqual("Column3", tbl.Columns[2].Name);
131131
}
@@ -251,16 +251,16 @@ public void TableDeleteTest()
251251
[TestMethod]
252252
public void DeleteTablesFromTemplate()
253253
{
254-
using(var p=new ExcelPackage())
254+
using (var p = new ExcelPackage())
255255
{
256-
var ws=p.Workbook.Worksheets.Add("Tablews1");
256+
var ws = p.Workbook.Worksheets.Add("Tablews1");
257257
ws.Tables.Add(new ExcelAddressBase("A1:C3"), "Table1");
258258
ws.Tables.Add(new ExcelAddressBase("D1:G7"), "Table2");
259259

260260
Assert.AreEqual(2, ws.Tables.Count);
261261
p.Save();
262262

263-
using(var p2=new ExcelPackage(p.Stream))
263+
using (var p2 = new ExcelPackage(p.Stream))
264264
{
265265
ws = p2.Workbook.Worksheets[0];
266266
Assert.AreEqual(2, ws.Tables.Count);
@@ -274,7 +274,7 @@ public void DeleteTablesFromTemplate()
274274
Assert.AreEqual(0, p3.Workbook.Worksheets[0].Tables.Count);
275275
}
276276
}
277-
}
277+
}
278278
}
279279
[TestMethod]
280280
public void ValidateTableSaveLoad()
@@ -394,7 +394,47 @@ public void AddRowShouldAdjustSubtotals()
394394

395395
SaveAndCleanup(package);
396396
}
397+
}
398+
[TestMethod]
399+
public void ValidateCalculatedColumn()
400+
{
401+
using (var package = OpenPackage("TableCalculatedColumn.xlsx", true))
402+
{
403+
var sheet = package.Workbook.Worksheets.Add("Tables");
404+
405+
// headers
406+
sheet.Cells["C1"].Value = "Month";
407+
sheet.Cells["D1"].Value = "Sales";
408+
sheet.Cells["E1"].Value = "VAT";
409+
sheet.Cells["F1"].Value = "Total";
410+
sheet.Cells["G1"].Value = "Formula";
411+
412+
var rnd = new Random();
413+
for (var row = 2; row < 12; row++)
414+
{
415+
sheet.Cells[row, 3].Value = new DateTimeFormatInfo().GetMonthName(row);
416+
sheet.Cells[row, 4].Value = rnd.Next(10000, 100000);
417+
sheet.Cells[row, 5].Formula = $"D{row} * 0.25";
418+
sheet.Cells[row, 6].Formula = $"D{row} + E{row}";
419+
}
420+
sheet.Cells["D2:G13"].Style.Numberformat.Format = "€#,##0.00";
421+
422+
var range = sheet.Cells["C1:G11"];
397423

424+
// create the table
425+
var table = sheet.Tables.Add(range, "myTable");
426+
// configure the table
427+
table.ShowHeader = true;
428+
table.ShowTotal = true;
429+
430+
var formula = "mytable[[#this row],[Sales]]+mytable[[#this row],[VAT]]";
431+
table.Columns[4].CalculatedColumnFormula = formula;
432+
Assert.AreEqual(formula, table.Columns[4].CalculatedColumnFormula);
433+
Assert.AreEqual(formula, sheet.Cells["G2"].Formula);
434+
Assert.AreEqual(formula, sheet.Cells["G3"].Formula);
435+
Assert.AreEqual(formula, sheet.Cells["G11"].Formula);
436+
SaveAndCleanup(package);
437+
}
398438
}
399439
}
400440
}

0 commit comments

Comments
 (0)