@adncorp/account-book-parser-adapter
Version:
parsing sheet adapter for account book
190 lines (159 loc) • 5.76 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 isNumber = require('../commonFunction/').isNumber; // 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);
}
function CV(config, callback) {
this.loadXlsx(config.input).then(function (wss) {
var wb = wss;
var ws = getWS(config, wb); // console.log(ws)
// skip line and start à line
var range = xlsx.utils.decode_range(ws['!ref']);
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 account = '';
var values = [];
var account_label = '';
var tour = false;
var fisrtSave = false;
var firstField = 0;
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 === 0) {
firstField = parseFloat((row[0] || '0').replace(/[,\s ]+/g, ''));
if (row[0] === undefined || row[0] === '') {
validateHeader = false;
} else if (!isNumber(firstField)) {
validateHeader = false;
}
}
if (validateHeader) {
if (index >= 0) {
if (tour === false) {
if (row[0] !== '') {
account = row[0].split(' ')[0];
account_label = row[0].split(' ')[1] + (row[0].split(' ')[2] === undefined ? '' : ' ' + row[0].split(' ')[2]) + (row[0].split(' ')[3] === undefined ? '' : ' ' + row[0].split(' ')[3]) + (row[0].split(' ')[4] === undefined ? '' : ' ' + row[0].split(' ')[4]) + (row[0].split(' ')[5] === undefined ? '' : ' ' + row[0].split(' ')[5]) + (row[0].split(' ')[6] === undefined ? '' : ' ' + row[0].split(' ')[6]) + (row[0].split(' ')[7] === undefined ? '' : ' ' + row[0].split(' ')[7]);
tour = true;
}
} else {
if (row[0].split(' ')[0].toLowerCase() !== 'total' && tour) {
var object = {};
row.forEach(function (column, index) {
if (row[0] === '' && index !== 0) {
if (index === 1) {
object['date'] = row[index];
}
if (index === 2) {
object['journal'] = row[index];
}
if (index === 3) {
object['reference'] = row[index];
}
if (index === 4) {
object['libelle'] = row[index];
}
if (index === 5) {
object['debit'] = row[index];
}
if (index === 6) {
object['credit'] = row[index];
}
}
}); // ajout des données relatives à un account
values.push(object);
fisrtSave = true;
} else {
if (fisrtSave && values.length !== 0) {
accountbookobject.account = account;
accountbookobject.account_label = account_label;
values = values.map(function (el) {
if (el.date === '') {
el.date = el.libelle.split(' ')[2];
}
return el;
});
accountbookobject.values = values;
accountbookobject.cumul_bal_debit = row[8];
accountbookobject.cumul_bal_credit = row[9];
accountbookobject.cumul_bal_total = 0;
record.push(accountbookobject);
values = [];
accountbookobject = {};
tour = false;
fisrtSave = false;
} else {
values = [];
accountbookobject = {};
tour = false;
fisrtSave = false;
}
}
}
}
}
}).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