Search in sources :

Example 1 with CellAddress

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

the class XSSFSheetXMLHandler method checkForEmptyCellComments.

/**
    * Do a check for, and output, comments in otherwise empty cells.
    */
private void checkForEmptyCellComments(EmptyCellCommentsCheckType type) {
    if (commentCellRefs != null && !commentCellRefs.isEmpty()) {
        //  comments we haven't yet already handled
        if (type == EmptyCellCommentsCheckType.END_OF_SHEET_DATA) {
            while (!commentCellRefs.isEmpty()) {
                outputEmptyCellComment(commentCellRefs.remove());
            }
            return;
        }
        // At the end of a row, handle any comments for "missing" rows before us
        if (this.cellRef == null) {
            if (type == EmptyCellCommentsCheckType.END_OF_ROW) {
                while (!commentCellRefs.isEmpty()) {
                    if (commentCellRefs.peek().getRow() == rowNum) {
                        outputEmptyCellComment(commentCellRefs.remove());
                    } else {
                        return;
                    }
                }
                return;
            } else {
                throw new IllegalStateException("Cell ref should be null only if there are only empty cells in the row; rowNum: " + rowNum);
            }
        }
        CellAddress nextCommentCellRef;
        do {
            CellAddress cellRef = new CellAddress(this.cellRef);
            CellAddress peekCellRef = commentCellRefs.peek();
            if (type == EmptyCellCommentsCheckType.CELL && cellRef.equals(peekCellRef)) {
                // remove the comment cell ref from the list if we're about to handle it alongside the cell content
                commentCellRefs.remove();
                return;
            } else {
                // fill in any gaps if there are empty cells with comment mixed in with non-empty cells
                int comparison = peekCellRef.compareTo(cellRef);
                if (comparison > 0 && type == EmptyCellCommentsCheckType.END_OF_ROW && peekCellRef.getRow() <= rowNum) {
                    nextCommentCellRef = commentCellRefs.remove();
                    outputEmptyCellComment(nextCommentCellRef);
                } else if (comparison < 0 && type == EmptyCellCommentsCheckType.CELL && peekCellRef.getRow() <= rowNum) {
                    nextCommentCellRef = commentCellRefs.remove();
                    outputEmptyCellComment(nextCommentCellRef);
                } else {
                    nextCommentCellRef = null;
                }
            }
        } while (nextCommentCellRef != null && !commentCellRefs.isEmpty());
    }
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress)

Example 2 with CellAddress

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

the class XSSFSheetXMLHandler method endElement.

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (uri != null && !uri.equals(NS_SPREADSHEETML)) {
        return;
    }
    String thisStr = null;
    // v => contents of a cell
    if (isTextTag(localName)) {
        vIsOpen = false;
        // Process the value contents as required, now we have it all
        switch(nextDataType) {
            case BOOLEAN:
                char first = value.charAt(0);
                thisStr = first == '0' ? "FALSE" : "TRUE";
                break;
            case ERROR:
                thisStr = "ERROR:" + value;
                break;
            case FORMULA:
                if (formulasNotResults) {
                    thisStr = formula.toString();
                } else {
                    String fv = value.toString();
                    if (this.formatString != null) {
                        try {
                            // Try to use the value as a formattable number
                            double d = Double.parseDouble(fv);
                            thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString);
                        } catch (NumberFormatException e) {
                            // Formula is a String result not a Numeric one
                            thisStr = fv;
                        }
                    } else {
                        // No formatting applied, just do raw value in all cases
                        thisStr = fv;
                    }
                }
                break;
            case INLINE_STRING:
                // TODO: Can these ever have formatting on them?
                XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
                thisStr = rtsi.toString();
                break;
            case SST_STRING:
                String sstIndex = value.toString();
                try {
                    int idx = Integer.parseInt(sstIndex);
                    XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx));
                    thisStr = rtss.toString();
                } catch (NumberFormatException ex) {
                    logger.log(POILogger.ERROR, "Failed to parse SST index '" + sstIndex, ex);
                }
                break;
            case NUMBER:
                String n = value.toString();
                if (this.formatString != null && n.length() > 0)
                    thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
                else
                    thisStr = n;
                break;
            default:
                thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
                break;
        }
        // Do we have a comment for this cell?
        checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
        XSSFComment comment = commentsTable != null ? commentsTable.findCellComment(new CellAddress(cellRef)) : null;
        // Output
        output.cell(cellRef, thisStr, comment);
    } else if ("f".equals(localName)) {
        fIsOpen = false;
    } else if ("is".equals(localName)) {
        isIsOpen = false;
    } else if ("row".equals(localName)) {
        // Handle any "missing" cells which had comments attached
        checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_ROW);
        // Finish up the row
        output.endRow(rowNum);
        // some sheets do not have rowNum set in the XML, Excel can read them so we should try to read them as well
        nextRowNum = rowNum + 1;
    } else if ("sheetData".equals(localName)) {
        // Handle any "missing" cells which had comments attached
        checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_SHEET_DATA);
    } else if ("oddHeader".equals(localName) || "evenHeader".equals(localName) || "firstHeader".equals(localName)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), true, localName);
    } else if ("oddFooter".equals(localName) || "evenFooter".equals(localName) || "firstFooter".equals(localName)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), false, localName);
    }
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFComment(org.apache.poi.xssf.usermodel.XSSFComment)

Example 3 with CellAddress

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

the class XSSFBSheetHandler method checkMissedComments.

//at start of next cell or end of row, return the cellAddress if it equals currentRow and col
private void checkMissedComments(int currentRow, int colNum) {
    if (comments == null) {
        return;
    }
    Queue<CellAddress> queue = comments.getAddresses();
    while (queue.size() > 0) {
        CellAddress cellAddress = queue.peek();
        if (cellAddress.getRow() == currentRow && cellAddress.getColumn() < colNum) {
            cellAddress = queue.remove();
            dumpEmptyCellComment(cellAddress, comments.get(cellAddress));
        } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() == colNum) {
            queue.remove();
            return;
        } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() > colNum) {
            return;
        } else if (cellAddress.getRow() > currentRow) {
            return;
        }
    }
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress)

Example 4 with CellAddress

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

the class CommentsTable method getCellComments.

/**
     * Returns all cell comments on this sheet.
     * @return A map of each Comment in this sheet, keyed on the cell address where
     * the comment is located.
     */
public Map<CellAddress, XSSFComment> getCellComments() {
    prepareCTCommentCache();
    final TreeMap<CellAddress, XSSFComment> map = new TreeMap<CellAddress, XSSFComment>();
    for (final Entry<CellAddress, CTComment> e : commentRefs.entrySet()) {
        map.put(e.getKey(), new XSSFComment(this, e.getValue(), null));
    }
    return map;
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) TreeMap(java.util.TreeMap) XSSFComment(org.apache.poi.xssf.usermodel.XSSFComment)

Example 5 with CellAddress

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

the class XSSFComment method setAddress.

@Override
public void setAddress(CellAddress address) {
    CellAddress oldRef = new CellAddress(_comment.getRef());
    if (address.equals(oldRef)) {
        // nothing to do
        return;
    }
    _comment.setRef(address.formatAsString());
    _comments.referenceUpdated(oldRef, _comment);
    if (_vmlShape != null) {
        CTClientData clientData = _vmlShape.getClientDataArray(0);
        clientData.setRowArray(0, new BigInteger(String.valueOf(address.getRow())));
        clientData.setColumnArray(0, new BigInteger(String.valueOf(address.getColumn())));
        avoidXmlbeansCorruptPointer(_vmlShape);
    }
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) BigInteger(java.math.BigInteger) CTClientData(com.microsoft.schemas.office.excel.CTClientData)

Aggregations

CellAddress (org.apache.poi.ss.util.CellAddress)43 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 CellReference (org.apache.poi.ss.util.CellReference)2 CommentsTable (org.apache.poi.xssf.model.CommentsTable)2 XSSFComment (org.apache.poi.xssf.usermodel.XSSFComment)2