Skip to content

Commit

Permalink
feat: check if all columns name in file match metadata (#57)
Browse files Browse the repository at this point in the history
* feat: check if all columns name in file match metadata

Checks on upload if all column names match the corresponding metadata model
Closes #29

* fix: remove columns in list report test file
  • Loading branch information
marianfoo authored Feb 18, 2023
1 parent 1be7170 commit 7c538a0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
Binary file modified examples/test/testFiles/ListReportOrdersNoErros.xlsx
Binary file not shown.
Binary file modified examples/test/testFiles/ListReportOrdersNoErrosV2.xlsx
Binary file not shown.
34 changes: 34 additions & 0 deletions src/controller/ExcelUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export default class ExcelUpload {
const data = await this.buffer_RS(stream);
let workbook = XLSX.read(data);
this.component.setErrorResults([]);
let columnNames
// reading all sheets
workbook.SheetNames.forEach((sheetName) => {
// Need special case for CSV Import
Expand All @@ -186,6 +187,7 @@ export default class ExcelUpload {
// }
// }
let data = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
columnNames = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { header: 1 })[0];
excelSheetsData.push(data);
});
// use only first sheet
Expand All @@ -198,6 +200,7 @@ export default class ExcelUpload {
}
// check if data is ok in extension method
this._checkMandatoryFields(firstSheet, this.component.getErrorResults());
this._checkColumnNames(columnNames, this.component.getErrorResults());
this.component.fireCheckBeforeRead({ sheetData: firstSheet });
if (this.component.getErrorResults().some((error) => error.counter > 0)) {
// error found in excel
Expand Down Expand Up @@ -468,6 +471,37 @@ export default class ExcelUpload {
return errorArray;
}

_checkColumnNames(columnNames, errorArray){
const fieldMatchType = this.component.getFieldMatchType();
for (let index = 0; index < columnNames.length; index++) {
const columnName = columnNames[index];
let found = false;
for (const key in this.typeLabelList) {
if (this.typeLabelList.hasOwnProperty(key)){
if (fieldMatchType === "label") {
if(this.typeLabelList[key].label === columnName){
found = true
break;
}
}
if (fieldMatchType === "labelTypeBrackets") {
if(columnName.includes(`[${key}]`)){
found = true
break;
}
}
}
}
if(!found){
const errorMessage = {
title: this._geti18nText("columnNotFound", [columnName]),
counter: 1,
};
errorArray.push(errorMessage)
}
}
}

_getValueFromRow(row, label, type) {
const fieldMatchType = this.component.getFieldMatchType();
let value;
Expand Down
1 change: 1 addition & 0 deletions src/i18n/i18n_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ selectFileUpload=Wähle Datei zum hochladen
uploadingFile=Datei wird hochgeladen
downloadingTemplate=Template Datei wird heruntergeladen
mandatoryFieldNotFilled=Pflichtfeld {0} ist nicht gefüllt
columnNotFound=Spalte in der Datei {0} passt zu keinem Datenfeld
1 change: 1 addition & 0 deletions src/i18n/i18n_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ selectFileUpload=Select file to Upload
uploadingFile=Uploading Excel File
downloadingTemplate=Template File Downloading...
mandatoryFieldNotFilled=Mandatory Field {0} not filled
columnNotFound=Column in the file {0} does not match any data field

0 comments on commit 7c538a0

Please sign in to comment.