예제 #1
0
        public void IsSheetEmptyTest()
        {
            InteropExcel.Application application = new InteropExcel.Application();

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

                // Get the target range that will be used to set the active sheet
                InteropExcel.Name targetName = workbook.Names.GetNamedRange("TestRangeTarget");

                // Activate the worksheet which contains the named range.
                ((_Worksheet)targetName.RefersToRange.Worksheet).Activate();

                InteropExcel._Worksheet worksheet = workbook.ActiveSheet as InteropExcel._Worksheet;

                bool expected = true;
                bool actual;
                actual = WorksheetExtensions.IsSheetEmpty(worksheet);
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                application.Close();
            }
        }
예제 #2
0
        public void GetRangeTest()
        {
            InteropExcel.Application application = new InteropExcel.Application();

            try
            {
                InteropExcel.Workbook book = application.OpenWorkbook("TestData.xlsx", false);

                InteropExcel._Worksheet worksheet = book.ActiveSheet as InteropExcel._Worksheet;

                // Get the named range stored in the test data excel file.
                InteropExcel.Name expected = book.Names.GetNamedRange("GetRange");

                InteropExcel.Range firstCell = worksheet.Cells[9, 5];
                int rowSize               = 8;
                int columnSize            = 5;
                InteropExcel.Range actual = null;

                // Get the range using the custom extension methods.
                actual = WorksheetExtensions.GetRange(worksheet, firstCell, rowSize, columnSize);

                Assert.AreEqual(expected.RefersToRange.Address, actual.Address);
            }
            finally
            {
                application.Close();
            }
        }
예제 #3
0
        public static void GetCell___Should_throw_ArgumentNullException___When_parameter_worksheet_is_null()
        {
            // Arrange, Act
            var actual = Record.Exception(() => WorksheetExtensions.GetCell(null, A.Dummy <PositiveInteger>(), A.Dummy <PositiveInteger>()));

            // Assert
            actual.Should().BeOfType <ArgumentNullException>();
            actual.Message.Should().Contain("worksheet");
        }
예제 #4
0
        public static void GetRange___Should_throw_ArgumentNullException___When_parameter_worksheet_is_null()
        {
            // Arrange
            var startRowNumber    = 5;
            var endRowNumber      = 10;
            var startColumnNumber = 30;
            var endColumnNumber   = 35;

            // Act
            var actual = Record.Exception(() => WorksheetExtensions.GetRange(null, startRowNumber, endRowNumber, startColumnNumber, endColumnNumber));

            // Assert
            actual.Should().BeOfType <ArgumentNullException>();
            actual.Message.Should().Contain("worksheet");
        }
예제 #5
0
        public void GetRangeNameForActiveCellTest()
        {
            InteropExcel.Application application = new InteropExcel.Application();

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

                // 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;

                // Get the target range that will be used to set the active sheet
                InteropExcel.Name targetName = workbook.Names.GetNamedRange("TestRangeTarget");

                // Activate the worksheet which contains the named range.
                ((_Worksheet)targetName.RefersToRange.Worksheet).Activate();

                InteropExcel._Worksheet worksheet = workbook.ActiveSheet as InteropExcel._Worksheet;

                // Select a cell in this sheet
                Range activeCell = targetName.RefersToRange.Cells[1, 1];
                activeCell.Select();

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

                // 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;
                namedRanges["TestRangeTarget"] = workbook.Names.GetNamedRange("TestRangeTarget").RefersToRange.Address;

                string expected = "TestRangeTarget";
                string actual;
                actual = WorksheetExtensions.GetRangeNameForActiveCell(worksheet, activeCell, namedRanges);
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                application.Close();
            }
        }