Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Fixed issue in formula match #505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions EPPlus/FormulaParsing/ExcelUtilities/ValueMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
* Mats Alm Added 2013-03-01 (Prior file history on https://github.com/swmal/ExcelFormulaParser)
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OfficeOpenXml.FormulaParsing.ExcelUtilities
{
Expand All @@ -52,14 +49,18 @@ public virtual int IsMatch(object o1, object o2)
{
return CompareStringToString(o1.ToString().ToLower(), o2.ToString().ToLower());
}
else if( o1.GetType() == typeof(string))
else if (o1.GetType() == typeof(string))
{
return CompareStringToObject(o1.ToString(), o2);
}
else if (o2.GetType() == typeof(string))
{
return CompareObjectToString(o1, o2.ToString());
}
else if (o1.GetType() == typeof(DateTime))
{
return CompareDatetimeToDouble(o1, o2.ToString());
}
return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
}

Expand Down Expand Up @@ -116,5 +117,15 @@ protected virtual int CompareObjectToString(object o1, string o2)
}
return IncompatibleOperands;
}

protected virtual int CompareDatetimeToDouble(object o1, string o2)
{
double dt1;
if (double.TryParse(o2, out dt1))
{
return DateTime.FromOADate(double.Parse(o2)).CompareTo(o1);
}
return IncompatibleOperands;
}
}
}
9 changes: 9 additions & 0 deletions EPPlusTest/FormulaParsing/ExcelUtilities/ValueMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,14 @@ public void ShouldReturnMînus2WhenTypesDifferAndStringConversionToDoubleFails()
var result = _matcher.IsMatch(o1, o2);
Assert.AreEqual(-2, result);
}

[TestMethod]
public void ShouldReturnMinus1WhenTypesAreDateTimeAndDouble()
{
object o1 = Convert.ToDateTime("18/01/2024");
object o2 = 41711;
var result = _matcher.IsMatch(o1, o2);
Assert.AreEqual(-1, result);
}
}
}