diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/Logical.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/Logical.cs index 391fed9..1b6695e 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/Logical.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/Logical.cs @@ -12,7 +12,7 @@ ce.RegisterFunction("AND", 1, int.MaxValue, And); ce.RegisterFunction("OR", 1, int.MaxValue, Or); ce.RegisterFunction("NOT", 1, Not); - ce.RegisterFunction("IF", 3, If); + ce.RegisterFunction("IF", 2, 3, If); ce.RegisterFunction("TRUE", 0, True); ce.RegisterFunction("FALSE", 0, False); } @@ -41,9 +41,14 @@ } static object If(List p) { - return (bool)p[0] - ? p[1].Evaluate() - : p[2].Evaluate(); + if ((bool)p[0] ) + { + return p[1].Evaluate(); + } + else + { + return p.Count > 2 ? p[2].Evaluate() : false; + } } static object True(List p) { diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index e7a6c06..94a3a32 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -1642,6 +1642,9 @@ } else if (value is TimeSpan || (!Double.TryParse(val, out dTest) && TimeSpan.TryParse(val, out tsTest))) { + if (!(value is TimeSpan) && TimeSpan.TryParse(val, out tsTest)) + val = tsTest.ToString(); + _dataType = XLCellValues.TimeSpan; if (style.NumberFormat.Format == String.Empty && style.NumberFormat.NumberFormatId == 0) Style.NumberFormat.NumberFormatId = 46; diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj index 3872960..2e26442 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -114,6 +114,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs index 4b89d09..8758174 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs @@ -4,9 +4,7 @@ namespace ClosedXML_Tests.Excel.CalcEngine { - /// - /// Summary description for UnitTest1 - /// + [TestFixture] public class FunctionsTests { diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/LogicalTests.cs b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/LogicalTests.cs new file mode 100644 index 0000000..c3aeee4 --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/LogicalTests.cs @@ -0,0 +1,37 @@ +using System; +using ClosedXML.Excel; +using NUnit.Framework; + +namespace ClosedXML_Tests.Excel.CalcEngine +{ + + [TestFixture] + public class LogicalTests + { + [Test] + public void If_2_Params_true() + { + Object actual = XLWorkbook.EvaluateExpr(@"if(1 = 1, ""T"")"); + Assert.AreEqual("T", actual); + } + [Test] + public void If_2_Params_false() + { + Object actual = XLWorkbook.EvaluateExpr(@"if(1 = 2, ""T"")"); + Assert.AreEqual(false, actual); + } + + [Test] + public void If_3_Params_true() + { + Object actual = XLWorkbook.EvaluateExpr(@"if(1 = 1, ""T"", ""F"")"); + Assert.AreEqual("T", actual); + } + [Test] + public void If_3_Params_false() + { + Object actual = XLWorkbook.EvaluateExpr(@"if(1 = 2, ""T"", ""F"")"); + Assert.AreEqual("F", actual); + } + } +} \ No newline at end of file