officefileapi-devexpress-dot-spreadsheet-dot-formulas-1c156ca4.md
A base class for implementing a custom visitor to traverse the expression tree.
Namespace : DevExpress.Spreadsheet.Formulas
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
public abstract class ExpressionVisitor :
IExpressionVisitor
Public MustInherit Class ExpressionVisitor
Implements IExpressionVisitor
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.
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
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Formulas;
using DevExpress.Spreadsheet.Functions;
FormulaEngine engine = spreadsheetControl1.Document.FormulaEngine;
if (spreadsheetControl1.ActiveCell != null){
// Get the formula for parsing.
string formula = spreadsheetControl1.ActiveCell.Formula;
if (formula != string.Empty)
{
// Parse the formula
ParsedExpression p_expr = engine.Parse(formula);
// Traverse the expression tree and modify it.
MyVisitor visitor = new MyVisitor();
p_expr.Expression.Visit(visitor);
// Reconstitute the formula.
string formula1 = p_expr.ToString();
// Place the formula back to the cell.
spreadsheetControl1.ActiveCell.Formula = formula1;
}
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++;
}
}
Public Class MyVisitor
Inherits DevExpress.Spreadsheet.Formulas.ExpressionVisitor
Public Overrides Sub Visit(ByVal expression As DevExpress.Spreadsheet.Formulas.CellReferenceExpression)
MyBase.Visit(expression)
expression.CellArea.TopRowIndex += 1
expression.CellArea.BottomRowIndex += 1
End Sub
End Class
Object ExpressionVisitor
See Also