arjansh commented on a change in pull request #230: dev/add datatypes to excel columns URL: https://github.com/apache/metamodel/pull/230#discussion_r328933810
########## File path: excel/src/main/java/org/apache/metamodel/excel/DefaultSpreadsheetReaderDelegate.java ########## @@ -167,50 +167,109 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { break; } } - if (hasColumns) { - createColumns(table, wb, row); + createColumns(table, wb, row, columnTypes); } } - return table; } + private ColumnType[] getColumnTypes(final Sheet sheet, final Row row) { + final Iterator<Row> data = ExcelUtils.getRowIterator(sheet, _configuration, false); + final int rowLength = row.getLastCellNum(); + int eagerness = 1000; + final ColumnType[] columnTypes = new ColumnType[rowLength]; + + while (data.hasNext() && eagerness-- > 0) { + Row currentRow = data.next(); + for (int index = 0; index < rowLength; index++) { + if (currentRow.getLastCellNum() == 0) { + continue; + } + if (currentRow.getCell(index) == null) { + checkColumnType(ColumnType.STRING, columnTypes, index); + } else { + CellType cellType = currentRow.getCell(index).getCellType(); + switch (cellType) { + case NUMERIC: + if (DateUtil.isCellDateFormatted(currentRow.getCell(index))) { + checkColumnType(ColumnType.DATE, columnTypes, index); + } else { + checkColumnType((currentRow.getCell(index).getNumericCellValue() % 1 == 0) + ? ColumnType.INTEGER : ColumnType.DOUBLE, columnTypes, index); + } + break; + case BOOLEAN: + checkColumnType(ColumnType.BOOLEAN, columnTypes, index); + break; + case ERROR: + // fall through + break; + case _NONE: + // fall through + case STRING: + // fall through + case FORMULA: + // fall through + case BLANK: + checkColumnType(ColumnType.STRING, columnTypes, index); + break; + } + } + } + } + return columnTypes; + } + + private void checkColumnType(final ColumnType columnType, final ColumnType[] columnTypes, int index) { + if (columnTypes[index] != null) { + if (!columnTypes[index].equals(ColumnType.STRING) && !columnTypes[index].equals(columnType)) { + columnTypes[index] = ColumnType.STRING; + } + } else { + columnTypes[index] = columnType; + } + } + /** * Builds columns based on row/cell values. * * @param table * @param wb * @param row */ - private void createColumns(MutableTable table, Workbook wb, Row row) { + private void createColumns(MutableTable table, Workbook wb, Row row, ColumnType[] columTypes) { Review comment: Can you make the parameters final? ---------------------------------------------------------------- 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