Skip to content
Merged
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
1 change: 1 addition & 0 deletions main/SS/Formula/Atp/AnalysisToolPak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private static Dictionary<String, FreeRefFunction> CreateFunctionsMap()
r(m, "CUBEVALUE", null);
r(m, "CUMIPMT", null);
r(m, "CUMPRINC", null);
r(m, "DAYS", Days.Instance);
r(m, "DEC2BIN", Dec2Bin.instance);
r(m, "DEC2HEX", Dec2Hex.instance);
r(m, "DEC2OCT", null);
Expand Down
69 changes: 69 additions & 0 deletions main/SS/Formula/Functions/Days.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using NPOI.SS.Formula.Atp;
using NPOI.SS.Formula.Eval;
using NPOI.SS.UserModel;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI.SS.Formula.Functions
{
public class Days : FreeRefFunction
{
public static Days Instance = new Days();

private Days() { }

public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
{
return Days.Evaluate(ec.RowIndex, ec.ColumnIndex, args[0], args[1]);
}
private static ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1)
{
double result;
try
{
var d0 = getDate(arg0, srcRowIndex, srcColumnIndex);
var d1 = getDate(arg1, srcRowIndex, srcColumnIndex);
result = Evaluate(d0, d1);
}
catch(EvaluationException e)
{
return e.GetErrorEval();
}
return new NumberEval(result);
}

private static double Evaluate(DateTime endDate, DateTime startDate)
{
return (endDate-startDate).Days;
}

private static DateTime getDate(ValueEval eval, int srcRowIndex, int srcColumnIndex)
{
ValueEval ve = OperandResolver.GetSingleValue(eval, srcRowIndex, srcColumnIndex);
try
{
double d0 = NumericFunction.SingleOperandEvaluate(ve, srcRowIndex, srcColumnIndex);
return getDate(d0);
}
catch(Exception e)
{
String strText1 = OperandResolver.CoerceValueToString(ve);
DateTime result = DateTime.MinValue;
if(DateTime.TryParse(strText1,out result))
return result;

throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
}

private static DateTime getDate(double date)
{
DateTime d = DateUtil.GetJavaDate(date, false);
return d.ToLocalTime();
}
}
}
47 changes: 47 additions & 0 deletions testcases/main/SS/Formula/Functions/TestDays.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;

namespace TestCases.SS.Formula.Functions
{
[TestFixture]
public class TestDays
{
//https://support.microsoft.com/en-us/office/days-function-57740535-d549-4395-8728-0f07bff0b9df
[Test]
public void TestMicrosoftExample1()
{
using(HSSFWorkbook wb = initWorkbook1())
{
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
var cell = wb.GetSheetAt(0).GetRow(0).CreateCell(12);
SS.Util.Utils.AssertDouble(fe, cell, "DAYS(\"15-MAR-2021\",\"1-FEB-2021\")", 42, 0.00000000001);
SS.Util.Utils.AssertDouble(fe, cell, "DAYS(A2,A3)", 364, 0.00000000001);
}
}

[Test]
public void TestInvalid()
{
using(HSSFWorkbook wb = initWorkbook1())
{
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
var cell = wb.GetSheetAt(0).GetRow(0).CreateCell(12);
SS.Util.Utils.AssertError(fe, cell, "DAYS(\"15-XYZ\",\"1-FEB-2021\")", FormulaError.VALUE);
SS.Util.Utils.AssertError(fe, cell, "DAYS(\"15-MAR-2021\",\"1-XYZ\")", FormulaError.VALUE);
}
}

private HSSFWorkbook initWorkbook1()
{
HSSFWorkbook wb = new HSSFWorkbook();
var sheet = wb.CreateSheet();
SS.Util.Utils.AddRow(sheet, 0, "Data");
SS.Util.Utils.AddRow(sheet, 1, DateUtil.GetExcelDate(DateTime.Parse("2021-12-31")));
SS.Util.Utils.AddRow(sheet, 2, DateUtil.GetExcelDate(DateTime.Parse("2021-01-01")));
return wb;
}
}
}
Loading