Skip to content

Commit 905f83c

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 5e0eab2 commit 905f83c

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;
@@ -99,13 +96,15 @@ protected void readOneFile(FileOffset fileOffset, TapTable tapTable, int eventBa
9996
Cell cell = row.getCell(k);
10097
checkCellType(k, cell, cellTypeMap);
10198
String fieldName = (String) headers[k - excelConfig.getFirstColumn() + 1];
99+
String fieldType = CellValueConvert.getFieldDataType(tapTable, fieldName);
102100
Object val;
103101
if (excelConfig.getJustString()) {
104102
Object cellValue = ExcelUtil.getCellValue(cell, formulaEvaluator);
105103
Object displayValue = ExcelUtil.getCellDisplayValue(cell, formulaEvaluator, dataFormatter);
106-
val = CellValueConvert.parseValue(cellValue, displayValue, CellValueConvert.getFieldDataType(tapTable, fieldName));
104+
val = CellValueConvert.parseValue(cellValue, displayValue, fieldType);
107105
} else {
108-
val = ExcelUtil.getCellValue(cell, formulaEvaluator);
106+
Object cellValue = ExcelUtil.getCellValue(cell, formulaEvaluator);
107+
val = CellValueConvert.parseOriginValue(cellValue, fieldType);
109108
}
110109
after.put(fieldName, val);
111110
}
@@ -114,13 +113,15 @@ protected void readOneFile(FileOffset fileOffset, TapTable tapTable, int eventBa
114113
Cell cell = row.getCell(k);
115114
checkCellType(k, cell, cellTypeMap);
116115
String fieldName = (String) headers[k - excelConfig.getFirstColumn() + 1];
116+
String fieldType = CellValueConvert.getFieldDataType(tapTable, fieldName);
117117
Object val;
118118
if (excelConfig.getJustString()) {
119119
Object cellValue = ExcelUtil.getMergedCellValue(mergedList, mergedDataMap, cell, formulaEvaluator);
120120
Object displayValue = ExcelUtil.getMergedCellDisplayValue(mergedList, mergedDataMap, cell, formulaEvaluator, dataFormatter);
121121
val = CellValueConvert.parseValue(cellValue, displayValue, CellValueConvert.getFieldDataType(tapTable, fieldName));
122122
} else {
123-
val = ExcelUtil.getMergedCellValue(mergedList, mergedDataMap, cell, formulaEvaluator);
123+
Object cellValue = ExcelUtil.getMergedCellValue(mergedList, mergedDataMap, cell, formulaEvaluator);
124+
val = CellValueConvert.parseOriginValue(cellValue, fieldType);
124125
}
125126
after.put(fieldName, val);
126127
}

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)