This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
695 lines (614 loc) · 18.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
* TODO:
* - Check the value of title cell is valid or not (only alphabet)
* - 增加 CLI 方法, 傳入檔案列表, 依列表依序 parse 各個資料
* - 將 tags 改為使用 Objec 傳入 { tagTitle: '!', tagIgnore: '#', tagAttr: '^' }
* - 改用 Asynchronous 的方式去寫, 能的話引入 async
* - 錯誤的地方使用明確的 exception, 而非回傳 null
* Refactoring:
* - 修改 function A(x,y) call function B(y), 但卻在 A, B 裡都詳細檢查判斷了 y;
* 修正成為只需要在 function B(y) 內檢查就好, A 只需要判斷回傳出來的值
*/
var excel2jsontemplate = (function () {
const DEFAULT_ATTR_SHEETNAME = 'sheetname';
const DEFAULT_ATTR_DATAS = 'datas';
function InitException(message) {
this.message = message;
this.name = 'InitException';
}
if (typeof require === 'undefined') {
throw new InitException('require is undefined');
}
var path = require('path');
var fs = require('fs');
var util = require('util');
var XLSX = require('xlsx');
var jsonfile = require('jsonfile');
var _u = require('underscore');
var _l = require('lodash');
/**
* Load the sheet by file path and sheet name.
*
* @method loadSheet
*
* @param {string} filePath
* @param {string} sheetName
*
* @return {Object} sheet Object of sheet.
*/
function loadSheet(filePath, sheetName) {
if (_u.isEmpty(filePath) || _u.isEmpty(sheetName)) {
return;
}
try {
// check file is exists or not
if (!fs.existsSync(filePath)) {
throw new Error('The file is not exists.');
}
var workbook = XLSX.readFile(filePath);
if (!workbook) {
return;
}
if (_l.indexOf(workbook.SheetNames, sheetName) === -1) {
return;
}
return workbook.Sheets[sheetName];
} catch (e) {
throw e;
}
}
/**
* The cell factory.
*
* @method factoryCell
*
* @param {Object} cell_address { c: column, r: row }
* @param {string} z_value number format string associated with the cell
* @param {Object} rawCell Raw cell object
*
* @return {Object} cell The cell Object.
*/
function factoryCell(cell_address, z_value, rawCell) {
var cell = {
o: rawCell,
v: rawCell ? rawCell.v : null, // raw value
w: rawCell ? rawCell.w : null, // formatted text (if applicable)
c: cell_address.c,
r: cell_address.r,
z: z_value,
};
return cell;
}
/**
* Check the cell is need ignore or not.
* <pre>
* Rules:
* 1. Same column with ignore cell.
* 2. If the column of ignore cell is zero, the same row with ignore cell will be ignored.
* </pre>
*
* @method isNeedIgnore
*
* @param {Object} cell
* @param {Array} ignore cells
*
* @return {Boolean}
*/
function isNeedIgnore(cell, ignores) {
for (var k in ignores) {
if (!ignores.hasOwnProperty(k)) {
continue;
}
var fc = ignores[k];
if (fc.c == cell.c) {
return true;
}
if (fc.c === 0 && fc.r == cell.r) {
return true;
}
}
return false;
}
/**
* Check is tag or not.
*
* @method isTag
*
* @param {Object} cell
* @param {string} tagChar
*
* @return {Boolean}
*/
function isTag(cell, tagChar) {
if (!cell || !cell.v) {
return false;
}
if (typeof cell.v !== 'string') {
return false;
}
if (cell.v != tagChar) {
return false;
}
return true;
}
/**
* Find the cell object of title tag by title char.
*
* @method findTitleTagCell
*
* @param {Object} worksheet
* @param {Object} range
* @param {string} tagTitle
*
* @return {Object} cell The cell of title.
*/
function findTitleTagCell(worksheet, range, tagTitle) {
if (!tagTitle) {
return;
}
for (var ir = range.s.r; ir <= range.e.r; ++ir) {
for (var ic = range.s.c; ic <= range.e.c; ++ic) {
var cell_address = {
c: ic,
r: ir
};
z = XLSX.utils.encode_cell(cell_address);
if (!isTag(worksheet[z], tagTitle)) {
continue;
}
var cell = factoryCell(cell_address, z, worksheet[z]);
return cell;
}
}
return;
}
/**
* Find the cells by tag char.
*
* @method findTagCells
*
* @param {Object} worksheet
* @param {Object} range
* @param {Object} endCell
* @param {string} tagChar
*
* @return {Array} cells The array of cell(s).
*/
function findTagCells(worksheet, range, endCell, tagChar) {
var cells = [];
if (!tagChar) {
return cells;
}
for (var ir = range.s.r; ir <= endCell.r; ++ir) {
for (var ic = range.s.c; ic <= range.e.c; ++ic) {
var cell_address = {
c: ic,
r: ir
};
z = XLSX.utils.encode_cell(cell_address);
if (!isTag(worksheet[z], tagChar)) {
continue;
}
var cell = factoryCell(cell_address, z, worksheet[z]);
cells.push(cell);
}
}
return cells;
}
/**
* Find the cells of title.
*
* @method findTitleCells
*
* @param {Object} worksheet
* @param {Object} range
* @param {Object} titleCell
* @param {Object} ignoreCells
*
* @return {Array} cells The cells of title.
*/
function findTitleCells(worksheet, range, titleCell, ignoreCells) {
var cells = [];
var ir = titleCell.r;
for (var ic = range.s.c; ic <= range.e.c; ++ic) {
var cell_address = {
c: ic,
r: ir
};
z = XLSX.utils.encode_cell(cell_address);
if (z == titleCell.z) {
continue;
}
var cell = factoryCell(cell_address, z, worksheet[z]);
// TODO: check the value is valid or not (only alphabet)
// check the cell is need to ignore or not
if (isNeedIgnore(cell, ignoreCells)) {
continue;
}
if (titleCell && cell.v && (cell.r == titleCell.r)) {
cells[ic] = cell;
}
}
return cells;
}
/**
* fetch all the attribute(s)
*
* @method fetchAttrJson
*
* @param {Object} ws
* @param {Array} attrCells
*
* @return {JSON} cells The JSON Object of attribute.
*/
function fetchAttrJson(ws, attrCells) {
var cells = {};
for (var cell of attrCells) {
var keyAddr = {
c: cell.c + 1,
r: cell.r
},
valAddr = {
c: cell.c + 2,
r: cell.r
};
var zKey = XLSX.utils.encode_cell(keyAddr),
zVal = XLSX.utils.encode_cell(valAddr);
cells[ws[zKey].v] = ws[zVal].v;
}
return cells;
}
/**
* fetch name of key
*
* @method fetchNameOfKeyJson
*
* @param {Object} ws
* @param {Array} cells
*
* @return {string} name of key.
*/
function fetchNameOfKeyJson(ws, cells) {
for (var cell of cells) {
var valAddr = {
c: cell.c + 1,
r: cell.r
};
var zVal = XLSX.utils.encode_cell(valAddr);
return ws[zVal].v;
}
}
/**
* fetch raw data(s)
*
* @method fetchRawData
*
* @param {Object} ws Object of worksheet
* @param {Object} range The range from XLSX.utils.decode_range
* @param {Object} titleCell
* @param {Array} titles
* @param {Array} ignoreCells
*
* @return {Array} rawDatas Array of Cells.
*/
function fetchRawData(ws, range, titleCell, titles, ignoreCells) {
var cells = [];
var rawDatas = [];
var cell_address = {};
for (var ir = titleCell.r + 1; ir <= range.e.r; ++ir) {
var data = {};
for (var ic = range.s.c; ic <= range.e.c; ++ic) {
cell_address = {
c: ic,
r: ir
};
z = XLSX.utils.encode_cell(cell_address);
var cell = factoryCell(cell_address, z, ws[z]);
// title row
if (cell.r == titleCell.r) {
continue;
}
// check the cell is need to ignore or not
if (isNeedIgnore(cell, ignoreCells)) {
continue;
}
// check title cell content
var t = titles[ic];
if (!t || !t.w) {
continue;
}
// save the data
data[t.w] = cell.w;
cells.push(cell);
}
if (_u.isEmpty(data)) {
continue;
}
rawDatas.push(data);
}
return rawDatas;
}
/**
* Parse the excel file and export the data with JSON format.
*
* @method parseSheet
*
* @param {string} filePath
* @param {string} sheetName
* @param {string} tagTitle
* @param {string} tagAttribute
* @param {string} tagNameOfKey
* @param {string} tagIgnore
*
* @return {JSON} result The data of JSON.
*/
function parseSheet(filePath, sheetName, tagTitle, tagAttribute, tagNameOfKey, tagIgnore) {
var ws = loadSheet(filePath, sheetName);
if (!ws) {
return;
}
var ref = ws['!ref'];
if (!ref) {
return;
}
var range = XLSX.utils.decode_range(ref);
// find all tag of title cells
var titleCell = findTitleTagCell(ws, range, tagTitle);
if (!titleCell) {
return;
}
// find all tag of attribute cells
var attrCells = findTagCells(ws, range, titleCell, tagAttribute);
// find all tag of ignore cells
var ignoreCells = findTagCells(ws, range, titleCell, tagIgnore);
// find all tag of name of key cells
var nameOfKeyCells = findTagCells(ws, range, titleCell, tagNameOfKey);
// find all titles with titleCell
var titles = findTitleCells(ws, range, titleCell, ignoreCells);
if (titles.length === 0) {
return;
}
// fetch all attribute cell(s)
var attrs = fetchAttrJson(ws, attrCells);
// fetch name of key
var nameOfKey = fetchNameOfKeyJson(ws, nameOfKeyCells);
// fetch all raw datas
var rawDatas = fetchRawData(ws, range, titleCell, titles, ignoreCells);
return {
nameOfKey: nameOfKey,
attrs: attrs,
rawDatas: rawDatas
};
}
/**
* Map the data to tempalte.
*
* @method map
*
* @param {Object} data
* @param {JSON} template
* @param {Boolean} isKeyVal
*
* @return {JSON} result Mapped data.
*/
function map(data, template, isKeyVal) {
var type = template.constructor;
var obj;
switch (type) {
case Array:
obj = [];
break;
case Object:
obj = {};
break;
default:
return data[template];
}
expr = /^\$/;
for (var key in template) {
if (!template.hasOwnProperty(key)) {
continue;
}
var isValKey = expr.test(key);
var keyVal = isValKey ? map(data, key.replace('$', '')) : key;
var val = template[key];
switch (val.constructor) {
case Array:
obj[keyVal] = [];
for (var index in val) {
var arrVal;
if (val.hasOwnProperty(index)) {
switch (val[index].constructor) {
case Array:
arrVal = map(data, val[index]);
break;
case Object:
arrVal = map(data, val[index]);
break;
default:
arrVal = data[val[index]];
break;
}
obj[keyVal].push(arrVal);
}
}
break;
case Object:
switch (type) {
case Array:
obj.push(map(data, val));
break;
case Object:
obj[keyVal] = map(data, val);
break;
}
break;
default:
switch (type) {
case Array:
obj.push(data[val]);
break;
case Object:
obj[keyVal] = data[val];
break;
}
break;
}
if (isKeyVal) {
obj._key = keyVal;
obj._val = obj[keyVal];
}
}
return obj;
}
/**
* Transform the raw data to the template JSON object.
*
* @method transform
*
* @param {Array} rawData
* @param {JSON} template
* @param {Boolean} isKeyVal
*
* @return {Array} result Array of JSON objects with template.
*/
function transform(rawData, template, isKeyVal) {
// init result object
var result = isKeyVal ? {} : [];
// foreach data to mapping to template
for (var index in rawData) {
if (!rawData.hasOwnProperty(index)) {
continue;
}
// mapping
var obj = map(rawData[index], template, isKeyVal);
if (isKeyVal) {
result[obj._key] = obj._val;
} else {
result.push(obj);
}
}
return result;
}
/**
* Parse the excel file and export the data with JSON format.
*
* @method parse
*
* @param {string} filePath
* @param {string} sheetName
* @param {JSON} template
* @param {Object} options
* @param {Boolean} options.useSheetname = false
* @param {Boolean} options.isKeyVal = false
* @param {string} options.tagTitle = '#'
* @param {string} options.tagAttribute = '^'
* @param {string} options.tagNameOfKey = '~'
* @param {string} options.tagIgnore = '!'
*
* @return {Object} The JSON Object of data
*/
function parse(filePath, sheetName, template, options) {
options = options || {};
nameOfKey = options.nameOfKey;
isAddSheetName = options.isAddSheetName || false;
isKeyVal = options.isKeyVal || false;
tagTitle = options.tagTitle || '#';
tagAttribute = options.tagAttribute || '^';
tagNameOfKey = options.tagNameOfKey || '~';
tagIgnore = options.tagIgnore || '!';
// check sheet name is empty or not
if (!sheetName) {
throw new Error("The sheet name is null or empty.");
}
// check template
template = template || {};
if (!_u.isObject(template) || _u.isArray(template)) {
throw new Error("The JSON template is invaild.");
}
// get data
var objJson = parseSheet(filePath, sheetName,
tagTitle, tagAttribute, tagNameOfKey, tagIgnore);
objJson = objJson || {
attrs: {},
rawDatas: []
};
// copy top-level attribute
var result = objJson.attrs;
if (isAddSheetName) {
result[DEFAULT_ATTR_SHEETNAME] = sheetName;
}
// transform the data if necessary
datas = _u.isEmpty(template) ? objJson.rawDatas : transform(objJson.rawDatas, template, isKeyVal);
if (isKeyVal) {
result = datas;
} else {
nameOfKey = nameOfKey ? nameOfKey : (objJson.nameOfKey || DEFAULT_ATTR_DATAS);
result[nameOfKey] = datas;
}
return result;
}
/**
* Save the JSON to the file.
*
* @method save
*
* @param {string} filePath
* @param {string} jsonObj
* @param {Object} options Set replacer for a JSON replacer[Options].
* @param {Function} callback
*
* @return
*/
function save(filePath, jsonObj, options, callback) {
if (!callback) {
callback = options;
options = {};
}
// check the output directory is exists or not, if not create it.
var dir = path.dirname(filePath);
fs.stat(dir, function (err, data) {
if (err) {
fs.mkdirSync(dir);
}
jsonfile.writeFile(filePath, jsonObj, options, function (err) {
if (callback) {
return callback(err);
}
});
});
}
/**
* Load json file.
*
* @method load
*
* @param {string} filePath
* @param {Function} callback
*
* @return
*/
function loadTemplate(filePath, callback) {
jsonfile.readFile(filePath, function (err, obj) {
if (callback) {
return callback(err, obj);
}
});
}
return {
save: save,
loadTemplate: loadTemplate,
parse: parse,
loadSheet: loadSheet,
factoryCell: factoryCell,
isNeedIgnore: isNeedIgnore,
isTag: isTag,
findTitleTagCell: findTitleTagCell,
findTagCells: findTagCells,
findTitleCells: findTitleCells,
parseSheet: parseSheet,
map: map,
transform: transform,
};
}());
module.exports = excel2jsontemplate;