Beispiel #1
0
        private void ResolveCellReference(object sender, SymbolEventArgs e)
        {
            var cell = table.GetCell(e.Name.ToUpper());

            if (cell != null)
            {
                var text = cell.GetText()
                           .Replace(AddIn.Culture.NumberFormat.CurrencySymbol, string.Empty)
                           .Replace(AddIn.Culture.NumberFormat.PercentSymbol, string.Empty);

                if (double.TryParse(text, out var value))
                {
                    maxdec = Math.Max(value.ToString().Length - ((int)value).ToString().Length - 1, maxdec);

                    e.Result = value;
                    e.Status = SymbolStatus.OK;
                }
                else
                {
                    e.Status = SymbolStatus.None;
                }
            }
            else
            {
                e.Status = SymbolStatus.UndefinedSymbol;
            }
        }
Beispiel #2
0
        /// <summary>
        /// This method evaluates a symbol name and returns its value.
        /// </summary>
        /// <param name="name">Name of symbol</param>
        /// <param name="pos">Position at start of symbol</param>
        /// <returns></returns>
        private double EvaluateSymbol(string name, int pos)
        {
            // built-in symbols

            if (string.Compare(name, "pi", true) == 0)
            {
                return(Math.PI);
            }
            else if (string.Compare(name, "e", true) == 0)
            {
                return(Math.E);
            }

            double result = default;

            // ask consumer to resolve symbol reference
            var status = SymbolStatus.UndefinedSymbol;

            if (ProcessSymbol != null)
            {
                var args = new SymbolEventArgs
                {
                    Name   = name,
                    Result = result,
                    Status = SymbolStatus.OK
                };

                ProcessSymbol(this, args);

                result = args.Result;
                status = args.Status;
            }

            if (status == SymbolStatus.UndefinedSymbol)
            {
                throw new FormulaException(string.Format(ErrUndefinedSymbol, name), pos);
            }

            if (status == SymbolStatus.None)
            {
                result = 0;
            }

            return(result);
        }