Skip to content

#2007 - fix for CircularReferenceException with dynamic arrays #2008

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

Merged
merged 2 commits into from
May 23, 2025
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
16 changes: 13 additions & 3 deletions src/EPPlus/FormulaParsing/DependencyChain/RpnFormulaExecution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OfficeOpenXml.Core.CellStore;
using OfficeOpenXml.Core.RangeQuadTree;
using OfficeOpenXml.FormulaParsing.Excel.Functions;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup.LookupUtils;
using OfficeOpenXml.FormulaParsing.Excel.Operators;
using OfficeOpenXml.FormulaParsing.Exceptions;
Expand Down Expand Up @@ -915,16 +916,25 @@ private static RangeHashset AddOrGetRDFromWsIx(RpnOptimizedDependencyChain depCh
private static void CheckCircularReferences(RpnOptimizedDependencyChain depChain, RpnFormula f, FormulaRangeAddress address, ExcelCalculationOption options)
{
if (f._ws == null) return;
if(f._arrayIndex>=0)
var wsIx = f._ws?.IndexInList ?? ushort.MaxValue;
if (f._arrayIndex>=0)
{
var sf = f._ws._sharedFormulas[f._arrayIndex];
var fa = new FormulaRangeAddress(depChain._parsingContext) { FromRow = sf.StartRow, ToRow = sf.EndRow, FromCol = sf.StartCol, ToCol = sf.EndCol, WorksheetIx = f._ws.IndexInList };
if (fa.CollidesWith(address) != eAddressCollition.No)
{
throw new CircularReferenceException($"Circular reference in array formula: {fa.Address}");
if(!options.AllowCircularReferences)
{
throw new CircularReferenceException($"Circular reference in array formula: {fa.Address}");
}
else
{
var toCell = ExcelCellBase.GetCellId(wsIx, sf.StartRow, sf.StartCol);
var fromCell = ExcelCellBase.GetCellId(f._ws.IndexInList, f._row, f._column);
depChain._circularReferences.Add(new CircularReference(fromCell, toCell));
}
}
}
var wsIx=f._ws?.IndexInList ?? ushort.MaxValue;
if (address.CollidesWith(wsIx, f._row, f._column))
{
var fId = ExcelCellBase.GetCellId(f._ws.IndexInList, f._row, f._column);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ internal abstract class ChooseBaseFunction : ExcelFunction
{
public override string NamespacePrefix => "_xlfn.";
public override int ArgumentMinLength => 2;

public override ExcelFunctionParametersInfo ParametersInfo => new ExcelFunctionParametersInfo(new Func<int, FunctionParameterInformation>((argumentIndex) =>
{
if (argumentIndex == 0)
{
return FunctionParameterInformation.IgnoreAddress;
}
return FunctionParameterInformation.Normal;
}));

protected List<int> GetChooseColumns(IList<FunctionArgument> arguments, out eErrorType? ev)
{
var cols = new List<int>();
Expand Down
18 changes: 18 additions & 0 deletions src/EPPlusTest/Issues/FormulaCalculationIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,24 @@ public void s858()

Assert.AreEqual(12977661.57, result2);
}

[TestMethod]
public void Issue864()
{
using var p1 = OpenTemplatePackage(@"sc864.xlsx");
var sheet = p1.Workbook.Worksheets["Aico data"];
try
{
sheet.Calculate(o => o.AllowCircularReferences = true);
}
catch(Exception ex)
{
int i = 0;
}

var f = sheet.Cells["C42"].Formula;
var v = sheet.Cells["C42"].Value;
}
}
}