Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
Updated the project to change set#67eadd18f55a.
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Jan 10, 2017
1 parent f889f7a commit aa201cb
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
EPPlus.Core
===========
`EPPlus.Core` is an **unofficial** port of the [EPPlus library](http:https://epplus.codeplex.com) to .NET Core. It's based on the [30/12/2016, change set#185f083ca608](http:https://epplus.codeplex.com/SourceControl/list/changesets).
`EPPlus.Core` is an **unofficial** port of the [EPPlus library](http:https://epplus.codeplex.com) to .NET Core. It's based on the [1/8/2017, change set#67eadd18f55a](http:https://epplus.codeplex.com/SourceControl/list/changesets).


Install via NuGet
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus.Core.FunctionalTests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"testRunner": "mstest",
"dependencies": {
"dotnet-test-mstest": "1.1.2-preview",
"EPPlus.Core": "1.1.0-*",
"EPPlus.Core": "1.2.0-*",
"MSTest.TestFramework": "1.0.6-preview",
"NETStandard.Library": "1.6.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ArgumentCollectionUtil()
_objectEnumerableArgConverter = objectEnumerableArgConverter;
}

public virtual IEnumerable<double> ArgsToDoubleEnumerable(bool ignoreHidden, bool ignoreErrors, IEnumerable<FunctionArgument> arguments, ParsingContext context)
public virtual IEnumerable<ExcelDoubleCellValue> ArgsToDoubleEnumerable(bool ignoreHidden, bool ignoreErrors, IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
return _doubleEnumerableArgConverter.ConvertArgs(ignoreHidden, ignoreErrors, arguments, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

namespace OfficeOpenXml.FormulaParsing.Excel.Functions
{
public class DoubleEnumerableArgConverter : CollectionFlattener<double>
public class DoubleEnumerableArgConverter : CollectionFlattener<ExcelDoubleCellValue>
{
public virtual IEnumerable<double> ConvertArgs(bool ignoreHidden, bool ignoreErrors, IEnumerable<FunctionArgument> arguments, ParsingContext context)
public virtual IEnumerable<ExcelDoubleCellValue> ConvertArgs(bool ignoreHidden, bool ignoreErrors, IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
return base.FuncArgsToFlatEnumerable(arguments, (arg, argList) =>
{
Expand All @@ -44,7 +44,8 @@ public virtual IEnumerable<double> ConvertArgs(bool ignoreHidden, bool ignoreErr
if(!ignoreErrors && cell.IsExcelError) throw new ExcelErrorValueException(ExcelErrorValue.Parse(cell.Value.ToString()));
if (!CellStateHelper.ShouldIgnore(ignoreHidden, cell, context) && ConvertUtil.IsNumeric(cell.Value))
{
argList.Add(cell.ValueDouble);
var val = new ExcelDoubleCellValue(cell.ValueDouble, cell.Row);
argList.Add(val);
}
}
}
Expand All @@ -53,13 +54,14 @@ public virtual IEnumerable<double> ConvertArgs(bool ignoreHidden, bool ignoreErr
if(!ignoreErrors && arg.ValueIsExcelError) throw new ExcelErrorValueException(arg.ValueAsExcelErrorValue);
if (ConvertUtil.IsNumeric(arg.Value) && !CellStateHelper.ShouldIgnore(ignoreHidden, arg, context))
{
argList.Add(ConvertUtil.GetValueDouble(arg.Value));
var val = new ExcelDoubleCellValue(ConvertUtil.GetValueDouble(arg.Value));
argList.Add(val);
}
}
});
}

public virtual IEnumerable<double> ConvertArgsIncludingOtherTypes(IEnumerable<FunctionArgument> arguments)
public virtual IEnumerable<ExcelDoubleCellValue> ConvertArgsIncludingOtherTypes(IEnumerable<FunctionArgument> arguments)
{
return base.FuncArgsToFlatEnumerable(arguments, (arg, argList) =>
{
Expand All @@ -69,7 +71,8 @@ public virtual IEnumerable<double> ConvertArgsIncludingOtherTypes(IEnumerable<Fu
{
foreach (var cell in (ExcelDataProvider.IRangeInfo)arg.Value)
{
argList.Add(cell.ValueDoubleLogical);
var val = new ExcelDoubleCellValue(cell.ValueDoubleLogical, cell.Row);
argList.Add(val);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OfficeOpenXml.FormulaParsing.Excel.Functions
{
public struct ExcelDoubleCellValue : IComparable<ExcelDoubleCellValue>, IComparable
{
public ExcelDoubleCellValue(double val)
{
Value = val;
CellRow = default(int?);
}

public ExcelDoubleCellValue(double val, int cellRow)
{
Value = val;
CellRow = cellRow;
}

public int? CellRow;

public double Value;

public static implicit operator double(ExcelDoubleCellValue d)
{
return d.Value;
}
// User-defined conversion from double to Digit
public static implicit operator ExcelDoubleCellValue(double d)
{
return new ExcelDoubleCellValue(d);
}

public int CompareTo(ExcelDoubleCellValue other)
{
return Value.CompareTo(other.Value);
}

public int CompareTo(object obj)
{
if(obj is double)
{
return Value.CompareTo((double)obj);
}
return Value.CompareTo(((ExcelDoubleCellValue)obj).Value);
}

public override bool Equals(object obj)
{
return CompareTo(obj) == 0;
}

public static bool operator ==(ExcelDoubleCellValue a, ExcelDoubleCellValue b)
{
return a.Value.CompareTo(b.Value) == 0;
}

public static bool operator ==(ExcelDoubleCellValue a, double b)
{
return a.Value.CompareTo(b) == 0;
}

public static bool operator !=(ExcelDoubleCellValue a, ExcelDoubleCellValue b)
{
return a.Value.CompareTo(b.Value) != 0;
}

public static bool operator !=(ExcelDoubleCellValue a, double b)
{
return a.Value.CompareTo(b) != 0;
}
}
}
31 changes: 27 additions & 4 deletions src/EPPlus.Core/FormulaParsing/Excel/Functions/ExcelFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected bool IsNumeric(object val)
#else
val.GetType().IsPrimitive
#endif
|| val is double || val is decimal || val is System.DateTime || val is TimeSpan);
|| val is double || val is decimal || val is System.DateTime || val is TimeSpan);
}

//protected virtual bool IsNumber(object obj)
Expand All @@ -339,7 +339,7 @@ protected bool AreEqual(double d1, double d2)
/// <param name="arguments"></param>
/// <param name="context"></param>
/// <returns></returns>
protected virtual IEnumerable<double> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments,
protected virtual IEnumerable<ExcelDoubleCellValue> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments,
ParsingContext context)
{
return ArgsToDoubleEnumerable(false, arguments, context);
Expand All @@ -353,7 +353,7 @@ protected bool AreEqual(double d1, double d2)
/// <param name="arguments"></param>
/// <param name="context"></param>
/// <returns></returns>
protected virtual IEnumerable<double> ArgsToDoubleEnumerable(bool ignoreHiddenCells, bool ignoreErrors, IEnumerable<FunctionArgument> arguments, ParsingContext context)
protected virtual IEnumerable<ExcelDoubleCellValue> ArgsToDoubleEnumerable(bool ignoreHiddenCells, bool ignoreErrors, IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
return _argumentCollectionUtil.ArgsToDoubleEnumerable(ignoreHiddenCells, ignoreErrors, arguments, context);
}
Expand All @@ -365,11 +365,34 @@ protected virtual IEnumerable<double> ArgsToDoubleEnumerable(bool ignoreHiddenCe
/// <param name="arguments"></param>
/// <param name="context"></param>
/// <returns></returns>
protected virtual IEnumerable<double> ArgsToDoubleEnumerable(bool ignoreHiddenCells, IEnumerable<FunctionArgument> arguments, ParsingContext context)
protected virtual IEnumerable<ExcelDoubleCellValue> ArgsToDoubleEnumerable(bool ignoreHiddenCells, IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
return ArgsToDoubleEnumerable(ignoreHiddenCells, true, arguments, context);
}

protected virtual IEnumerable<double> ArgsToDoubleEnumerableZeroPadded(bool ignoreHiddenCells, ExcelDataProvider.IRangeInfo rangeInfo, ParsingContext context)
{
var startRow = rangeInfo.Address.Start.Row;
var endRow = rangeInfo.Address.End.Row;
var funcArg = new FunctionArgument(rangeInfo);
var result = ArgsToDoubleEnumerable(ignoreHiddenCells, new List<FunctionArgument> { funcArg }, context);
var dict = new Dictionary<int, double>();
result.ToList().ForEach(x => dict.Add(x.CellRow.Value, x.Value));
var resultList = new List<double>();
for (var row = startRow; row <= endRow; row++)
{
if(dict.ContainsKey(row))
{
resultList.Add(dict[row]);
}
else
{
resultList.Add(0d);
}
}
return resultList;
}

/// <summary>
/// Will return the arguments as an enumerable of objects.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public bool IgnoreHiddenValues
set;
}

protected override IEnumerable<double> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments, ParsingContext context)
protected override IEnumerable<ExcelDoubleCellValue> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
return ArgsToDoubleEnumerable(arguments, context, true);
}

protected IEnumerable<double> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments, ParsingContext context, bool ignoreErrors)
protected IEnumerable<ExcelDoubleCellValue> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments, ParsingContext context, bool ignoreErrors)
{
if (!arguments.Any())
{
return Enumerable.Empty<double>();
return Enumerable.Empty<ExcelDoubleCellValue>();
}
if (IgnoreHiddenValues)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Stdev : HiddenValuesHandlingFunction
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 1);
var values = ArgsToDoubleEnumerable(arguments, context, false);
var values = ArgsToDoubleEnumerable(arguments, context, false).Select(x => (double)x);
return CreateResult(StandardDeviation(values), DataType.Decimal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class StdevP : HiddenValuesHandlingFunction
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
var args = ArgsToDoubleEnumerable(IgnoreHiddenValues, false, arguments, context);
return CreateResult(StandardDeviation(args), DataType.Decimal);
return CreateResult(StandardDeviation(args.Select(x => (double)x)), DataType.Decimal);
}

private static double StandardDeviation(IEnumerable<double> values)
Expand Down
14 changes: 12 additions & 2 deletions src/EPPlus.Core/FormulaParsing/Excel/Functions/Math/VarMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,26 @@ private static double Divide(double left, double right)
return left / right;
}

public static double Var(IEnumerable<ExcelDoubleCellValue> args)
{
return Var(args.Select(x => (double)x));
}

public static double Var(IEnumerable<double> args)
{
double avg = args.Average();
double avg = args.Select(x => (double)x).Average();
double d = args.Aggregate(0.0, (total, next) => total += System.Math.Pow(next - avg, 2));
return Divide(d, (args.Count() - 1));
}

public static double VarP(IEnumerable<ExcelDoubleCellValue> args)
{
return VarP(args.Select(x => (double)x));
}

public static double VarP(IEnumerable<double> args)
{
double avg = args.Average();
double avg = args.Select(x => (double)x).Average();
double d = args.Aggregate(0.0, (total, next) => total += System.Math.Pow(next - avg, 2));
return Divide(d, args.Count());
}
Expand Down
22 changes: 15 additions & 7 deletions src/EPPlus.Core/FormulaParsing/ExpressionGraph/CompileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* The GNU Lesser General Public License can be viewed at http:https://www.opensource.org/licenses/lgpl-license.php
* If you unfamiliar with this license or have questions about it, here is an http:https://www.gnu.org/licenses/gpl-faq.html
*
* All code and executables are provided "as is" with no warranty either express or implied.
* All code and executables are provided "as is" with no warranty either express or implied.
* The author accepts no liability for any damage or loss of business that this product may cause.
*
* Code change notes:
*
*
* Author Change Date
* ******************************************************************************
* Mats Alm Added 2013-03-01 (Prior file history on https://github.com/swmal/ExcelFormulaParser)
Expand All @@ -35,6 +35,7 @@
using System.Text;
using System.Text.RegularExpressions;
using OfficeOpenXml.Utils;
using OfficeOpenXml.FormulaParsing.Excel.Functions;

namespace OfficeOpenXml.FormulaParsing.ExpressionGraph
{
Expand All @@ -51,7 +52,14 @@ public static CompileResult Empty

public CompileResult(object result, DataType dataType)
{
Result = result;
if(result is ExcelDoubleCellValue)
{
Result = ((ExcelDoubleCellValue)result).Value;
}
else
{
Result = result;
}
DataType = dataType;
}

Expand Down Expand Up @@ -137,12 +145,12 @@ public DataType DataType
get;
private set;
}

public bool IsNumeric
{
get
get
{
return DataType == DataType.Decimal || DataType == DataType.Integer || DataType == DataType.Empty || DataType == DataType.Boolean || DataType == DataType.Date;
return DataType == DataType.Decimal || DataType == DataType.Integer || DataType == DataType.Empty || DataType == DataType.Boolean || DataType == DataType.Date;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus.Core/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.0-*",
"version": "1.2.0-*",

"authors": [ "Vahid Nasiri" ],
"packOptions": {
Expand Down

0 comments on commit aa201cb

Please sign in to comment.