ParseFormula

Namespace: OpenXmlPowerTools
We found 10 examples in language CSharp for this search. You will see 60 fragments of code.

        public void AddAccent(string formula, string accentName)
        {
            AddAccent(ParseFormula(formula), accentName);
        }

        public void AddEmbraced(string formula, char leftChar, char rightChar)
        {
            AddEmbraced(ParseFormula(formula), leftChar, rightChar);
        }

        public void AddEmbraced(string formula, string leftSymbol, string rightSymbol)
        {
            AddEmbraced(ParseFormula(formula), leftSymbol, rightSymbol);
        }

        public void AddFraction(string numerator, string denominator, bool drawLine)
        {
            AddFraction(ParseFormula(numerator), ParseFormula(denominator), drawLine);
        }

        public void AddFraction(string numerator, TexFormula denominator, bool drawLine)
        {
            AddFraction(ParseFormula(numerator), denominator, drawLine);
        }

        public void AddFraction(string numerator, string denominator, bool drawLine, TexAlignment numeratorAlignment,
            TexAlignment denominatorAlignment)
        {
            AddFraction(ParseFormula(numerator), ParseFormula(denominator), drawLine, numeratorAlignment,
                denominatorAlignment);
        }


        [Test]
        public static void AddElementToFormula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C2H3N2O");

            Element n = PeriodicTable.GetElement(7);

            formulaA.Add(n, 1);

            Assert.AreEqual(formulaA, formulaB);
        }

        [Test]
        public static void Multiply()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            formulaA.Multiply(2);
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C4H6N2O2");

            Assert.AreEqual(formulaA, formulaB);

            Assert.IsFalse(formulaA.Equals(null));
        }

        [Test]
        public static void AddFormulaToFormula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("H2O");
            ChemicalFormula formulaC = ChemicalFormula.ParseFormula("C2H5NO2");

            formulaA.Add(formulaB);

            Assert.AreEqual(formulaA, formulaC);
        }

        [Test]
        public static void AddFormulaToItself()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C4H6N2O2");

            formulaA.Add(new ChemicalFormula(formulaA));

            Assert.AreEqual(formulaA, formulaB);
        }

        [Test]
        public static void AddIChemicalFormulaToFormula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            IHasChemicalFormula formulaB = new PhysicalObjectWithChemicalFormula("H2O");
            ChemicalFormula formulaC = ChemicalFormula.ParseFormula("C2H5NO2");

            formulaA.Add(formulaB);

            Assert.AreEqual(formulaA, formulaC);
        }

        [Test]
        public static void AddIsotopeToFormula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C2H3H{1}NO");

            Isotope h1 = PeriodicTable.GetElement("H")[1];

            formulaA.Add(h1, 1);

            Assert.AreEqual(formulaA, formulaB);
        }


        public void AddAccent(string formula, string accentName)
        {
            AddAccent(ParseFormula(formula), accentName);
        }

        public void AddEmbraced(string formula, char leftChar, char rightChar)
        {
            AddEmbraced(ParseFormula(formula), leftChar, rightChar);
        }

        public void AddEmbraced(string formula, string leftSymbol, string rightSymbol)
        {
            AddEmbraced(ParseFormula(formula), leftSymbol, rightSymbol);
        }

        public void AddFraction(string numerator, string denominator, bool drawLine)
        {
            AddFraction(ParseFormula(numerator), ParseFormula(denominator), drawLine);
        }

        public void AddFraction(string numerator, TexFormula denominator, bool drawLine)
        {
            AddFraction(ParseFormula(numerator), denominator, drawLine);
        }

        public void AddFraction(string numerator, string denominator, bool drawLine, TexAlignment numeratorAlignment,
            TexAlignment denominatorAlignment)
        {
            AddFraction(ParseFormula(numerator), ParseFormula(denominator), drawLine, numeratorAlignment,
                denominatorAlignment);
        }

        [Fact]
        public void A1()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("A1"));

            TestHelper.AssertExpression(tree, Expression.Cell("A1", CellReferenceType.Relative));
        }

        [Fact]
        public void ADollar1()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("A$1"));

            TestHelper.AssertExpression(tree, Expression.Cell("A$1", CellReferenceType.Mixed));
        }

        [Fact]
        public void DollarA1()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("$A1"));

            TestHelper.AssertExpression(tree, Expression.Cell("$A1", CellReferenceType.Mixed));
        }

        [Fact]
        public void DollarADollar1()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("$A$1"));

            TestHelper.AssertExpression(tree, Expression.Cell("$A$1", CellReferenceType.Absolute));
        }

        [Fact]
        public void A1ToA4()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("A1:A4"));

            TestHelper.AssertExpression(
                tree,
                Expression.CellRange(
                    Expression.Cell("A1", CellReferenceType.Relative),
                    Expression.Cell("A4", CellReferenceType.Relative)
                )
             );
        }

        [Fact]
        public void DollarA1ToADollar4()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("$A1:A$4"));

            TestHelper.AssertExpression(
                tree,
                Expression.CellRange(
                    Expression.Cell("$A1", CellReferenceType.Mixed),
                    Expression.Cell("A$4", CellReferenceType.Mixed)
                )
             );
        }

        [Fact]
        public void Test_Formula_Is_1()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("1"));

            TestHelper.AssertExpression(tree, Expression.Number(1));
        }

        [Fact]
        public void Test_Formula_Is_1Expo2()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("1E-2"));

            TestHelper.AssertExpression(tree, Expression.Number(0.01));
        }

        [Fact]
        public void Test_Formula_Is_10_Percent()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("10%"));

            TestHelper.AssertExpression(tree, Expression.Number(0.1));
        }

        [Fact]
        public void Test_Formula_Is_Negative_1()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("-1"));

            TestHelper.AssertExpression(
                tree,
                Expression.UnaryExpression(UnaryOperatorType.Negate, Expression.Number(1)));
        }

        [Fact]
        public void Test_Formula_Is_String__abc__()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("\"abc\""));

            TestHelper.AssertExpression(tree, Expression.Text("abc"));
        }

        [Fact]
        public void Test_Formula_Is_TRUE()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("TRUE"));

            TestHelper.AssertExpression(tree, Expression.Logical(true));
        }


        private TexFormula ParseFormula(string source) =>
            _formulaParser.Parse(new SourceSpan("Predefined formula fragment", source, 0, source.Length));

        public void SetFixedTypes(TexAtomType leftType, TexAtomType rightType)
        {
            this.Formula.RootAtom = new TypedAtom(this._source, this.Formula.RootAtom, leftType, rightType);
        }

        public void CenterOnAxis()
        {
            this.Formula.RootAtom = new VerticalCenteredAtom(this._source, this.Formula.RootAtom);
        }

        public void AddAccent(string formula, string accentName)
        {
            AddAccent(ParseFormula(formula), accentName);
        }

        public void AddAccent(TexFormula baseAtom, string accentName)
        {
            this.Add(new AccentedAtom(_source, baseAtom?.RootAtom, accentName));
        }

        public void AddAccent(TexFormula baseAtom, TexFormula accent)
        {
            this.Add(new AccentedAtom(null, baseAtom?.RootAtom, accent));
        }

        [Fact]
        public void Sum()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("SUM()"));

            TestHelper.AssertExpression(tree, Expression.FunctionCall("SUM"));
        }

        [Fact]
        public void NegateSum()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("-SUM()"));

            TestHelper.AssertExpression(tree, 
                Expression.UnaryExpression(
                    UnaryOperatorType.Negate,
                    Expression.FunctionCall("SUM")
                )
            );
        }

        [Fact]
        public void SumOne()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("SUM(1)"));

            TestHelper.AssertExpression(tree,
                Expression.FunctionCall(
                    "SUM",
                    Expression.Number(1)        
                )
            );
        }

        [Fact]
        public void SumOneAndTwo()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("SUM(1, 2)"));

            TestHelper.AssertExpression(tree,
                Expression.FunctionCall(
                    "SUM",
                    Expression.Number(1),
                    Expression.Number(2)
                )
            );
        }

        [Fact]
        public void SumOneAndSumTwoAndThree()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("SUM(1, SUM(2, 3))"));

            TestHelper.AssertExpression(tree,
                Expression.FunctionCall(
                    "SUM",
                    Expression.Number(1),
                    Expression.FunctionCall(
                        "SUM",
                        Expression.Number(2),
                        Expression.Number(3)
                    )
                )
            );
        }

        [Fact]
        public void SumTenDivideFourAndSumTwoAndThree()
        {
            var tree = ExpressionTreeBuilder.ParseFormula(Tokenizer.Tokenize("SUM(10 / 4, SUM(2, 3))"));

            TestHelper.AssertExpression(tree,
                Expression.FunctionCall(
                    "SUM",
                    Expression.BinaryExpression(
                        BinaryOperatorType.Divide,
                        Expression.Number(10),
                        Expression.Number(4)
                    ),
                    Expression.FunctionCall(
                        "SUM",
                        Expression.Number(2),
                        Expression.Number(3)
                    )
                )                
            );
        }

        /// <summary>
        ///  Some of the tests are depending on the american culture.
        /// </summary>
        [SetUp]
        public void PrepareCulture()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
        }


        /**
         * @return Parsed token array alReady Confirmed not <c>null</c>
         */
        /* package */
        public static Ptg[] ParseFormula(String formula)
        {
            Ptg[] result = HSSFFormulaParser.Parse(formula, null);
            Assert.IsNotNull(result, "Ptg array should not be null");
            return result;
        }
        [Test]
        public void TestSimpleFormula()
        {
            Ptg[] ptgs = ParseFormula("2+2");
            Assert.AreEqual(3, ptgs.Length);
        }
        [Test]
        public void TestFormulaWithSpace1()
        {
            Ptg[] ptgs = ParseFormula(" 2 + 2 ");
            Assert.AreEqual(3, ptgs.Length);
            Assert.IsTrue((ptgs[0] is IntPtg));
            Assert.IsTrue((ptgs[1] is IntPtg));
            Assert.IsTrue((ptgs[2] is AddPtg));
        }
        [Test]
        public void TestFormulaWithSpace2()
        {
            Ptg[] ptgs = ParseFormula("2+ sum( 3 , 4) ");
            Assert.AreEqual(5, ptgs.Length);
        }
        [Test]
        public void TestFormulaWithSpaceNRef()
        {
            Ptg[] ptgs = ParseFormula("sum( A2:A3 )");
            Assert.AreEqual(2, ptgs.Length);
        }

        /// <summary>
        ///  Some of the tests are depending on the american culture.
        /// </summary>
        [SetUp]
        public void PrepareCulture()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
        }


        /**
         * @return Parsed token array alReady Confirmed not <c>null</c>
         */
        /* package */
        public static Ptg[] ParseFormula(String formula)
        {
            Ptg[] result = HSSFFormulaParser.Parse(formula, null);
            Assert.IsNotNull(result, "Ptg array should not be null");
            return result;
        }
        [Test]
        public void TestSimpleFormula()
        {
            Ptg[] ptgs = ParseFormula("2+2");
            Assert.AreEqual(3, ptgs.Length);
        }
        [Test]
        public void TestFormulaWithSpace1()
        {
            Ptg[] ptgs = ParseFormula(" 2 + 2 ");
            Assert.AreEqual(3, ptgs.Length);
            Assert.IsTrue((ptgs[0] is IntPtg));
            Assert.IsTrue((ptgs[1] is IntPtg));
            Assert.IsTrue((ptgs[2] is AddPtg));
        }
        [Test]
        public void TestFormulaWithSpace2()
        {
            Ptg[] ptgs = ParseFormula("2+ sum( 3 , 4) ");
            Assert.AreEqual(5, ptgs.Length);
        }
        [Test]
        public void TestFormulaWithSpaceNRef()
        {
            Ptg[] ptgs = ParseFormula("sum( A2:A3 )");
            Assert.AreEqual(2, ptgs.Length);
        }


        /**
         * @return Parsed token array alReady Confirmed not <c>null</c>
         */
        /* package */
        public static Ptg[] ParseFormula(String formula)
        {
            Ptg[] result = HSSFFormulaParser.Parse(formula, null);
            Assert.IsNotNull(result, "Ptg array should not be null");
            return result;
        }
        [TestMethod]
        public void TestSimpleFormula()
        {
            Ptg[] ptgs = ParseFormula("2+2");
            Assert.AreEqual(3, ptgs.Length);
        }
        [TestMethod]
        public void TestFormulaWithSpace1()
        {
            Ptg[] ptgs = ParseFormula(" 2 + 2 ");
            Assert.AreEqual(3, ptgs.Length);
            Assert.IsTrue((ptgs[0] is IntPtg));
            Assert.IsTrue((ptgs[1] is IntPtg));
            Assert.IsTrue((ptgs[2] is AddPtg));
        }
        [TestMethod]
        public void TestFormulaWithSpace2()
        {
            Ptg[] ptgs = ParseFormula("2+ sum( 3 , 4) ");
            Assert.AreEqual(5, ptgs.Length);
        }
        [TestMethod]
        public void TestFormulaWithSpaceNRef()
        {
            Ptg[] ptgs = ParseFormula("sum( A2:A3 )");
            Assert.AreEqual(2, ptgs.Length);
        }
        [TestMethod]
        public void TestFormulaWithString()
        {
            Ptg[] ptgs = ParseFormula("\"hello\" & \"world\" ");
            Assert.AreEqual(3, ptgs.Length);
        }

ExcelFormula.ParseFormula : Object

Constructors :

public ParseFormula(String formula = )

Methods :

public String ReplaceSheetName(String oldName = , String newName = )
public String ReplaceRelativeCell(Int32 rowOffset = , Int32 colOffset = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()