embulk-parser-poi_excel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +120 -0
- data/build.gradle +77 -0
- data/classpath/commons-codec-1.9.jar +0 -0
- data/classpath/embulk-parser-poi_excel-0.1.0.jar +0 -0
- data/classpath/embulk-standards-0.7.5.jar +0 -0
- data/classpath/poi-3.13.jar +0 -0
- data/classpath/poi-ooxml-3.13.jar +0 -0
- data/classpath/poi-ooxml-schemas-3.13.jar +0 -0
- data/classpath/stax-api-1.0.1.jar +0 -0
- data/classpath/xmlbeans-2.6.0.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +164 -0
- data/gradlew.bat +90 -0
- data/lib/embulk/guess/poi_excel.rb +61 -0
- data/lib/embulk/parser/poi_excel.rb +3 -0
- data/src/main/java/org/embulk/parser/poi_excel/PoiExcelColumnValueType.java +39 -0
- data/src/main/java/org/embulk/parser/poi_excel/PoiExcelParserPlugin.java +199 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/AbstractPoiExcelCellAttributeVisitor.java +133 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellCommentVisitor.java +68 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellFontVisitor.java +117 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellStyleVisitor.java +205 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellVisitor.java +194 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColorVisitor.java +81 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColumnIndex.java +174 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColumnVisitor.java +146 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorFactory.java +171 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorValue.java +63 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/BooleanCellVisitor.java +54 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/CellVisitor.java +41 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/DoubleCellVisitor.java +54 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/LongCellVisitor.java +54 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/StringCellVisitor.java +63 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/TimestampCellVisitor.java +73 -0
- data/src/test/java/org/embulk/parser/EmbulkPluginTester.java +176 -0
- data/src/test/java/org/embulk/parser/EmbulkTestFileInputPlugin.java +83 -0
- data/src/test/java/org/embulk/parser/EmbulkTestOutputPlugin.java +193 -0
- data/src/test/java/org/embulk/parser/EmbulkTestParserConfig.java +51 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin.java +187 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellComment.java +42 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellFont.java +125 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellStyle.java +132 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_columnNumber.java +188 -0
- data/src/test/resources/org/embulk/parser/poi_excel/test1.xls +0 -0
- metadata +118 -0
@@ -0,0 +1,205 @@
|
|
1
|
+
package org.embulk.parser.poi_excel.visitor;
|
2
|
+
|
3
|
+
import java.util.Collections;
|
4
|
+
import java.util.HashMap;
|
5
|
+
import java.util.Map;
|
6
|
+
|
7
|
+
import org.apache.poi.ss.usermodel.Cell;
|
8
|
+
import org.apache.poi.ss.usermodel.CellStyle;
|
9
|
+
import org.apache.poi.ss.usermodel.Workbook;
|
10
|
+
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
11
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
|
12
|
+
import org.embulk.spi.Column;
|
13
|
+
import org.embulk.spi.type.StringType;
|
14
|
+
|
15
|
+
public class PoiExcelCellStyleVisitor extends AbstractPoiExcelCellAttributeVisitor<CellStyle> {
|
16
|
+
|
17
|
+
public PoiExcelCellStyleVisitor(PoiExcelVisitorValue visitorValue) {
|
18
|
+
super(visitorValue);
|
19
|
+
}
|
20
|
+
|
21
|
+
@Override
|
22
|
+
protected CellStyle getAttributeSource(Column column, ColumnOptionTask option, Cell cell) {
|
23
|
+
return cell.getCellStyle();
|
24
|
+
}
|
25
|
+
|
26
|
+
protected boolean acceptKey(String key) {
|
27
|
+
if (key.equals("border")) {
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
return true;
|
31
|
+
}
|
32
|
+
|
33
|
+
@Override
|
34
|
+
protected Map<String, AttributeSupplier<CellStyle>> getAttributeSupplierMap() {
|
35
|
+
return SUPPLIER_MAP;
|
36
|
+
}
|
37
|
+
|
38
|
+
protected static final Map<String, AttributeSupplier<CellStyle>> SUPPLIER_MAP;
|
39
|
+
|
40
|
+
static {
|
41
|
+
Map<String, AttributeSupplier<CellStyle>> map = new HashMap<>(32);
|
42
|
+
map.put("alignment", new AttributeSupplier<CellStyle>() {
|
43
|
+
@Override
|
44
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
45
|
+
return (long) style.getAlignment();
|
46
|
+
}
|
47
|
+
});
|
48
|
+
map.put("border", new AttributeSupplier<CellStyle>() {
|
49
|
+
@Override
|
50
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
51
|
+
int n0 = style.getBorderTop();
|
52
|
+
int n1 = style.getBorderBottom();
|
53
|
+
int n2 = style.getBorderLeft();
|
54
|
+
int n3 = style.getBorderRight();
|
55
|
+
if (column.getType() instanceof StringType) {
|
56
|
+
return String.format("%02x%02x%02x%02x", n0, n1, n2, n3);
|
57
|
+
}
|
58
|
+
return (long) ((n0 << 24) | (n1 << 16) | (n2 << 8) | n3);
|
59
|
+
}
|
60
|
+
});
|
61
|
+
map.put("border_bottom", new AttributeSupplier<CellStyle>() {
|
62
|
+
@Override
|
63
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
64
|
+
return (long) style.getBorderBottom();
|
65
|
+
}
|
66
|
+
});
|
67
|
+
map.put("border_left", new AttributeSupplier<CellStyle>() {
|
68
|
+
@Override
|
69
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
70
|
+
return (long) style.getBorderLeft();
|
71
|
+
}
|
72
|
+
});
|
73
|
+
map.put("border_right", new AttributeSupplier<CellStyle>() {
|
74
|
+
@Override
|
75
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
76
|
+
return (long) style.getBorderRight();
|
77
|
+
}
|
78
|
+
});
|
79
|
+
map.put("border_top", new AttributeSupplier<CellStyle>() {
|
80
|
+
@Override
|
81
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
82
|
+
return (long) style.getBorderTop();
|
83
|
+
}
|
84
|
+
});
|
85
|
+
map.put("border_bottom_color", new AttributeSupplier<CellStyle>() {
|
86
|
+
@Override
|
87
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
88
|
+
if (style instanceof XSSFCellStyle) {
|
89
|
+
return ((XSSFCellStyle) style).getBottomBorderXSSFColor();
|
90
|
+
} else {
|
91
|
+
Workbook book = cell.getSheet().getWorkbook();
|
92
|
+
short color = style.getBottomBorderColor();
|
93
|
+
return PoiExcelColorVisitor.getHssfColor(book, color);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
});
|
97
|
+
map.put("border_left_color", new AttributeSupplier<CellStyle>() {
|
98
|
+
@Override
|
99
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
100
|
+
if (style instanceof XSSFCellStyle) {
|
101
|
+
return ((XSSFCellStyle) style).getLeftBorderXSSFColor();
|
102
|
+
} else {
|
103
|
+
Workbook book = cell.getSheet().getWorkbook();
|
104
|
+
short color = style.getLeftBorderColor();
|
105
|
+
return PoiExcelColorVisitor.getHssfColor(book, color);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
});
|
109
|
+
map.put("border_right_color", new AttributeSupplier<CellStyle>() {
|
110
|
+
@Override
|
111
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
112
|
+
if (style instanceof XSSFCellStyle) {
|
113
|
+
return ((XSSFCellStyle) style).getRightBorderXSSFColor();
|
114
|
+
} else {
|
115
|
+
Workbook book = cell.getSheet().getWorkbook();
|
116
|
+
short color = style.getRightBorderColor();
|
117
|
+
return PoiExcelColorVisitor.getHssfColor(book, color);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
});
|
121
|
+
map.put("border_top_color", new AttributeSupplier<CellStyle>() {
|
122
|
+
@Override
|
123
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
124
|
+
if (style instanceof XSSFCellStyle) {
|
125
|
+
return ((XSSFCellStyle) style).getTopBorderXSSFColor();
|
126
|
+
} else {
|
127
|
+
Workbook book = cell.getSheet().getWorkbook();
|
128
|
+
short color = style.getTopBorderColor();
|
129
|
+
return PoiExcelColorVisitor.getHssfColor(book, color);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
});
|
133
|
+
map.put("data_format", new AttributeSupplier<CellStyle>() {
|
134
|
+
@Override
|
135
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
136
|
+
if (column.getType() instanceof StringType) {
|
137
|
+
return style.getDataFormatString();
|
138
|
+
} else {
|
139
|
+
return (long) style.getDataFormat();
|
140
|
+
}
|
141
|
+
}
|
142
|
+
});
|
143
|
+
map.put("fill_background_color", new AttributeSupplier<CellStyle>() {
|
144
|
+
@Override
|
145
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
146
|
+
return style.getFillBackgroundColorColor();
|
147
|
+
}
|
148
|
+
});
|
149
|
+
map.put("fill_foreground_color", new AttributeSupplier<CellStyle>() {
|
150
|
+
@Override
|
151
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
152
|
+
return style.getFillForegroundColorColor();
|
153
|
+
}
|
154
|
+
});
|
155
|
+
map.put("fill_pattern", new AttributeSupplier<CellStyle>() {
|
156
|
+
@Override
|
157
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
158
|
+
return (long) style.getFillPattern();
|
159
|
+
}
|
160
|
+
});
|
161
|
+
map.put("font_index", new AttributeSupplier<CellStyle>() {
|
162
|
+
@Override
|
163
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
164
|
+
return (long) style.getFontIndex();
|
165
|
+
}
|
166
|
+
});
|
167
|
+
map.put("hidden", new AttributeSupplier<CellStyle>() {
|
168
|
+
@Override
|
169
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
170
|
+
return style.getHidden();
|
171
|
+
}
|
172
|
+
});
|
173
|
+
map.put("indention", new AttributeSupplier<CellStyle>() {
|
174
|
+
@Override
|
175
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
176
|
+
return (long) style.getIndention();
|
177
|
+
}
|
178
|
+
});
|
179
|
+
map.put("locked", new AttributeSupplier<CellStyle>() {
|
180
|
+
@Override
|
181
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
182
|
+
return style.getLocked();
|
183
|
+
}
|
184
|
+
});
|
185
|
+
map.put("rotation", new AttributeSupplier<CellStyle>() {
|
186
|
+
@Override
|
187
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
188
|
+
return (long) style.getRotation();
|
189
|
+
}
|
190
|
+
});
|
191
|
+
map.put("vertical_alignment", new AttributeSupplier<CellStyle>() {
|
192
|
+
@Override
|
193
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
194
|
+
return (long) style.getVerticalAlignment();
|
195
|
+
}
|
196
|
+
});
|
197
|
+
map.put("wrap_text", new AttributeSupplier<CellStyle>() {
|
198
|
+
@Override
|
199
|
+
public Object get(Column column, Cell cell, CellStyle style) {
|
200
|
+
return style.getWrapText();
|
201
|
+
}
|
202
|
+
});
|
203
|
+
SUPPLIER_MAP = Collections.unmodifiableMap(map);
|
204
|
+
}
|
205
|
+
}
|
@@ -0,0 +1,194 @@
|
|
1
|
+
package org.embulk.parser.poi_excel.visitor;
|
2
|
+
|
3
|
+
import java.text.MessageFormat;
|
4
|
+
import java.util.List;
|
5
|
+
|
6
|
+
import org.apache.poi.ss.usermodel.Cell;
|
7
|
+
import org.apache.poi.ss.usermodel.CellValue;
|
8
|
+
import org.apache.poi.ss.usermodel.CreationHelper;
|
9
|
+
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
10
|
+
import org.apache.poi.ss.usermodel.Row;
|
11
|
+
import org.apache.poi.ss.usermodel.Sheet;
|
12
|
+
import org.apache.poi.ss.usermodel.Workbook;
|
13
|
+
import org.apache.poi.ss.util.CellRangeAddress;
|
14
|
+
import org.embulk.parser.poi_excel.PoiExcelColumnValueType;
|
15
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
|
16
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.FormulaReplaceTask;
|
17
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.PluginTask;
|
18
|
+
import org.embulk.parser.poi_excel.visitor.embulk.CellVisitor;
|
19
|
+
import org.embulk.spi.Column;
|
20
|
+
import org.embulk.spi.Exec;
|
21
|
+
import org.embulk.spi.PageBuilder;
|
22
|
+
import org.slf4j.Logger;
|
23
|
+
|
24
|
+
import com.google.common.base.Optional;
|
25
|
+
|
26
|
+
public class PoiExcelCellVisitor {
|
27
|
+
private final Logger log = Exec.getLogger(getClass());
|
28
|
+
|
29
|
+
protected final PoiExcelVisitorValue visitorValue;
|
30
|
+
protected final PageBuilder pageBuilder;
|
31
|
+
|
32
|
+
public PoiExcelCellVisitor(PoiExcelVisitorValue visitorValue) {
|
33
|
+
this.visitorValue = visitorValue;
|
34
|
+
this.pageBuilder = visitorValue.getPageBuilder();
|
35
|
+
}
|
36
|
+
|
37
|
+
public void visitCellValue(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
|
38
|
+
assert cell != null;
|
39
|
+
|
40
|
+
int cellType = cell.getCellType();
|
41
|
+
switch (cellType) {
|
42
|
+
case Cell.CELL_TYPE_NUMERIC:
|
43
|
+
visitor.visitCellValueNumeric(column, cell, cell.getNumericCellValue());
|
44
|
+
return;
|
45
|
+
case Cell.CELL_TYPE_STRING:
|
46
|
+
visitor.visitCellValueString(column, cell, cell.getStringCellValue());
|
47
|
+
return;
|
48
|
+
case Cell.CELL_TYPE_FORMULA:
|
49
|
+
PoiExcelColumnValueType valueType = option.getValueTypeEnum();
|
50
|
+
if (valueType == PoiExcelColumnValueType.CELL_FORMULA) {
|
51
|
+
visitor.visitCellFormula(column, cell);
|
52
|
+
} else {
|
53
|
+
visitCellValueFormula(column, option, cell, visitor);
|
54
|
+
}
|
55
|
+
return;
|
56
|
+
case Cell.CELL_TYPE_BLANK:
|
57
|
+
visitCellValueBlank(column, option, cell, visitor);
|
58
|
+
return;
|
59
|
+
case Cell.CELL_TYPE_BOOLEAN:
|
60
|
+
visitor.visitCellValueBoolean(column, cell, cell.getBooleanCellValue());
|
61
|
+
return;
|
62
|
+
case Cell.CELL_TYPE_ERROR:
|
63
|
+
visitCellValueError(column, option, cell, cell.getErrorCellValue(), visitor);
|
64
|
+
return;
|
65
|
+
default:
|
66
|
+
throw new IllegalStateException(MessageFormat.format("unsupported POI cellType={0}", cellType));
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
protected void visitCellValueBlank(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
|
71
|
+
assert cell.getCellType() == Cell.CELL_TYPE_BLANK;
|
72
|
+
|
73
|
+
PluginTask task = visitorValue.getPluginTask();
|
74
|
+
boolean search = option.getSearchMergedCell().or(task.getSearchMergedCell());
|
75
|
+
if (!search) {
|
76
|
+
visitor.visitCellValueBlank(column, cell);
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
|
80
|
+
int r = cell.getRowIndex();
|
81
|
+
int c = cell.getColumnIndex();
|
82
|
+
|
83
|
+
Sheet sheet = cell.getSheet();
|
84
|
+
int size = sheet.getNumMergedRegions();
|
85
|
+
for (int i = 0; i < size; i++) {
|
86
|
+
CellRangeAddress range = visitorValue.getSheet().getMergedRegion(i);
|
87
|
+
if (range.isInRange(r, c)) {
|
88
|
+
Row firstRow = sheet.getRow(range.getFirstRow());
|
89
|
+
if (firstRow == null) {
|
90
|
+
visitCellNull(column);
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
Cell firstCell = firstRow.getCell(range.getFirstColumn());
|
94
|
+
if (firstCell == null) {
|
95
|
+
visitCellNull(column);
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
|
99
|
+
visitCellValue(column, option, firstCell, visitor);
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
visitor.visitCellValueBlank(column, cell);
|
105
|
+
}
|
106
|
+
|
107
|
+
protected void visitCellValueFormula(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
|
108
|
+
assert cell.getCellType() == Cell.CELL_TYPE_FORMULA;
|
109
|
+
|
110
|
+
Optional<List<FormulaReplaceTask>> replaceOption = option.getFormulaReplace();
|
111
|
+
if (!replaceOption.isPresent()) {
|
112
|
+
PluginTask task = visitorValue.getPluginTask();
|
113
|
+
replaceOption = task.getFormulaReplace();
|
114
|
+
}
|
115
|
+
if (replaceOption.isPresent()) {
|
116
|
+
String formula = cell.getCellFormula();
|
117
|
+
String old = formula;
|
118
|
+
|
119
|
+
List<FormulaReplaceTask> list = replaceOption.get();
|
120
|
+
for (FormulaReplaceTask replace : list) {
|
121
|
+
String regex = replace.getRegex();
|
122
|
+
String replacement = replace.getTo();
|
123
|
+
|
124
|
+
replacement = replacement.replace("${row}", Integer.toString(cell.getRowIndex() + 1));
|
125
|
+
|
126
|
+
formula = formula.replaceAll(regex, replacement);
|
127
|
+
}
|
128
|
+
|
129
|
+
if (!formula.equals(old)) {
|
130
|
+
log.debug("formula replaced. old=\"{}\", new=\"{}\"", old, formula);
|
131
|
+
try {
|
132
|
+
cell.setCellFormula(formula);
|
133
|
+
} catch (Exception e) {
|
134
|
+
throw new RuntimeException(MessageFormat.format("setCellFormula error. formula={0}", formula), e);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
CellValue cellValue;
|
140
|
+
try {
|
141
|
+
Workbook book = cell.getSheet().getWorkbook();
|
142
|
+
CreationHelper helper = book.getCreationHelper();
|
143
|
+
FormulaEvaluator evaluator = helper.createFormulaEvaluator();
|
144
|
+
cellValue = evaluator.evaluate(cell);
|
145
|
+
} catch (Exception e) {
|
146
|
+
PluginTask task = visitorValue.getPluginTask();
|
147
|
+
boolean setNull = option.getFormulaErrorNull().or(task.getFormulaErrorNull());
|
148
|
+
if (setNull) {
|
149
|
+
pageBuilder.setNull(column);
|
150
|
+
return;
|
151
|
+
}
|
152
|
+
|
153
|
+
throw new RuntimeException(MessageFormat.format("evaluate error. formula={0}", cell.getCellFormula()), e);
|
154
|
+
}
|
155
|
+
|
156
|
+
int cellType = cellValue.getCellType();
|
157
|
+
switch (cellType) {
|
158
|
+
case Cell.CELL_TYPE_NUMERIC:
|
159
|
+
visitor.visitCellValueNumeric(column, cellValue, cellValue.getNumberValue());
|
160
|
+
return;
|
161
|
+
case Cell.CELL_TYPE_STRING:
|
162
|
+
visitor.visitCellValueString(column, cellValue, cellValue.getStringValue());
|
163
|
+
return;
|
164
|
+
case Cell.CELL_TYPE_BLANK:
|
165
|
+
visitor.visitCellValueBlank(column, cellValue);
|
166
|
+
return;
|
167
|
+
case Cell.CELL_TYPE_BOOLEAN:
|
168
|
+
visitor.visitCellValueBoolean(column, cellValue, cellValue.getBooleanValue());
|
169
|
+
return;
|
170
|
+
case Cell.CELL_TYPE_ERROR:
|
171
|
+
visitCellValueError(column, option, cellValue, cellValue.getErrorValue(), visitor);
|
172
|
+
return;
|
173
|
+
case Cell.CELL_TYPE_FORMULA:
|
174
|
+
default:
|
175
|
+
throw new IllegalStateException(MessageFormat.format("unsupported POI cellType={0}", cellType));
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
protected void visitCellValueError(Column column, ColumnOptionTask option, Object cell, int errorCode,
|
180
|
+
CellVisitor visitor) {
|
181
|
+
PluginTask task = visitorValue.getPluginTask();
|
182
|
+
boolean setNull = option.getCellErrorNull().or(task.getCellErrorNull());
|
183
|
+
if (setNull) {
|
184
|
+
pageBuilder.setNull(column);
|
185
|
+
return;
|
186
|
+
}
|
187
|
+
|
188
|
+
visitor.visitCellValueError(column, cell, errorCode);
|
189
|
+
}
|
190
|
+
|
191
|
+
protected void visitCellNull(Column column) {
|
192
|
+
pageBuilder.setNull(column);
|
193
|
+
}
|
194
|
+
}
|
@@ -0,0 +1,81 @@
|
|
1
|
+
package org.embulk.parser.poi_excel.visitor;
|
2
|
+
|
3
|
+
import java.text.MessageFormat;
|
4
|
+
|
5
|
+
import org.apache.poi.hssf.usermodel.HSSFPalette;
|
6
|
+
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
7
|
+
import org.apache.poi.hssf.util.HSSFColor;
|
8
|
+
import org.apache.poi.ss.usermodel.Color;
|
9
|
+
import org.apache.poi.ss.usermodel.Workbook;
|
10
|
+
import org.apache.poi.xssf.usermodel.XSSFColor;
|
11
|
+
import org.embulk.parser.poi_excel.visitor.embulk.CellVisitor;
|
12
|
+
import org.embulk.spi.Column;
|
13
|
+
import org.embulk.spi.PageBuilder;
|
14
|
+
import org.embulk.spi.type.StringType;
|
15
|
+
|
16
|
+
public class PoiExcelColorVisitor {
|
17
|
+
|
18
|
+
protected final PoiExcelVisitorValue visitorValue;
|
19
|
+
|
20
|
+
public PoiExcelColorVisitor(PoiExcelVisitorValue visitorValue) {
|
21
|
+
this.visitorValue = visitorValue;
|
22
|
+
}
|
23
|
+
|
24
|
+
public void visitCellColor(Column column, short colorIndex, CellVisitor visitor) {
|
25
|
+
Color color = getHssfColor(colorIndex);
|
26
|
+
visitCellColor(column, color, visitor);
|
27
|
+
}
|
28
|
+
|
29
|
+
public void visitCellColor(Column column, Color color, CellVisitor visitor) {
|
30
|
+
int rgb = getRGB(color);
|
31
|
+
if (rgb < 0) {
|
32
|
+
PageBuilder pageBuilder = visitorValue.getPageBuilder();
|
33
|
+
pageBuilder.setNull(column);
|
34
|
+
return;
|
35
|
+
}
|
36
|
+
|
37
|
+
if (column.getType() instanceof StringType) {
|
38
|
+
String s = String.format("%06x", rgb);
|
39
|
+
visitor.visitCellValueString(column, color, s);
|
40
|
+
} else {
|
41
|
+
visitor.visitValueLong(column, color, rgb);
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
public Color getHssfColor(short colorIndex) {
|
46
|
+
HSSFWorkbook book = (HSSFWorkbook) visitorValue.getSheet().getWorkbook();
|
47
|
+
return getHssfColor(book, colorIndex);
|
48
|
+
}
|
49
|
+
|
50
|
+
public static Color getHssfColor(Workbook workbook, short colorIndex) {
|
51
|
+
HSSFWorkbook book = (HSSFWorkbook) workbook;
|
52
|
+
HSSFPalette palette = book.getCustomPalette();
|
53
|
+
HSSFColor color = palette.getColor(colorIndex);
|
54
|
+
return color;
|
55
|
+
}
|
56
|
+
|
57
|
+
public static int getRGB(Color color) {
|
58
|
+
if (color == null) {
|
59
|
+
return -1;
|
60
|
+
}
|
61
|
+
|
62
|
+
int[] rgb = new int[3];
|
63
|
+
if (color instanceof HSSFColor) {
|
64
|
+
HSSFColor hssf = (HSSFColor) color;
|
65
|
+
short[] s = hssf.getTriplet();
|
66
|
+
rgb[0] = s[0] & 0xff;
|
67
|
+
rgb[1] = s[1] & 0xff;
|
68
|
+
rgb[2] = s[2] & 0xff;
|
69
|
+
} else if (color instanceof XSSFColor) {
|
70
|
+
XSSFColor xssf = (XSSFColor) color;
|
71
|
+
byte[] b = xssf.getRGB();
|
72
|
+
rgb[0] = b[0] & 0xff;
|
73
|
+
rgb[1] = b[1] & 0xff;
|
74
|
+
rgb[2] = b[2] & 0xff;
|
75
|
+
} else {
|
76
|
+
throw new IllegalStateException(MessageFormat.format("unsupported POI color={0}", color));
|
77
|
+
}
|
78
|
+
|
79
|
+
return (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
|
80
|
+
}
|
81
|
+
}
|
@@ -0,0 +1,174 @@
|
|
1
|
+
package org.embulk.parser.poi_excel.visitor;
|
2
|
+
|
3
|
+
import java.text.MessageFormat;
|
4
|
+
import java.util.LinkedHashMap;
|
5
|
+
import java.util.List;
|
6
|
+
import java.util.Map;
|
7
|
+
|
8
|
+
import org.apache.poi.ss.util.CellReference;
|
9
|
+
import org.embulk.parser.poi_excel.PoiExcelColumnValueType;
|
10
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
|
11
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.PluginTask;
|
12
|
+
import org.embulk.spi.Column;
|
13
|
+
import org.embulk.spi.Exec;
|
14
|
+
import org.embulk.spi.Schema;
|
15
|
+
import org.slf4j.Logger;
|
16
|
+
|
17
|
+
import com.google.common.base.Optional;
|
18
|
+
|
19
|
+
public class PoiExcelColumnIndex {
|
20
|
+
private final Logger log = Exec.getLogger(getClass());
|
21
|
+
|
22
|
+
protected final Map<String, Integer> indexMap = new LinkedHashMap<>();
|
23
|
+
|
24
|
+
public void initializeColumnIndex(PluginTask task, List<ColumnOptionTask> columnOptions) {
|
25
|
+
int index = -1;
|
26
|
+
indexMap.clear();
|
27
|
+
|
28
|
+
Schema schema = task.getColumns().toSchema();
|
29
|
+
for (Column column : schema.getColumns()) {
|
30
|
+
ColumnOptionTask option = columnOptions.get(column.getIndex());
|
31
|
+
|
32
|
+
String type = option.getValueType().trim();
|
33
|
+
int n = type.indexOf('.');
|
34
|
+
if (n >= 0) {
|
35
|
+
String suffix = type.substring(n + 1).trim();
|
36
|
+
option.setValueTypeSuffix(suffix);
|
37
|
+
type = type.substring(0, n).trim();
|
38
|
+
}
|
39
|
+
|
40
|
+
PoiExcelColumnValueType valueType;
|
41
|
+
try {
|
42
|
+
valueType = PoiExcelColumnValueType.valueOf(type.toUpperCase());
|
43
|
+
} catch (Exception e) {
|
44
|
+
throw new RuntimeException(MessageFormat.format("illegal value_type={0}", type), e);
|
45
|
+
}
|
46
|
+
option.setValueTypeEnum(valueType);
|
47
|
+
|
48
|
+
if (valueType.useCell()) {
|
49
|
+
index = resolveColumnIndex(column, option, index, valueType);
|
50
|
+
if (index < 0) {
|
51
|
+
index = 0;
|
52
|
+
}
|
53
|
+
log.info("column.name={} <- cell column={}, value_type={}", column.getName(),
|
54
|
+
CellReference.convertNumToColString(index), valueType);
|
55
|
+
option.setColumnIndex(index);
|
56
|
+
indexMap.put(column.getName(), index);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
protected int resolveColumnIndex(Column column, ColumnOptionTask option, int index,
|
62
|
+
PoiExcelColumnValueType valueType) {
|
63
|
+
Optional<String> numberOption = option.getColumnNumber();
|
64
|
+
if (numberOption.isPresent()) {
|
65
|
+
String columnNumber = numberOption.get();
|
66
|
+
if (columnNumber.length() >= 1) {
|
67
|
+
char c = columnNumber.charAt(0);
|
68
|
+
String arg = columnNumber.substring(1).trim();
|
69
|
+
switch (c) {
|
70
|
+
case '=':
|
71
|
+
return resolveSameColumnIndex(column, index, columnNumber, arg);
|
72
|
+
case '+':
|
73
|
+
return resolveNextColumnIndex(column, index, columnNumber, arg);
|
74
|
+
case '-':
|
75
|
+
return resolvePreviousColumnIndex(column, index, columnNumber, arg);
|
76
|
+
default:
|
77
|
+
break;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
return convertColumnIndex(column, columnNumber);
|
81
|
+
} else {
|
82
|
+
if (valueType.nextIndex()) {
|
83
|
+
index++;
|
84
|
+
}
|
85
|
+
return index;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
protected int resolveSameColumnIndex(Column column, int index, String columnNumber, String arg) {
|
90
|
+
if (arg.isEmpty()) {
|
91
|
+
return index;
|
92
|
+
}
|
93
|
+
|
94
|
+
Integer value = indexMap.get(arg);
|
95
|
+
if (value == null) {
|
96
|
+
throw new RuntimeException(MessageFormat.format("not found column name={0} before {1}", arg, column));
|
97
|
+
}
|
98
|
+
return value;
|
99
|
+
}
|
100
|
+
|
101
|
+
protected int resolveNextColumnIndex(Column column, int index, String columnNumber, String arg) {
|
102
|
+
if (index < 0) {
|
103
|
+
index = 0;
|
104
|
+
}
|
105
|
+
int add = 1;
|
106
|
+
if (!arg.isEmpty()) {
|
107
|
+
try {
|
108
|
+
add = Integer.parseInt(arg);
|
109
|
+
} catch (Exception e) {
|
110
|
+
Integer value = indexMap.get(arg);
|
111
|
+
if (value == null) {
|
112
|
+
throw new RuntimeException(
|
113
|
+
MessageFormat.format("not found column name={0} before {1}", arg, column));
|
114
|
+
}
|
115
|
+
index = value;
|
116
|
+
add = 1;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
index += add;
|
121
|
+
checkIndex(column, index);
|
122
|
+
return index;
|
123
|
+
}
|
124
|
+
|
125
|
+
protected int resolvePreviousColumnIndex(Column column, int index, String columnNumber, String arg) {
|
126
|
+
if (index < 0) {
|
127
|
+
index = 0;
|
128
|
+
}
|
129
|
+
int sub = 1;
|
130
|
+
if (!arg.isEmpty()) {
|
131
|
+
try {
|
132
|
+
sub = Integer.parseInt(arg);
|
133
|
+
} catch (Exception e) {
|
134
|
+
Integer value = indexMap.get(arg);
|
135
|
+
if (value == null) {
|
136
|
+
throw new RuntimeException(
|
137
|
+
MessageFormat.format("not found column name={0} before {1}", arg, column));
|
138
|
+
}
|
139
|
+
index = value;
|
140
|
+
sub = 1;
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
144
|
+
index -= sub;
|
145
|
+
checkIndex(column, index);
|
146
|
+
return index;
|
147
|
+
}
|
148
|
+
|
149
|
+
protected void checkIndex(Column column, int index) {
|
150
|
+
if (index < 0) {
|
151
|
+
throw new RuntimeException(MessageFormat.format("column_number out of range at {0}", column));
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
protected int convertColumnIndex(Column column, String columnNumber) {
|
156
|
+
int index;
|
157
|
+
try {
|
158
|
+
char c = columnNumber.charAt(0);
|
159
|
+
if ('0' <= c && c <= '9') {
|
160
|
+
index = Integer.parseInt(columnNumber) - 1;
|
161
|
+
} else {
|
162
|
+
index = CellReference.convertColStringToIndex(columnNumber);
|
163
|
+
}
|
164
|
+
} catch (Exception e) {
|
165
|
+
throw new RuntimeException(MessageFormat.format("illegal column_number=\"{0}\" at {1}", columnNumber,
|
166
|
+
column), e);
|
167
|
+
}
|
168
|
+
if (index < 0) {
|
169
|
+
throw new RuntimeException(MessageFormat.format("illegal column_number=\"{0}\" at {1}", columnNumber,
|
170
|
+
column));
|
171
|
+
}
|
172
|
+
return index;
|
173
|
+
}
|
174
|
+
}
|