GerardDellemann commented on a change in pull request #234: MM-82 Detect Column Types URL: https://github.com/apache/metamodel/pull/234#discussion_r355971263
########## File path: excel/src/main/java/org/apache/metamodel/excel/ExcelUtils.java ########## @@ -232,53 +249,119 @@ public static String getCellValue(Workbook wb, Cell cell) { throw new IllegalStateException("Unknown cell type: " + cell.getCellType()); } - logger.debug("cell {} resolved to value: {}", cellCoordinate, result); + logger.debug("cell ({},{}) resolved to value: {}", cell.getRowIndex(), cell.getColumnIndex(), result); return result; } - private static String getFormulaCellValue(Workbook wb, Cell cell) { + private static Object getErrorResult(final Cell cell) { + String errorResult; + try { + errorResult = FormulaError.forInt(cell.getErrorCellValue()).getString(); + } catch (final RuntimeException e) { + logger + .debug("Getting error code for ({},{}) failed!: {}", cell.getRowIndex(), cell.getColumnIndex(), e + .getMessage()); + if (cell instanceof XSSFCell) { + // hack to get error string, which is available + errorResult = ((XSSFCell) cell).getErrorCellString(); + } else { + logger + .error("Couldn't handle unexpected error scenario in cell: ({},{})", cell.getRowIndex(), cell + .getColumnIndex()); + throw e; + } + } + return errorResult; + } + + private static Object getCellValueChecked(final Workbook workbook, final Cell cell, + final ColumnType expectedColumnType) { + final Object value = getCellValueAsObject(workbook, cell); + if (value == null || value.getClass().equals(expectedColumnType.getJavaEquivalentClass())) { + return value; + } + + // Don't log when an Integer value is in a Double column type + if (!(value.getClass().equals(Integer.class) && expectedColumnType + .getJavaEquivalentClass() + .equals(Double.class)) && logger.isWarnEnabled()) { + logger + .warn("Cell ({},{}) has the value '{}' of data type '{}', which doesn't match the detected " + + "column's data type '{}'. This cell gets value NULL in the DataSet.", cell.getRowIndex(), + cell.getColumnIndex(), value, value.getClass().getSimpleName(), expectedColumnType); + } + return null; + } + + private static String getFormulaCellValue(Workbook workbook, Cell cell) { // first try with a cached/precalculated value try { double numericCellValue = cell.getNumericCellValue(); - // TODO: Consider not formatting it, but simple using - // Double.toString(...) - return _numberFormat.format(numericCellValue); + return getNumericCellValueAsString(cell.getCellStyle(), numericCellValue); } catch (Exception e) { if (logger.isInfoEnabled()) { logger.info("Failed to fetch cached/precalculated formula value of cell: " + cell, e); } } // evaluate cell first, if possible + final Cell evaluatedCell = getEvaluatedCell(workbook, cell); + if (evaluatedCell != null) { + return getCellValue(workbook, evaluatedCell); + } else { + // last resort: return the string formula + return cell.getCellFormula(); + } + } + + private static Object getFormulaCellValueAsObject(final Workbook workbook, final Cell cell) { + // first try with a cached/precalculated value + try { + return getDoubleAsNumber(cell.getNumericCellValue()); + } catch (final Exception e) { + if (logger.isInfoEnabled()) { + logger.info("Failed to fetch cached/precalculated formula value of cell: " + cell, e); + } + } + + // evaluate cell first, if possible + final Cell evaluatedCell = getEvaluatedCell(workbook, cell); + if (evaluatedCell != null) { + return getCellValueAsObject(workbook, evaluatedCell); + } else { + // last resort: return the string formula + return cell.getCellFormula(); + } + } + + private static Cell getEvaluatedCell(final Workbook workbook, final Cell cell) { Review comment: sure ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services