Я пытаюсь написать программу js (одновременно изучая js) для преобразования файла XRS в JSON.Я хочу загрузить файл xrs и вывести JSON-файл для загрузки
Файл загружается и разбивается на массив
0: ["Отметить Кол-во Готово Размер сечения Длина Степень (мм) Площадь (м)) Вт (кг) "] 1: [" 22800 1 S2 UB406 * 140 * 39 S355J0 6273 9,3 275,006 "] 2: [" 22801 1 S2 UB406 * 140 * 39 S355J0 7112 9,7 290,443 "]
Iполучить ошибку, так как массив является объектом, он должен быть строкой
let headerjson = [];
let datajson = [];
let datajson1 = [];
let header = {
customer:"",
doctitle:"",
author:"",
contract:"",
date:'01.02.2018',
client:"",
draughtsman: "",
contractname:"",
phase:"",
}
const input = document.querySelector('input[type="file"]')
input.addEventListener('change', function (e){
const reader = new FileReader()
reader.onload = function (){
const lines = reader.result.split('\n').map(function (line){
return line.split(',')
})
getdata(lines);
}
reader.readAsText(input.files[0])
}, false)
function trimArray(arr){
for(i=0;i<arr.length;i++)
{
arr[i] = arr[i].replace(/^\s\s*/, '').replace(-/\s\s*$/, '');
}
return arr;
}
function makeheader(){
//console.log(headerjson)
var tempstore = arrayToString(headerjson[0]);
header.customer = tempstore.slice(0,13).trim();
header.doctitle = tempstore.slice(29,39).trim();
header.author = tempstore.slice(39,49).trim();
var tempstore = arrayToString(headerjson[1]);
header.contract = + tempstore.slice(15,25).trim();
header.date = tempstore.slice((tempstore.length-11),tempstore.length).trim();
var tempstore = arrayToString(headerjson[2]);
header.client = tempstore.slice(15,35).trim();
header.draughtsman = tempstore.slice(67).trim();
var tempstore = arrayToString(headerjson[3]);
header.contractname = tempstore.slice(15,35).trim();
header.phase = tempstore.slice(67).trim();
}
function makedata(){
splitdata(datajson)
}
function arrayToString(arr) {
let str = '';
arr.forEach(function(i, index) {
str += i;
if (index != (arr.length - 1)) {
str += ',';
};
});
return str;
}
function getdata(linesc){
for (var i = 2; i < 7; i++){
if (i != 3){
headerjson.push(trimArray(linesc[i]));
}
}
makeheader();
// removes unwanted lines and cleans up the data
totallines = (linesc.length);
totallines = totallines -4; // remove 4 extra lines
datalines = 60;
linescount = 0;
startpoint = 10;
datajson.push(trimArray(linesc[8])); //header information
for (var i = startpoint; i < (totallines); i = i + 2){
datajson.push(trimArray(linesc[i]));
if( linescount == datalines){
i = i + 12;
linescount = 0;
}
else{
linescount = linescount +2;
}
}
console.log(datajson);
makedata();
}
function splitdata(str){
console.log(str);
str = arrayToString(str);
console.log(str);
var cells = str.split('\n').map(function (el) { return el.split(/\s+/); });
var headings = cells.shift();
var out = cells.map(function (el) {
var obj = {};
for (var i = 0, l = el.length; i < l; i++) {
obj[headings[i]] = isNaN(Number(el[i])) ? el[i] : +el[i];
}
return obj;
});
console.log(cells)
//console.log(JSON.stringify(out, null, 2));
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS FileReader</title>
</head>
<body>
<input type="file" />
<script src="js/bundle.js" charset="utf-8"></script>
</body>
</html>
это пример файла формата XRS
СПИСОК ЧАСТЕЙ BDC Ltd (по марке) Страница: 1
Контракт №: 79 Дата: 09.07.2018 Клиент: Крупный Строитель Чертежник: MULTI Название контракта: St James - bubblington Фаза: 22B_
Марка Кол-во Конец Размер секции Марка Длина Длина (мм) Площадь(м²) Вт (кг)
22800 1 S2 UB406 * 140 * 39 S355J0 6273 9,3 275,006
22801 1 S2 UB406 * 140 * 39 S355J0 7112 9,7 290,443
22802 1 S2 UB406 * 140 * 39 S355J0 7313 10,0 298,376
22803 1 S2 UB406 * 140 * 39 S355J0 7253 10,1 291,541
22804 1 S2 UB406 * 140 * 39 S355J0 7112 9,7 290,443
22805 1 S2 UB406 * 140 * 39 S355J0 7294 10,0 298,579
22806 1 S2 UB406 * 140 * 39S355J0 7313 10,0 298,376
22807 1 S2 UB406 * 140 * 39 S355J0 7095 9,7 293,354
22808 1 S2 UB406 * 140 * 39 S355J0 7095 9,9 295,709
1052 *
22809 1 S2 UB406 * 140 * 39 S355J0 7481 10,2 301,355
BDC Ltd ЧАСТЬ СПИСОК ЧАСТЕЙ (по марке) Страница: 2