EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Circular Reference detection does not consider workbook

Open colbybhearn opened this issue 2 years ago • 2 comments

Good day, gentlemen.

We have worksheet called LinkTab in workbook wb1 containing formulas that reference cells in a worksheet called LinkTab in workbook wb2. The sheets were named identically like this as part of a convention for humans to readily know the linked cells between the two workbooks.

With the external link intact in wb1, EPPlus has always said there's a circular reference despite Excel saying there are none. I now think Epplus is not considering the containing workbook when looking for circular references while resolving the address. Since wb1's LinkTab sheet contains references to a sheet called "LinkTab" (despite it being a reference to a sheet in the external reference wb2), EPPlus throws CircularReferenceExceptions. I had ExcelCalculationOption.AllowCircularReferences set to false, as I knew the Excel workbooks were free of circular references. For a long time, we just always broke the links in wb1 to get around this whole mystery in production.

https://github.com/EPPlusSoftware/EPPlus/blob/ca1bd9cbdcee1608e348df9fb0318e6139202883/src/EPPlus/FormulaParsing/DependencyChain/DependenyChainFactory.cs#L351-L359

Now, this is not terribly new - it has been going since v5 when I started using EPPlus. We realized this piece of the puzzle only recently and I confirmed our idea solution by renaming the sheet in wb2 from LinkTab to LinkTab2 while both workbooks were simultaneously open in Excel. The CircularReferenceExceptions stopped being thrown subsequently, despite the wb1 link being intact and AllowCircularReferences still set to false.

Also, as a side note, the Circular Reference exceptions thrown at the bottom of DependencyChainFactory included the wrong tab name. The cell address relative to a sheet has always turned out to be correct, but the sheet name being reported never lines up. However, I did not verify that it's also wrong outside my false positive circular reference scenario.

I hope this is helpful and clear. I'm happy to provide more detail if not. -Colby

colbybhearn avatar Feb 09 '23 16:02 colbybhearn

Apologies for the delayed response to this issue. I had totally missed. it. EPPlus only checks for circular references within the workbook. No checks are made on external references at this time. I will check the issue wrong worksheet name, to make sure the correct worksheet name is returned in the exception. Using the new EPPlus 7 preview, just release, might fix this issue, as this part has been rewritten.

JanKallman avatar May 19 '23 12:05 JanKallman

No worries on overlooking. Thank you all so much for investigating and doing what you do!

colbybhearn avatar Jul 13 '23 21:07 colbybhearn