Пример #1
0
        public void BasePos()
        {
            var pos1 = new ReoGridPos(0, 0);
            AssertEquals(pos1.Row, 0);
            AssertEquals(pos1.Col, 0);

            var pos2 = new ReoGridPos(10, 20);
            AssertEquals(pos2.Row, 10);
            AssertEquals(pos2.Col, 20);

            AssertTrue(pos1 != pos2);
            AssertEquals(pos2, new ReoGridPos(10, 20));
        }
Пример #2
0
        public void CodeFormat()
        {
            var pos1 = new ReoGridPos(0, 0);
            var pos2 = new ReoGridPos(10, 20);

            AssertEquals(pos1.ToStringCode(), "A1");
            AssertEquals(pos2.ToStringCode(), "U11");

            AssertEquals(pos1, new ReoGridPos("A1"));
            AssertEquals(pos2, new ReoGridPos("U11"));

            var r1 = new ReoGridRange(0, 0, 1, 1);
            var r2 = new ReoGridRange(0, 0, 3, 3);
            var r3 = new ReoGridRange(10, 20, 5, 5);

            AssertEquals(r1.ToStringCode(), "A1:A1");
            AssertEquals(r2.ToStringCode(), "A1:C3");
            AssertEquals(r3.ToStringCode(), "U11:Y15");

            AssertEquals(r1, new ReoGridRange("A1:A1"));
            AssertEquals(r2, new ReoGridRange("A1:C3"));
            AssertEquals(r3, new ReoGridRange("U11:Y15"));
        }
Пример #3
0
        public object Evaluate(ReoGridCell cell, ICompiledFormula cformula)
        {
            var grid = this.Workbook;

            if (grid == null)
            {
                return(null);
            }

            var formulaContext = (ReoScriptCompiledFormula)cformula;

            //var cell = formulaContext.Cell;
            var formula = formulaContext.Formula;

            List <ReferenceRange> referencedRanges = formulaContext.ReferencedCellOrRanges as List <ReferenceRange>;

            if (referencedRanges == null)
            {
                formulaContext.ReferencedCellOrRanges = referencedRanges = new List <ReferenceRange>();
            }
            else
            {
                referencedRanges.Clear();
            }

            // todo: improve: only create script context once
            //                when set data to a range
            var ctx = grid.Srm.CreateContext();

            // create an global variable getter
            ctx.ExternalVariableGetter = (id) =>
            {
#if FORMULA_CELL_INSTANCE_REF
                if (id.StartsWith("$"))
                {
                    var address = id.Substring(1);
                    if (ReoGridPos.IsValidAddress(address))
                    {
                        var pos = new ReoGridPos(address);
                        return(new RSCellObject(this, pos, cells[pos.Row, pos.Col]));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
#endif // FORMULA_CELL_INSTANCE_REF
                if (ReoGridPos.IsValidAddress(id))
                {
                    var pos = new ReoGridPos(id);
                    referencedRanges.Add(new ReferenceRange(grid, pos));

                    var cell = grid.GetCell(pos);
                    return(cell == null ? 0 : cell.InnerData);
                }
                else
                {
                    NamedRange range = grid.GetNamedRange(id);

                    if (range != null)
                    {
                        referencedRanges.Add(range);

                        var referencedCell = grid.GetCell(range.StartPos);
                        return((referencedCell == null || referencedCell.InnerData == null) ? 0 : referencedCell.InnerData);
                    }
                    else
                    {
                        return(null);
                    }
                }
            };

            try
            {
                // preprocess range syntax
                formula = RGUtility.RangeReferenceRegex.Replace(formula, (m) =>
                {
                    if (m.Groups["to_col"].Length > 0 && m.Groups["to_row"].Length > 0 &&
                        m.Groups["from_col"].Length > 0 && m.Groups["from_row"].Length > 0)
                    {
                        // range
                        int fromRow = -1;
                        if (!int.TryParse(m.Groups["from_row"].Value, out fromRow))
                        {
                            return("null");
                        }
                        fromRow--;

                        int toRow = -1;
                        if (!int.TryParse(m.Groups["to_row"].Value, out toRow))
                        {
                            return("null");
                        }
                        toRow--;

                        int fromCol = RGUtility.GetNumberOfChar(m.Groups["from_col"].Value);
                        int toCol   = RGUtility.GetNumberOfChar(m.Groups["to_col"].Value);

                        if (fromRow < 0)
                        {
                            fromRow = 0;
                        }
                        if (fromCol < 0)
                        {
                            fromCol = 0;
                        }
                        if (toRow > grid.RowCount - 1)
                        {
                            toRow = grid.RowCount - 1;
                        }
                        if (toCol > grid.RowCount - 1)
                        {
                            toCol = grid.ColumnCount - 1;
                        }

                        ReoGridRange range = new ReoGridRange(fromRow, fromCol, toRow - fromRow + 1, toCol - fromCol + 1);
                        referencedRanges.Add(new ReferenceRange(grid, range));

                        return(string.Format("new Range({0},{1},{2},{3})", range.Row, range.Col, range.Rows, range.Cols));
                    }
                    else
                    {
                        return(m.Value);
                    }
                });

                return(grid.Srm.CalcExpression(formula, ctx));
            }
            catch (ReoScriptException ex)
            {
                Logger.Log("formula", string.Format("error to evaluate formula: ", ex.Message));
                throw new FormulaEvalutionException(ex, "#ERR: " + ex.Message);
            }
        }
Пример #4
0
 public RGPositionEventArgs(ReoGridPos pos)
 {
     this.Position = pos;
 }
Пример #5
0
 internal Point CellPositionToControl(ReoGridPos pos)
 {
     return Grid.ViewportController.CellPositionToControl(pos);
 }
Пример #6
0
 public RGCellMouseEventArgs(ReoGridControl grid, ReoGridCell cell, ReoGridPos cellPosition,
     Point cursorPos, MouseButtons buttons, int clicks)
 {
     this.Grid = grid;
     this.Cell = cell;
     this.CursorPosition = cursorPos;
     this.Buttons = buttons;
     this.Clicks = clicks;
     this.CellPosition = cellPosition;
 }
Пример #7
0
 public RGCellMouseEventArgs(ReoGridControl grid, ReoGridPos cellPosition, Point cursorPos, MouseButtons buttons, int clicks)
     : this(grid, null, cellPosition, cursorPos, buttons, clicks)
 {
 }
Пример #8
0
 public RGCellMouseEventArgs(ReoGridControl grid, ReoGridPos cellPosition)
     : this(grid, null, cellPosition, Point.Empty, MouseButtons.None, 0)
 {
 }
Пример #9
0
 public ReoGridCellNotFoundException(ReoGridPos pos)
     : base(pos)
 {
 }
Пример #10
0
 public ReoGridCellException(ReoGridPos pos)
 {
     this.Pos = pos;
 }
Пример #11
0
 /// <summary>
 /// Recalculate and get the value of formula stored in the specified cell.
 /// </summary>
 /// <param name="pos">Position of cell to be recalculated</param>
 public void RecalcCell(ReoGridPos pos)
 {
     RecalcCell(pos.Row, pos.Col);
 }
Пример #12
0
        private void UpdateValueByCursorX(ReoGridPos cellPos, int x)
        {
            // calcutate value by cursor position
            int value = (int)Math.Round(x * 100f / (Bounds.Width - 2f));

            if (value < 0) value = 0;
            if (value > 100) value = 100;

            Grid.SetCellData(cellPos, value);
        }