Skip to content

Commit

Permalink
MARP-637: Cells with "wrong" numbers should not be reconized as numbe…
Browse files Browse the repository at this point in the history
…rs (#59)

* MARP-637: Fix column with both string and numeric types

* MARP-637: Handle feedback

* MARP-637: update unit test

* MARP-637: handle feedback

* MARP-637: handle feedback
  • Loading branch information
Phạm Duy Linh authored Aug 13, 2024
1 parent ac4970d commit 62669b4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ void parseColumns_xlsx(@TempDir Path dir) throws IOException {
assertThat(columns).contains(
new Column("Firstname", String.class, 255), new Column("ZIP", Integer.class),
new Column("Amount", Double.class), new Column("Birthdate", Timestamp.class), // should be a date
new Column("Note", String.class, 811)
new Column("Note", String.class, 811),
new Column("Column contains texts in incorrect number format", String.class, 255),
new Column("Column contains both text and numeric", String.class, 255)
);
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.axonivy.util.excel.importer;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
Expand Down Expand Up @@ -87,11 +89,40 @@ private static void updateColumn(Column column, Cell cell) {
&& !CellUtils.isInteger(cell)) {
column.setType(Double.class);
}
if (cell.getCellType() == CellType.STRING) {
column.setType(String.class);
column.setDatabaseFieldLength(DEFAULT_STRING_LENGTH);
}
if (column.getType().equals(String.class)) {
var cellLength = cell.getStringCellValue().length();
if (cellLength > column.getDatabaseFieldLength()) {
column.setDatabaseFieldLength(cellLength);
var cellValue = getCellValueAsString(cell);
if (cellValue.length() > column.getDatabaseFieldLength()) {
column.setDatabaseFieldLength(cellValue.length());
}
}
}

public static String getCellValueAsString(Cell cell) {
if (cell == null) {
return StringUtils.EMPTY;
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString();
} else {
DecimalFormat decimalFormat = new DecimalFormat("#");
decimalFormat.setMaximumFractionDigits(0);
decimalFormat.setGroupingUsed(false);
return decimalFormat.format(cell.getNumericCellValue());
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
return cell.getCellFormula();
default:
return StringUtils.EMPTY;
}
}
}

0 comments on commit 62669b4

Please sign in to comment.