-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult10.js
160 lines (145 loc) · 4 KB
/
result10.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Node.Js program to generate Excel File from Text File sent by CBSE
Made by: Mehul Ambastha
contact: [email protected]
NOTE: You must have node installed, be in the directory of the the file, and then run the file by running 'node result10.js' in the terminal
NOTE: The SAMPLE_FILE.txt must be in the same folder, and result-10.xlsx will be generated in the same folder
*/
const fs = require('fs');
const excel = require('excel4node');
let workbook = new excel.Workbook({
defaultFont: {
name: 'Arial',
},
});
let worksheet = workbook.addWorksheet('Result-sheet');
let headerStyle = workbook.createStyle({
font: {
color: '#000000',
size: 12,
},
numberFormat: '$#, ##0.00; ($#,##0.00); -',
});
let dataStyle = workbook.createStyle({
font: {
color: '#060606',
size: 10.5,
},
alignment: {
vertical: 'center',
},
});
let headers = [
'Roll no',
'Gender',
'Student Name',
'English',
'Hindi',
'Maths-Basic',
'Science',
'Social Science',
'Computer',
'Maths-Standard',
'Painting',
'Music',
'Sanskrit',
'Result',
];
for (let i = 1; i <= headers.length; i++) {
worksheet
.cell(1, i)
.string(headers[i - 1])
.style(headerStyle);
}
//Reading the sample-result file
let readTxtFile = fs.createReadStream('./SAMPLE_FILE.txt');
let lines;
let data = '';
readTxtFile
.on('data', text => {
data += text;
lines = data.split(/\r?\n/);
})
.on('end', () => {
lines.pop();
processData(lines);
});
function processData(lines) {
let row = 2;
let count = 1;
for (let k = 0; k < lines.length; k++) {
line = lines[k].split(/\s+/);
if (count % 2 === 1) {
line.pop();
var name;
var roll = line[0];
var gender = line[1];
var subjectWiseData = new Map();
var subjectsArray = [];
var marksPerChild = [];
let iterable;
var result = line[line.length - 1];
if (isNaN(line[4])) {
name = `${line[2]} ${line[4]}`;
iterable = 5;
} else {
name = `${line[2]} ${line[3]}`;
iterable = 4;
}
for (let i = iterable; i < line.length - 1; i++) {
subjectsArray.push(line[i]);
subjectWiseData.set(line[i], '');
}
} else {
line.shift();
for (let i = 0; i < line.length; i++) {
if (i % 2 == 0) {
marksPerChild.push(line[i]);
}
}
for (let j = 0; j < marksPerChild.length; j++) {
subjectWiseData.set(subjectsArray[j], marksPerChild[j]);
}
writeToExcel(roll, name, gender, subjectWiseData, result, row);
row += 1;
}
count += 1;
}
}
var fileName = 'result-10';
var filePath = `./${fileName}.xlsx`;
function writeToExcel(roll, name, gender, subjectData, result, row) {
worksheet.cell(row, 1).string(roll).style(dataStyle);
worksheet.cell(row, 2).string(gender).style(dataStyle);
worksheet.cell(row, 3).string(name).style(dataStyle);
worksheet.cell(row, 14).string(result).style(dataStyle);
for (let code of subjectData.keys()) {
if (code == '184') {
worksheet.cell(row, 4).string(subjectData.get(code)).style(dataStyle);
} else if (code == '085') {
worksheet.cell(row, 5).string(subjectData.get(code)).style(dataStyle);
} else if (code == '241') {
worksheet.cell(row, 6).string(subjectData.get(code)).style(dataStyle);
} else if (code == '086') {
worksheet.cell(row, 7).string(subjectData.get(code)).style(dataStyle);
} else if (code == '087') {
worksheet.cell(row, 8).string(subjectData.get(code)).style(dataStyle);
} else if (code == '165') {
worksheet.cell(row, 9).string(subjectData.get(code)).style(dataStyle);
} else if (code == '041') {
worksheet.cell(row, 10).string(subjectData.get(code)).style(dataStyle);
} else if (code == '049') {
worksheet.cell(row, 11).string(subjectData.get(code)).style(dataStyle);
} else if (code == '034') {
worksheet.cell(row, 12).string(subjectData.get(code)).style(dataStyle);
} else if (code == '122') {
worksheet.cell(row, 13).string(subjectData.get(code)).style(dataStyle);
}
}
workbook.write(filePath);
}
if (!fs.existsSync(filePath)) {
console.log(
`The file has been generated with the name of ${fileName}.xlsx . Please check the folder...`
);
}