arjansh commented on a change in pull request #234: MM-82 Detect Column Types
URL: https://github.com/apache/metamodel/pull/234#discussion_r355410599
 
 

 ##########
 File path: excel/src/main/java/org/apache/metamodel/excel/ExcelUtils.java
 ##########
 @@ -232,18 +230,93 @@ 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 Object getCellValueAsObject(final Workbook workbook, final 
Cell cell) {
+        if (cell == null) {
+            return null;
+        }
+
+        final Object result;
+
+        switch (cell.getCellType()) {
+        case BLANK:
+        case _NONE:
+            result = null;
+            break;
+        case BOOLEAN:
+            result = Boolean.valueOf(cell.getBooleanCellValue());
+            break;
+        case ERROR:
+            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() + ")", e);
+                    throw e;
+                }
+            }
+            result = errorResult;
+            break;
+        case FORMULA:
+            result = getFormulaCellValueAsObject(workbook, cell);
+            break;
+        case NUMERIC:
+            if (DateUtil.isCellDateFormatted(cell)) {
+                result = cell.getDateCellValue();
+            } else {
+                result = getDoubleAsNumber(cell.getNumericCellValue());
+            }
+            break;
+        case STRING:
+            result = cell.getRichStringCellValue().getString();
+            break;
+        default:
+            throw new IllegalStateException("Unknown cell type: " + 
cell.getCellType());
+        }
+
+        logger.debug("cell ({},{}) resolved to value: {}", cell.getRowIndex(), 
cell.getColumnIndex(), result);
+
+        return result;
+    }
+
+    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()) {
+            final String warning = String
 
 Review comment:
   Why assign this to a String outside the `logger.warn` invocation, which has 
built in logic for doing stuff like this?

----------------------------------------------------------------
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

Reply via email to