EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Removing a `CalculatedColumnFormula` resets cell styles for that column

Open craigbrown opened this issue 1 year ago • 2 comments
trafficstars

If I set a table column's CalculatedColumnFormula to be the empty string or null, the styles for all cells in that column are removed. I think this might be a bug.

[TestMethod]
public void CalculatedColumnFormula_SetToEmptyString_CellStyle()
{
    using (var pck = new ExcelPackage())
    {
        // Set up a worksheet containing a table
        var wks = pck.Workbook.Worksheets.Add("Sheet1");
        wks.Cells["A1"].Value = "Col1";
        wks.Cells["B1"].Value = "Col2";
        wks.Cells["C1"].Value = "Col3";
        wks.Cells["A2"].Value = 1;
        wks.Cells["B2"].Value = 2;
        var table1 = wks.Tables.Add(wks.Cells["A1:C2"], "Table1");
        var formula = "Table1[[#This Row],[Col1]]+Table1[[#This Row],[Col2]]";
        table1.Columns[2].CalculatedColumnFormula = formula;

        // Add a style to the cell containing the formula
        wks.Cells["C2"].Style.Font.Bold = true;
        wks.Cells["C2"].Style.Font.Size = 16;
        wks.Cells["C2"].Style.Font.Color.SetColor(eThemeSchemeColor.Text2);
        wks.Cells["C2"].Style.Font.Color.Tint = 0.39997558519241921m;

        // Check the style has been applied
        Assert.AreEqual(true, wks.Cells["C2"].Style.Font.Bold);
        Assert.AreEqual(16, wks.Cells["C2"].Style.Font.Size, 1E-3);
        Assert.AreEqual(eThemeSchemeColor.Text2, wks.Cells["C2"].Style.Font.Color.Theme);
        Assert.AreEqual(0.39997558519241921m, wks.Cells["C2"].Style.Font.Color.Tint);

        // Remove the calculated column formula from the table
        table1.Columns["Col3"].CalculatedColumnFormula = "";

        // Check the style hasn't changed
        Assert.AreEqual(true, wks.Cells["C2"].Style.Font.Bold);
        Assert.AreEqual(16, wks.Cells["C2"].Style.Font.Size, 1E-3);
        Assert.AreEqual(eThemeSchemeColor.Text2, wks.Cells["C2"].Style.Font.Color.Theme);
        Assert.AreEqual(0.39997558519241921m, wks.Cells["C2"].Style.Font.Color.Tint);
    }
}

craigbrown avatar Mar 04 '24 17:03 craigbrown

We'll have a look at this.

swmal avatar Mar 05 '24 13:03 swmal

Thanks for the unit test @craigbrown , we could replicate this unexpected behaviour - it will be fixed in the next version.

swmal avatar Mar 05 '24 13:03 swmal

Should be fixed in 7.1

swmal avatar Apr 10 '24 08:04 swmal