@adncorp/account-book-parser-adapter
Version:
parsing sheet adapter for account book
170 lines (143 loc) • 6.55 kB
JavaScript
;
/* eslint-disable spellcheck/spell-checker */
/* eslint-disable camelcase */
/**
* Build by modifying te following package source code : https://github.com/rahil471/node-xls-json
*/
var xlsx = require('xlsx');
var cvcsv = require('csv');
var fileUtils = require('../utils/fileUtils.js');
var xlsxTocsv = require('../utils/xlsxTocsv.js');
var date = require('date-and-time'); // eslint-disable-next-line camelcase
exports = module.exports = XLSXJson;
function XLSXJson(config, callback) {
if (!config.input) {
console.error('You miss an input file');
process.exit(1);
} // eslint-disable-next-line no-unused-vars
var cv = new CV(config, callback);
}
var nbrRow = 0;
function CV(config, callback) {
this.loadXlsx(config.input).then(function (wss) {
var wb = wss;
var ws = getWS(config, wb); // skip line and start à line
var range = xlsx.utils.decode_range(ws['!ref']);
nbrRow = range.e.r;
range.s.r += config.skip === undefined || config.skip === null ? 0 : config.skip;
ws['!ref'] = xlsx.utils.encode_range(range);
var csv = getCSV(ws);
cvjson(csv, config.lowerCaseHeaders, config.mapping, callback);
})["catch"](function (err) {
if (err) {
callback(err, [], config.lowerCaseHeaders);
} else {
callback(null, [], config.lowerCaseHeaders);
}
});
}
CV.prototype.loadXlsx = function (input) {
if (fileUtils.isUri(input)) {
return new Promise(function (resolve, reject) {
fileUtils.readFileUri(input).then(function (data) {
resolve(xlsx.read(data, {
type: 'buffer'
}));
})["catch"](function (err) {
reject(err);
});
});
}
return new Promise(function (resolve) {
resolve(xlsx.readFile(input));
});
};
var getWS = function getWS(config, wb) {
var targetSheet = config.sheet;
if (targetSheet == null) {
targetSheet = wb.SheetNames[0];
}
var ws = wb.Sheets[targetSheet];
return ws;
};
var getCSV = function getCSV(ws) {
var csvFile = xlsxTocsv.sheet_to_csv(ws);
return csvFile;
};
var cvjson = function cvjson(csv, lowerCaseHeaders, mapping, callback, settings) {
var record = [];
var accountbookobject = {};
var compte = '';
var values = [];
var cumul_bal_debit = '';
var cumul_bal_credit = '';
var cumul_bal_total = '';
var tour = false;
var columnMap = mapping === undefined || mapping === null ? [] : mapping.slice(0); // clone
var header = columnMap;
var validateHeader = true;
cvcsv().from.string(csv).on('record', function (row, index) {
if (index === 8) {
// console.log(row)
if (row[0] === undefined || row[0] === '' || row[0] === null || row[6] === undefined || row[6] === '' || row[6] === null || row[7] === undefined || row[7] === '' || row[7] === null || row[8] === undefined || row[8] === '' || row[8] === null) {
validateHeader = false;
} else if (row[0].trim().toUpperCase() !== header[0].trim().toUpperCase() && row[6].trim().toUpperCase() !== header[6].trim().toUpperCase() && row[7].trim().toUpperCase() !== header[7].trim().toUpperCase() && row[8].trim().toUpperCase() !== header[8].trim().toUpperCase()) {
validateHeader = false;
}
}
if (validateHeader) {
if (index > 8) {
if (tour === false) {
compte = row[0].trim().toLowerCase();
cumul_bal_debit = row[6].trim();
cumul_bal_credit = row[7].trim();
tour = true;
} else {
if (date.isValid(row[0], 'YYYY-MM-DD') || row[0] === null || row[0].toLowerCase() === 'null' || row[0] === '') {
var object = {};
header.forEach(function (column, index) {
var key = lowerCaseHeaders ? column.trim().toLowerCase() : column.trim();
if (key.trim() !== '') {
object[key] = row[index];
}
}); // ajout des données relatives à un compte
object.account = 0;
values.push(object);
if (nbrRow === index) {
// console.log(row[0].toLowerCase())
accountbookobject.account = compte.replace(/[, \- ]+/g, ' ').split(' ')[0];
accountbookobject.account_label = compte.replace(/[, \- ]+/g, ' ').split(' ')[1] + (compte.split(' ')[2] === undefined ? '' : ' ' + compte.split(' ')[2]) + (compte.split(' ')[3] === undefined ? '' : ' ' + compte.split(' ')[3]) + (compte.split(' ')[4] === undefined ? '' : ' ' + compte.split(' ')[4]) + (compte.split(' ')[5] === undefined ? '' : ' ' + compte.split(' ')[5]) + (compte.split(' ')[6] === undefined ? '' : ' ' + compte.split(' ')[6]) + (compte.split(' ')[7] === undefined ? '' : ' ' + compte.split(' ')[7]);
accountbookobject.values = values;
accountbookobject.cumul_bal_debit = cumul_bal_debit;
accountbookobject.cumul_bal_credit = cumul_bal_credit;
record.push(accountbookobject);
accountbookobject = {};
}
} else {
// console.log(row[0].toLowerCase())
accountbookobject.account = compte.replace(/[, \- ]+/g, ' ').split(' ')[0];
accountbookobject.account_label = compte.replace(/[, \- ]+/g, ' ').split(' ')[1] + (compte.split(' ')[2] === undefined ? '' : ' ' + compte.split(' ')[2]) + (compte.split(' ')[3] === undefined ? '' : ' ' + compte.split(' ')[3]) + (compte.split(' ')[4] === undefined ? '' : ' ' + compte.split(' ')[4]) + (compte.split(' ')[5] === undefined ? '' : ' ' + compte.split(' ')[5]) + (compte.split(' ')[6] === undefined ? '' : ' ' + compte.split(' ')[6]) + (compte.split(' ')[7] === undefined ? '' : ' ' + compte.split(' ')[7]);
accountbookobject.values = values;
accountbookobject.cumul_bal_debit = cumul_bal_debit;
accountbookobject.cumul_bal_credit = cumul_bal_credit;
accountbookobject.cumul_bal_total = cumul_bal_total;
record.push(accountbookobject);
values = [];
accountbookobject = {};
compte = row[0].trim().toLowerCase();
cumul_bal_debit = row[6].trim();
cumul_bal_credit = row[7].trim();
cumul_bal_total = row[8].trim();
}
}
}
}
}).on('end', function (count) {
// when writing to a file, use the 'close' event
// the 'end' event may fire before the file has been written
callback(null, record, header);
}).on('error', function (error) {
console.error(error.message);
});
};
//# sourceMappingURL=parser.js.map