EPPlus returns #VALUE! instead of the cell content when used with Formula OFFSET, INDIRECT & MATCH

Ash

I have an Excel file with a table tblPhoneCode and with two columns Country and Code, and I have a cell B1 with List Data Validation pointing to the Country column

enter image description here

and a cell B2 basically it displays the Code for the selected Country.

enter image description here

Cell B2 uses the following formula

OFFSET(INDIRECT("tblPhoneCode[#Headers]"),MATCH(B1,INDIRECT("tblPhoneCode[Country]"),0),1,1,1)

Everything in Excel works as it should, but the issue is when I am reading the value of B2 using EPPlus in C# I am getting #VALUE! instead of the actual Phone Code. I've tried .Calculate() from workbook, worksheet, to cell and tried to access the value is still the same. I've attached the logger and it turns up empty and there is no error logged in it.

C# Code

static void Main(string[] args)
{
    var excelFile = new FileInfo(@"C:\Users\Ash\Desktop\Epplus.xlsx");
    using (var package = new ExcelPackage(excelFile))
    {
        // Output from the logger will be written to the following file
        var logfile = new FileInfo(@"C:\Users\Ash\Desktop\EpplusLogFile.txt");
        // Attach the logger before the calculation is performed.
        package.Workbook.FormulaParserManager.AttachLogger(logfile);
        // Calculate - can also be executed on sheet- or range level.
        package.Workbook.Calculate();

        Debug.Print(String.Format("Country: \t{0}", package.Workbook.Worksheets[1].Cells["B1"].Value));
        Debug.Print(String.Format("Phone Code:\t{0}", package.Workbook.Worksheets[1].Cells["B2"].Value));

        // The following method removes any logger attached to the workbook.
        package.Workbook.FormulaParserManager.DetachLogger();
    }
}

Output:

Country:    US
Phone Code: #VALUE!

Any help or insight is much appreciated, I am using MS Excel 2010, .NET 4.0, EPPlus 4.1.0, and Windows 10 64bit

kuujinbo

Created a quick replica of your spreadsheet:

enter image description here

and confirmed the same results you're getting - cell B2 has a value of #VALUE!. From reading the documentation / EPPlus source code examples, the result is surprising, and looks like a bug or 'feature'. Went through some of the other ExcelWorksheet and ExcelWorkbook members, and found setting the ExcelWorkbook.FullCalcOnLoad property solves the issue:

using (var package = new ExcelPackage(fileInfo))
{
    // UPDATED answer - doesn't look like this is needed either
    // package.Workbook.FullCalcOnLoad = true;
    // same result as question code if Calculate() is called instead: '#VALUE!' 
    // package.Workbook.Calculate();
    var ws = package.Workbook.Worksheets[1];
    Console.WriteLine("Country: \t{0}", ws.Cells["B1"].Value);
    Console.WriteLine("Phone Code:\t{0}", ws.Cells["B2"].Value.ToString());
}

Output:

Country:        Germany
Phone Code:     49

UPDATE: After posting, checked the default value of FullCalcOnLoad, which appears to be true. And indeed, removing both the Calculate() call and leaving FullCalcOnLoad at it's default (not setting the property) seems to work as well, and gives the desired output.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you get the formula from a cell instead of the value?

How to change cell reference when dragging an INDIRECT formula?

How to set value of Excel cell by name instead of using coordinates with EPPlus?

Is it possible to copy the Excel formula of a cell instead of the value using python?

Excel: Use value of cell instead of formula in another formula

EPPlus cell.calculate() error when formula contains TRIM function

Trying to find the content of a cell next to the array formula's match

Indirect expansion returns variable name instead of value

reference a cell value inside a formula (indirect)

Excel Indirect command returns #value

I'm getting the formula instead of the cell content

Reading a cell value that contains a formula returns 0.0 when using xlrd

Evaluating the content of a cell for a formula

Excel setting an indirect cell value

Excel Formula - FORECAST, OFFSET, MATCH

Counting non-blank cell range using R1C1, an indirect cell reference, and an offset value

VBA Offset.Value returns empty for a full cell

How to let Visio shapesheet SETF() get the cell formula instead of the value?

How to make Excel represent cell value as it is instead of formula?

VBA - Show Formula in cell when variable value

AverageIf formula through VBA macro returns "False" instead of a value

OFFSET and MATCH issue. MATCH value not being recognised as reference within OFFSET formula

Average Offset Index Match formula

How to dynamically get cell value in a Filter Match formula in Google Sheets?

How to select a value from another MATCH formula if first MATCH formula returns null in excel

How do I return a cell's value instead of it's formula?

Formula Works in cell but not when used in conditional formatting

See cell content instead of formula in formulabox in Excel using VBA

VBA code or formula to Extract and Insert Value based on cell content