diff --git a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj index 825a242..d6d0784 100644 --- a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj @@ -130,6 +130,8 @@ + + diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/CalcEngine.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/CalcEngine.cs index b0d924a..7d93111 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/CalcEngine.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/CalcEngine.cs @@ -7,6 +7,7 @@ using System.Text; using System.Text.RegularExpressions; using ClosedXML.Excel.CalcEngine; +using ClosedXML.Excel.CalcEngine.Functions; namespace ClosedXML.Excel.CalcEngine { @@ -288,6 +289,7 @@ MathTrig.Register(this); Text.Register(this); Statistical.Register(this); + DateAndTime.Register(this); } return _fnTbl; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/Database.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/Database.cs new file mode 100644 index 0000000..c7aed5f --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/Database.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ClosedXML.Excel.CalcEngine.Functions +{ + internal static class Database + { + public static void Register(CalcEngine ce) + { + //ce.RegisterFunction("DAVERAGE", 3, Daverage); // Returns the average of selected database entries + //ce.RegisterFunction("DCOUNT", 1, Dcount); // Counts the cells that contain numbers in a database + //ce.RegisterFunction("DCOUNTA", 1, Dcounta); // Counts nonblank cells in a database + //ce.RegisterFunction("DGET", 1, Dget); // Extracts from a database a single record that matches the specified criteria + //ce.RegisterFunction("DMAX", 1, Dmax); // Returns the maximum value from selected database entries + //ce.RegisterFunction("DMIN", 1, Dmin); // Returns the minimum value from selected database entries + //ce.RegisterFunction("DPRODUCT", 1, Dproduct); // Multiplies the values in a particular field of records that match the criteria in a database + //ce.RegisterFunction("DSTDEV", 1, Dstdev); // Estimates the standard deviation based on a sample of selected database entries + //ce.RegisterFunction("DSTDEVP", 1, Dstdevp); // Calculates the standard deviation based on the entire population of selected database entries + //ce.RegisterFunction("DSUM", 1, Dsum); // Adds the numbers in the field column of records in the database that match the criteria + //ce.RegisterFunction("DVAR", 1, Dvar); // Estimates variance based on a sample from selected database entries + //ce.RegisterFunction("DVARP", 1, Dvarp); // Calculates variance based on the entire population of selected database entries + } + + static object Daverage(List p) + { + var b = true; + foreach (var v in p) + { + b = b && (bool)v; + } + return b; + } + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/DateAndTime.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/DateAndTime.cs new file mode 100644 index 0000000..bf9009d --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/CalcEngine/Functions/DateAndTime.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ClosedXML.Excel.CalcEngine.Functions +{ + internal static class DateAndTime + { + public static void Register(CalcEngine ce) + { + ce.RegisterFunction("DATE", 3, Date); // Returns the serial number of a particular date + ce.RegisterFunction("DATEVALUE", 1, Datevalue); // Converts a date in the form of text to a serial number + //ce.RegisterFunction("DAY", 1, Day); // Converts a serial number to a day of the month + //ce.RegisterFunction("DAYS360", 1, Days360); // Calculates the number of days between two dates based on a 360-day year + //ce.RegisterFunction("EDATE", 1, Edate); // Returns the serial number of the date that is the indicated number of months before or after the start date + //ce.RegisterFunction("EOMONTH", 1, Eomonth); // Returns the serial number of the last day of the month before or after a specified number of months + //ce.RegisterFunction("HOUR", 1, Hour); // Converts a serial number to an hour + //ce.RegisterFunction("MINUTE", 1, Minute); // Converts a serial number to a minute + //ce.RegisterFunction("MONTH", 1, Month); // Converts a serial number to a month + //ce.RegisterFunction("NETWORKDAYS", 1, Networkdays); // Returns the number of whole workdays between two dates + //ce.RegisterFunction("NOW", 1, Now); // Returns the serial number of the current date and time + //ce.RegisterFunction("SECOND", 1, Second); // Converts a serial number to a second + //ce.RegisterFunction("TIME", 1, Time); // Returns the serial number of a particular time + //ce.RegisterFunction("TIMEVALUE", 1, Timevalue); // Converts a time in the form of text to a serial number + //ce.RegisterFunction("TODAY", 1, Today); // Returns the serial number of today's date + //ce.RegisterFunction("WEEKDAY", 1, Weekday); // Converts a serial number to a day of the week + //ce.RegisterFunction("WEEKNUM", 1, Weeknum); // Converts a serial number to a number representing where the week falls numerically with a year + //ce.RegisterFunction("WORKDAY", 1, Workday); // Returns the serial number of the date before or after a specified number of workdays + //ce.RegisterFunction("YEAR", 1, Year); // Converts a serial number to a year + //ce.RegisterFunction("YEARFRAC", 1, Yearfrac); // Returns the year fraction representing the number of whole days between start_date and end_date + + } + + static object Date(List p) + { + var year = (int)p[0]; + var month = (int)p[1]; + var day = (int)p[2]; + + return (int)Math.Floor(new DateTime(year, month, day).ToOADate()); + } + + static object Datevalue(List p) + { + var date = (string)p[0]; + + return (int)Math.Floor(DateTime.Parse(date).ToOADate()); + } + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML_Net3.5/ClosedXML_Net3.5.csproj b/ClosedXML/ClosedXML/ClosedXML_Net3.5/ClosedXML_Net3.5.csproj index 2b8329e..8da1ad5 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Net3.5/ClosedXML_Net3.5.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Net3.5/ClosedXML_Net3.5.csproj @@ -174,6 +174,12 @@ Excel\CalcEngine\FunctionDefinition.cs + + Excel\CalcEngine\Functions\Database.cs + + + Excel\CalcEngine\Functions\DateAndTime.cs + Excel\CalcEngine\Functions\Logical.cs diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj index 71c551e..ed9b2d6 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -76,6 +76,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/DateAndTimeTests.cs b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/DateAndTimeTests.cs new file mode 100644 index 0000000..b1b0c4e --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/CalcEngine/DateAndTimeTests.cs @@ -0,0 +1,31 @@ +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; +using System.Linq; +using ClosedXML.Excel; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ClosedXML_Tests.Excel.DataValidations +{ + /// + /// Summary description for UnitTest1 + /// + [TestClass] + public class DateAndTimeTests + { + [TestMethod] + public void Date() + { + Object actual = XLWorkbook.EvaluateExpr("Date(2008, 1, 1)"); + Assert.AreEqual(39448, actual); + } + + [TestMethod] + public void Datevalue() + { + Object actual = XLWorkbook.EvaluateExpr("DateValue(\"8/22/2008\")"); + Assert.AreEqual(39682, actual); + } + } +}