123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==================================================================== */
-
- package org.apache.poi.xssf.usermodel;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertSame;
-
- import java.io.IOException;
-
- import org.apache.poi.ss.SpreadsheetVersion;
- import org.apache.poi.ss.usermodel.BaseTestRow;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellCopyPolicy;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.xssf.XSSFITestDataProvider;
- import org.junit.Test;
-
- /**
- * Tests for XSSFRow
- */
- public final class TestXSSFRow extends BaseTestRow {
-
- public TestXSSFRow() {
- super(XSSFITestDataProvider.instance);
- }
-
- @Test
- public void testRowBounds() throws IOException {
- baseTestRowBounds(SpreadsheetVersion.EXCEL2007.getLastRowIndex());
- }
-
- @Test
- public void testCellBounds() throws IOException {
- baseTestCellBounds(SpreadsheetVersion.EXCEL2007.getLastColumnIndex());
- }
-
- public void testCopyRowFrom() throws IOException {
- final XSSFWorkbook workbook = new XSSFWorkbook();
- final XSSFSheet sheet = workbook.createSheet("test");
- final XSSFRow srcRow = sheet.createRow(0);
- srcRow.createCell(0).setCellValue("Hello");
- final XSSFRow destRow = sheet.createRow(1);
-
- destRow.copyRowFrom(srcRow, new CellCopyPolicy());
- assertNotNull(destRow.getCell(0));
- assertEquals("Hello", destRow.getCell(0).getStringCellValue());
-
- workbook.close();
- }
-
- public void testCopyRowFromExternalSheet() throws IOException {
- final XSSFWorkbook workbook = new XSSFWorkbook();
- final Sheet srcSheet = workbook.createSheet("src");
- final XSSFSheet destSheet = workbook.createSheet("dest");
- workbook.createSheet("other");
-
- final Row srcRow = srcSheet.createRow(0);
- int col = 0;
- //Test 2D and 3D Ref Ptgs (Pxg for OOXML Workbooks)
- srcRow.createCell(col++).setCellFormula("B5");
- srcRow.createCell(col++).setCellFormula("src!B5");
- srcRow.createCell(col++).setCellFormula("dest!B5");
- srcRow.createCell(col++).setCellFormula("other!B5");
-
- //Test 2D and 3D Ref Ptgs with absolute row
- srcRow.createCell(col++).setCellFormula("B$5");
- srcRow.createCell(col++).setCellFormula("src!B$5");
- srcRow.createCell(col++).setCellFormula("dest!B$5");
- srcRow.createCell(col++).setCellFormula("other!B$5");
-
- //Test 2D and 3D Area Ptgs (Pxg for OOXML Workbooks)
- srcRow.createCell(col++).setCellFormula("SUM(B5:D$5)");
- srcRow.createCell(col++).setCellFormula("SUM(src!B5:D$5)");
- srcRow.createCell(col++).setCellFormula("SUM(dest!B5:D$5)");
- srcRow.createCell(col++).setCellFormula("SUM(other!B5:D$5)");
-
- //////////////////
-
- final XSSFRow destRow = destSheet.createRow(1);
- destRow.copyRowFrom(srcRow, new CellCopyPolicy());
-
- //////////////////
-
- //Test 2D and 3D Ref Ptgs (Pxg for OOXML Workbooks)
- col = 0;
- Cell cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("RefPtg", "B6", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Ref3DPtg", "src!B6", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Ref3DPtg", "dest!B6", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Ref3DPtg", "other!B6", cell.getCellFormula());
-
- /////////////////////////////////////////////
-
- //Test 2D and 3D Ref Ptgs with absolute row (Ptg row number shouldn't change)
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("RefPtg", "B$5", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Ref3DPtg", "src!B$5", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Ref3DPtg", "dest!B$5", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Ref3DPtg", "other!B$5", cell.getCellFormula());
-
- //////////////////////////////////////////
-
- //Test 2D and 3D Area Ptgs (Pxg for OOXML Workbooks)
- // Note: absolute row changes from last cell to first cell in order
- // to maintain topLeft:bottomRight order
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Area2DPtg", "SUM(B$5:D6)", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(cell);
- assertEquals("Area3DPtg", "SUM(src!B$5:D6)", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(destRow.getCell(6));
- assertEquals("Area3DPtg", "SUM(dest!B$5:D6)", cell.getCellFormula());
-
- cell = destRow.getCell(col++);
- assertNotNull(destRow.getCell(7));
- assertEquals("Area3DPtg", "SUM(other!B$5:D6)", cell.getCellFormula());
-
- workbook.close();
- }
-
- public void testCopyRowOverwritesExistingRow() throws IOException {
- final XSSFWorkbook workbook = new XSSFWorkbook();
- final XSSFSheet sheet1 = workbook.createSheet("Sheet1");
- final Sheet sheet2 = workbook.createSheet("Sheet2");
-
- final Row srcRow = sheet1.createRow(0);
- final XSSFRow destRow = sheet1.createRow(1);
- final Row observerRow = sheet1.createRow(2);
- final Row externObserverRow = sheet2.createRow(0);
-
- srcRow.createCell(0).setCellValue("hello");
- srcRow.createCell(1).setCellValue("world");
- destRow.createCell(0).setCellValue(5.0); //A2 -> 5.0
- destRow.createCell(1).setCellFormula("A1"); // B2 -> A1 -> "hello"
- observerRow.createCell(0).setCellFormula("A2"); // A3 -> A2 -> 5.0
- observerRow.createCell(1).setCellFormula("B2"); // B3 -> B2 -> A1 -> "hello"
- externObserverRow.createCell(0).setCellFormula("Sheet1!A2"); //Sheet2!A1 -> Sheet1!A2 -> 5.0
-
- // overwrite existing destRow with row-copy of srcRow
- destRow.copyRowFrom(srcRow, new CellCopyPolicy());
-
- // copyRowFrom should update existing destRow, rather than creating a new row and reassigning the destRow pointer
- // to the new row (and allow the old row to be garbage collected)
- // this is mostly so existing references to rows that are overwritten are updated
- // rather than allowing users to continue updating rows that are no longer part of the sheet
- assertSame("existing references to srcRow are still valid", srcRow, sheet1.getRow(0));
- assertSame("existing references to destRow are still valid", destRow, sheet1.getRow(1));
- assertSame("existing references to observerRow are still valid", observerRow, sheet1.getRow(2));
- assertSame("existing references to externObserverRow are still valid", externObserverRow, sheet2.getRow(0));
-
- // Make sure copyRowFrom actually copied row (this is tested elsewhere)
- assertEquals(Cell.CELL_TYPE_STRING, destRow.getCell(0).getCellType());
- assertEquals("hello", destRow.getCell(0).getStringCellValue());
-
- // We don't want #REF! errors if we copy a row that contains cells that are referred to by other cells outside of copied region
- assertEquals("references to overwritten cells are unmodified", "A2", observerRow.getCell(0).getCellFormula());
- assertEquals("references to overwritten cells are unmodified", "B2", observerRow.getCell(1).getCellFormula());
- assertEquals("references to overwritten cells are unmodified", "Sheet1!A2", externObserverRow.getCell(0).getCellFormula());
-
- workbook.close();
- }
- }
|