diff --git a/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/Excel/Cells/XLCell.cs index ff51a6c..ffe4a95 100644 --- a/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/Excel/Cells/XLCell.cs @@ -271,6 +271,8 @@ _dataType = XLDataType.Text; } + CachedValue = null; + return this; } @@ -448,11 +450,15 @@ /// /// Flag indicating whether a recalculation must be performed even is cell does not need it. /// Null if cell does not contain a formula. Calculated value otherwise. - public object Evaluate(bool force = false) + public Object Evaluate(Boolean force = false) { if (force || NeedsRecalculation) { - CachedValue = RecalculateFormula(FormulaA1); + if (HasFormula) + CachedValue = RecalculateFormula(FormulaA1); + else + CachedValue = null; + EvaluatedAtVersion = Worksheet.Workbook.RecalculationCounter; NeedsRecalculation = false; } @@ -516,6 +522,8 @@ if (!SetRichText(value)) SetValue(value); + CachedValue = null; + if (_cellValue.Length > 32767) throw new ArgumentException("Cells can hold only 32,767 characters."); } } @@ -962,7 +970,7 @@ { if (_dataType == value) return; - if (_richText != null) + if (HasRichText) { _cellValue = _richText.ToString(); _richText = null; @@ -1048,6 +1056,11 @@ } _dataType = value; + + if (HasFormula) + CachedValue = StringToCellDataType(_cellValue); + else + CachedValue = null; } } @@ -1288,7 +1301,25 @@ /// private long NeedsRecalculationEvaluatedAtVersion { get; set; } - public object CachedValue { get; private set; } + private Object cachedValue; + + public Object CachedValue + { + get + { + if (!HasFormula && cachedValue == null) + cachedValue = Value; + + return cachedValue; + } + private set + { + if (value != null && !HasFormula) + throw new InvalidOperationException("Cached values can be set only for cells with formulas"); + + cachedValue = value; + } + } [Obsolete("Use CachedValue instead")] public string ValueCached { get; internal set; } diff --git a/ClosedXML_Tests/Excel/CalcEngine/FormulaCachingTests.cs b/ClosedXML_Tests/Excel/CalcEngine/FormulaCachingTests.cs index 5ac60f4..ddd4600 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/FormulaCachingTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/FormulaCachingTests.cs @@ -64,7 +64,6 @@ } } - [Test] public void EditFormulaA1InvalidatesDependentCells() { @@ -89,7 +88,6 @@ } } - [Test] public void EditFormulaR1C1InvalidatesDependentCells() { @@ -113,6 +111,7 @@ Assert.AreEqual(15 + 30 + 300, res2); } } + [Test] public void InsertRowInvalidatesValues() { @@ -131,7 +130,6 @@ } } - [Test] public void DeleteRowInvalidatesValues() { @@ -201,13 +199,12 @@ } } - [Test] - [TestCase("C4", new string[] {"C5"})] + [TestCase("C4", new string[] { "C5" })] [TestCase("D4", new string[] { })] - [TestCase("A1", new string[] {"A2", "A3", "A4", "C1", "C2", "C3", "C5" })] - [TestCase("B2", new string[] {"B3", "B4", "C2", "C3", "C5" })] - [TestCase("C2", new string[] {"C5" })] + [TestCase("A1", new string[] { "A2", "A3", "A4", "C1", "C2", "C3", "C5" })] + [TestCase("B2", new string[] { "B3", "B4", "C2", "C3", "C5" })] + [TestCase("C2", new string[] { "C5" })] public void EditingDoesNotAffectNonDependingCells(string changedCell, string[] affectedCells) { using (var wb = new XLWorkbook()) @@ -266,7 +263,6 @@ } } - [Test] public void CircularReferenceRecalculationNeededDoesNotFail() { @@ -316,5 +312,26 @@ Assert.Throws(typeof(ArgumentOutOfRangeException), getValue); } } + + [Test] + public void TestValueCellsCachedValue() + { + using (var wb = new XLWorkbook()) + { + var sheet = wb.Worksheets.Add("TestSheet"); + var cell = sheet.Cell(1, 1); + + var date = new DateTime(2018, 4, 19); ; + cell.Value = date; + + Assert.AreEqual(XLDataType.DateTime, cell.DataType); + Assert.AreEqual(date, cell.CachedValue); + + cell.DataType = XLDataType.Number; + + Assert.AreEqual(XLDataType.Number, cell.DataType); + Assert.AreEqual(date.ToOADate(), cell.CachedValue); + } + } } }