Search in sources :

Example 31 with CellType

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);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) AssertionFailedError(junit.framework.AssertionFailedError)

Example 32 with 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;
}
Also used : RichTextString(org.apache.poi.ss.usermodel.RichTextString) AttributedString(java.text.AttributedString) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) AttributedString(java.text.AttributedString) CellType(org.apache.poi.ss.usermodel.CellType) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet)

Aggregations

CellType (org.apache.poi.ss.usermodel.CellType)32 Test (org.junit.Test)17 STCellType (org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType)7 RichTextString (org.apache.poi.ss.usermodel.RichTextString)5 Cell (org.apache.poi.ss.usermodel.Cell)4 CellValue (org.apache.poi.ss.usermodel.CellValue)4 Row (org.apache.poi.ss.usermodel.Row)4 Ignore (org.junit.Ignore)4 CellStyle (org.apache.poi.ss.usermodel.CellStyle)3 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)2 CTCellFormula (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)2 AttributedString (java.text.AttributedString)1 AssertionFailedError (junit.framework.AssertionFailedError)1 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)1 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 CellReference (org.apache.poi.hssf.util.CellReference)1 Font (org.apache.poi.ss.usermodel.Font)1