Skip to content

Commit

Permalink
fix: Enable loading XSLX with blank rows
Browse files Browse the repository at this point in the history
It is possible to load XLSX with multiple sheets that may contain blank pages or rows, as source data or schema.
Wizard: add the last 2 steps in one single step.

ING-4075
  • Loading branch information
emanuelaepure10 committed Oct 23, 2023
1 parent 3ec598b commit 543cb1a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public boolean isValid() {
*/
private void showMessage() {
if (header == null || header.length == 0)
setErrorMessage("The file contains no data");
setErrorMessage("The file contains no data or not valid data");
else if (!sfe.isValid())
setErrorMessage("Please enter a valid Type Name");
else if (!isValid)
Expand Down
7 changes: 0 additions & 7 deletions io/plugins/eu.esdihumboldt.hale.io.xls.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
ref="eu.esdihumboldt.hale.io.xls.reader.schema">
</provider>
</configPage>
<configPage
class="eu.esdihumboldt.hale.io.csv.ui.TypeSelectionPage"
order="0">
<provider
ref="eu.esdihumboldt.hale.io.xls.reader.instance">
</provider>
</configPage>
<configPage
class="eu.esdihumboldt.hale.io.xls.ui.XLSInstanceExportConfigurationPage"
order="0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,23 @@
import eu.esdihumboldt.hale.common.core.io.Value;
import eu.esdihumboldt.hale.common.instance.io.InstanceReader;
import eu.esdihumboldt.hale.io.csv.InstanceTableIOConstants;
import eu.esdihumboldt.hale.ui.io.config.AbstractConfigurationPage;
import eu.esdihumboldt.hale.ui.io.instance.InstanceImportWizard;
import eu.esdihumboldt.hale.io.csv.ui.TypeSelectionPage;

/**
* Configuration page for the instance export provider of Excel files
*
* @author Patrick Lieb
*/
public class XLSInstanceImportConfigurationPage extends
AbstractConfigurationPage<InstanceReader, InstanceImportWizard> {
public class XLSInstanceImportConfigurationPage extends TypeSelectionPage {

private Combo sheetSelection;

/**
* Default Constructor
*/
public XLSInstanceImportConfigurationPage() {
super("xls.instance.import.sheet.selection");
setTitle("Sheet selection");
setDescription("Select sheet to import instances");
setDescription("Select sheet to import instances, your Type and Data reading setting");
}

/**
Expand All @@ -55,21 +52,16 @@ public XLSInstanceImportConfigurationPage() {
@Override
protected void createContent(Composite page) {

page.setLayout(new GridLayout(1, false));
page.setLayout(new GridLayout(2, false));

Composite menu = new Composite(page, SWT.NONE);
menu.setLayout(new GridLayout(2, false));

GridDataFactory.fillDefaults().grab(true, false).applyTo(menu);

Label sheetLabel = new Label(menu, SWT.None);
Label sheetLabel = new Label(page, SWT.None);
sheetLabel.setText("Select sheet");

sheetSelection = new Combo(menu, SWT.DROP_DOWN | SWT.READ_ONLY);
sheetSelection = new Combo(page, SWT.DROP_DOWN | SWT.READ_ONLY);
GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false)
.applyTo(sheetSelection);

setPageComplete(false);
super.createContent(page);
}

/**
Expand Down Expand Up @@ -97,7 +89,7 @@ protected void onShowPage(boolean firstShow) {
}
super.onShowPage(firstShow);
sheetSelection.select(0);
setPageComplete(true);
setPageComplete(false);
}

/**
Expand All @@ -107,7 +99,8 @@ protected void onShowPage(boolean firstShow) {
public boolean updateConfiguration(InstanceReader provider) {
provider.setParameter(InstanceTableIOConstants.SHEET_INDEX,
Value.of(sheetSelection.getSelectionIndex()));
return true;

return super.updateConfiguration(provider);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void widgetSelected(SelectionEvent event) {
} catch (Exception e) {
setPageComplete(false);
clearSuperPage();
setErrorMessage("The sheet is empty!");
setErrorMessage("The sheet is empty or the header is not valid!");
}

}
Expand All @@ -117,7 +117,7 @@ public void widgetDefaultSelected(SelectionEvent e) {
} catch (Exception e1) {
setPageComplete(false);
clearSuperPage();
setErrorMessage("The sheet is empty!");
setErrorMessage("The sheet is empty or the header is not valid!");
}
}

Expand Down Expand Up @@ -159,11 +159,12 @@ protected void onShowPage(boolean firstShow) {
}
ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < numberOfSheets; i++) {
items.add(wb.getSheetAt(i).getSheetName());
// only add items if there is a header (no empty sheet)
Row row = wb.getSheetAt(i).getRow(0);
if (row == null && newLocation != null && !newLocation.equals(oldLocation)) {
sheetNum++;
items.add(wb.getSheetAt(i).getSheetName());
if (row != null) {
update(i);
sheetNum = i;
}
}

Expand All @@ -183,18 +184,24 @@ protected void onShowPage(boolean firstShow) {

} catch (OldExcelFormatException e) {
// the setup is not in a valid state
clearPage();
clearSuperPage();
setErrorMessage("Old excel format detected (format 5.0/7.0 (BIFF5)). Please convert the excel file to BIFF8 from Excel versions 97/2000/XP/2003.");
setPageComplete(false);
setErrorMessage(
"Old excel format detected (format 5.0/7.0 (BIFF5)). Please convert the excel file to BIFF8 from Excel versions 97/2000/XP/2003.");
clearFromException();
} catch (Exception e) {
clearPage();
clearSuperPage();
setErrorMessage("Excel file cannot be loaded!");
setPageComplete(false);
clearFromException();
}
}

/**
* clear page and super page
*/
private void clearFromException() {
clearPage();
clearSuperPage();
setPageComplete(false);
}

/**
* Use this if an error occurs
*
Expand Down Expand Up @@ -223,8 +230,8 @@ private void sheetSelectionChanged(int sheetNum) throws Exception {
private void update(int sheetNum) throws Exception {

// if the sheet is empty an Exception occurs
AnalyseXLSSchemaTable analyser = new AnalyseXLSSchemaTable(getWizard().getProvider()
.getSource().getLocation(), sheetNum);
AnalyseXLSSchemaTable analyser = new AnalyseXLSSchemaTable(
getWizard().getProvider().getSource().getLocation(), sheetNum);

setHeader(analyser.getHeader().toArray(new String[0]));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected void analyse(URI location) throws Exception {
* @param location the file location
* @param sheetNum number of the sheet that should be loaded (0-based)
* @throws Exception if an error occurs loading the file
* @throws XLSException if an error occurs loading the header, content
*/
protected void analyse(URI location, int sheetNum) throws Exception {
InputStream inp = new BufferedInputStream(location.toURL().openStream());
Expand Down Expand Up @@ -95,13 +96,19 @@ protected void analyse(URI location, int sheetNum) throws Exception {
*/
protected void analyseHeader(Sheet sheet) {
Row header = sheet.getRow(0);

// identify columns
for (int i = header.getFirstCellNum(); i < header.getLastCellNum(); i++) {
Cell cell = header.getCell(i);
String text = extractText(cell);

headerCell(i, text);
if (header != null) {

// identify columns
int count = 0;
for (int i = header.getFirstCellNum(); i < header.getLastCellNum(); i++) {
Cell cell = header.getCell(i);
String text = extractText(cell);
// cell cannot be empty to extract the text
if (text != null) {
headerCell(count, text);
count++;
}
}
}
}

Expand All @@ -120,7 +127,9 @@ private void analyseContent(Sheet sheet) {
// for each row starting from the second
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
analyseRow(i, row);
if (row != null) {
analyseRow(i, row);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public AnalyseXLSSchemaTable(URI location, int sheetNum) throws Exception {
*/
@Override
protected void headerCell(int num, String text) {
if (num == header.size())
if (num == header.size()) {
header.add(text);
}
header.set(num, text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,12 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
Boolean skipType = getParameter(CommonSchemaConstants.PARAM_SKIP_N_LINES).as(Boolean.class);

if (skipType == null) {

skipNlines = getParameter(CommonSchemaConstants.PARAM_SKIP_N_LINES).as(Integer.class);

}
else if (skipType) {

skipNlines = 1;

}
else {

skipNlines = 0;
}

Expand Down

0 comments on commit 543cb1a

Please sign in to comment.