Search in sources :

Example 1 with CommentsTable

use of org.apache.poi.xssf.model.CommentsTable in project poi by apache.

the class TestXSSFComment method testBug58175.

@Test
public void testBug58175() throws IOException {
    Workbook wb = new SXSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(3);
        cell.setCellValue("F4");
        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);
        XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
        // create comments and vmlDrawing parts if they don't exist
        CommentsTable comments = ((SXSSFWorkbook) wb).getXSSFWorkbook().getSheetAt(0).getCommentsTable(true);
        XSSFVMLDrawing vml = ((SXSSFWorkbook) wb).getXSSFWorkbook().getSheetAt(0).getVMLDrawing(true);
        CTShape vmlShape1 = vml.newCommentShape();
        if (ca.isSet()) {
            String position = ca.getCol1() + ", 0, " + ca.getRow1() + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2() + ", 0";
            vmlShape1.getClientDataArray(0).setAnchorArray(0, position);
        }
        // create the comment in two different ways and verify that there is no difference
        XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1);
        shape1.setColumn(ca.getCol1());
        shape1.setRow(ca.getRow1());
        CTShape vmlShape2 = vml.newCommentShape();
        if (ca.isSet()) {
            String position = ca.getCol1() + ", 0, " + ca.getRow1() + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2() + ", 0";
            vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
        }
        CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
        XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
        assertEquals(shape1.getAuthor(), shape2.getAuthor());
        assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());
        assertEquals(shape1.getColumn(), shape2.getColumn());
        assertEquals(shape1.getRow(), shape2.getRow());
        assertEquals(shape1.getCTComment().toString(), shape2.getCTComment().toString());
        assertEquals(shape1.getCTComment().getRef(), shape2.getCTComment().getRef());
        /*CommentsTable table1 = shape1.getCommentsTable();
            CommentsTable table2 = shape2.getCommentsTable();
            assertEquals(table1.getCTComments().toString(), table2.getCTComments().toString());
            assertEquals(table1.getNumberOfComments(), table2.getNumberOfComments());
            assertEquals(table1.getRelations(), table2.getRelations());*/
        assertEquals("The vmlShapes should have equal content afterwards", vmlShape1.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"), vmlShape2.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"));
    } finally {
        wb.close();
    }
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) CTShape(com.microsoft.schemas.vml.CTShape) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) CommentsTable(org.apache.poi.xssf.model.CommentsTable) CellAddress(org.apache.poi.ss.util.CellAddress) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 2 with CommentsTable

use of org.apache.poi.xssf.model.CommentsTable in project poi by apache.

the class TestXSSFComment method constructor.

/**
     * test properties of a newly constructed comment
     */
@Test
public void constructor() {
    CommentsTable sheetComments = new CommentsTable();
    assertNotNull(sheetComments.getCTComments().getCommentList());
    assertNotNull(sheetComments.getCTComments().getAuthors());
    assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
    assertEquals(1, sheetComments.getNumberOfAuthors());
    CTComment ctComment = sheetComments.newComment(CellAddress.A1);
    CTShape vmlShape = CTShape.Factory.newInstance();
    XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
    assertEquals(null, comment.getString());
    assertEquals(0, comment.getRow());
    assertEquals(0, comment.getColumn());
    assertEquals("", comment.getAuthor());
    assertEquals(false, comment.isVisible());
}
Also used : CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) CTShape(com.microsoft.schemas.vml.CTShape) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Example 3 with CommentsTable

use of org.apache.poi.xssf.model.CommentsTable in project poi by apache.

the class TestXSSFComment method getSetCol.

@Test
public void getSetCol() {
    CommentsTable sheetComments = new CommentsTable();
    XSSFVMLDrawing vml = new XSSFVMLDrawing();
    CTComment ctComment = sheetComments.newComment(CellAddress.A1);
    CTShape vmlShape = vml.newCommentShape();
    XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
    comment.setColumn(1);
    assertEquals(1, comment.getColumn());
    assertEquals(1, new CellReference(ctComment.getRef()).getCol());
    assertEquals(1, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
    comment.setColumn(5);
    assertEquals(5, comment.getColumn());
    assertEquals(5, new CellReference(ctComment.getRef()).getCol());
    assertEquals(5, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
}
Also used : CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) CTShape(com.microsoft.schemas.vml.CTShape) CellReference(org.apache.poi.ss.util.CellReference) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Example 4 with CommentsTable

use of org.apache.poi.xssf.model.CommentsTable in project poi by apache.

the class TestXSSFSheet method commentsTable.

@Test
public void commentsTable() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet1 = wb1.createSheet();
    CommentsTable comment1 = sheet1.getCommentsTable(false);
    assertNull(comment1);
    comment1 = sheet1.getCommentsTable(true);
    assertNotNull(comment1);
    assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
    assertSame(comment1, sheet1.getCommentsTable(true));
    //second sheet
    XSSFSheet sheet2 = wb1.createSheet();
    CommentsTable comment2 = sheet2.getCommentsTable(false);
    assertNull(comment2);
    comment2 = sheet2.getCommentsTable(true);
    assertNotNull(comment2);
    assertSame(comment2, sheet2.getCommentsTable(true));
    assertEquals("/xl/comments2.xml", comment2.getPackagePart().getPartName().getName());
    //comment1 and  comment2 are different objects
    assertNotSame(comment1, comment2);
    wb1.close();
    //now test against a workbook containing cell comments
    XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
    sheet1 = wb2.getSheetAt(0);
    comment1 = sheet1.getCommentsTable(true);
    assertNotNull(comment1);
    assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
    assertSame(comment1, sheet1.getCommentsTable(true));
    wb2.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Example 5 with CommentsTable

use of org.apache.poi.xssf.model.CommentsTable in project poi by apache.

the class XSSFSheet method read.

protected void read(InputStream is) throws IOException {
    try {
        worksheet = WorksheetDocument.Factory.parse(is, DEFAULT_XML_OPTIONS).getWorksheet();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
    initRows(worksheet);
    columnHelper = new ColumnHelper(worksheet);
    // Look for bits we're interested in
    for (RelationPart rp : getRelationParts()) {
        POIXMLDocumentPart p = rp.getDocumentPart();
        if (p instanceof CommentsTable) {
            sheetComments = (CommentsTable) p;
        }
        if (p instanceof XSSFTable) {
            tables.put(rp.getRelationship().getId(), (XSSFTable) p);
        }
        if (p instanceof XSSFPivotTable) {
            getWorkbook().getPivotTables().add((XSSFPivotTable) p);
        }
    }
    // Process external hyperlinks for the sheet, if there are any
    initHyperlinks();
}
Also used : ColumnHelper(org.apache.poi.xssf.usermodel.helpers.ColumnHelper) XmlException(org.apache.xmlbeans.XmlException) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) CommentsTable(org.apache.poi.xssf.model.CommentsTable)

Aggregations

CommentsTable (org.apache.poi.xssf.model.CommentsTable)12 Test (org.junit.Test)7 CTShape (com.microsoft.schemas.vml.CTShape)4 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)4 InputStream (java.io.InputStream)3 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)2 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)2 Cell (org.apache.poi.ss.usermodel.Cell)2 CellAddress (org.apache.poi.ss.util.CellAddress)2 CellReference (org.apache.poi.ss.util.CellReference)2 ReadOnlySharedStringsTable (org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable)2 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)2 StylesTable (org.apache.poi.xssf.model.StylesTable)2 XmlException (org.apache.xmlbeans.XmlException)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)1 POIXMLException (org.apache.poi.POIXMLException)1 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)1