This repository was archived by the owner on Jun 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
dev/add datatypes to excel columns #230
Open
SociopathicPixel
wants to merge
23
commits into
apache:master
Choose a base branch
from
SociopathicPixel:dev/add-datatypes-to-excel-columns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
90303fa
updated poi-ooxml
999647c
refactored [tabs] to [spaces]
5a75486
resolved review comments
b82e728
resolved review comments part 1
SociopathicPixel 8c21387
+ minor fix which sneaked in when reverting few changes.
SociopathicPixel 8919900
+ minor fix which sneaked in when reverting few changes.
SociopathicPixel aae088f
removed empty spaces in whitelines
SociopathicPixel 7ae1b26
added a test, however datatypes are not found...
SociopathicPixel 77a1b80
added test case for datatypes
84c6f06
fixed another test that fel over
b4d9a20
commit part 1; did some indentation fixes
1a34285
commit part 1.01; did some indentation fixes
3785c91
resolving review comments
66749e9
Merge branch 'dev/add-datatypes-to-excel-columns' of https://github.c…
SociopathicPixel a8ab4b6
resolving indentation filler
SociopathicPixel 57108bc
revert all code style changes
138b147
wrote some tests, added update check
5088744
fixed assert that was set wrong
SociopathicPixel 4bfcc41
resolving review comments 1 of many
0701bab
resolving review comments
9c3976e
resolving review comments, still there are a few thingies that could …
cc85725
still need to pull apache/master into this branch
SociopathicPixel 23a7781
resolving review comments, not finished yet
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |
| import org.apache.metamodel.util.FileHelper; | ||
| import org.apache.metamodel.util.Resource; | ||
| import org.apache.poi.ss.usermodel.Cell; | ||
| import org.apache.poi.ss.usermodel.CellType; | ||
| import org.apache.poi.ss.usermodel.Row; | ||
| import org.apache.poi.ss.usermodel.Sheet; | ||
| import org.apache.poi.ss.usermodel.Workbook; | ||
|
|
@@ -103,9 +104,8 @@ public void notifyTablesModified() { | |
| // do nothing | ||
| } | ||
|
|
||
| private MutableTable createTable(final Workbook wb, final Sheet sheet) { | ||
| private MutableTable createTable(Workbook wb, Sheet sheet) { | ||
| final MutableTable table = new MutableTable(sheet.getSheetName(), TableType.TABLE); | ||
|
|
||
SociopathicPixel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (sheet.getPhysicalNumberOfRows() <= 0) { | ||
| // no physical rows in sheet | ||
| return table; | ||
|
|
@@ -117,9 +117,7 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| // no physical rows in sheet | ||
| return table; | ||
| } | ||
|
|
||
| Row row = null; | ||
|
|
||
| if (_configuration.isSkipEmptyLines()) { | ||
| while (row == null && rowIterator.hasNext()) { | ||
| row = rowIterator.next(); | ||
|
|
@@ -128,6 +126,12 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| row = rowIterator.next(); | ||
| } | ||
|
|
||
| // Get first 1000 rows for the eager-read | ||
| final Iterator<Row> data = ExcelUtils.getRowIterator(sheet, _configuration, false); | ||
| int rowLength = row.getLastCellNum(); | ||
| ColumnType[] columnTypes = new ColumnType[rowLength]; | ||
|
|
||
| setColumnType(data, rowLength, columnTypes); | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final int columnNameLineNumber = _configuration.getColumnNameLineNumber(); | ||
| if (columnNameLineNumber == ExcelConfiguration.NO_COLUMN_NAME_LINE) { | ||
|
|
||
|
|
@@ -137,27 +141,22 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| while (row == null && rowIterator.hasNext()) { | ||
| row = rowIterator.next(); | ||
| } | ||
|
|
||
SociopathicPixel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // build columns without any intrinsic column names | ||
| final ColumnNamingStrategy columnNamingStrategy = _configuration.getColumnNamingStrategy(); | ||
| try (final ColumnNamingSession columnNamingSession = columnNamingStrategy.startColumnNamingSession()) { | ||
| final int offset = getColumnOffset(row); | ||
| for (int i = 0; i < offset; i++) { | ||
| columnNamingSession.getNextColumnName(new ColumnNamingContextImpl(i)); | ||
| } | ||
|
|
||
| for (int j = offset; j < row.getLastCellNum(); j++) { | ||
| final ColumnNamingContext namingContext = new ColumnNamingContextImpl(table, null, j); | ||
| final Column column = new MutableColumn(columnNamingSession.getNextColumnName(namingContext), | ||
| ColumnType.STRING, table, j, true); | ||
| columnTypes[j], table, j, true); | ||
| table.addColumn(column); | ||
| } | ||
| } | ||
|
|
||
| } else { | ||
|
|
||
| boolean hasColumns = true; | ||
|
|
||
| // iterate to the column name line number (if above 1) | ||
| for (int j = 1; j < columnNameLineNumber; j++) { | ||
| if (rowIterator.hasNext()) { | ||
|
|
@@ -167,50 +166,114 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| break; | ||
| } | ||
| } | ||
|
|
||
| if (hasColumns) { | ||
| createColumns(table, wb, row); | ||
| createColumns(table, wb, row, columnTypes); | ||
| } | ||
| } | ||
|
|
||
| return table; | ||
| } | ||
|
|
||
| private void setColumnType(Iterator<Row> data, int rowLength, ColumnType[] columnTypes) { | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| while (data.hasNext()) { | ||
|
||
| Row row = data.next(); | ||
| for (int index = 0; index < rowLength; index++) { | ||
| if (row.getLastCellNum() == 0) { | ||
| continue; | ||
| } | ||
| if (row.getCell(index) == null) { | ||
| columnTypes = checkColumnType(ColumnType.STRING, columnTypes, index); | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| CellType cellType = row.getCell(index).getCellType(); | ||
| if (cellType.getCode() != 0 && cellType.getCode() <= 2) { | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| columnTypes = checkColumnType(ColumnType.STRING, columnTypes, index); | ||
| } else if (cellType.getCode() == 0) { | ||
| columnTypes = checkColumnType((row.getCell(index).getNumericCellValue() % 1 == 0) | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ? ColumnType.INTEGER : ColumnType.DOUBLE, columnTypes, index); | ||
| } else if (cellType.getCode() == 4) { | ||
| columnTypes = checkColumnType(ColumnType.BOOLEAN, columnTypes, index); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private ColumnType[] checkColumnType(ColumnType columnType, ColumnType[] columnTypes, int index) { | ||
| if (columnTypes[index] != null) { | ||
| if (!columnTypes[index].equals(ColumnType.STRING) && !columnTypes[index].equals(columnType)) { | ||
| columnTypes[index] = ColumnType.STRING; | ||
| } | ||
| } else { | ||
| columnTypes[index] = columnType; | ||
| } | ||
| return columnTypes; | ||
| } | ||
|
|
||
| private void determineColumnDatatype(Object[] datatypes, Row row) { | ||
| for (int index = 0; index < row.getLastCellNum(); index++) { | ||
| CellType type = ((Cell) row.getCell(index)).getCellType(); | ||
|
|
||
| if (datatypes[index] instanceof Object) { | ||
| datatypes[index] = type; | ||
| } else if (datatypes[index] instanceof CellType) { | ||
| if (datatypes[index].equals(type)) { | ||
| continue; | ||
| } else { | ||
| datatypes[index] = CellType.STRING; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds columns based on row/cell values. | ||
| * | ||
| * @param table | ||
| * @param wb | ||
| * @param row | ||
| */ | ||
| private void createColumns(MutableTable table, Workbook wb, Row row) { | ||
| private void createColumns(MutableTable table, Workbook wb, Row row, ColumnType[] columTypes) { | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (row == null) { | ||
| logger.warn("Cannot create columns based on null row!"); | ||
| return; | ||
| } | ||
| final short rowLength = row.getLastCellNum(); | ||
|
|
||
| final int offset = getColumnOffset(row); | ||
|
|
||
| // build columns based on cell values. | ||
| try (final ColumnNamingSession columnNamingSession = _configuration.getColumnNamingStrategy() | ||
| try (final ColumnNamingSession columnNamingSession = _configuration | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .getColumnNamingStrategy() | ||
| .startColumnNamingSession()) { | ||
| for (int j = offset; j < rowLength; j++) { | ||
| final Cell cell = row.getCell(j); | ||
| final String intrinsicColumnName = ExcelUtils.getCellValue(wb, cell); | ||
| final ColumnNamingContext columnNamingContext = new ColumnNamingContextImpl(table, intrinsicColumnName, | ||
| j); | ||
| final String columnName = columnNamingSession.getNextColumnName(columnNamingContext); | ||
| final Column column = new MutableColumn(columnName, ColumnType.VARCHAR, table, j, true); | ||
| Column column = null; | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (columTypes == null) { | ||
| column = new MutableColumn(columnName, ColumnType.VARCHAR, table, j, true); | ||
| } else { | ||
| column = new MutableColumn(columnName, columTypes[j], table, j, true); | ||
| } | ||
| table.addColumn(column); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the column offset (first column to include). This is dependent on | ||
| * the row used for column processing and whether the skip empty columns | ||
| * property is set. | ||
| * Builds columns based on row/cell values. | ||
| * | ||
| * @param table | ||
| * @param wb | ||
| * @param row | ||
| */ | ||
| private void createColumns(MutableTable table, Workbook wb, Row row) { | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| createColumns(table, wb, row, null); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the column offset (first column to include). This is dependent on the | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * row used for column processing and whether the skip empty columns property is | ||
| * set. | ||
| * | ||
| * @param row | ||
| * @return | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.