Example #1
0
        public void Start()
        {
            // remove old sprites
            battleground.Sprites.Clear();

            // clear script context, all of global variables will be removed
            srm.Reset();

            // create an extended function to create .NET color object
            srm["rgb"] = new NativeFunctionObject("rgb", (ctx, owner, args) =>
            {
                int r = ScriptRunningMachine.GetIntParam(args, 0, 0);
                int g = ScriptRunningMachine.GetIntParam(args, 1, 0);
                int b = ScriptRunningMachine.GetIntParam(args, 2, 0);

                // this object will be returned into the world of script
                return(Color.FromArgb(r, g, b));
            });

            // add battleground object
            srm["battleground"] = battleground;

            // run script
            srm.Run(editor.Text);
        }
Example #2
0
        public void ReoScriptFunction()
        {
            // custom function in script
            Grid.RunScript("script.myfun = data => '[' + data + ']'; ");
            worksheet[10, 0] = "=myfun(\"abc\")";
            AssertEquals(worksheet.GetCellText(10, 0), "[abc]");

            // custom function in .NET
            Grid.Srm["add"] = new NativeFunctionObject("add", (ctx, owner, args) =>
            {
                int v1 = ScriptRunningMachine.GetIntParam(args, 0);
                int v2 = ScriptRunningMachine.GetIntParam(args, 1);
                return(v1 + v2);
            });

            worksheet[0, 10] = "=add(2,3)";
            AssertEquals(worksheet.GetCellText(0, 10), "5");
        }
Example #3
0
        public RSWorksheet(Worksheet sheet)
        {
            this.sheet         = sheet;
            sheet.worksheetObj = this;

            #region Attributes
            this["readonly"] = new ExternalProperty(
                () => { return(sheet.HasSettings(WorksheetSettings.Edit_Readonly)); },
                (v) => { sheet.SetSettings(WorksheetSettings.Edit_Readonly, (v as bool?) ?? false); });
            #endregion

            #region Selection
            this["selection"] = new ExternalProperty(() =>
            {
                if (this.selection == null)
                {
                    this.selection = new RSSelectionObject(sheet);
                }
                return(this.selection);
            }, (obj) =>
            {
                sheet.SelectionRange = RSUtility.GetRangeFromValue(sheet, obj);
            });

            this["selectRange"] = new NativeFunctionObject("selectRange", (srm, owner, args) =>
            {
                RangePosition range = RSUtility.GetRangeFromArgs(sheet, args);
                if (range.IsEmpty)
                {
                    return(false);
                }

                try
                {
                    sheet.SelectRange(range);
                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
            });

            this["selectionMode"] = new ExternalProperty(() => null);
            #endregion

            #region Rows & Cols
            this["rows"]   = new ExternalProperty(() => { return(sheet.RowCount); }, (v) => { sheet.SetRows(ScriptRunningMachine.GetIntValue(v)); });
            this["cols"]   = new ExternalProperty(() => { return(sheet.ColumnCount); }, (v) => { sheet.SetCols(ScriptRunningMachine.GetIntValue(v)); });
            this["getRow"] = new NativeFunctionObject("getRow", (srm, owner, args) =>
            {
                return(args.Length == 0 ? null : new RSRowObject(sheet, sheet.GetRowHeader(ScriptRunningMachine.GetIntValue(args[0]))));
            });
            this["getCol"] = new NativeFunctionObject("getCol", (srm, owner, args) =>
            {
                return(args.Length == 0 ? null : new RSColumnObject(sheet, sheet.GetColumnHeader(ScriptRunningMachine.GetIntValue(args[0]))));
            });
            #endregion

            #region Cell & Style
            this["setRangeStyle"] = new NativeFunctionObject("setRangeStyle", (ctx, owner, args) =>
            {
                if (args.Length < 1)
                {
                    return(false);
                }

                RangePosition range          = RSUtility.GetRangeFromValue(sheet, args[0]);
                WorksheetRangeStyle styleObj = RSUtility.GetRangeStyleObject(args[0]);

                sheet.SetRangeStyles(range, styleObj);

                return(styleObj);
            });

            this["getCell"] = new NativeFunctionObject("getCell", (srm, owner, args) =>
            {
                if (args.Length < 1)
                {
                    return(null);
                }

                CellPosition pos = RSUtility.GetPosFromValue(sheet, args);

                return(new RSCellObject(sheet, pos, sheet.GetCell(pos)));
            });

            #endregion

            #region Range
            this["mergeRange"] = new NativeFunctionObject("mergeRange", (srm, owner, args) =>
            {
                RangePosition range = RSUtility.GetRangeFromArgs(sheet, args);
                if (range.IsEmpty)
                {
                    return(false);
                }

                try
                {
                    sheet.MergeRange(range);
                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
            });

            this["unmergeRange"] = new NativeFunctionObject("unmergeRange", (srm, owner, args) =>
            {
                RangePosition range = RSUtility.GetRangeFromArgs(sheet, args);
                if (range.IsEmpty)
                {
                    return(false);
                }

                try
                {
                    sheet.UnmergeRange(range);
                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
            });
            #endregion

            this["reset"] = new NativeFunctionObject("reset", (ctx, owner, args) =>
            {
                if (args.Length == 2)
                {
                    sheet.Reset(ScriptRunningMachine.GetIntParam(args, 0, 1),
                                ScriptRunningMachine.GetIntParam(args, 1, 1));
                }
                else
                {
                    sheet.Reset();
                }
                return(null);
            });

            this["focuspos"] = new ExternalProperty(() => sheet.FocusPos,
                                                    (focuspos) => sheet.FocusPos = RSUtility.GetPosFromValue(sheet, focuspos));

            this["fixPos"] = new NativeFunctionObject("fixPos", (ctx, own, args) =>
            {
                var pos = RSUtility.GetPosFromArgs(this.sheet, args);
                return(RSUtility.CreatePosObject(this.sheet.FixPos(pos)));
            });

            this["fixRange"] = new NativeFunctionObject("fixRange", (ctx, own, args) =>
            {
                var range = RSUtility.GetRangeFromArgs(this.sheet, args);
                return(new RSRangeObject(this.sheet, this.sheet.FixRange(range)));
            });
        }