demo20230512/Browser_Local/js/ImportService.js

42 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-05-12 10:20:28 +08:00
/*
Logic Solution
WeeeCarbonFootprint
Factory for CarbonFootprint
*/
angular.module('Services')
.service('ImportCsvService', function ($q) {
this.startImportAsync = function (LCAID, modelBuilder, csvFile, processPercentageFn) {
var deferred = $q.defer();
var result = {};
var resultItem;
result.data = [];
result.errors = [];
result.totalParsed = 0;
Papa.parse(csvFile, { // use papa parse csv convert plugin
skipEmptyLines: true,
complete: function (a) {
a.data.shift() //remove first row because it is headers
angular.forEach(a.data, function (entry, index) {
angular.forEach(entry, function (column, index) {//remove new line in data column
entry[index] = column.replace(/(\r\n|\n|\r)/gm, " ");
})
resultItem = modelBuilder.create(entry, LCAID);
//line = index +2 : index is 0 base and the start line is 2
if (resultItem.error) result.errors.push({ line: (index + 2), error: resultItem.error })
else result.data.push(resultItem);
if (processPercentageFn)
processPercentageFn(index / a.data.length)
})
result.totalParsed = a.data.length;
deferred.resolve(result)
}
});
return deferred.promise;
}
})