Skip to content

Commit

Permalink
Merge pull request #182 from spreadsheetlab/fix-issue-151
Browse files Browse the repository at this point in the history
Support user defined functions in parser references
  • Loading branch information
joerivv authored Sep 14, 2023
2 parents 728e211 + 963434b commit e790709
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/XLParser.Tests/FormulaAnalysisTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,22 @@ public void MultiAreaRangeFormula()
Assert.AreEqual("Sheet1!$A$1", references[0].LocationString);
Assert.AreEqual("Sheet1!$B$2", references[1].LocationString);
}

[TestMethod]
public void UserDefinedFunction()
{
List<ParserReference> references = new FormulaAnalyzer(@"='C:\AddIns\Something.xlam'!fT_Value(A1)").ParserReferences().ToList();
Assert.AreEqual(2, references.Count);

Assert.AreEqual(ReferenceType.UserDefinedFunction, references[0].ReferenceType);
Assert.AreEqual(@"C:\AddIns\", references[0].FilePath);
Assert.AreEqual("Something.xlam", references[0].FileName);
Assert.AreEqual("fT_Value", references[0].Name);
Assert.AreEqual(@"'C:\AddIns\Something.xlam'!fT_Value", references[0].LocationString);

Assert.AreEqual(ReferenceType.Cell, references[1].ReferenceType);
Assert.AreEqual("A1", references[1].LocationString);
}
#endregion

#region Depth() and ConditionalComplexity()
Expand Down
12 changes: 11 additions & 1 deletion src/XLParser/ParserReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum ReferenceType
HorizontalRange,
VerticalRange,
RefError,
Table
Table,
UserDefinedFunction
}

public class ParserReference
Expand Down Expand Up @@ -102,10 +103,19 @@ public void InitializeReference(ParseTreeNode node)
case GrammarNames.RefError:
ReferenceType = ReferenceType.RefError;
break;
case GrammarNames.UDFunctionCall:
ReferenceType = ReferenceType.UserDefinedFunction;
Name = node.ChildNodes[0].ChildNodes[0].Token.ValueString.TrimEnd('(');
break;
}

ReferenceNode = node;
LocationString = node.Print();

if (ReferenceType == ReferenceType.UserDefinedFunction && Name != null)
{
LocationString = LocationString.Substring(0, LocationString.IndexOf('(', LocationString.LastIndexOf(Name, System.StringComparison.Ordinal)));
}
}

private string UnEscape(string value, string escapeCharacter)
Expand Down

0 comments on commit e790709

Please sign in to comment.