Beispiel #1
0
        public void GetAffectedNamedRangesTest()
        {
            InteropExcel.Application application = new InteropExcel.Application();

            try
            {
                InteropExcel.Workbook   workbook  = application.OpenWorkbook("TestData.xlsx", false);
                InteropExcel._Worksheet worksheet = workbook.ActiveSheet as InteropExcel._Worksheet;

                Dictionary <string, string> namedRanges = new Dictionary <string, string>();

                // Initialize ranges
                workbook.Names.GetNamedRange("TestRangeOne").Visible    = false;
                workbook.Names.GetNamedRange("TestRangeTwo").Visible    = false;
                workbook.Names.GetNamedRange("TestRangeThree").Visible  = false;
                workbook.Names.GetNamedRange("TestRangeTarget").Visible = false;

                // Build the dictionary of name : address pairs
                namedRanges["TestRangeOne"]   = workbook.Names.GetNamedRange("TestRangeOne").RefersToRange.Address;
                namedRanges["TestRangeTwo"]   = workbook.Names.GetNamedRange("TestRangeTwo").RefersToRange.Address;
                namedRanges["TestRangeThree"] = workbook.Names.GetNamedRange("TestRangeThree").RefersToRange.Address;

                // Get the target range that will be tested for intersection with the above ranges
                InteropExcel.Range targetRange = workbook.Names.GetNamedRange("TestRangeTarget").RefersToRange;

                // Build the expected output
                Dictionary <string, string> expected = new Dictionary <string, string>();
                expected["TestRangeTwo"]   = namedRanges["TestRangeTwo"];
                expected["TestRangeThree"] = namedRanges["TestRangeThree"];

                // Get the actual output
                Dictionary <string, string> actual;
                actual = worksheet.GetAffectedNamedRanges(targetRange, namedRanges);

                Assert.AreEqual(expected.Count, actual.Count);
                foreach (string rangeName in expected.Keys)
                {
                    Assert.IsTrue(actual.ContainsKey(rangeName));
                }
            }
            finally
            {
                application.Close();
            }
        }