Exemplo n.º 1
0
        private static List <Token> ResolveFormulaReferences(string formula, TotalsFunctionHelper totalsCalculator, IEnumerable <CacheFieldNode> calculatedFields)
        {
            var resolvedFormulaTokens = new List <Token>();
            var tokens = totalsCalculator.Tokenize(formula);

            foreach (var token in tokens)
            {
                if (token.TokenType == TokenType.NameValue)
                {
                    // If a token references another calculated field, resolve the chain of formulas.
                    var field = calculatedFields.FirstOrDefault(f => f.Name.IsEquivalentTo(token.Value));
                    if (field != null)
                    {
                        var resolvedReferences = PivotTableDataManager.ResolveFormulaReferences(field.Formula, totalsCalculator, calculatedFields);
                        resolvedFormulaTokens.Add(new Token("(", TokenType.OpeningParenthesis));
                        resolvedFormulaTokens.AddRange(resolvedReferences);
                        resolvedFormulaTokens.Add(new Token(")", TokenType.ClosingParenthesis));
                    }
                    else
                    {
                        resolvedFormulaTokens.Add(token);
                    }
                }
                else
                {
                    resolvedFormulaTokens.Add(token);
                }
            }
            return(resolvedFormulaTokens);
        }