我正在尝试编写js程序(同时学习js)以将XRS文件转换为JSON。我想加载一个xrs文件并输出一个JSON下载文件
文件已加载并拆分为数组
0:[“标记数量完成部分尺寸等级长度(mm)面积(m)重量(Kg) “] 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 “]
由于数组是一个对象,我需要返回一个错误
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-布布灵顿阶段:22B _
标记数量完成部分尺寸等级长度(mm)面积(m²)重量(千克)
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 * 39 S355J0 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
22809 1 S2 UB406 * 140 * 39 S355J0 7481 10.2 301.355
BDC Ltd零件清单(按标记)页面:2