public void RemoveName(bool recursive = false)
 {
     if (recursive)
     {
         RemoveAllNamesRecursive(this);
     }
     else
     {
         _namedRange.Delete();
     }
 }
        public void TestCreateSimplePanel()
        {
            XLWorkbook   wb    = new XLWorkbook();
            IXLWorksheet ws    = wb.AddWorksheet("Test");
            IXLRange     range = ws.Range(ws.Cell(1, 1), ws.Cell(2, 2));

            range.AddToNamed("s_Test", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("s_Test");

            var report            = new object();
            var templateProcessor = Substitute.For <ITemplateProcessor>();
            var parseSettings     = new PanelParsingSettings
            {
                SimplePanelPrefix    = "s",
                PanelPrefixSeparator = "_",
            };

            var factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            var panel   = (ExcelPanel)factory.Create(namedRange, new Dictionary <string, string>
            {
                [nameof(ExcelPanel.Type)]                   = PanelType.Horizontal.ToString(),
                [nameof(ExcelPanel.ShiftType)]              = ShiftType.Row.ToString(),
                [nameof(ExcelPanel.RenderPriority)]         = "5",
                [nameof(ExcelPanel.BeforeRenderMethodName)] = "BeforeRenderMethodName",
                [nameof(ExcelPanel.AfterRenderMethodName)]  = "AfterRenderMethodName",
            });

            Assert.AreEqual(PanelType.Horizontal, panel.Type);
            Assert.AreEqual(ShiftType.Row, panel.ShiftType);
            Assert.AreEqual(5, panel.RenderPriority);
            Assert.AreEqual("BeforeRenderMethodName", panel.BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethodName", panel.AfterRenderMethodName);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));

            namedRange.Delete();
            range.AddToNamed("SS--Test", XLScope.Workbook);
            namedRange = wb.NamedRange("SS--Test");

            parseSettings.SimplePanelPrefix    = "ss";
            parseSettings.PanelPrefixSeparator = "--";
            factory = new ExcelPanelFactory(report, templateProcessor, parseSettings);
            panel   = (ExcelPanel)factory.Create(namedRange, null);

            Assert.IsInstanceOf <ExcelPanel>(panel);
            Assert.AreEqual(PanelType.Vertical, panel.Type);
            Assert.AreEqual(ShiftType.Cells, panel.ShiftType);
            Assert.AreEqual(0, panel.RenderPriority);
            Assert.AreEqual(0, panel.Children.Count);
            Assert.IsNull(panel.BeforeRenderMethodName);
            Assert.IsNull(panel.AfterRenderMethodName);
            Assert.IsNull(panel.Parent);
            Assert.AreEqual(namedRange.Ranges.First(), panel.Range);
            Assert.AreSame(report, panel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
            Assert.AreSame(templateProcessor, panel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(panel));
        }
        public static IXLNamedRange MoveNamedRange(IXLNamedRange namedRange, IXLCell cell)
        {
            string   name     = namedRange.Name;
            IXLRange newRange = MoveRange(namedRange.Ranges.ElementAt(0), cell);

            namedRange.Delete();
            newRange.AddToNamed(name, XLScope.Worksheet);
            return(cell.Worksheet.NamedRange(name));
        }
示例#4
0
        public void TestCopy()
        {
            var          wb                = new XLWorkbook();
            IXLWorksheet ws                = wb.AddWorksheet("Test");
            var          excelReport       = Substitute.For <object>();
            var          templateProcessor = Substitute.For <ITemplateProcessor>();

            IXLRange range = ws.Range(1, 1, 3, 4);

            range.AddToNamed("Parent", XLScope.Worksheet);
            IXLNamedRange namedRange = ws.NamedRange("Parent");

            IXLRange childRange = ws.Range(2, 1, 3, 4);

            childRange.AddToNamed("Child", XLScope.Worksheet);
            IXLNamedRange namedChildRange = ws.NamedRange("Child");

            IXLRange childOfChildRange = ws.Range(3, 1, 3, 4);

            childOfChildRange.AddToNamed("ChildOfChild", XLScope.Worksheet);
            IXLNamedRange namedChildOfChildRange = ws.NamedRange("ChildOfChild");

            var panel = new ExcelNamedPanel(namedRange, excelReport, templateProcessor)
            {
                Children = new List <IExcelPanel>
                {
                    new ExcelNamedPanel(namedChildRange, excelReport, templateProcessor)
                    {
                        Children = new List <IExcelPanel>
                        {
                            new ExcelDataSourcePanel("fn:DataSource:Method()", namedChildOfChildRange, excelReport, templateProcessor)
                            {
                                RenderPriority                 = 30,
                                Type                           = PanelType.Horizontal,
                                ShiftType                      = ShiftType.Row,
                                BeforeRenderMethodName         = "BeforeRenderMethod3",
                                AfterRenderMethodName          = "AfterRenderMethod3",
                                BeforeDataItemRenderMethodName = "BeforeDataItemRenderMethodName",
                                AfterDataItemRenderMethodName  = "AfterDataItemRenderMethodName",
                                GroupBy                        = "2,4",
                            }
                        },
                        RenderPriority         = 20,
                        ShiftType              = ShiftType.Row,
                        BeforeRenderMethodName = "BeforeRenderMethod2",
                        AfterRenderMethodName  = "AfterRenderMethod2",
                    }
                },
                RenderPriority         = 10,
                Type                   = PanelType.Horizontal,
                ShiftType              = ShiftType.NoShift,
                BeforeRenderMethodName = "BeforeRenderMethod1",
                AfterRenderMethodName  = "AfterRenderMethod1",
            };

            IExcelNamedPanel copiedPanel = (IExcelNamedPanel)panel.Copy(ws.Cell(5, 5));

            Assert.AreSame(excelReport, copiedPanel.GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel));
            Assert.AreSame(templateProcessor, copiedPanel.GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel));
            Assert.IsTrue(Regex.IsMatch(copiedPanel.Name, @"Parent_[0-9a-f]{32}"));
            Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell());
            Assert.AreEqual(10, copiedPanel.RenderPriority);
            Assert.AreEqual(PanelType.Horizontal, copiedPanel.Type);
            Assert.AreEqual(ShiftType.NoShift, copiedPanel.ShiftType);
            Assert.AreEqual("BeforeRenderMethod1", copiedPanel.BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethod1", copiedPanel.AfterRenderMethodName);
            Assert.IsNull(copiedPanel.Parent);

            Assert.AreEqual(1, copiedPanel.Children.Count);
            Assert.AreSame(excelReport, copiedPanel.Children.First().GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First()));
            Assert.AreSame(templateProcessor, copiedPanel.Children.First().GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First()));
            Assert.IsTrue(Regex.IsMatch(((IExcelNamedPanel)copiedPanel.Children.First()).Name, @"Parent_[0-9a-f]{32}_Child"));
            Assert.AreEqual(ws.Cell(6, 5), copiedPanel.Children.First().Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.First().Range.LastCell());
            Assert.AreEqual(20, copiedPanel.Children.First().RenderPriority);
            Assert.AreEqual(PanelType.Vertical, copiedPanel.Children.First().Type);
            Assert.AreEqual(ShiftType.Row, copiedPanel.Children.First().ShiftType);
            Assert.AreEqual("BeforeRenderMethod2", copiedPanel.Children.First().BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethod2", copiedPanel.Children.First().AfterRenderMethodName);
            Assert.AreSame(copiedPanel, copiedPanel.Children.First().Parent);

            Assert.AreEqual(1, copiedPanel.Children.First().Children.Count);
            Assert.AreSame(excelReport, copiedPanel.Children.First().Children.First().GetType().GetField("_report", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First().Children.First()));
            Assert.AreSame(templateProcessor, copiedPanel.Children.First().Children.First().GetType().GetField("_templateProcessor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(copiedPanel.Children.First().Children.First()));
            Assert.IsTrue(Regex.IsMatch(((IExcelNamedPanel)copiedPanel.Children.First().Children.First()).Name, @"Parent_[0-9a-f]{32}_Child_ChildOfChild"));
            Assert.IsInstanceOf <ExcelDataSourcePanel>(copiedPanel.Children.First().Children.First());
            Assert.AreEqual(ws.Cell(7, 5), copiedPanel.Children.First().Children.First().Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.First().Children.First().Range.LastCell());
            Assert.AreEqual(30, copiedPanel.Children.First().Children.First().RenderPriority);
            Assert.AreEqual(PanelType.Horizontal, copiedPanel.Children.First().Children.First().Type);
            Assert.AreEqual(ShiftType.Row, copiedPanel.Children.First().Children.First().ShiftType);
            Assert.AreEqual("BeforeRenderMethod3", copiedPanel.Children.First().Children.First().BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethod3", copiedPanel.Children.First().Children.First().AfterRenderMethodName);
            Assert.AreEqual("BeforeDataItemRenderMethodName", ((ExcelDataSourcePanel)copiedPanel.Children.First().Children.First()).BeforeDataItemRenderMethodName);
            Assert.AreEqual("AfterDataItemRenderMethodName", ((ExcelDataSourcePanel)copiedPanel.Children.First().Children.First()).AfterDataItemRenderMethodName);
            Assert.AreEqual("2,4", ((ExcelDataSourcePanel)copiedPanel.Children.First().Children.First()).GroupBy);
            Assert.AreSame(copiedPanel.Children.First(), copiedPanel.Children.First().Children.First().Parent);

            namedRange.Delete();
            namedChildRange.Delete();
            copiedPanel.Delete();
            copiedPanel.Children.First().Delete();

            IExcelPanel globalParent = new ExcelPanel(ws.Range(1, 1, 20, 20), excelReport, templateProcessor);

            range = ws.Range(1, 1, 3, 4);
            range.AddToNamed("Parent", XLScope.Worksheet);
            namedRange = ws.NamedRange("Parent");

            IXLRange childRange1 = ws.Range(1, 1, 1, 4);

            childRange1.AddToNamed("Child", XLScope.Worksheet);
            namedChildRange = ws.NamedRange("Child");

            IXLRange childRange2 = ws.Range(2, 1, 3, 4);

            childOfChildRange = ws.Range(3, 1, 3, 4);
            childOfChildRange.AddToNamed("ChildOfChild", XLScope.Worksheet);
            namedChildOfChildRange = ws.NamedRange("ChildOfChild");

            panel = new ExcelNamedPanel(namedRange, excelReport, templateProcessor)
            {
                Parent   = globalParent,
                Children = new List <IExcelPanel>
                {
                    new ExcelNamedPanel(namedChildRange, excelReport, templateProcessor),
                    new ExcelPanel(childRange2, excelReport, templateProcessor)
                    {
                        Children = new List <IExcelPanel>
                        {
                            new ExcelNamedPanel(namedChildOfChildRange, excelReport, templateProcessor)
                        },
                        RenderPriority         = 10,
                        Type                   = PanelType.Horizontal,
                        ShiftType              = ShiftType.NoShift,
                        BeforeRenderMethodName = "BeforeRenderMethod",
                        AfterRenderMethodName  = "AfterRenderMethod",
                    },
                },
            };

            copiedPanel = (IExcelNamedPanel)panel.Copy(ws.Cell(5, 5));
            Assert.IsTrue(Regex.IsMatch(copiedPanel.Name, @"Parent_[0-9a-f]{32}"));
            Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell());
            Assert.AreSame(globalParent, copiedPanel.Parent);

            Assert.AreEqual(2, copiedPanel.Children.Count);
            Assert.IsTrue(Regex.IsMatch(((IExcelNamedPanel)copiedPanel.Children.First()).Name, @"Parent_[0-9a-f]{32}_Child"));
            Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Children.First().Range.FirstCell());
            Assert.AreEqual(ws.Cell(5, 8), copiedPanel.Children.First().Range.LastCell());
            Assert.AreSame(copiedPanel, copiedPanel.Children.First().Parent);
            Assert.IsInstanceOf <ExcelPanel>(copiedPanel.Children.Last());
            Assert.IsNotInstanceOf <ExcelNamedPanel>(copiedPanel.Children.Last());
            Assert.AreEqual(ws.Cell(6, 5), copiedPanel.Children.Last().Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.Last().Range.LastCell());
            Assert.AreEqual(10, copiedPanel.Children.Last().RenderPriority);
            Assert.AreEqual(PanelType.Horizontal, copiedPanel.Children.Last().Type);
            Assert.AreEqual(ShiftType.NoShift, copiedPanel.Children.Last().ShiftType);
            Assert.AreEqual("BeforeRenderMethod", copiedPanel.Children.Last().BeforeRenderMethodName);
            Assert.AreEqual("AfterRenderMethod", copiedPanel.Children.Last().AfterRenderMethodName);

            Assert.AreSame(copiedPanel, copiedPanel.Children.Last().Parent);

            Assert.AreEqual(1, copiedPanel.Children.Last().Children.Count);
            Assert.IsTrue(Regex.IsMatch(((IExcelNamedPanel)copiedPanel.Children.Last().Children.First()).Name, @"ChildOfChild_[0-9a-f]{32}"));
            Assert.AreEqual(ws.Cell(7, 5), copiedPanel.Children.Last().Children.First().Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Children.Last().Children.First().Range.LastCell());
            Assert.AreSame(copiedPanel.Children.Last(), copiedPanel.Children.Last().Children.First().Parent);

            namedRange.Delete();
            namedChildRange.Delete();
            namedChildOfChildRange.Delete();
            copiedPanel.Delete();
            copiedPanel.Children.First().Delete();
            copiedPanel.Children.Last().Children.First().Delete();

            globalParent = new ExcelPanel(ws.Range(1, 1, 7, 7), excelReport, templateProcessor);
            range        = ws.Range(1, 1, 3, 4);
            range.AddToNamed("Parent", XLScope.Worksheet);
            namedRange = ws.NamedRange("Parent");

            childRange = ws.Range(1, 1, 1, 4);
            childRange.AddToNamed("Child", XLScope.Worksheet);
            namedChildRange = ws.NamedRange("Child");

            panel = new ExcelNamedPanel(namedRange, excelReport, templateProcessor)
            {
                Parent   = globalParent,
                Children = new List <IExcelPanel> {
                    new ExcelNamedPanel(namedChildRange, excelReport, templateProcessor)
                },
            };

            copiedPanel = (IExcelNamedPanel)panel.Copy(ws.Cell(5, 5));
            Assert.IsTrue(Regex.IsMatch(copiedPanel.Name, @"Parent_[0-9a-f]{32}"));
            Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell());
            Assert.IsNull(copiedPanel.Parent);

            Assert.AreEqual(1, copiedPanel.Children.Count);
            Assert.IsTrue(Regex.IsMatch(((IExcelNamedPanel)copiedPanel.Children.First()).Name, @"Parent_[0-9a-f]{32}_Child"));
            Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Children.First().Range.FirstCell());
            Assert.AreEqual(ws.Cell(5, 8), copiedPanel.Children.First().Range.LastCell());
            Assert.AreSame(copiedPanel, copiedPanel.Children.First().Parent);

            copiedPanel = (IExcelNamedPanel)panel.Copy(ws.Cell(5, 5), false);
            Assert.IsTrue(Regex.IsMatch(copiedPanel.Name, @"Parent_[0-9a-f]{32}"));
            Assert.AreEqual(ws.Cell(5, 5), copiedPanel.Range.FirstCell());
            Assert.AreEqual(ws.Cell(7, 8), copiedPanel.Range.LastCell());
            Assert.IsNull(copiedPanel.Parent);
            Assert.AreEqual(0, copiedPanel.Children.Count);

            ExceptionAssert.Throws <ArgumentNullException>(() => panel.Copy(null));

            //wb.SaveAs("test.xlsx");
        }