Ejemplo n.º 1
0
 internal static void Flush(IExcelContext ctx, int idx)
 {
     while (ctx.CmdValues.Count > idx)
     {
         ctx.CmdValues.Pop();
     }
 }
Ejemplo n.º 2
0
        void IExcelCommand.Execute(IExcelContext ctx, ref Action after)
        {
            var ctx2 = (ExcelContext)ctx;

            ctx2.WS    = ctx2.WB.Worksheets[Name];
            ctx.XStart = ctx.Y = 1;
        }
Ejemplo n.º 3
0
        public static object GetRowValue(this IExcelContext ctx, int row, RowValueKind valueKind)
        {
            var row_ = ((ExcelContext)ctx).WS.Row(row);

            switch (valueKind)
            {
            case RowValueKind.Collapsed: return(row_.Collapsed);

            case RowValueKind.CustomHeight: return(row_.CustomHeight);

            case RowValueKind.Height: return(row_.Height);

            case RowValueKind.Hidden: return(row_.Hidden);

            case RowValueKind.Merged: return(row_.Merged);

            case RowValueKind.OutlineLevel: return(row_.OutlineLevel);

            case RowValueKind.PageBreak: return(row_.PageBreak);

            case RowValueKind.Phonetic: return(row_.Phonetic);

            case RowValueKind.StyleName: return(row_.StyleName);

            default: throw new ArgumentOutOfRangeException(nameof(valueKind));
            }
        }
Ejemplo n.º 4
0
 internal static void Flush(IExcelContext ctx, int idx)
 {
     while (ctx.Sets.Count > idx)
     {
         ctx.Sets.Pop().Execute(ctx);
     }
 }
Ejemplo n.º 5
0
        public static CommandRtn ExecuteCol(this IExcelContext ctx, Collection <string> s, object v, out Action after)
        {
            var cr           = CommandRtn.None;
            var afterActions = new List <Action>();

            foreach (var cmd in ctx.CmdCols)
            {
                var r = cmd.Func(ctx, s, v);
                if ((r & CommandRtn.Execute) == CommandRtn.Execute)
                {
                    ctx.Frame = ctx.ExecuteCmd(cmd.Cmds, out Action action);
                    if (action != null)
                    {
                        afterActions.Add(action);
                    }
                }
                cr |= r;
            }
            after = afterActions.Count > 0 ? () => { foreach (var action in afterActions)
                                                     {
                                                         action?.Invoke();
                                                     }
            } : (Action)null;
            return(cr);
        }
Ejemplo n.º 6
0
        public static void RowValue(this IExcelContext ctx, int row, object value, RowValueKind valueKind)
        {
            var row_ = ((ExcelContext)ctx).WS.Row(row);

            switch (valueKind)
            {
            case RowValueKind.Collapsed: row_.Collapsed = value.CastValue <bool>(); break;

            case RowValueKind.CustomHeight: row_.CustomHeight = value.CastValue <bool>(); break;

            case RowValueKind.Height: row_.Height = value.CastValue <double>(); break;

            case RowValueKind.Hidden: row_.Hidden = value.CastValue <bool>(); break;

            case RowValueKind.Merged: row_.Merged = value.CastValue <bool>(); break;

            case RowValueKind.OutlineLevel: row_.OutlineLevel = value.CastValue <int>(); break;

            case RowValueKind.PageBreak: row_.PageBreak = value.CastValue <bool>(); break;

            case RowValueKind.Phonetic: row_.Phonetic = value.CastValue <bool>(); break;

            case RowValueKind.StyleName: row_.StyleName = value.CastValue <string>(); break;

            default: throw new ArgumentOutOfRangeException(nameof(valueKind));
            }
        }
Ejemplo n.º 7
0
        public static object GetCellsValue(this IExcelContext ctx, string cells, CellValueKind valueKind = CellValueKind.Value)
        {
            var range = ((ExcelContext)ctx).WS.Cells[ctx.DecodeAddress(cells)];

            switch (valueKind)
            {
            case CellValueKind.Value: return(range.Value);

            case CellValueKind.Text: return(range.Text);

            case CellValueKind.AutoFilter: return(range.AutoFilter);

            case CellValueKind.Comment: return(range.Comment.Text);

            case CellValueKind.ConditionalFormattingMore: return(null);

            case CellValueKind.Formula: return(range.Formula);

            case CellValueKind.FormulaR1C1: return(range.FormulaR1C1);

            case CellValueKind.Hyperlink: return(range.Hyperlink);

            case CellValueKind.Merge: return(range.Merge);

            case CellValueKind.StyleName: return(range.StyleName);

            default: throw new ArgumentOutOfRangeException(nameof(valueKind));
            }
        }
Ejemplo n.º 8
0
        public static object ExecuteCmd(this IExcelContext ctx, IExcelCommand[] cmds, out Action after)
        {
            var    frame        = ctx.Frame;
            var    afterActions = new List <Action>();
            Action action2      = null;

            foreach (var cmd in cmds)
            {
                if (cmd.When <= When.Before)
                {
                    cmd.Execute(ctx, ref action2); if (action2 != null)
                    {
                        afterActions.Add(action2); action2 = null;
                    }
                }
                else
                {
                    afterActions.Add(() => { cmd.Execute(ctx, ref action2); if (action2 != null)
                                             {
                                                 afterActions.Add(action2); action2 = null;
                                             }
                                     });
                }
            }
            after = afterActions.Count > 0 ? () => { foreach (var action in afterActions)
                                                     {
                                                         action?.Invoke();
                                                     }
            } : (Action)null;
            return(frame);
        }
Ejemplo n.º 9
0
        void IExcelCommand.Execute(IExcelContext ctx, ref Action after)
        {
            var ctx2     = (ExcelContext)ctx;
            var pathFile = new FileInfo(Path);

            ctx2.OpenWorkbook(pathFile, Password);
        }
Ejemplo n.º 10
0
        public static object GetColumnValue(this IExcelContext ctx, int col, ColumnValueKind valueKind)
        {
            var column = ((ExcelContext)ctx).WS.Column(col);

            switch (valueKind)
            {
            case ColumnValueKind.BestFit: return(column.BestFit);

            case ColumnValueKind.Merged: return(column.Merged);

            case ColumnValueKind.Width: return(column.Width);

            default: throw new ArgumentOutOfRangeException(nameof(valueKind));
            }
        }
Ejemplo n.º 11
0
        public static void ViewAction(this IExcelContext ctx, object value, ViewActionKind actionKind)
        {
            var view = ((ExcelContext)ctx).WS.View;

            switch (actionKind)
            {
            case ViewActionKind.FreezePane: ExcelService.CellToInts((string)value, out var row, out var col); view.FreezePanes(row, col); break;

            case ViewActionKind.SetTabSelected: view.SetTabSelected(); break;

            case ViewActionKind.UnfreezePane: view.UnFreezePanes(); break;

            default: throw new ArgumentOutOfRangeException(nameof(actionKind));
            }
        }
Ejemplo n.º 12
0
        public static string DecodeAddress(this IExcelContext ctx, string address)
        {
            if (!address.StartsWith("^"))
            {
                return(address);
            }
            var vec = address.Substring(1).Split(':').Select(x => int.Parse(x)).ToArray();
            var rel = (vec[0] & (int)Address.Rel) == (int)Address.Rel;

            if (vec.Length == 3)
            {
                int row       = rel ? ctx.Y + vec[1] : vec[1],
                    col       = rel ? ctx.X + vec[2] : vec[2],
                    coltocol1 = rel ? ctx.X + vec[1] : vec[1],
                    coltocol2 = rel ? ctx.X + vec[2] : vec[2],
                    rowtorow1 = rel ? ctx.Y + vec[1] : vec[1],
                    rowtorow2 = rel ? ctx.Y + vec[2] : vec[2];
                switch ((Address)(vec[0] & 0xF))
                {
                case Address.CellAbs: return(ExcelCellBase.GetAddress(row, col));

                case Address.RangeAbs: return(ExcelCellBase.GetAddress(ctx.Y, ctx.X, row, col));

                case Address.RowOrCol: return(row != 0 ? ExcelCellBase.GetAddressRow(row) : col != 0 ? ExcelCellBase.GetAddressCol(col) : null);

                case Address.ColToCol: return($"{ExcelCellBase.GetAddressCol(coltocol1).Split(':')[0]}:{ExcelCellBase.GetAddressCol(coltocol2).Split(':')[0]}");

                case Address.RowToRow: return($"{rowtorow1}:{rowtorow2}");

                default: throw new ArgumentOutOfRangeException(nameof(address));
                }
            }
            else if (vec.Length == 5)
            {
                int fromRow = rel ? ctx.Y + vec[1] : vec[1], toRow = rel ? ctx.Y + vec[3] : vec[3],
                    fromCol = rel ? ctx.X + vec[2] : vec[2], toCol = rel ? ctx.X + vec[4] : vec[4];
                switch ((Address)(vec[0] & 0xF))
                {
                case Address.RangeAbs: return(ExcelCellBase.GetAddress(fromRow, fromCol, toRow, toCol));

                default: throw new ArgumentOutOfRangeException(nameof(address));
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(address));
            }
        }
Ejemplo n.º 13
0
        public static void ColumnValue(this IExcelContext ctx, int col, object value, ColumnValueKind valueKind)
        {
            var column = ((ExcelContext)ctx).WS.Column(col);

            switch (valueKind)
            {
            case ColumnValueKind.AutoFit: column.AutoFit(); break;

            case ColumnValueKind.BestFit: column.BestFit = value.CastValue <bool>(); break;

            case ColumnValueKind.Merged: column.Merged = value.CastValue <bool>(); break;

            case ColumnValueKind.Width: column.Width = value.CastValue <double>(); break;

            case ColumnValueKind.TrueWidth: column.SetTrueColumnWidth(value.CastValue <double>()); break;

            default: throw new ArgumentOutOfRangeException(nameof(valueKind));
            }
        }
Ejemplo n.º 14
0
        public static void CellsValue(this IExcelContext ctx, string cells, object value, CellValueKind valueKind = CellValueKind.Value)
        {
            var range = ((ExcelContext)ctx).WS.Cells[ctx.DecodeAddress(cells)];

            switch (valueKind)
            {
            case CellValueKind.Value: range.Value = value; break;

            case CellValueKind.AutoFilter: range.AutoFilter = value.CastValue <bool>(); break;

            case CellValueKind.AutoFitColumns: range.AutoFitColumns(); break;

            case CellValueKind.Comment: range.Comment.Text = (string)value; break;

            case CellValueKind.CommentMore: break;

            case CellValueKind.ConditionalFormattingMore: break;

            case CellValueKind.Copy:
                var range2 = ((ExcelContext)ctx).WS.Cells[ctx.DecodeAddress((string)value)];
                range.Copy(range2); break;

            case CellValueKind.Formula: range.Formula = (string)value; break;

            case CellValueKind.FormulaR1C1: range.FormulaR1C1 = (string)value; break;

            case CellValueKind.Hyperlink: range.Hyperlink = new Uri((string)value); break;

            case CellValueKind.Merge: range.Merge = value.CastValue <bool>(); break;

            case CellValueKind.RichText: range.RichText.Add((string)value); break;

            case CellValueKind.RichTextClear: range.RichText.Clear(); break;

            case CellValueKind.StyleName: range.StyleName = (string)value; break;

            default: throw new ArgumentOutOfRangeException(nameof(valueKind));
            }
            if (value is DateTime)
            {
                range.Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
            }
        }
Ejemplo n.º 15
0
        public static void WriteRow(this IExcelContext ctx, Collection <string> s)
        {
            var ws = ((ExcelContext)ctx).EnsureWorksheet();

            ctx.X = ctx.XStart;
            // execute-row-before
            var cr = ctx.ExecuteRow(When.Before, s, out Action after);

            if ((cr & CommandRtn.Skip) == CommandRtn.Skip)
            {
                return;
            }
            //
            for (var i = 0; i < s.Count; i++)
            {
                ctx.CsvX = i + 1;
                var v = s[i].ParseValue();
                // execute-col
                cr = ctx.ExecuteCol(s, v, out Action action);
                if ((cr & CommandRtn.Skip) == CommandRtn.Skip)
                {
                    continue;
                }
                if (ctx.Y > 0 && ctx.X > 0)
                {
                    if ((cr & CommandRtn.Formula) != CommandRtn.Formula)
                    {
                        ws.SetValue(ctx.Y, ctx.X, v);
                    }
                    else
                    {
                        ws.Cells[ctx.Y, ctx.X].Formula = s[i];
                    }
                    //if (v is DateTime) ws.Cells[ExcelCellBase.GetAddress(ctx.Y, ctx.X)].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
                }
                ctx.X += ctx.DeltaX;
                action?.Invoke();
            }
            after?.Invoke();
            ctx.Y += ctx.DeltaY;
            // execute-row-after
            ctx.ExecuteRow(When.After, s, out Action after2);
        }
Ejemplo n.º 16
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.CmdCols.Push(this);
Ejemplo n.º 17
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.WorksheetGet(Name);
Ejemplo n.º 18
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.CellStyle(Cells, Styles);
Ejemplo n.º 19
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.Flush();
Ejemplo n.º 20
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.VbaModule(Code, ModuleKind);
Ejemplo n.º 21
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => Action(ctx);
Ejemplo n.º 22
0
 public static string GetAddress(this IExcelContext ctx, Address r, int fromRow, int fromColumn, int toRow, int toColumn) => DecodeAddress(ctx, GetAddress(r, fromRow, fromColumn, toRow, toColumn));
Ejemplo n.º 23
0
 public void Configure()
 {
     _simpleCmds   = new IExcelCommand[] { new CellStyle("A1", "f1") };
     _excelContext = new ExcelContext(false);
 }
Ejemplo n.º 24
0
        void IExcelCommand.Execute(IExcelContext ctx, ref Action after)
        {
            var ctx2 = (ExcelContext)ctx;

            ctx2.WB.Worksheets.Delete(Name);
        }
Ejemplo n.º 25
0
 public static string GetAddress(this IExcelContext ctx, Address r, int row, int col) => DecodeAddress(ctx, GetAddress(r, row, col));
Ejemplo n.º 26
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.CellsValue(Cells, Value.CastValue(ValueType), ValueKind);
Ejemplo n.º 27
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.ConditionalFormatting(Address, Json, FormattingKind, Priority, StopIfTrue);
Ejemplo n.º 28
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.ViewAction(Value, ActionKind);
Ejemplo n.º 29
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.Drawing(Address, Name, Value, DrawingKind);
Ejemplo n.º 30
0
 void IExcelCommand.Execute(IExcelContext ctx, ref Action after) => ctx.RowValue(Row, Value?.DeserializeValue(ValueType), ValueKind);