use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class TestMultiSheetEval method confirmExpectedResult.
private static void confirmExpectedResult(String msg, Cell expected, CellValue actual) {
if (expected == null) {
throw new AssertionFailedError(msg + " - Bad setup data expected value is null");
}
if (actual == null) {
throw new AssertionFailedError(msg + " - actual value was null");
}
final CellType cellType = expected.getCellTypeEnum();
switch(cellType) {
case BLANK:
assertEquals(msg, CellType.BLANK, actual.getCellTypeEnum());
break;
case BOOLEAN:
assertEquals(msg, CellType.BOOLEAN, actual.getCellTypeEnum());
assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue());
break;
case ERROR:
assertEquals(msg, CellType.ERROR, actual.getCellTypeEnum());
assertEquals(msg, ErrorEval.getText(expected.getErrorCellValue()), ErrorEval.getText(actual.getErrorValue()));
break;
case // will never be used, since we will call method after formula evaluation
FORMULA:
throw new AssertionFailedError("Cannot expect formula as result of formula evaluation: " + msg);
case NUMERIC:
assertEquals(msg, CellType.NUMERIC, actual.getCellTypeEnum());
TestMathX.assertEquals(msg, expected.getNumericCellValue(), actual.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR);
break;
case STRING:
assertEquals(msg, CellType.STRING, actual.getCellTypeEnum());
assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue());
break;
default:
throw new AssertionFailedError("Unexpected cell type: " + cellType);
}
}
use of org.apache.poi.ss.usermodel.CellType in project poi by apache.
the class SheetUtil method getCellWidth.
/**
* Compute width of a single cell
*
* @param cell the cell whose width is to be calculated
* @param defaultCharWidth the width of a single character
* @param formatter formatter used to prepare the text to be measured
* @param useMergedCells whether to use merged cells
* @return the width in pixels or -1 if cell is empty
*/
public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
Sheet sheet = cell.getSheet();
Workbook wb = sheet.getWorkbook();
Row row = cell.getRow();
int column = cell.getColumnIndex();
// FIXME: this looks very similar to getCellWithMerges below. Consider consolidating.
// We should only be checking merged regions if useMergedCells is true. Why are we doing this for-loop?
int colspan = 1;
for (CellRangeAddress region : sheet.getMergedRegions()) {
if (region.isInRange(row.getRowNum(), column)) {
if (!useMergedCells) {
// If we're not using merged cells, skip this one and move on to the next.
return -1;
}
cell = row.getCell(region.getFirstColumn());
colspan = 1 + region.getLastColumn() - region.getFirstColumn();
}
}
CellStyle style = cell.getCellStyle();
CellType cellType = cell.getCellTypeEnum();
// for formula cells we compute the cell width for the cached formula result
if (cellType == CellType.FORMULA)
cellType = cell.getCachedFormulaResultTypeEnum();
Font font = wb.getFontAt(style.getFontIndex());
double width = -1;
if (cellType == CellType.STRING) {
RichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
for (String line : lines) {
String txt = line + defaultChar;
AttributedString str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
if (rt.numFormattingRuns() > 0) {
// TODO: support rich text fragments
}
width = getCellWidth(defaultCharWidth, colspan, style, width, str);
}
} else {
String sval = null;
if (cellType == CellType.NUMERIC) {
// Try to get it formatted to look the same as excel
try {
sval = formatter.formatCellValue(cell, dummyEvaluator);
} catch (Exception e) {
sval = String.valueOf(cell.getNumericCellValue());
}
} else if (cellType == CellType.BOOLEAN) {
sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase(Locale.ROOT);
}
if (sval != null) {
String txt = sval + defaultChar;
AttributedString str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
width = getCellWidth(defaultCharWidth, colspan, style, width, str);
}
}
return width;
}
Aggregations