Esempio n. 1
0
        public static object WRptExploreDependencies(CallExpr ce, Generator.Ctx callerCtx)//(object tmplFileName, string[] forCells)
        {
            var ctx = new Generator.Ctx(callerCtx);

            ctx.CreateValue(FuncDefs_Core.stateTryGetConst, string.Empty);
            var templateFileName = Convert.ToString(Generator.Generate(ce.args[0], ctx));
            var sheets           = new Dictionary <string, XlsxR.Sheet>();

            var xr = new Xlsx.Reader(templateFileName);

            foreach (var s in xr.EnumSheetInfos())
            {
                sheets.Add(s.sheetName, new XlsxR.Sheet(s.sheetCells.ToArray(), xr.styles));
            }

            var forCells = Utils.AsIList(Generator.Generate(ce.args[1], ctx))
                           .Cast <object>()
                           .Select(o => Convert.ToString(o))
                           .ToArray();

            var code = DepsHelper.DumpDeps(ctx, sheets, xr.stringTable, forCells);

            return(code);
        }
Esempio n. 2
0
            public static string DumpDeps(Generator.Ctx parentCtx,
                                          Dictionary <string, XlsxR.Sheet> sheets, string[] stringTable, string[] forCells)
            {
                var sb = new StringBuilder();

                foreach (var sheetCells in forCells.Select(SheetAndCell).GroupBy(sc => sc.Sheet))
                {
                    var sheetKey = (sheetCells.Key == null) ? sheets.Keys.First() : sheetCells.Key;
                    var hlp      = new DepsHelper()
                    {
                        already     = new Dictionary <string, bool>(),
                        sheet       = sheets[sheetKey],
                        stringTable = stringTable,
                        parentCtx   = parentCtx,
                    };
                    foreach (var r in hlp.sheet.vals.Keys)
                    {
                        if (hlp.maxNdxColVal < r.col)
                        {
                            hlp.maxNdxColVal = r.col;
                        }
                        if (hlp.maxNdxRowVal < r.row)
                        {
                            hlp.maxNdxRowVal = r.row;
                        }
                    }

                    var deps = new List <(CellRange cell, Expr expr)>();
                    foreach (var sc in sheetCells)
                    {
                        deps.AddRange(hlp.DumpDepsImpl(sc.Cell));
                    }

                    deps.Sort((a, b) => CellRange.Compare(a.cell, b.cell));

                    var dict = deps.ToDictionary(d => d.cell.name, d => d.expr);

                    var sorted = TopoSort.DoSort(
                        deps.Select(d => d.cell.name),
                        cell => dict[cell].EnumerateReferences()
                        );

                    if (sb.Length > 0)
                    {
                        sb.AppendLine();
                    }
                    sb.AppendLine($"//********** sheet: {sheetKey}");

                    foreach (var cell in sorted)
                    {
                        var info = hlp.GetCode(CellRange.FromName(cell), dict[cell]);
                        if (!string.IsNullOrEmpty(info.rowComm))
                        {
                            sb.AppendLine($"//row: {info.rowComm}");
                        }
                        if (!string.IsNullOrEmpty(info.colComm))
                        {
                            sb.AppendLine($"//col: {info.colComm}");
                        }
                        sb.AppendLine(info.rngExpr);
                    }
                }
                return(sb.ToString());
            }