/** * エラーセルの値を評価する。 * @since 0.8.3 * @param errorValue エラーセルの値。 * @param locale * @return */ private CellFormatResult getErrorCellValue(final byte errorValue, final Locale locale) { final FormulaError error = FormulaError.forInt(errorValue); final CellFormatResult result = new CellFormatResult(); result.setCellType(FormatCellType.Error); result.setValue(error.getCode()); if(isErrorCellAsEmpty()) { result.setText(""); } else { result.setText(error.getString()); } return result; }
protected void visitCellValueError(PoiExcelColumnBean bean, Object cell, int errorCode, CellVisitor visitor) { Column column = bean.getColumn(); ErrorStrategy strategy = bean.getCellErrorStrategy(); switch (strategy.getStrategy()) { default: pageBuilder.setNull(column); return; case CONSTANT: String value = strategy.getValue(); if (value == null) { pageBuilder.setNull(column); } else { visitor.visitCellValueString(column, cell, value); } return; case ERROR_CODE: break; case EXCEPTION: FormulaError error = FormulaError.forInt((byte) errorCode); throw new RuntimeException(MessageFormat.format("encount cell error. error_code={0}({1})", errorCode, error.getString())); } visitor.visitCellValueError(column, cell, errorCode); }
@Theory public void testCellError_code(String excelFile) throws Exception { try (EmbulkPluginTester tester = new EmbulkPluginTester()) { tester.addParserPlugin(PoiExcelParserPlugin.TYPE, PoiExcelParserPlugin.class); EmbulkTestParserConfig parser = tester.newParserConfig(PoiExcelParserPlugin.TYPE); parser.set("sheet", "test1"); parser.set("skip_header_lines", 7); parser.set("on_cell_error", "error_code"); parser.addColumn("b", "boolean").set("column_number", "A"); parser.addColumn("l", "long").set("column_number", "A"); parser.addColumn("d", "double").set("column_number", "A"); parser.addColumn("s", "string").set("column_number", "A"); URL inFile = getClass().getResource(excelFile); List<OutputRecord> result = tester.runParser(inFile, parser); OutputRecord r = result.get(0); assertThat(r.getAsBoolean("b"), is(nullValue())); assertThat(r.getAsLong("l"), is((long) FormulaError.DIV0.getCode())); assertThat(r.getAsDouble("d"), is((double) FormulaError.DIV0.getCode())); assertThat(r.getAsString("s"), is("#DIV/0!")); } }
private boolean updatePrecalculatedCellValue(Cell destination, CellValue val) { if(val != null) { switch(val.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return updatePrecalculatedBoolean(destination, val.getBooleanValue()); case Cell.CELL_TYPE_NUMERIC: return updatePrecalculatedNumeric(destination, val.getNumberValue()); case Cell.CELL_TYPE_STRING: return updatePrecalculatedString(destination, val.getStringValue()); case Cell.CELL_TYPE_BLANK: return updatePrecalculatedBlank(destination); case Cell.CELL_TYPE_ERROR: return updatePrecalculatedError(destination, FormulaError.forInt(val.getErrorValue())); default: return false; } } else { return updatePrecalculatedError(destination, FormulaError.REF); } }
/** * <p> * Returns the formatted value of a cell as a <tt>String</tt> regardless * of the cell type. If the Excel format pattern cannot be parsed then the * cell value will be formatted using a default format. * </p> * <p>When passed a null or blank cell, this method will return an empty * String (""). Formula cells will be evaluated using the given * {@link FormulaEvaluator} if the evaluator is non-null. If the * evaluator is null, then the formula String will be returned. The caller * is responsible for setting the currentRow on the evaluator *</p> * * @param cell The cell (can be null) * @param evaluator The FormulaEvaluator (can be null) * @return a string value of the cell */ public String formatCellValue(Cell cell, FormulaEvaluator evaluator) { localeChangedObervable.checkForLocaleChange(); if (cell == null) { return ""; } int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_FORMULA) { if (evaluator == null) { return cell.getCellFormula(); } cellType = evaluator.evaluateFormulaCell(cell); } switch (cellType) { case Cell.CELL_TYPE_NUMERIC : if (ExcelDateUtil.isCellDateFormatted(cell)) { return getFormattedDateString(cell); } return getFormattedNumberString(cell); case Cell.CELL_TYPE_STRING : return cell.getRichStringCellValue().getString(); case Cell.CELL_TYPE_BOOLEAN : return String.valueOf(cell.getBooleanCellValue()); case Cell.CELL_TYPE_BLANK : return ""; case Cell.CELL_TYPE_ERROR: return FormulaError.forInt(cell.getErrorCellValue()).getString(); } throw new RuntimeException("Unexpected celltype (" + cellType + ")"); }
private boolean updatePrecalculatedValue(Cell destination, Cell source, FormulaEvaluator sEvaluator) { if(source != null) { switch(source.getCellType()) { case Cell.CELL_TYPE_BLANK: return updatePrecalculatedBlank(destination); case Cell.CELL_TYPE_BOOLEAN: return updatePrecalculatedBoolean(destination, source.getBooleanCellValue()); case Cell.CELL_TYPE_ERROR: return updatePrecalculatedError(destination, FormulaError.forInt(source.getErrorCellValue())); case Cell.CELL_TYPE_FORMULA: try { return updatePrecalculatedCellValue(destination, sEvaluator.evaluate(source)); } catch(Exception e) { switch(source.getCachedFormulaResultType()) { case Cell.CELL_TYPE_NUMERIC: return updatePrecalculatedNumeric(destination, source.getNumericCellValue()); case Cell.CELL_TYPE_STRING: return updatePrecalculatedString(destination, source.getStringCellValue()); case Cell.CELL_TYPE_BOOLEAN: return updatePrecalculatedBoolean(destination, source.getBooleanCellValue()); case Cell.CELL_TYPE_ERROR: return updatePrecalculatedError(destination, FormulaError.forInt(source.getErrorCellValue())); } } case Cell.CELL_TYPE_NUMERIC: return updatePrecalculatedNumeric(destination, source.getNumericCellValue()); case Cell.CELL_TYPE_STRING: return updatePrecalculatedString(destination, source.getStringCellValue()); default: return false; } } else { return updatePrecalculatedError(destination, FormulaError.REF); } }
private boolean updatePrecalculatedError(Cell destination, FormulaError sError) { if(isFormula(destination)) { try { FormulaError dError = FormulaError.forInt(destination.getErrorCellValue()); if(sError != dError) { destination.setCellErrorValue(sError.getCode()); return true; } } catch(Exception e) { destination.setCellErrorValue(sError.getCode()); return true; } } return false; }
public int getErrorCellValue() { if (CELL_TYPE_ERROR != ConverterUtils.resolveCellType(this.value)) { throw new CalculationEngineException("Trying to get error value from non-error cell."); } return FormulaError.forString((String) this.value.get()).getCode(); }
@Override public void visitCellValueError(Column column, Object source, int code) { FormulaError error = FormulaError.forInt((byte) code); String value = error.getString(); pageBuilder.setString(column, value); }
public static String getCellValue(Workbook wb, Cell cell) { if (cell == null) { return null; } final String cellCoordinate = "(" + cell.getRowIndex() + "," + cell.getColumnIndex() + ")"; final String result; switch (cell.getCellTypeEnum()) { case BLANK: case _NONE: result = null; break; case BOOLEAN: result = Boolean.toString(cell.getBooleanCellValue()); break; case ERROR: String errorResult; try { byte errorCode = cell.getErrorCellValue(); FormulaError formulaError = FormulaError.forInt(errorCode); errorResult = formulaError.getString(); } catch (RuntimeException e) { logger.debug("Getting error code for {} failed!: {}", cellCoordinate, e.getMessage()); if (cell instanceof XSSFCell) { // hack to get error string, which is available String value = ((XSSFCell) cell).getErrorCellString(); errorResult = value; } else { logger.error("Couldn't handle unexpected error scenario in cell: " + cellCoordinate, e); throw e; } } result = errorResult; break; case FORMULA: // result = cell.getCellFormula(); result = getFormulaCellValue(wb, cell); break; case NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (date == null) { result = null; } else { result = DateUtils.createDateFormat().format(date); } } else { // TODO: Consider not formatting it, but simple using // Double.toString(...) result = _numberFormat.format(cell.getNumericCellValue()); } break; case STRING: result = cell.getRichStringCellValue().getString(); break; default: throw new IllegalStateException("Unknown cell type: " + cell.getCellTypeEnum()); } logger.debug("cell {} resolved to value: {}", cellCoordinate, result); return result; }
private CellData getCellValue(CellData cellData) { //CellData cellData = new CellData(); if(cellData.cell != null) { // evaluator.evaluateFormulaCell(cell); int cellTypeEvaluator = cellData.workBookObject.evaluator.evaluateFormulaCell(cellData.cell); int cellType = cellTypeEvaluator > -1 ? cellTypeEvaluator : cellData.cell .getCellType(); switch (cellType) { case Cell.CELL_TYPE_BLANK: // cellData.put("type",HSSFCell.CELL_TYPE_BLANK); cellData.value = cellData.cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: // cellData.put("type",HSSFCell.CELL_TYPE_NUMERIC); cellData.value = Double.toString(cellData.cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: // cellData.put("type",HSSFCell.CELL_TYPE_STRING); cellData.value = cellData.cell.getStringCellValue(); break; case Cell.CELL_TYPE_ERROR: cellData.error = FormulaError.forInt(cellData.cell.getErrorCellValue()).name(); cellData.value = "ERROR"; break; case Cell.CELL_TYPE_FORMULA: // cellData.put("type", HSSFCell.CELL_TYPE_ERROR); cellData.value = cellData.cell.getCellFormula(); break; default: // cellData.put("type",HSSFCell.CELL_TYPE_STRING); cellData.value = cellData.cell.getCellStyle().getDataFormatString(); } // si la celda es pocentaje multiplico por 100 if (cellData.cell.getCellStyle().getDataFormatString().contains("%") && cellType == Cell.CELL_TYPE_NUMERIC) { Double perceValue = Double.parseDouble(cellData.value) * 100; cellData.value = perceValue.toString(); } } else { cellData.value = null; } return cellData; }