[]
        
(Showing Draft Content)

GrapeCity.Documents.Excel.Expressions.FormulaSyntaxTree

FormulaSyntaxTree Class

Represents a formula as a syntax tree.

This class is the entry point for the formula expression API. A FormulaSyntaxTree can be created as an empty tree and then populated by assigning a root SyntaxNode, or it can be created from formula text by using Parse(string). After the tree is built, you can inspect its structure with Root or convert it back to formula text with ToString().

Inheritance
FormulaSyntaxTree
Implements
Namespace: GrapeCity.Documents.Excel.Expressions
Assembly: DS.Documents.Excel.dll
Syntax
public class FormulaSyntaxTree : ICloneable
Public Class FormulaSyntaxTree
    Implements ICloneable
Examples
string formula = "LET(AppUpTime,NOW()-DATE(2020,4,17)+366, YEAR(AppUpTime)-1900-1 & \" years\")";
FormulaSyntaxTree syntaxTree = FormulaSyntaxTree.Parse(formula);
FunctionNode find = new FunctionNode("NOW");
FunctionNode replacement = new FunctionNode("DATE");
replacement.Children.Add(new NumberNode(2021));
replacement.Children.Add(new NumberNode(2));
replacement.Children.Add(new NumberNode(14));

Stack<SyntaxNode> nodes = new Stack<SyntaxNode>();
nodes.Push(syntaxTree.Root);
while (nodes.Count > 0)
{
    IList<SyntaxNode> children = nodes.Pop().Children;
    for (int i = 0; i < children.Count; i++)
    {
        SyntaxNode child = children[i];
        if (child.Equals(find))
        {
            children[i] = replacement;
        }
        else
        {
            nodes.Push(child);
        }
    }
}
string formulaText = syntaxTree.ToString();

Constructors

Name Description
FormulaSyntaxTree()

Initializes a new instance of the FormulaSyntaxTree class.

Properties

Name Description
Root

Gets the root element of the syntax tree for this formula.

Methods

Name Description
Clone()

Creates a copy of the FormulaSyntaxTree instance.

Equals(object)

Indicates whether this formula syntax tree is equal to the specified object.

Two FormulaSyntaxTree instances are considered equal when the specified object is also a FormulaSyntaxTree and their root syntax nodes are equal. This method returns false if obj is null or is not a FormulaSyntaxTree.

GetHashCode()

This object doesn't support getting hash code, because all fields are mutable.

Parse(string)

Creates a new FormulaSyntaxTree from a formula string.

The specified text should contain only the formula expression. It should not start with = and should not be surrounded with {= }. If text is null or empty, this method returns an empty FormulaSyntaxTree.

Use Parse(string, ParseContext) when the formula needs parsing options such as base row, base column, or R1C1 reference style.

Parse(string, ParseContext)

Parses formula text into a FormulaSyntaxTree by using the specified ParseContext.

Use the parse context to control how references in the formula text are interpreted, including the zero-based base row, zero-based base column, and whether the formula uses R1C1 reference style.

If text is null or empty, this method returns an empty FormulaSyntaxTree whose root node is null.

ToString()

Returns the formula text represented by this syntax tree.

This method returns the same result as calling ToString(UnParseContext) with the default unparse context. If this syntax tree has no root node, an empty string is returned.

ToString(UnParseContext)

Returns the text for this formula, optionally specifies the formatting behavior.