Exemple #1
0
        /// <summary>
        /// Get range information from script value
        /// </summary>
        /// <param name="sheet">worksheet instance</param>
        /// <param name="args">script value to be converted</param>
        /// <returns></returns>
        public static RangePosition GetRangeFromArgs(Worksheet sheet, object[] args)
        {
            if (args.Length == 0)
            {
                return(RangePosition.Empty);
            }

            if (args.Length == 1)
            {
                return(GetRangeFromValue(sheet, args[0]));
            }
            else
            {
                RangePosition range = RangePosition.Empty;

                range.Row = Convert.ToInt32(args[0]);
                if (args.Length > 1)
                {
                    range.Col = ScriptRunningMachine.GetIntValue(args[1]);
                }
                if (args.Length > 2)
                {
                    range.Rows = ScriptRunningMachine.GetIntValue(args[2]);
                }
                if (args.Length > 3)
                {
                    range.Cols = ScriptRunningMachine.GetIntValue(args[3]);
                }

                return(range);
            }
        }
Exemple #2
0
 public int this[string key]
 {
     get { return(arr[ScriptRunningMachine.GetIntValue(key.Substring(4))]); }
     set
     {
         int idx = ScriptRunningMachine.GetIntValue(key.Substring(4));
         arr[idx] = value;
     }
 }
Exemple #3
0
        /// <summary>
        /// Get range information from script value
        /// </summary>
        /// <param name="sheet">worksheet instance</param>
        /// <param name="arg">script object to be converted</param>
        /// <returns></returns>
        public static RangePosition GetRangeFromValue(Worksheet sheet, object arg)
        {
            if (arg is RangePosition)
            {
                return((RangePosition)arg);
            }
            else if (arg is string)
            {
                var        addr = (string)arg;
                NamedRange namedRange;
                if (RangePosition.IsValidAddress(addr))
                {
                    return(new RangePosition(addr));
                }
                else if (NamedRange.IsValidName(addr) &&
                         sheet.TryGetNamedRange(addr, out namedRange))
                {
                    return((RangePosition)namedRange);
                }
                else
                {
                    throw new InvalidAddressException(addr);
                }
            }
            else if (arg is ReferenceRange)
            {
                return(((ReferenceRange)arg).Position);
            }
            else if (arg is RSSelectionObject)
            {
                return(sheet.SelectionRange);
            }
            else if (arg is RSRangeObject)
            {
                return(((RSRangeObject)arg).Range);
            }

            ObjectValue obj = arg as ObjectValue;

            if (obj == null)
            {
                return(RangePosition.Empty);
            }

            RangePosition range = RangePosition.Empty;

            range.Row  = ScriptRunningMachine.GetIntValue(obj["row"]);
            range.Col  = ScriptRunningMachine.GetIntValue(obj["col"]);
            range.Rows = ScriptRunningMachine.GetIntValue(obj["rows"]);
            range.Cols = ScriptRunningMachine.GetIntValue(obj["cols"]);

            return(range);
        }
Exemple #4
0
        public RSColumnObject(Worksheet sheet, ColumnHeader colHeader)
        {
            this.sheet     = sheet;
            this.colHeader = colHeader;

            this["width"] = new ExternalProperty(
                () => colHeader.Width,
                (v) => colHeader.Width = (ushort)ScriptRunningMachine.GetIntValue(v));

            this["visible"] = new ExternalProperty(
                () => colHeader.IsVisible,
                (v) => colHeader.IsVisible = ScriptRunningMachine.GetBoolValue(v));
        }
Exemple #5
0
        public RSRowObject(Worksheet sheet, RowHeader rowHeader)
        {
            this.sheet     = sheet;
            this.rowHeader = rowHeader;

            this["height"] = new ExternalProperty(
                () => rowHeader.Height,
                (v) => rowHeader.Height = (ushort)ScriptRunningMachine.GetIntValue(v));

            this["visible"] = new ExternalProperty(
                () => rowHeader.IsVisible,
                (v) => rowHeader.IsVisible = ScriptRunningMachine.GetBoolValue(v));
        }
Exemple #6
0
        public static bool TryGetPosFromValue(Worksheet sheet, object arg, out CellPosition pos)
        {
            if (arg is object[])
            {
                object[] args = (object[])arg;

                return(TryGetPosFromArgs(sheet, args, out pos));
            }
            else if (arg is ObjectValue)
            {
                var obj = (ObjectValue)arg;

                pos = new CellPosition(ScriptRunningMachine.GetIntValue(obj["row"]),
                                       ScriptRunningMachine.GetIntValue(obj["col"]));
                return(true);
            }

            pos = CellPosition.Empty;
            return(false);
        }
Exemple #7
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Attach a property to global object using 'ExternalProperty'.
            //
            // ExternalProperty class provides a chance to do something, The delegate method
            // defined in ExternalProperty will be fired when the property be getted or setted
            // in script running.
            //
            srm.SetGlobalVariable("percent", new ExternalProperty(

                                      // property getter
                                      //
                                      // this will be called when property value is required in script.
                                      () => { return(trackValue.Value); },

                                      // property setter
                                      //
                                      // this will be called when a property value will be setted in script.
                                      (v) => { trackValue.Value = ScriptRunningMachine.GetIntValue(v); }

                                      ));
        }
Exemple #8
0
        public static bool TryGetPosFromArgs(Worksheet sheet, object[] args, out CellPosition pos)
        {
            if (args.Length == 0)
            {
                pos = CellPosition.Empty;
                return(false);
            }

            else if (args.Length == 1)
            {
                if (args[0] is CellPosition)
                {
                    pos = (CellPosition)args[0];
                    return(true);
                }
                else if (args[0] is string || args[0] is System.Text.StringBuilder)
                {
                    var addressOrName = Convert.ToString(args[0]);

                    if (CellPosition.IsValidAddress(addressOrName))
                    {
                        pos = new CellPosition(addressOrName);
                        return(true);
                    }

                    NamedRange namedRange;
                    if (sheet.TryGetNamedRange(addressOrName, out namedRange))
                    {
                        pos = namedRange.StartPos;
                        return(true);
                    }

                    pos = CellPosition.Empty;
                    return(false);
                }
                else if (args[0] is ObjectValue)
                {
                    var obj = (ObjectValue)args[0];

                    pos = new CellPosition(ScriptRunningMachine.GetIntValue(obj["row"]),
                                           ScriptRunningMachine.GetIntValue(obj["col"]));

                    return(true);
                }
            }
            else                     //if (arr.Length == 2)
            {
                pos = new CellPosition(ScriptRunningMachine.GetIntValue(args[0], 0),
                                       ScriptRunningMachine.GetIntValue(args[1], 0));

                return(true);
            }
            //ReoGridPos pos = ReoGridPos.Empty;

            //if (args.Length == 1)
            //{
            //	ObjectValue obj = args[0] as ObjectValue;
            //	if (obj == null) return ReoGridPos.Empty;

            //	pos.Row = ScriptRunningMachine.GetIntValue(obj["row"]);
            //	pos.Col = ScriptRunningMachine.GetIntValue(obj["col"]);
            //}
            //else
            //{
            //	pos.Row = Convert.ToInt32(args[0]);
            //	if (args.Length > 1) pos.Col = Convert.ToInt32(args[1]);
            //}

            //return pos;
            pos = CellPosition.Empty;
            return(false);
        }
Exemple #9
0
        public RSWorkbook(IWorkbook workbook)
        {
            this.workbook = workbook;

            this["worksheets"] = new ExternalProperty(() =>
            {
                if (worksheetCollection == null)
                {
                    worksheetCollection = new RSWorksheetCollection(workbook);
                }

                return(worksheetCollection);
            });

            this["currentWorksheet"] = new ExternalProperty(() =>
            {
                if (ControlInstance != null)
                {
                    var rsWorksheet = ControlInstance.CurrentWorksheet.worksheetObj;

                    if (rsWorksheet == null)
                    {
                        rsWorksheet = new RSWorksheet(ControlInstance.CurrentWorksheet);
                        ControlInstance.CurrentWorksheet.worksheetObj = rsWorksheet;
                    }

                    return(rsWorksheet);
                }
                else
                {
                    return(null);
                }
            });

            this["createWorksheet"] = new NativeFunctionObject("createWorksheet", (ctx, owner, args) =>
            {
                return(new RSWorksheet(workbook.CreateWorksheet(args.Length > 0 ? ScriptRunningMachine.ConvertToString(args[0]) : null)));
            });

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

                workbook.AddWorksheet(RSWorksheet.Unbox(args[0]));

                return(null);
            });

            this["insertWorksheet"] = new NativeFunctionObject("createWorksheet", (ctx, owner, args) =>
            {
                if (args.Length < 2)
                {
                    return(null);
                }

                workbook.InsertWorksheet(ScriptRunningMachine.GetIntValue(args[0]), RSWorksheet.Unbox(args[1]));

                return(null);
            });

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

                Worksheet sheet = RSWorksheet.Unbox(args[0]);

                if (sheet != null)
                {
                    return(workbook.RemoveWorksheet(sheet));
                }
                else
                {
                    return(workbook.RemoveWorksheet(ScriptRunningMachine.GetIntValue(args[0])));
                }
            });
        }
Exemple #10
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)));
            });
        }