Skip to content

Commit ebafcba

Browse files
fix(TAP-12724): Exclude cell type does not exist or unsupported type values cannot be written to death and processed as empty strings
1 parent 411b2f0 commit ebafcba

3 files changed

Lines changed: 34 additions & 13 deletions

File tree

connectors/excel-connector/src/main/java/io/tapdata/connector/excel/ExcelConnector.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
2525
import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
2626

2727
import java.io.IOException;
28-
import java.math.BigDecimal;
2928
import java.time.LocalDate;
30-
import java.time.LocalDateTime;
3129
import java.time.LocalTime;
32-
import java.time.format.DateTimeFormatter;
3330
import java.util.*;
3431
import java.util.concurrent.ConcurrentMap;
3532
import java.util.concurrent.atomic.AtomicReference;
@@ -100,13 +97,15 @@ protected void readOneFile(FileOffset fileOffset, TapTable tapTable, int eventBa
10097
Cell cell = row.getCell(k);
10198
checkCellType(k, cell, cellTypeMap);
10299
String fieldName = (String) headers[k - excelConfig.getFirstColumn() + 1];
100+
String fieldType = CellValueConvert.getFieldDataType(tapTable, fieldName);
103101
Object val;
104102
if (excelConfig.getJustString()) {
105103
Object cellValue = ExcelUtil.getCellValue(cell, formulaEvaluator);
106104
Object displayValue = ExcelUtil.getCellDisplayValue(cell, formulaEvaluator, dataFormatter);
107-
val = CellValueConvert.parseValue(cellValue, displayValue, CellValueConvert.getFieldDataType(tapTable, fieldName));
105+
val = CellValueConvert.parseValue(cellValue, displayValue, fieldType);
108106
} else {
109-
val = ExcelUtil.getCellValue(cell, formulaEvaluator);
107+
Object cellValue = ExcelUtil.getCellValue(cell, formulaEvaluator);
108+
val = CellValueConvert.parseOriginValue(cellValue, fieldType);
110109
}
111110
after.put(fieldName, val);
112111
}
@@ -115,13 +114,15 @@ protected void readOneFile(FileOffset fileOffset, TapTable tapTable, int eventBa
115114
Cell cell = row.getCell(k);
116115
checkCellType(k, cell, cellTypeMap);
117116
String fieldName = (String) headers[k - excelConfig.getFirstColumn() + 1];
117+
String fieldType = CellValueConvert.getFieldDataType(tapTable, fieldName);
118118
Object val;
119119
if (excelConfig.getJustString()) {
120120
Object cellValue = ExcelUtil.getMergedCellValue(mergedList, mergedDataMap, cell, formulaEvaluator);
121121
Object displayValue = ExcelUtil.getMergedCellDisplayValue(mergedList, mergedDataMap, cell, formulaEvaluator, dataFormatter);
122122
val = CellValueConvert.parseValue(cellValue, displayValue, CellValueConvert.getFieldDataType(tapTable, fieldName));
123123
} else {
124-
val = ExcelUtil.getMergedCellValue(mergedList, mergedDataMap, cell, formulaEvaluator);
124+
Object cellValue = ExcelUtil.getMergedCellValue(mergedList, mergedDataMap, cell, formulaEvaluator);
125+
val = CellValueConvert.parseOriginValue(cellValue, fieldType);
125126
}
126127
after.put(fieldName, val);
127128
}

connectors/excel-connector/src/main/java/io/tapdata/connector/excel/util/CellValueConvert.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.tapdata.entity.schema.TapField;
44
import io.tapdata.entity.schema.TapTable;
55
import io.tapdata.kit.EmptyKit;
6+
import org.apache.commons.lang3.StringUtils;
67

78
import java.math.BigDecimal;
89
import java.time.LocalDate;
@@ -40,6 +41,24 @@ public static String toExcelDataType(Object val) {
4041
return val.getClass().getSimpleName().toUpperCase();
4142
}
4243

44+
static Object stringValue(Object val) {
45+
String temporalValue = formatTemporalValue(val);
46+
if (temporalValue != null) {
47+
return temporalValue;
48+
}
49+
return parseValue(val);
50+
}
51+
52+
public static Object parseOriginValue(Object val, String fieldType) {
53+
if (isStringField(fieldType)) {
54+
return stringValue(val);
55+
}
56+
if (val instanceof String && StringUtils.isBlank((String) val)) {
57+
return null;
58+
}
59+
return val;
60+
}
61+
4362
public static Object parseValue(Object val) {
4463
if (val instanceof Double || val instanceof Float || val instanceof Long) {
4564
val = BigDecimal.valueOf(((Number) val).doubleValue()).stripTrailingZeros().toPlainString();
@@ -55,11 +74,10 @@ public static Object parseValue(Object val, String fieldDataType) {
5574

5675
public static Object parseValue(Object val, Object displayValue, String fieldDataType) {
5776
if (isStringField(fieldDataType)) {
58-
String temporalValue = formatTemporalValue(val);
59-
if (temporalValue != null) {
60-
return temporalValue;
61-
}
62-
return parseValue(displayValue);
77+
return stringValue(val);
78+
}
79+
if (displayValue instanceof String && StringUtils.isBlank((String) displayValue)) {
80+
return null;
6381
}
6482
return parseValue(val);
6583
}

connectors/excel-connector/src/main/java/io/tapdata/connector/excel/util/ExcelUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,20 @@ public static Object getCellValue(Cell cell, FormulaEvaluator formulaEvaluator)
198198
case FORMULA:
199199
return cell.getCellFormula();
200200
case BLANK:
201+
return "";
201202
case ERROR:
202203
case _NONE:
203204
default:
204-
return "";
205+
return null;
205206
}
206207
}
207208
return cell.getCellFormula();
208209
case BLANK:
210+
return "";
209211
case ERROR:
210212
case _NONE:
211213
default:
212-
return "";
214+
return null;
213215
}
214216
}
215217

0 commit comments

Comments
 (0)