-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
61 lines (51 loc) · 1.51 KB
/
start.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
var xlsx = require('node-xlsx').default;
var fs = require('fs');
const path = require('path');
var file; // входные данные пока константой
function xlDoc(filename){
this.file = filename;
}
xlDoc.prototype.parse = function(cbError) {
console.log('xlDoc_started');
if(fs.existsSync(this.file)){
if(this.checksign()){
// подключаем к библиотеке
const workSheetsFromFile = xlsx.parse(this.file);
return workSheetsFromFile[0]['data'];
}
else{
// console.warn('file format invald!');
if(cbError != undefined) cbError('file format invalid');
return '';
}
}
else{
// console.warn('file %s not found',this.file);
if(cbError != undefined) cbError('file not found');
return '';
}
};
xlDoc.prototype.checksign = function() {
var bytes = new Buffer(16);
var fd = fs.openSync(this.file, 'r');
fs.readSync(fd,bytes,0,4,0);
var t = bytes.readUInt32BE();
//console.log('checksign: '+ bytes.readInt32BE().toString(16));
if(t == 0x504b0304) return true; // xlsx опознан
// далее надо проверить на соответствие формату xls
else if(t == 0xD0CF11E0) return true; // xls опознан
return false;
}
module.exports = xlDoc;
var xls = new xlDoc('test/z1.doc');
var json_data;
try{
json_data = xls.parse(callback);
}
catch(err){
console.warn('parse error');
}
if(json_data != undefined) console.log(json_data);
function callback(txt) {
console.warn('ERROR:'+ txt);
}