예제 #1
0
        /// <summary>
        /// The try extrapolate.
        /// </summary>
        /// <param name="cell">
        /// The cell.
        /// </param>
        /// <param name="p1">
        /// The p 1.
        /// </param>
        /// <param name="p2">
        /// The p 2.
        /// </param>
        /// <param name="result">
        /// The result.
        /// </param>
        /// <returns>
        /// The try extrapolate.
        /// </returns>
        private bool TryExtrapolate(CellRef cell, CellRef p1, CellRef p2, out object result)
        {
            try
            {
                var v1 = this.GetCellValue(p1);
                var v2 = this.GetCellValue(p2);

                try
                {
                    double d1 = Convert.ToDouble(v1);
                    double d2 = Convert.ToDouble(v2);
                    v1 = d1;
                    v2 = d2;
                }
                catch (Exception)
                {
                }

                int    deltaColumns = p2.Column - p1.Column;
                int    deltaRows    = p2.Row - p1.Row;
                double f            = 0;
                if (cell.Column < p1.Column || cell.Column > p2.Column)
                {
                    if (deltaColumns > 0)
                    {
                        f = 1.0 * (cell.Column - p1.Column) / deltaColumns;
                    }
                }

                if (cell.Row < p1.Row || cell.Row > p2.Row)
                {
                    if (deltaRows > 0)
                    {
                        f = 1.0 * (cell.Row - p1.Row) / deltaRows;
                    }
                }

                if (f == 0)
                {
                    result = v1;
                    return(true);
                }

                object tmp1, tmp2, tmp3;
                ReflectionMath.TrySubtract(v2, v1, out tmp1);
                ReflectionMath.TryMultiply(tmp1, f, out tmp2);
                ReflectionMath.TryAdd(v1, tmp2, out tmp3);
                result = tmp3;
                return(true);
            }
            catch
            {
                result = null;
                return(false);
            }
        }
        /// <summary>
        /// Tries to extrapolate.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <param name="p1">The first source cell.</param>
        /// <param name="p2">The second source cell.</param>
        /// <param name="result">The result.</param>
        /// <returns>
        /// True is extrapolation was successful.
        /// </returns>
        // ReSharper disable once UnusedMethodReturnValue.Local
        private bool TryExtrapolate(CellRef cell, CellRef p1, CellRef p2, out object result)
        {
            try
            {
                var v1 = this.getCellValue(p1);
                var v2 = this.getCellValue(p2);

                if (v1 == null || v2 == null)
                {
                    result = null;
                    return(false);
                }

                int    deltaColumns = p2.Column - p1.Column;
                int    deltaRows    = p2.Row - p1.Row;
                double f            = 0;
                if (cell.Column < p1.Column || cell.Column > p2.Column)
                {
                    if (deltaColumns > 0)
                    {
                        f = 1.0 * (cell.Column - p1.Column) / deltaColumns;
                    }
                }

                if (cell.Row < p1.Row || cell.Row > p2.Row)
                {
                    if (deltaRows > 0)
                    {
                        f = 1.0 * (cell.Row - p1.Row) / deltaRows;
                    }
                }

                if (f.Equals(0))
                {
                    result = v1;
                    return(true);
                }

                object tmp1, tmp2, tmp3;
                ReflectionMath.TrySubtract(v2, v1, out tmp1);
                if (tmp1 == null)
                {
                    result = null;
                    return(false);
                }

                ReflectionMath.TryMultiply(tmp1, f, out tmp2);
                if (tmp2 == null)
                {
                    result = null;
                    return(false);
                }

                ReflectionMath.TryAdd(v1, tmp2, out tmp3);

                result = tmp3;
                return(tmp3 != null);
            }
            catch
            {
                result = null;
                return(false);
            }
        }