Example #1
0
        internal static WorksheetRangeStyle GetRangeStyleObject(object p)
        {
            if (p is WorksheetRangeStyle)
            {
                return((WorksheetRangeStyle)p);
            }
            else if (p is ObjectValue)
            {
                var obj = (ObjectValue)p;

                WorksheetRangeStyle style = new WorksheetRangeStyle();

                SolidColor color;

                object backColor = obj["backgroundColor"];
                if (TextFormatHelper.DecodeColor(ScriptRunningMachine.ConvertToString(backColor), out color))
                {
                    style.Flag     |= PlainStyleFlag.BackColor;
                    style.BackColor = color;
                }

                return(style);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        public RSCellObject(Worksheet sheet, CellPosition pos, Cell cell)
        {
            this.sheet = sheet;
            this.Pos   = pos;
            this.Cell  = cell;

            this["style"] = new ExternalProperty(() =>
            {
                if (cell == null)
                {
                    cell = sheet.CreateAndGetCell(pos);
                }
                if (this.Style == null)
                {
                    this.Style = new RSCellStyleObject(sheet, cell);
                }
                return(this.Style);
            }, null);

            this["data"] = new ExternalProperty(
                () => cell == null ? null : cell.InnerData,
                (v) =>
            {
                if (cell == null)
                {
                    sheet[pos] = v;
                }
                else if (!cell.IsReadOnly)
                {
                    sheet[cell] = v;
                }
            });

#if FORMULA
            this["formula"] = new ExternalProperty(
                () => cell == null ? string.Empty : cell.InnerFormula,
                (v) =>
            {
                if (cell == null)
                {
                    sheet.SetCellFormula(pos, ScriptRunningMachine.ConvertToString(v));
                }
                else if (!cell.IsReadOnly)
                {
                    cell.Formula = ScriptRunningMachine.ConvertToString(v);
                }
            });
#endif // FORMULA

            this["text"] = new ExternalProperty(() => cell == null ? string.Empty : cell.DisplayText);

            this["pos"] = new ExternalProperty(() => cell == null ? Pos : cell.InternalPos);
            this["row"] = new ExternalProperty(() => cell == null ? Pos.Row : cell.InternalRow);
            this["col"] = new ExternalProperty(() => cell == null ? Pos.Col : cell.InternalCol);

            this["address"] = new ExternalProperty(() => cell.Position.ToAddress());
        }
Example #3
0
        public RSCellStyleObject(Worksheet sheet, Cell cell)
        {
            this.sheet = sheet;
            this.Cell  = cell;

            this["backgroundColor"] = new ExternalProperty(
                () => TextFormatHelper.EncodeColor(cell.InnerStyle.BackColor),
                (v) =>
            {
                SolidColor color;

                if (TextFormatHelper.DecodeColor(ScriptRunningMachine.ConvertToString(v), out color))
                {
                    this.sheet.SetCellStyleOwn(cell.InternalPos, new WorksheetRangeStyle
                    {
                        Flag      = PlainStyleFlag.BackColor,
                        BackColor = color,
                    });

                    this.sheet.RequestInvalidate();
                }
            });

            this["color"] = new ExternalProperty(
                () => TextFormatHelper.EncodeColor(cell.InnerStyle.TextColor),
                (v) =>
            {
                SolidColor color;

                if (TextFormatHelper.DecodeColor(ScriptRunningMachine.ConvertToString(v), out color))
                {
                    this.sheet.SetCellStyleOwn(cell.InternalPos, new WorksheetRangeStyle
                    {
                        Flag      = PlainStyleFlag.TextColor,
                        TextColor = color,
                    });

                    this.sheet.RequestInvalidate();
                }
            });

            this["fontName"] = new ExternalProperty(() => cell.Style.FontName,
                                                    (v) => cell.Style.FontName = ScriptRunningMachine.ConvertToString(v));

            this["fontSize"] = new ExternalProperty(
                () => cell.Style.FontSize,
                (v) => cell.Style.FontSize = TextFormatHelper.GetFloatPixelValue(ScriptRunningMachine.ConvertToString(v),
                                                                                 System.Drawing.SystemFonts.DefaultFont.Size));

            this["align"] = new ExternalProperty(
                () => cell.Style.HAlign,
                (v) =>
            {
                this.sheet.SetCellStyleOwn(cell.InternalPos, new WorksheetRangeStyle
                {
                    Flag   = PlainStyleFlag.HorizontalAlign,
                    HAlign = XmlFileFormatHelper.DecodeHorizontalAlign(ScriptRunningMachine.ConvertToString(v)),
                });

                this.sheet.RequestInvalidate();
            });

            this["valign"] = new ExternalProperty(
                () => cell.Style.VAlign,
                (v) =>
            {
                this.sheet.SetCellStyleOwn(cell.InternalPos, new WorksheetRangeStyle
                {
                    Flag   = PlainStyleFlag.VerticalAlign,
                    VAlign = XmlFileFormatHelper.DecodeVerticalAlign(ScriptRunningMachine.ConvertToString(v)),
                });

                this.sheet.RequestInvalidate();
            });
        }
Example #4
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])));
                }
            });
        }
Example #5
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine(
                    @"ReoScript(TM) Running Machine
Copyright(c) 2012-2013 unvell, All Rights Reserved.

Usage: ReoScript.exe [filename|-workpath|-debug|-exec|-console]");
                return;
            }


            List <string> files      = new List <string>();
            string        workPath   = null;
            bool          debug      = false;
            string        initScript = null;

            bool consoleMode = false;
            bool compileMode = false;

            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    string arg = args[i];

                    if (arg.StartsWith("-"))
                    {
                        string param = arg.Substring(1);

                        switch (param)
                        {
                        case "workpath":
                            workPath = GetParameter(args, i);
                            i++;
                            break;

                        case "debug":
                            debug = true;
                            break;

                        case "exec":
                            initScript = GetParameter(args, i);
                            i++;
                            break;

                        case "com":
                            compileMode = true;
                            break;

                        case "console":
                            consoleMode = true;
                            break;
                        }
                    }
                    else
                    {
                        files.Add(arg);
                    }
                }
            }
            catch (Exception ex)
            {
                OutLn(ex.Message);
                return;
            }

            List <FileInfo> sourceFiles = new List <FileInfo>();

            foreach (string file in files)
            {
                FileInfo fi = new FileInfo(string.IsNullOrEmpty(workPath)
                                        ? file : Path.Combine(workPath, file));

                if (!fi.Exists)
                {
                    Console.WriteLine("Resource not found: " + fi.FullName);
                }
                else
                {
                    sourceFiles.Add(fi);

                    if (string.IsNullOrEmpty(workPath))
                    {
                        workPath = fi.DirectoryName;
                    }
                }
            }

            if (string.IsNullOrEmpty(workPath))
            {
                workPath = Environment.CurrentDirectory;
            }

            // for test!
            if (compileMode)
            {
                using (StreamReader sr = new StreamReader(new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    Console.WriteLine(unvell.ReoScript.Compiler.ReoScriptCompiler.Run(sr.ReadToEnd()));
                }
                return;
            }

            // create SRM
            ScriptRunningMachine srm = new ScriptRunningMachine(CoreFeatures.FullFeatures);

            if (debug)
            {
                new ScriptDebugger(srm);
            }

            // set to full work mode
            srm.WorkMode |= MachineWorkMode.AllowImportTypeInScript
                            | MachineWorkMode.AllowCLREventBind
                            | MachineWorkMode.AllowDirectAccess;

            // change work path
            srm.WorkPath = workPath;

            // add built-in output listener
            srm.AddStdOutputListener(new BuiltinConsoleOutputListener());

            // not finished yet!
            //srm.SetGlobalVariable("File", new FileConstructorFunction());

            try
            {
                foreach (FileInfo file in sourceFiles)
                {
                    // load script file
                    srm.Run(file);
                }

                if (!string.IsNullOrEmpty(initScript))
                {
                    srm.Run(initScript);
                }
            }
            catch (ReoScriptException ex)
            {
                string str = string.Empty;

                if (ex.ErrorObject == null)
                {
                    str = ex.ToString();
                }
                else if (ex.ErrorObject is ErrorObject)
                {
                    ErrorObject e = (ErrorObject)ex.ErrorObject;

                    str += "Error: " + e.GetFullErrorInfo();
                }
                else
                {
                    str += Convert.ToString(ex.ErrorObject);
                }

                Console.WriteLine(str);
            }

            if (consoleMode)
            {
                OutLn("\nReady.\n");

                bool isQuitRequired = false;

                while (!isQuitRequired)
                {
                    Prompt();

                    string line = In().Trim();
                    if (line == null)
                    {
                        isQuitRequired = true;
                        break;
                    }
                    else if (line.StartsWith("."))
                    {
                        srm.Load(line.Substring(1, line.Length - 1));
                    }
                    else if (line.StartsWith("/"))
                    {
                        string consoleCmd = line.Substring(1);

                        switch (consoleCmd)
                        {
                        case "q":
                        case "quit":
                        case "exit":
                            isQuitRequired = true;
                            break;

                        case "h":
                        case "help":
                            Help();
                            break;

                        default:
                            break;
                        }
                    }
                    else if (line.Equals("?"))
                    {
                        ObjectValue obj = srm.DefaultContext.ThisObject as ObjectValue;
                        if (obj != null)
                        {
                            OutLn(obj.DumpObject());
                        }
                    }
                    else if (line.StartsWith("?"))
                    {
                        string expression = line.Substring(1);
                        try
                        {
                            object value = srm.CalcExpression(expression);
                            if (value is ObjectValue)
                            {
                                OutLn(((ObjectValue)value).DumpObject());
                            }
                            else
                            {
                                OutLn(ScriptRunningMachine.ConvertToString(value));
                            }
                        }
                        catch (Exception ex)
                        {
                            OutLn("error: " + ex.Message);
                        }
                    }
                    else if (line.Length == 0)
                    {
                        continue;
                    }
                    else
                    {
                        try
                        {
                            srm.Run(line);
                        }
                        catch (ReoScriptException ex)
                        {
                            Console.WriteLine("error: " + ex.Message + "\n");
                        }
                    }
                }

                OutLn("Bye.");
            }
        }
Example #6
0
        public void Run()
        {
            if (IsDebugMode)
            {
                OutLn("ReoScript Machine Console (ver1.1)");
                OutLn("type /help to see help topic.\n");
            }

            foreach (string file in fileList)
            {
                try
                {
                    if (!IsQuietMode)
                    {
                        Out("loading " + file + "... ");
                    }
                    srm.Load(file);
                    if (!IsQuietMode)
                    {
                        OutLn("ok.");
                    }
                }
                catch (Exception ex) {
                    if (!IsQuietMode)
                    {
                        OutLn("failed: " + ex.Message);
                    }
                }
            }

            if (IsDebugMode)
            {
                if (!IsQuietMode)
                {
                    OutLn("\nReady.\n");
                }

                bool isQuitRequired = false;

                while (!isQuitRequired)
                {
                    Prompt();

                    string line = In().Trim();
                    if (line == null)
                    {
                        isQuitRequired = true;
                        break;
                    }
                    else if (line.StartsWith("."))
                    {
                        srm.Load(line.Substring(1, line.Length - 1));
                    }
                    else if (line.StartsWith("/"))
                    {
                        string consoleCmd = line.Substring(1);

                        switch (consoleCmd)
                        {
                        case "q":
                        case "quit":
                        case "exit":
                            isQuitRequired = true;
                            break;

                        case "h":
                        case "help":
                            Help();
                            break;

                        default:
                            break;
                        }
                    }
                    else if (line.Equals("?"))
                    {
                        OutLn(srm.GlobalObject.DumpObject());
                    }
                    else if (line.StartsWith("?"))
                    {
                        string expression = line.Substring(1);
                        try
                        {
                            object value = srm.CalcExpression(expression);
                            OutLn(value == null ? "undefined" : value.ToString());
                        }
                        catch (Exception ex)
                        {
                            OutLn("error: " + ex.Message);
                        }
                    }
                    else if (line.Length == 0)
                    {
                        continue;
                    }
                    else
                    {
                        try
                        {
                            var res = srm.Run(line);

                            if (res is ObjectValue)
                            {
                                OutLn(((ObjectValue)res).DumpObject());
                            }
                            else
                            {
                                OutLn(ScriptRunningMachine.ConvertToString(res));
                            }
                        }
                        catch (ReoScriptException ex)
                        {
                            Console.WriteLine("error: " + ex.Message + "\n");
                        }
                    }
                }

                OutLn("Bye.");
            }
        }