-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (144 loc) · 4.91 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var XLSX = require("xlsx");
let filesArr = [];
//requiring path and fs modules
const path = require('path');
const fs = require('fs');
const async = require("async")
//joining path of directory
const directoryPath = path.join(__dirname, 'files');
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
//Add the filenames in the array
filesArr.push(file);
});
//Start the dataMiner
for (var file in filesArr) {
q.push(filesArr[file], function (err,file,res) {
console.log("Started to read: " + file );
});
}
});
// FUNCTION WHICH READS THE SPECIFIC ROWS FROM EVERY XCELL IN THE FOLDER: "/files"
function getdata(file){
//start row
let crow = 11;
//some specific data
let plc = 'C4';
//other rows
let lac;
let lacname;
let module;
let modulename;
let compname;
let layoutPosition;
var workbook = XLSX.readFile('./files/'+file);
//last row of each file
let maxRows = 1058
//array we return for processing
let rows = []
//Customizable logic for extraction:
//define sheet names or check
workbook.SheetNames.forEach(sheet =>{
if(sheet.startsWith('F')){
crow = 11;
let ws = workbook.Sheets[sheet]
for(let i=0; i<=maxRows; i++){
module = 'B'+crow.toString()
modulename = 'C'+crow.toString()
moduletext = 'H'+crow.toString()
compname = 'AE'+crow.toString()
productid = 'F'+crow.toString()
moduletype = 'E'+crow.toString()
layoutPosition = 'D'+crow.toString()
content = ''
if(ws['L'+crow.toString()] != undefined){
lac = 'L'+crow.toString()
}
if(ws['M'+crow.toString()] != undefined){
lacname = 'M'+crow.toString()
}
if(ws[modulename]!=undefined && ws[modulename].h != ' '){
// PLC
if(ws[plc]!=undefined){
content += ws[plc].h + ';'
}else{
content += ';'
}
// POSITION TYPE
if(ws[lacname]!=undefined){
content += ws[lacname].h + ';'
}else{
content += ';'
}
// LAC
if(ws[lac]!=undefined){
content += ws[lac].h + ';'
}else{
content += ';'
}
//MODULE NUM
if(ws[module]!=undefined){
content += ws[module].w + ';'
}else{
content += ';'
}
//MODULE NAME
content += ws[modulename].h + ';'
//COMPOINT
if(ws[compname]!=undefined){
content += ws[compname].h.replace(';',',') + ';'
}else{
content += ';'
}
//MODULE TEXT
if(ws[moduletext]!=undefined){
content += ws[moduletext].h + ';'
}else{
content += ';'
}
//PRODUCT ID
if(ws[productid]!=undefined){
content += ws[productid].h + ';'
}else{
content += ';'
}
//MODULE TYPE
if(ws[moduletype]!=undefined){
content += ws[moduletype].w + ';'
}else{
content += ';'
}
//LAYOUT POSITION
if(ws[layoutPosition]!=undefined){
content += ws[layoutPosition].w + ';'
}else{
content += ';'
}
content += "\r\n"
rows.push(content)
}
crow++
}
}
})
return rows
}
// CREATE THE QUEUE
var q = async.queue(function(file, callback) {
dataArray = getdata(file)
dataArray.forEach(el=>{
fs.appendFileSync('file.csv', el, err => {
if (err) {
console.error(err);
}
console.log('Data extracted and saved!')
});
})
callback(null,file);
}, 1);