SociopathicPixel commented on a change in pull request #230: dev/add datatypes to excel columns URL: https://github.com/apache/metamodel/pull/230#discussion_r328571114
########## File path: excel/src/main/java/org/apache/metamodel/excel/DefaultSpreadsheetReaderDelegate.java ########## @@ -167,50 +166,114 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { break; } } - if (hasColumns) { - createColumns(table, wb, row); + createColumns(table, wb, row, columnTypes); } } - return table; } + private void setColumnType(Iterator<Row> data, int rowLength, ColumnType[] columnTypes) { + while (data.hasNext()) { + Row row = data.next(); + for (int index = 0; index < rowLength; index++) { + if (row.getLastCellNum() == 0) { + continue; + } + if (row.getCell(index) == null) { + columnTypes = checkColumnType(ColumnType.STRING, columnTypes, index); + } else { + CellType cellType = row.getCell(index).getCellType(); + if (cellType.getCode() != 0 && cellType.getCode() <= 2) { + columnTypes = checkColumnType(ColumnType.STRING, columnTypes, index); + } else if (cellType.getCode() == 0) { + columnTypes = checkColumnType((row.getCell(index).getNumericCellValue() % 1 == 0) + ? ColumnType.INTEGER : ColumnType.DOUBLE, columnTypes, index); + } else if (cellType.getCode() == 4) { + columnTypes = checkColumnType(ColumnType.BOOLEAN, columnTypes, index); + } + } + } + } + } + + private ColumnType[] checkColumnType(ColumnType columnType, 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; + } + return columnTypes; + } + + private void determineColumnDatatype(Object[] datatypes, Row row) { + for (int index = 0; index < row.getLastCellNum(); index++) { + CellType type = ((Cell) row.getCell(index)).getCellType(); + + if (datatypes[index] instanceof Object) { + datatypes[index] = type; + } else if (datatypes[index] instanceof CellType) { + if (datatypes[index].equals(type)) { + continue; + } else { + datatypes[index] = CellType.STRING; + } + } + } + } + /** * 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) { if (row == null) { logger.warn("Cannot create columns based on null row!"); return; } final short rowLength = row.getLastCellNum(); - final int offset = getColumnOffset(row); - // build columns based on cell values. - try (final ColumnNamingSession columnNamingSession = _configuration.getColumnNamingStrategy() + try (final ColumnNamingSession columnNamingSession = _configuration + .getColumnNamingStrategy() .startColumnNamingSession()) { for (int j = offset; j < rowLength; j++) { final Cell cell = row.getCell(j); final String intrinsicColumnName = ExcelUtils.getCellValue(wb, cell); final ColumnNamingContext columnNamingContext = new ColumnNamingContextImpl(table, intrinsicColumnName, j); final String columnName = columnNamingSession.getNextColumnName(columnNamingContext); - final Column column = new MutableColumn(columnName, ColumnType.VARCHAR, table, j, true); + Column column = null; + if (columTypes == null) { + column = new MutableColumn(columnName, ColumnType.VARCHAR, table, j, true); + } else { + column = new MutableColumn(columnName, columTypes[j], table, j, true); + } table.addColumn(column); } } } /** - * Gets the column offset (first column to include). This is dependent on - * the row used for column processing and whether the skip empty columns - * property is set. + * Builds columns based on row/cell values. + * + * @param table + * @param wb + * @param row + */ + private void createColumns(MutableTable table, Workbook wb, Row row) { Review comment: done with next commit ---------------------------------------------------------------- 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