Skip to content

Commit

Permalink
refactor(core): extract searching column by name method
Browse files Browse the repository at this point in the history
For easy extension

Refs: 304
  • Loading branch information
aerfus committed Feb 18, 2024
1 parent a43533d commit d659fbb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.util.StringUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -265,14 +266,22 @@ private FieldAnnotationDetail getFieldColumn(final Field field) {
if (excelCellName != null) {
annotationDetail.setMandatoryCell(excelCellName.mandatoryCell());
annotationDetail.setColumnName(excelCellName.value());
final String titleName = formatting.transform(options, excelCellName.value());
Integer column = titleToIndex.get(titleName);
Integer column = findTitleColumn(excelCellName);
annotationDetail.setColumn(column);
}
}
return annotationDetail;
}

private Integer findTitleColumn(ExcelCellName excelCellName) {
if (!StringUtil.isBlank(excelCellName.value())) {
final String titleName = formatting.transform(options, excelCellName.value());
return titleToIndex.get(titleName);
}

return null;
}

private <T> void constructTypeValue(Row currentRow, T instance, Field field,
FieldAnnotationDetail annotationDetail) {
Cell cell = currentRow.getCell(annotationDetail.getColumn());
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.util.StringUtil;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

Expand Down Expand Up @@ -171,8 +172,7 @@ private boolean setValue(Field field, int column, String content, Object ins) {
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
if (excelCellName != null) {
excelCellNameAnnotations.add(excelCellName);
final String titleName = formatting.transform(options, excelCellName.value());
final Integer titleColumn = titleToIndex.get(titleName);
final Integer titleColumn = findTitleColumn(excelCellName);
// Fix both columns mapped to name passing this condition below
if (titleColumn != null && titleColumn == column) {
Object o = casting.castValue(field, content, internalRow, column, options);
Expand All @@ -184,6 +184,15 @@ private boolean setValue(Field field, int column, String content, Object ins) {
return false;
}

private Integer findTitleColumn(ExcelCellName excelCellName) {
if (!StringUtil.isBlank(excelCellName.value())) {
final String titleName = formatting.transform(options, excelCellName.value());
return titleToIndex.get(titleName);
}

return null;
}

@Override
public void startRow(int rowNum) {
if (rowNum + 1 > options.skip()) {
Expand Down

0 comments on commit d659fbb

Please sign in to comment.