Search in sources :

Example 1 with Comment

use of org.apache.poi.ss.usermodel.Comment in project poi by apache.

the class TestXSSFComment method testBug58175a.

@Ignore("Used for manual testing with opening the resulting Workbook in Excel")
@Test
public void testBug58175a() throws IOException {
    Workbook wb = new SXSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(3);
        cell.setCellValue("F4");
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        CreationHelper factory = wb.getCreationHelper();
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(row.getRowNum());
        anchor.setRow2(row.getRowNum() + 3);
        // Create the comment and set the text+author
        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString("Hello, World!");
        comment.setString(str);
        comment.setAuthor("Apache POI");
        /* fixed the problem as well 
             * comment.setColumn(cell.getColumnIndex());
             * comment.setRow(cell.getRowIndex());
             */
        // Assign the comment to the cell
        cell.setCellComment(comment);
        OutputStream out = new FileOutputStream("C:\\temp\\58175.xlsx");
        try {
            wb.write(out);
        } finally {
            out.close();
        }
    } finally {
        wb.close();
    }
}
Also used : BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with Comment

use of org.apache.poi.ss.usermodel.Comment in project poi by apache.

the class TestXSSFComment method bug57838DeleteRowsWthCommentsBug.

@Test
public void bug57838DeleteRowsWthCommentsBug() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57838.xlsx");
    Sheet sheet = wb.getSheetAt(0);
    Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
    assertNotNull(comment1);
    Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
    assertNotNull(comment2);
    Row row = sheet.getRow(2);
    assertNotNull(row);
    // Remove row from index 2
    sheet.removeRow(row);
    row = sheet.getRow(2);
    // Row is null since we deleted it.
    assertNull(row);
    comment1 = sheet.getCellComment(new CellAddress(2, 1));
    // comment should be null but will fail due to bug
    assertNull(comment1);
    comment2 = sheet.getCellComment(new CellAddress(2, 2));
    // comment should be null but will fail due to bug
    assertNull(comment2);
    wb.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 3 with Comment

use of org.apache.poi.ss.usermodel.Comment in project poi by apache.

the class TestCommentsTable method writeRead.

@Test
public void writeRead() {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
    XSSFSheet sheet1 = workbook.getSheetAt(0);
    XSSFSheet sheet2 = workbook.getSheetAt(1);
    assertTrue(sheet1.hasComments());
    assertFalse(sheet2.hasComments());
    // Change on comment on sheet 1, and add another into
    //  sheet 2
    Row r5 = sheet1.getRow(4);
    Comment cc5 = r5.getCell(2).getCellComment();
    cc5.setAuthor("Apache POI");
    cc5.setString(new XSSFRichTextString("Hello!"));
    Row r2s2 = sheet2.createRow(2);
    Cell c1r2s2 = r2s2.createCell(1);
    assertNull(c1r2s2.getCellComment());
    Drawing<?> dg = sheet2.createDrawingPatriarch();
    Comment cc2 = dg.createCellComment(new XSSFClientAnchor());
    cc2.setAuthor("Also POI");
    cc2.setString(new XSSFRichTextString("A new comment"));
    c1r2s2.setCellComment(cc2);
    // Save, and re-load the file
    workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    // Check we still have comments where we should do
    sheet1 = workbook.getSheetAt(0);
    sheet2 = workbook.getSheetAt(1);
    assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
    assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
    assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
    // And check they still have the contents they should do
    assertEquals("Apache POI", sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
    assertEquals("Nick Burch", sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
    assertEquals("Also POI", sheet2.getRow(2).getCell(1).getCellComment().getAuthor());
    assertEquals("Hello!", sheet1.getRow(4).getCell(2).getCellComment().getString().getString());
}
Also used : Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFClientAnchor(org.apache.poi.xssf.usermodel.XSSFClientAnchor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 4 with Comment

use of org.apache.poi.ss.usermodel.Comment 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 5 with Comment

use of org.apache.poi.ss.usermodel.Comment 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)

Aggregations

Comment (org.apache.poi.ss.usermodel.Comment)14 Sheet (org.apache.poi.ss.usermodel.Sheet)8 Test (org.junit.Test)8 Cell (org.apache.poi.ss.usermodel.Cell)7 Row (org.apache.poi.ss.usermodel.Row)7 Workbook (org.apache.poi.ss.usermodel.Workbook)6 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)6 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)5 BaseTestCellComment (org.apache.poi.ss.usermodel.BaseTestCellComment)4 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)4 RichTextString (org.apache.poi.ss.usermodel.RichTextString)4 CellAddress (org.apache.poi.ss.util.CellAddress)4 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)4 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)4 XSSFClientAnchor (org.apache.poi.xssf.usermodel.XSSFClientAnchor)3 FileOutputStream (java.io.FileOutputStream)2 POITestCase.skipTest (org.apache.poi.POITestCase.skipTest)2 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)2 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)2