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());
}
}
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);
}
}
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;
}
}
}
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;
}
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);
}
}
Aggregations