[]
CloneableThis 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 getRoot() or convert it back to formula text with toString().
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.getChildren().add(new NumberNode(2021));
replacement.getChildren().add(new NumberNode(2));
replacement.getChildren().add(new NumberNode(14));
java.util.Stack<SyntaxNode> nodes = new java.util.Stack<SyntaxNode>();
nodes.push(syntaxTree.getRoot());
while (!nodes.isEmpty()) {
java.util.List<SyntaxNode> children = nodes.pop().getChildren();
for (int i = 0; i < children.size(); i++) {
SyntaxNode child = children.get(i);
if (child.equals(find)) {
children.set(i, replacement);
} else {
nodes.push(child);
}
}
}
String formulaText = syntaxTree.toString();
final FormulaSyntaxTreeclone()FormulaSyntaxTree instance.booleanfinal SyntaxNodegetRoot()inthashCode()FormulaSyntaxTree instance.static FormulaSyntaxTreeFormulaSyntaxTree from a formula string.static FormulaSyntaxTreeParse(String text,
ParseContext context) FormulaSyntaxTree by using the specified ParseContext.final voidsetRoot(SyntaxNode value) toString()final StringtoString(UnParseContext context)
FormulaSyntaxTree syntaxTree = FormulaSyntaxTree.Parse("SUM(A1:A3)+B1");
SyntaxNode root = syntaxTree.getRoot();
String formulaText = root.toString();
SyntaxNode of the formula syntax tree.Use this method to replace the current formula expression with a different SyntaxNode. Passing null clears the current root node.
String formula = "SUM(A1:A3)+B1";
FormulaSyntaxTree syntaxTree = FormulaSyntaxTree.Parse(formula);
SyntaxNode root = SyntaxNode.Parse("A1*A2");
syntaxTree.setRoot(root);
value - The root SyntaxNode to assign to the syntax tree, or null to clear the current root node.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.
String formula = "SUM(A1:A3)+B1";
FormulaSyntaxTree syntaxTree = FormulaSyntaxTree.Parse(formula);
SyntaxNode root = syntaxTree.getRoot();
text - The formula text to parse. This value can be null or empty.FormulaSyntaxTree populated from the specified formula text, or an empty FormulaSyntaxTree if text is null or empty.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.
ParseContext context = new ParseContext();
context.setBaseRow(1);
context.setBaseColumn(4);
context.setIsR1C1(true);
FormulaSyntaxTree syntaxTree = FormulaSyntaxTree.Parse("R1C:R3C[1]", context);
SyntaxNode root = syntaxTree.getRoot();
text - The formula text to parse. If text is null or empty, an empty FormulaSyntaxTree is returned.context - The parse context that specifies the base row, base column, and reference style used to interpret the formula text.FormulaSyntaxTree created from the specified formula text. Returns an empty FormulaSyntaxTree if text is null or empty.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.
String formula = "SUM(A1:A3)+B1";
FormulaSyntaxTree syntaxTree = FormulaSyntaxTree.Parse(formula);
String formulaText = syntaxTree.toString();
Use UnParseContext to control how references are converted when the formula text is generated, such as the base row, base column, and reference style.
ParseContext parseContext = new ParseContext();
parseContext.setIsR1C1(true);
FormulaSyntaxTree tree = FormulaSyntaxTree.Parse("R1C:R2C[1]", parseContext);
UnParseContext unparseContext = new UnParseContext();
unparseContext.setBaseRow(0);
unparseContext.setBaseColumn(2);
String formulaText = tree.toString(unparseContext);
context - The context that specifies how the syntax tree is converted to formula text, or null to omit conversion options.FormulaSyntaxTree instance.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.
equals in class Objectobj - The object to compare with this formula syntax tree. May be null.true if obj is a FormulaSyntaxTree with an equal root syntax node; otherwise, false.FormulaSyntaxTree instance.This method is not intended to provide a content-based hash code for the syntax tree. Because the tree is mutable, the returned value is based on the runtime type rather than the current formula structure.