| 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 |
30x
30x
30x
30x
30x
67x
67x
2855x
2855x
2678x
2678x
55x
2678x
172x
2678x
55x
2678x
739x
67x
1504x
1504x
12x
1492x
1862x
1340x
152x
158x
1352x
19x
139x
413x
980x
375x
38x
141x
25x
116x
116x
381x
31x
85x
18x
18x
18x
264x
78x
186x
36x
18x
30x
30x
30x
294x
294x
294x
294x
294x
294x
2088x
948x
1140x
846x
30x
6x
24x
6x
18x
156x
156x
18x
18x
144x
18x
138x
138x
30x
30x
6x
30x
108x
66x
66x
66x
66x
732x
732x
732x
3564x
3564x
66x
66x
66x
102x
102x
102x
102x
156x
156x
156x
36x
156x
192x
66x
102x
102x
102x
18x
18x
66x
61x
2x
2x
1x
1x
2x
2x
1x
1x
30x
| /*
* Copyright (c) AXA Shared Services Spain S.A.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const XLSX = require('xlsx');
const XTable = require('./xtable');
const XTableUtils = require('./xtable-utils');
class XDoc {
constructor() {
this.tables = [];
this.tablesByName = {};
}
getRect(sheet) {
const keys = Object.keys(sheet);
let minRow;
let maxRow;
let minColumn;
let maxColumn;
for (let i = 0, l = keys.length; i < l; i += 1) {
const key = keys[i];
if (key[0] !== '!') {
const coord = XTableUtils.excel2coord(key);
if (minColumn === undefined || minColumn > coord.column) {
minColumn = coord.column;
}
if (maxColumn === undefined || maxColumn < coord.column) {
maxColumn = coord.column;
}
if (minRow === undefined || minRow > coord.row) {
minRow = coord.row;
}
if (maxRow === undefined || maxRow < coord.row) {
maxRow = coord.row;
}
}
}
return {
top: minRow,
bottom: maxRow,
left: minColumn,
right: maxColumn,
};
}
isEmptyRow(block, index) {
const row = block[index];
if (!row) {
return true;
}
for (let i = 0; i < row.length; i += 1) {
if (row[i]) {
return false;
}
}
return true;
}
findEmptyRow(block) {
for (let i = 0; i < block.length; i += 1) {
if (this.isEmptyRow(block, i)) {
return i;
}
}
return -1;
}
isEmptyColum(block, index) {
for (let i = 0; i < block.length; i += 1) {
if (block[i][index]) {
return false;
}
}
return true;
}
findEmptyColumn(block) {
if (!block || block.length === 0) {
return -1;
}
const l = block[0].length;
for (let i = 0; i < l; i += 1) {
if (this.isEmptyColum(block, i)) {
return i;
}
}
return -1;
}
splitByRow(block, emptyRowIndex, nextEmptyRowIndex) {
const block1 = [];
const block2 = [];
for (let i = 0; i < block.length; i += 1) {
if (i < emptyRowIndex) {
block1.push(block[i]);
} else if (i > nextEmptyRowIndex) {
block2.push(block[i]);
}
}
return [block1, block2];
}
splitByColumn(block, emptyColumnIndex, nextEmptyColumnIndex) {
const block1 = [];
const block2 = [];
for (let i = 0; i < block.length; i += 1) {
const row = block[i];
const row1 = [];
const row2 = [];
block1.push(row1);
block2.push(row2);
for (let j = 0; j < row.length; j += 1) {
if (j < emptyColumnIndex) {
row1.push(row[j]);
} else if (j > nextEmptyColumnIndex) {
row2.push(row[j]);
}
}
}
if (block2[0].length === 0) {
return [block1];
}
if (block1[0].length === 0) {
return [block2];
}
return [block1, block2];
}
splitBlock(block) {
const emptyRowIndex = this.findEmptyRow(block);
if (emptyRowIndex > -1) {
let nextEmptyRowIndex = emptyRowIndex;
while (
nextEmptyRowIndex < block.length &&
this.isEmptyRow(block, nextEmptyRowIndex + 1)
) {
nextEmptyRowIndex += 1;
}
return this.splitByRow(block, emptyRowIndex, nextEmptyRowIndex);
}
const emptyColumnIndex = this.findEmptyColumn(block);
if (emptyColumnIndex > -1) {
let nextEmptyColumnIndex = emptyColumnIndex;
while (
nextEmptyColumnIndex < block[0].length &&
this.isEmptyColum(block, nextEmptyColumnIndex + 1)
) {
nextEmptyColumnIndex += 1;
}
return this.splitByColumn(block, emptyColumnIndex, nextEmptyColumnIndex);
}
return [block];
}
processSheet(sheet) {
const rect = this.getRect(sheet);
let pendingBlocks = [];
let currentBlock = [];
for (let j = rect.top; j <= rect.bottom; j += 1) {
const currentRow = [];
currentBlock.push(currentRow);
for (let i = rect.left; i <= rect.right; i += 1) {
const cellRef = XTableUtils.coord2excel({ row: j, column: i });
currentRow.push(sheet[cellRef]);
}
}
pendingBlocks.push(currentBlock);
let modified = true;
while (modified) {
modified = false;
const oldBlocks = pendingBlocks;
pendingBlocks = [];
for (let i = 0; i < oldBlocks.length; i += 1) {
currentBlock = oldBlocks[i];
const newBlocks = this.splitBlock(currentBlock);
if (newBlocks.length > 1 && !modified) {
modified = true;
}
for (let j = 0; j < newBlocks.length; j += 1) {
pendingBlocks.push(newBlocks[j]);
}
}
}
for (let i = 0; i < pendingBlocks.length; i += 1) {
const table = new XTable(pendingBlocks[i]);
this.tables.push(table);
this.tablesByName[table.name] = table;
}
}
read(filename) {
const wb = XLSX.readFile(filename);
for (let i = 0, l = wb.SheetNames.length; i < l; i += 1) {
this.processSheet(wb.Sheets[wb.SheetNames[i]]);
}
}
getTable(name) {
return this.tablesByName[name];
}
find(name, query) {
const table = this.tablesByName[name];
if (!table) {
return [];
}
return table.find(query);
}
findOne(name, query) {
const table = this.tablesByName[name];
if (!table) {
return undefined;
}
return table.findOne(query);
}
}
module.exports = XDoc;
|