Skip to content

Commit 81572f2

Browse files
authored
Update FormulaParser.cs
Add support for embedded formulae
1 parent d1d7361 commit 81572f2

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Source/ExcelDna.IntelliSense/UIMonitor/FormulaParser.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Text.RegularExpressions;
34

45
namespace ExcelDna.IntelliSense
@@ -8,16 +9,28 @@ static class FormulaParser
89
// Set from IntelliSenseDisplay.Initialize
910
public static char ListSeparator = ',';
1011

11-
// TODO: This needs a proper implementation, considering subformulae
1212
internal static bool TryGetFormulaInfo(string formulaPrefix, out string functionName, out int currentArgIndex)
1313
{
14-
var match = Regex.Match(formulaPrefix, @"^=(?<functionName>[\w|.]*)\(");
15-
if (match.Success)
14+
formulaPrefix = Regex.Replace(formulaPrefix, "(\"[^\"]*\")|(\\([^\\(\\)]*\\))| ", string.Empty);
15+
16+
while (Regex.IsMatch(formulaPrefix, "\\([^\\(\\)]*\\)"))
17+
{
18+
formulaPrefix = Regex.Replace(formulaPrefix, "\\([^\\(\\)]*\\)", string.Empty);
19+
}
20+
21+
int lastOpeningParenthesis = formulaPrefix.LastIndexOf("(", formulaPrefix.Length - 1, StringComparison.Ordinal);
22+
23+
if (lastOpeningParenthesis > -1)
1624
{
17-
functionName = match.Groups["functionName"].Value;
18-
currentArgIndex = formulaPrefix.Count(c => c == ListSeparator);
19-
return true;
25+
var match = Regex.Match(formulaPrefix.Substring(0, lastOpeningParenthesis), @"[^\w](?<functionName>\w*)$");
26+
if (match.Success)
27+
{
28+
functionName = match.Groups["functionName"].Value;
29+
currentArgIndex = formulaPrefix.Substring(lastOpeningParenthesis, formulaPrefix.Length - lastOpeningParenthesis).Count(c => c == ListSeparator);
30+
return true;
31+
}
2032
}
33+
2134
functionName = null;
2235
currentArgIndex = -1;
2336
return false;

0 commit comments

Comments
 (0)