Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date functions throw exceptions for blank and empty cells #2321

Open
3 of 6 tasks
Brainman643 opened this issue Apr 2, 2024 · 0 comments
Open
3 of 6 tasks

Date functions throw exceptions for blank and empty cells #2321

Brainman643 opened this issue Apr 2, 2024 · 0 comments

Comments

@Brainman643
Copy link

Brainman643 commented Apr 2, 2024

Read and complete the full issue template

Do not randomly delete sections. They are here for a reason.

Do you want to request a feature or report a bug?

  • Bug
  • Feature
  • Question

Did you test against the latest CI build?

  • Yes
  • No

Version of ClosedXML

0.104.0-preview3

What is the current behavior?

If the cell is blank date functions throw System.InvalidCastException with the message "Null object cannot be converted to a value type."
If the cell is an empty string date functions throw System.FormatException with the message "String '' was not recognized as a valid DateTime."

What is the expected behavior or new feature?

If the cell is blank date functions return the same value as if the cell had zero value.
If the cell is an empty string date functions return an "error" value.

Is this a regression from the previous version?

No, it's not. I also checked version 0.102.2.

Reproducibility

I wrote two tests.

Code to reproduce problem:

        [TestCase(31, "EOMONTH(A1,0)")] //System.InvalidCastException : Null object cannot be converted to a value type.
        [TestCase(5, "A1+5")] //Passed
        public void DateFormula_ReturnCorrectValue_WhenCellIsBlank(int expectedValue, string formula)
        {
            using (var wb = new XLWorkbook())
            {
                wb.ReferenceStyle = XLReferenceStyle.A1;

                var ws = wb.AddWorksheet("Sheet1");

                Assert.AreEqual(expectedValue, ws.Evaluate(formula).GetNumber());
            }
        }

        [TestCase("EOMONTH(A1,0)")] //System.FormatException : String '' was not recognized as a valid DateTime.
        [TestCase("A1+1")] //Passed
        public void DateFormula_ReturnError_WhenCellIsEmpty(string formula)
        {
            using (var wb = new XLWorkbook())
            {
                wb.ReferenceStyle = XLReferenceStyle.A1;

                var ws = wb.AddWorksheet("Sheet1");
                ws.Cell("A1").SetValue("");

                Assert.IsTrue(ws.Evaluate(formula).IsError);
            }
        }
  • I attached a sample spreadsheet. (You can drag files on to this issue)
    In the file wb.xlsx cell A1 contains the formula for a blank cell, cell B1 contains the formula for an empty string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant