Search in sources :

Example 21 with CellAddress

use of org.apache.poi.ss.util.CellAddress in project poi by apache.

the class TestXSSFSheetShiftRows method testBug57828_OnlyOneCommentShiftedInRow.

/** Shifting rows with cell comments only shifts comments from first such cell. Other cell comments not shifted */
@Test
public void testBug57828_OnlyOneCommentShiftedInRow() throws IOException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57828.xlsx");
    XSSFSheet sheet = wb.getSheetAt(0);
    Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
    assertNotNull(comment1);
    Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
    assertNotNull(comment2);
    Comment comment3 = sheet.getCellComment(new CellAddress(1, 1));
    assertNull("NO comment in (1,1) and it should be null", comment3);
    sheet.shiftRows(2, 2, -1);
    comment3 = sheet.getCellComment(new CellAddress(1, 1));
    assertNotNull("Comment in (2,1) moved to (1,1) so its not null now.", comment3);
    comment1 = sheet.getCellComment(new CellAddress(2, 1));
    assertNull("No comment currently in (2,1) and hence it is null", comment1);
    comment2 = sheet.getCellComment(new CellAddress(1, 2));
    assertNotNull("Comment in (2,2) should have moved as well because of shift rows. But its not", comment2);
    wb.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) Comment(org.apache.poi.ss.usermodel.Comment) POITestCase.skipTest(org.apache.poi.POITestCase.skipTest) Test(org.junit.Test)

Example 22 with CellAddress

use of org.apache.poi.ss.util.CellAddress in project poi by apache.

the class TestXSSFSheetShiftRows method testBug56017.

/** Shifting rows with comment result - Unreadable content error and comment deletion */
@Test
public void testBug56017() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56017.xlsx");
    Sheet sheet = wb.getSheetAt(0);
    Comment comment = sheet.getCellComment(new CellAddress(0, 0));
    assertNotNull(comment);
    assertEquals("Amdocs", comment.getAuthor());
    assertEquals("Amdocs:\ntest\n", comment.getString().getString());
    sheet.shiftRows(0, 1, 1);
    // comment in row 0 is gone
    comment = sheet.getCellComment(new CellAddress(0, 0));
    assertNull(comment);
    // comment is now in row 1
    comment = sheet.getCellComment(new CellAddress(1, 0));
    assertNotNull(comment);
    assertEquals("Amdocs", comment.getAuthor());
    assertEquals("Amdocs:\ntest\n", comment.getString().getString());
    Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
    wb.close();
    assertNotNull(wbBack);
    Sheet sheetBack = wbBack.getSheetAt(0);
    // comment in row 0 is gone
    comment = sheetBack.getCellComment(new CellAddress(0, 0));
    assertNull(comment);
    // comment is now in row 1
    comment = sheetBack.getCellComment(new CellAddress(1, 0));
    assertNotNull(comment);
    assertEquals("Amdocs", comment.getAuthor());
    assertEquals("Amdocs:\ntest\n", comment.getString().getString());
    wbBack.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) Comment(org.apache.poi.ss.usermodel.Comment) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) POITestCase.skipTest(org.apache.poi.POITestCase.skipTest) Test(org.junit.Test)

Example 23 with CellAddress

use of org.apache.poi.ss.util.CellAddress in project poi by apache.

the class TestXSSFSheet method getActiveCell.

@Test
public void getActiveCell() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    CellAddress R5 = new CellAddress("R5");
    sheet.setActiveCell(R5);
    assertEquals(R5, sheet.getActiveCell());
    workbook.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 24 with CellAddress

use of org.apache.poi.ss.util.CellAddress in project poi by apache.

the class BaseTestCellComment method getAddress.

@Test
public void getAddress() {
    Workbook wb = _testDataProvider.createWorkbook();
    Sheet sh = wb.createSheet();
    CreationHelper factory = wb.getCreationHelper();
    Drawing<?> patriarch = sh.createDrawingPatriarch();
    Comment comment = patriarch.createCellComment(factory.createClientAnchor());
    assertEquals(CellAddress.A1, comment.getAddress());
    Cell C2 = sh.createRow(1).createCell(2);
    C2.setCellComment(comment);
    assertEquals(new CellAddress("C2"), comment.getAddress());
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Test(org.junit.Test)

Example 25 with CellAddress

use of org.apache.poi.ss.util.CellAddress in project poi by apache.

the class BaseTestCellComment method create.

@Test
public final void create() throws IOException {
    String cellText = "Hello, World";
    String commentText = "We can set comments in POI";
    String commentAuthor = "Apache Software Foundation";
    int cellRow = 3;
    int cellColumn = 1;
    Workbook wb1 = _testDataProvider.createWorkbook();
    CreationHelper factory = wb1.getCreationHelper();
    Sheet sheet = wb1.createSheet();
    assertNull(sheet.getCellComment(new CellAddress(cellRow, cellColumn)));
    Cell cell = sheet.createRow(cellRow).createCell(cellColumn);
    cell.setCellValue(factory.createRichTextString(cellText));
    assertNull(cell.getCellComment());
    assertNull(sheet.getCellComment(new CellAddress(cellRow, cellColumn)));
    Drawing<?> patr = sheet.createDrawingPatriarch();
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(2);
    anchor.setCol2(5);
    anchor.setRow1(1);
    anchor.setRow2(2);
    Comment comment = patr.createCellComment(anchor);
    assertFalse(comment.isVisible());
    comment.setVisible(true);
    assertTrue(comment.isVisible());
    RichTextString string1 = factory.createRichTextString(commentText);
    comment.setString(string1);
    comment.setAuthor(commentAuthor);
    cell.setCellComment(comment);
    assertNotNull(cell.getCellComment());
    assertNotNull(sheet.getCellComment(new CellAddress(cellRow, cellColumn)));
    //verify our settings
    assertEquals(commentAuthor, comment.getAuthor());
    assertEquals(commentText, comment.getString().getString());
    assertEquals(cellRow, comment.getRow());
    assertEquals(cellColumn, comment.getColumn());
    Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    cell = sheet.getRow(cellRow).getCell(cellColumn);
    comment = cell.getCellComment();
    assertNotNull(comment);
    assertEquals(commentAuthor, comment.getAuthor());
    assertEquals(commentText, comment.getString().getString());
    assertEquals(cellRow, comment.getRow());
    assertEquals(cellColumn, comment.getColumn());
    assertTrue(comment.isVisible());
    // Change slightly, and re-test
    comment.setString(factory.createRichTextString("New Comment Text"));
    comment.setVisible(false);
    Workbook wb3 = _testDataProvider.writeOutAndReadBack(wb2);
    wb2.close();
    sheet = wb3.getSheetAt(0);
    cell = sheet.getRow(cellRow).getCell(cellColumn);
    comment = cell.getCellComment();
    assertNotNull(comment);
    assertEquals(commentAuthor, comment.getAuthor());
    assertEquals("New Comment Text", comment.getString().getString());
    assertEquals(cellRow, comment.getRow());
    assertEquals(cellColumn, comment.getColumn());
    assertFalse(comment.isVisible());
    // Test Comment.equals and Comment.hashCode
    assertEquals(comment, cell.getCellComment());
    assertEquals(comment.hashCode(), cell.getCellComment().hashCode());
    wb3.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Test(org.junit.Test)

Aggregations

CellAddress (org.apache.poi.ss.util.CellAddress)41 Test (org.junit.Test)25 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)5 Sheet (org.apache.poi.ss.usermodel.Sheet)5 Workbook (org.apache.poi.ss.usermodel.Workbook)5 POITestCase.skipTest (org.apache.poi.POITestCase.skipTest)4 Cell (org.apache.poi.ss.usermodel.Cell)4 Comment (org.apache.poi.ss.usermodel.Comment)4 Row (org.apache.poi.ss.usermodel.Row)4 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)4 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)4 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)2 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)2 RichTextString (org.apache.poi.ss.usermodel.RichTextString)2 CommentsTable (org.apache.poi.xssf.model.CommentsTable)2 XSSFComment (org.apache.poi.xssf.usermodel.XSSFComment)2 CTClientData (com.microsoft.schemas.office.excel.CTClientData)1