Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

FormulaEngine.Parse(String) Method

Parses the specified formula using the default context.

Namespace: DevExpress.Spreadsheet.Formulas

Assembly: DevExpress.Spreadsheet.v24.2.Core.dll

NuGet Package: DevExpress.Spreadsheet.Core

#Declaration

ParsedExpression Parse(
    string formula
)

#Parameters

Name Type Description
formula String

A string that is the expression to parse. The leading equals “=” symbol is optional.

#Returns

Type Description
ParsedExpression

A ParsedExpression object providing access to the expression tree.

#Remarks

This example uses the FormulaEngine.Parse method to parse an expression and create an expression tree. The expression tree is available by using the ParsedExpression.Expression property.

You can use an expression tree visitor to traverse an existing expression tree and to perform required actions when it visits a specific node. To implement a visitor, create a descendant from the ExpressionVisitor class and override the ExpressionVisitor.Visit method which corresponds to the node type you wish to visit.

In this code snippet, the MyVisitor visitor changes the range reference to shift it down by one row.

After the visitor finishes, the ParsedExpression.ToString method is used to assemble a valid formula from the expression tree.

View Example

Imports DevExpress.Spreadsheet
Imports DevExpress.Spreadsheet.Formulas
Imports DevExpress.Spreadsheet.Functions
            Dim engine As FormulaEngine = spreadsheetControl1.Document.FormulaEngine
            If spreadsheetControl1.ActiveCell IsNot Nothing Then
                ' Get the formula for parsing. 
                Dim formula As String = spreadsheetControl1.ActiveCell.Formula
                If formula <> String.Empty Then
                    ' Parse the formula
                    Dim p_expr As ParsedExpression = engine.Parse(formula)
                    ' Traverse the expression tree and modify it.
                    Dim visitor As New MyVisitor()
                    p_expr.Expression.Visit(visitor)
                    ' Reconstitute the formula.
                    Dim formula1 As String = p_expr.ToString()
                    ' Place the formula back to the cell.
                    spreadsheetControl1.ActiveCell.Formula = formula1
                End If
public class MyVisitor : DevExpress.Spreadsheet.Formulas.ExpressionVisitor
{
    public override void Visit(DevExpress.Spreadsheet.Formulas.CellReferenceExpression expression)
    {
        base.Visit(expression);

        expression.CellArea.TopRowIndex++;
        expression.CellArea.BottomRowIndex++;
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Parse(String) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also