Search in sources :

Example 36 with CellAddress

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

the class BaseTestCellComment method readComments.

/**
     * test that we can read cell comments from an existing workbook.
     */
@Test
public final void readComments() throws IOException {
    Workbook wb = _testDataProvider.openSampleWorkbook("SimpleWithComments." + _testDataProvider.getStandardFileNameExtension());
    Sheet sheet = wb.getSheetAt(0);
    Cell cell;
    Row row;
    Comment comment;
    for (int rownum = 0; rownum < 3; rownum++) {
        row = sheet.getRow(rownum);
        cell = row.getCell(0);
        comment = cell.getCellComment();
        assertNull("Cells in the first column are not commented", comment);
        assertNull(sheet.getCellComment(new CellAddress(rownum, 0)));
    }
    for (int rownum = 0; rownum < 3; rownum++) {
        row = sheet.getRow(rownum);
        cell = row.getCell(1);
        comment = cell.getCellComment();
        assertNotNull("Cells in the second column have comments", comment);
        assertNotNull("Cells in the second column have comments", sheet.getCellComment(new CellAddress(rownum, 1)));
        assertEquals("Yegor Kozlov", comment.getAuthor());
        assertFalse("cells in the second column have not empyy notes", "".equals(comment.getString().getString()));
        assertEquals(rownum, comment.getRow());
        assertEquals(cell.getColumnIndex(), comment.getColumn());
    }
    wb.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Test(org.junit.Test)

Example 37 with CellAddress

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

the class BaseTestCellComment method setAddress.

@Test
public void setAddress() {
    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());
    CellAddress C2 = new CellAddress("C2");
    assertEquals("C2", C2.formatAsString());
    comment.setAddress(C2);
    assertEquals(C2, comment.getAddress());
    CellAddress E10 = new CellAddress(9, 4);
    assertEquals("E10", E10.formatAsString());
    comment.setAddress(9, 4);
    assertEquals(E10, comment.getAddress());
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Test(org.junit.Test)

Example 38 with CellAddress

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

the class CommentsTable method referenceUpdated.

/**
     * Called after the reference is updated, so that
     *  we can reflect that in our cache
     *  @param oldReference the comment to remove from the commentRefs map
     *  @param comment the comment to replace in the commentRefs map
     */
public void referenceUpdated(CellAddress oldReference, CTComment comment) {
    if (commentRefs != null) {
        commentRefs.remove(oldReference);
        commentRefs.put(new CellAddress(comment.getRef()), comment);
    }
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress)

Example 39 with CellAddress

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

the class XSSFBHyperlinksTable method getHyperLinks.

/**
     *
     * @return a map of the hyperlinks. The key is the top left cell address in their CellRange
     */
public Map<CellAddress, List<XSSFHyperlinkRecord>> getHyperLinks() {
    Map<CellAddress, List<XSSFHyperlinkRecord>> hyperlinkMap = new TreeMap<CellAddress, List<XSSFHyperlinkRecord>>(new TopLeftCellAddressComparator());
    for (XSSFHyperlinkRecord hyperlinkRecord : hyperlinkRecords) {
        CellAddress cellAddress = new CellAddress(hyperlinkRecord.getCellRangeAddress().getFirstRow(), hyperlinkRecord.getCellRangeAddress().getFirstColumn());
        List<XSSFHyperlinkRecord> list = hyperlinkMap.get(cellAddress);
        if (list == null) {
            list = new ArrayList<XSSFHyperlinkRecord>();
        }
        list.add(hyperlinkRecord);
        hyperlinkMap.put(cellAddress, list);
    }
    return hyperlinkMap;
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap)

Example 40 with CellAddress

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

the class XSSFDrawing method createCellComment.

/**
	 * Creates a comment.
	 * @param anchor the client anchor describes how this comment is attached
	 *               to the sheet.
	 * @return the newly created comment.
	 */
@Override
public XSSFComment createCellComment(ClientAnchor anchor) {
    XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
    XSSFSheet sheet = getSheet();
    //create comments and vmlDrawing parts if they don't exist
    CommentsTable comments = sheet.getCommentsTable(true);
    XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
    com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape();
    if (ca.isSet()) {
        // convert offsets from emus to pixels since we get a DrawingML-anchor
        // but create a VML Drawing
        int dx1Pixels = ca.getDx1() / Units.EMU_PER_PIXEL;
        int dy1Pixels = ca.getDy1() / Units.EMU_PER_PIXEL;
        int dx2Pixels = ca.getDx2() / Units.EMU_PER_PIXEL;
        int dy2Pixels = ca.getDy2() / Units.EMU_PER_PIXEL;
        String position = ca.getCol1() + ", " + dx1Pixels + ", " + ca.getRow1() + ", " + dy1Pixels + ", " + ca.getCol2() + ", " + dx2Pixels + ", " + ca.getRow2() + ", " + dy2Pixels;
        vmlShape.getClientDataArray(0).setAnchorArray(0, position);
    }
    CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
    if (comments.findCellComment(ref) != null) {
        throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
    }
    return new XSSFComment(comments, comments.newComment(ref), vmlShape);
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) CommentsTable(org.apache.poi.xssf.model.CommentsTable)

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