-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathAbsoluteCellRange.ts
541 lines (454 loc) · 16.8 KB
/
AbsoluteCellRange.ts
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
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {
CellRange,
equalSimpleCellAddress,
isSimpleCellAddress,
simpleCellAddress,
SimpleCellAddress,
SimpleColumnAddress,
SimpleRowAddress
} from './Cell'
import {DependencyGraph} from './DependencyGraph'
import {SheetsNotEqual} from './errors'
import {Maybe} from './Maybe'
import {AstNodeType, CellRangeAst} from './parser'
import {ColumnRangeAst, RowRangeAst} from './parser/Ast'
import {RowsSpan, Span} from './Span'
export const WRONG_RANGE_SIZE = 'AbsoluteCellRange: Wrong range size'
export interface SimpleCellRange {
start: SimpleCellAddress,
end: SimpleCellAddress,
}
export function isSimpleCellRange(obj: any): obj is SimpleCellRange {
if (obj && (typeof obj === 'object' || typeof obj === 'function')) {
return 'start' in obj && isSimpleCellAddress(obj.start) && 'end' in obj && isSimpleCellAddress(obj.end)
} else {
return false
}
}
export const simpleCellRange = (start: SimpleCellAddress, end: SimpleCellAddress) => ({start, end})
export class AbsoluteCellRange implements SimpleCellRange {
public readonly start: SimpleCellAddress
public readonly end: SimpleCellAddress
constructor(
start: SimpleCellAddress,
end: SimpleCellAddress,
) {
if (start.sheet !== end.sheet) {
throw new SheetsNotEqual(start.sheet, end.sheet)
}
this.start = simpleCellAddress(start.sheet, start.col, start.row)
this.end = simpleCellAddress(end.sheet, end.col, end.row)
}
public get sheet() {
return this.start.sheet
}
public static fromSimpleCellAddresses(start: SimpleCellAddress, end: SimpleCellAddress): AbsoluteCellRange {
if (start.sheet !== end.sheet) {
throw new SheetsNotEqual(start.sheet, end.sheet)
}
const width = end.col - start.col
const height = end.row - start.row
if (Number.isFinite(height) && Number.isFinite(width)) {
return new AbsoluteCellRange(start, end)
}
if (Number.isFinite(height)) {
return new AbsoluteRowRange(start.sheet, start.row, end.row)
}
return new AbsoluteColumnRange(start.sheet, start.col, end.col)
}
public static fromAst(ast: CellRangeAst | ColumnRangeAst | RowRangeAst, baseAddress: SimpleCellAddress): AbsoluteCellRange {
if (ast.type === AstNodeType.CELL_RANGE) {
return AbsoluteCellRange.fromCellRange(ast, baseAddress)
} else if (ast.type === AstNodeType.COLUMN_RANGE) {
return AbsoluteColumnRange.fromColumnRange(ast, baseAddress)
} else {
return AbsoluteRowRange.fromRowRangeAst(ast, baseAddress)
}
}
public static fromAstOrUndef(ast: CellRangeAst | ColumnRangeAst | RowRangeAst, baseAddress: SimpleCellAddress): Maybe<AbsoluteCellRange> {
try {
return AbsoluteCellRange.fromAst(ast, baseAddress)
} catch (_e) {
return undefined
}
}
public static fromCellRange(x: CellRange, baseAddress: SimpleCellAddress): AbsoluteCellRange {
return new AbsoluteCellRange(
x.start.toSimpleCellAddress(baseAddress),
x.end.toSimpleCellAddress(baseAddress),
)
}
public static spanFrom(topLeftCorner: SimpleCellAddress, width: number, height: number): AbsoluteCellRange {
const ret = AbsoluteCellRange.spanFromOrUndef(topLeftCorner, width, height)
if (ret === undefined) {
throw new Error(WRONG_RANGE_SIZE)
}
return ret
}
public static spanFromOrUndef(topLeftCorner: SimpleCellAddress, width: number, height: number): Maybe<AbsoluteCellRange> {
if (!Number.isFinite(width) && Number.isFinite(height)) {
if (topLeftCorner.col !== 0) {
return undefined
}
return new AbsoluteRowRange(topLeftCorner.sheet, topLeftCorner.row, topLeftCorner.row + height - 1)
} else if (!Number.isFinite(height) && Number.isFinite(width)) {
if (topLeftCorner.row !== 0) {
return undefined
}
return new AbsoluteColumnRange(topLeftCorner.sheet, topLeftCorner.col, topLeftCorner.col + width - 1)
} else if (Number.isFinite(height) && Number.isFinite(width)) {
return new AbsoluteCellRange(
topLeftCorner,
simpleCellAddress(topLeftCorner.sheet, topLeftCorner.col + width - 1, topLeftCorner.row + height - 1),
)
}
return undefined
}
public static fromCoordinates(sheet: number, x1: number, y1: number, x2: number, y2: number): AbsoluteCellRange {
return new AbsoluteCellRange(simpleCellAddress(sheet, x1, y1), simpleCellAddress(sheet, x2, y2))
}
public isFinite(): boolean {
return Number.isFinite(this.size())
}
public doesOverlap(other: AbsoluteCellRange): boolean {
if (this.start.sheet != other.start.sheet) {
return false
}
if (this.end.row < other.start.row || this.start.row > other.end.row) {
return false
}
if (this.end.col < other.start.col || this.start.col > other.end.col) {
return false
}
return true
}
public addressInRange(address: SimpleCellAddress): boolean {
if (this.sheet !== address.sheet) {
return false
}
return this.start.row <= address.row
&& this.end.row >= address.row
&& this.start.col <= address.col
&& this.end.col >= address.col
}
public columnInRange(address: SimpleColumnAddress): boolean {
if (this.sheet !== address.sheet) {
return false
}
return this.start.col <= address.col && this.end.col >= address.col
}
public rowInRange(address: SimpleRowAddress): boolean {
if (this.sheet !== address.sheet) {
return false
}
return this.start.row <= address.row && this.end.row >= address.row
}
public containsRange(range: AbsoluteCellRange): boolean {
return this.addressInRange(range.start) && this.addressInRange(range.end)
}
public intersectionWith(other: AbsoluteCellRange): Maybe<AbsoluteCellRange> {
if (this.sheet !== other.start.sheet) {
return undefined
}
const startRow = Math.max(this.start.row, other.start.row)
const endRow = Math.min(this.end.row, other.end.row)
const startCol = Math.max(this.start.col, other.start.col)
const endCol = Math.min(this.end.col, other.end.col)
if (startRow > endRow || startCol > endCol) {
return undefined
}
return new AbsoluteCellRange(
simpleCellAddress(this.sheet, startCol, startRow),
simpleCellAddress(this.sheet, endCol, endRow),
)
}
public includesRow(row: number): boolean {
return this.start.row < row && this.end.row >= row
}
public includesColumn(column: number): boolean {
return this.start.col < column && this.end.col >= column
}
public shiftByRows(numberOfRows: number) {
this.start.row += numberOfRows
this.end.row += numberOfRows
}
public expandByRows(numberOfRows: number) {
this.end.row += numberOfRows
}
public shiftByColumns(numberOfColumns: number) {
this.start.col += numberOfColumns
this.end.col += numberOfColumns
}
public shifted(byCols: number, byRows: number): AbsoluteCellRange {
return AbsoluteCellRange.spanFrom(simpleCellAddress(this.sheet, this.start.col + byCols, this.start.row + byRows), this.width(), this.height())
}
public expandByColumns(numberOfColumns: number) {
this.end.col += numberOfColumns
}
public moveToSheet(toSheet: number) {
this.start.sheet = toSheet
this.end.sheet = toSheet
}
public removeSpan(span: Span) {
if (span instanceof RowsSpan) {
this.removeRows(span.start, span.end)
} else {
this.removeColumns(span.start, span.end)
}
}
public shouldBeRemoved(): boolean {
return this.width() <= 0 || this.height() <= 0
}
public rangeWithSameWidth(startRow: number, numberOfRows: number): AbsoluteCellRange {
return AbsoluteCellRange.spanFrom(simpleCellAddress(this.sheet, this.start.col, startRow), this.width(), numberOfRows)
}
public rangeWithSameHeight(startColumn: number, numberOfColumns: number): AbsoluteCellRange {
return AbsoluteCellRange.spanFrom(simpleCellAddress(this.sheet, startColumn, this.start.row), numberOfColumns, this.height())
}
public toString(): string {
return `${this.start.sheet},${this.start.col},${this.start.row},${this.end.col},${this.end.row}`
}
public width(): number {
return this.end.col - this.start.col + 1
}
public height(): number {
return this.end.row - this.start.row + 1
}
public size(): number {
return this.height() * this.width()
}
public arrayOfAddressesInRange(): SimpleCellAddress[][] {
const result: SimpleCellAddress[][] = []
for (let y = 0; y < this.height(); ++y) {
result[y] = []
for (let x = 0; x < this.width(); ++x) {
const value = simpleCellAddress(this.sheet, this.start.col + x, this.start.row + y)
result[y].push(value)
}
}
return result
}
public withStart(newStart: SimpleCellAddress): AbsoluteCellRange {
return new AbsoluteCellRange(newStart, this.end)
}
public sameDimensionsAs(other: AbsoluteCellRange) {
return this.width() === other.width() && this.height() === other.height()
}
public sameAs(other: AbsoluteCellRange) {
return equalSimpleCellAddress(this.start, other.start) && equalSimpleCellAddress(this.end, other.end)
}
public addressesArrayMap<T>(dependencyGraph: DependencyGraph, op: (arg: SimpleCellAddress) => T): T[][] {
const ret = []
let currentRow = this.start.row
while (currentRow <= this.effectiveEndRow(dependencyGraph)) {
let currentColumn = this.start.col
const tmp = []
while (currentColumn <= this.effectiveEndColumn(dependencyGraph)) {
tmp.push(op(simpleCellAddress(this.start.sheet, currentColumn, currentRow)))
currentColumn++
}
ret.push(tmp)
currentRow++
}
return ret
}
public addresses(dependencyGraph: DependencyGraph): SimpleCellAddress[] {
const ret = []
let currentRow = this.start.row
const limitRow = this.effectiveEndRow(dependencyGraph)
const limitColumn = this.effectiveEndColumn(dependencyGraph)
while (currentRow <= limitRow) {
let currentColumn = this.start.col
while (currentColumn <= limitColumn) {
ret.push(simpleCellAddress(this.start.sheet, currentColumn, currentRow))
currentColumn++
}
currentRow++
}
return ret
}
public* addressesWithDirection(right: number, bottom: number, dependencyGraph: DependencyGraph): IterableIterator<SimpleCellAddress> {
if (right > 0) {
if (bottom > 0) {
let currentRow = this.effectiveEndRow(dependencyGraph)
while (currentRow >= this.start.row) {
let currentColumn = this.effectiveEndColumn(dependencyGraph)
while (currentColumn >= this.start.col) {
yield simpleCellAddress(this.start.sheet, currentColumn, currentRow)
currentColumn -= 1
}
currentRow -= 1
}
} else {
let currentRow = this.start.row
while (currentRow <= this.effectiveEndRow(dependencyGraph)) {
let currentColumn = this.effectiveEndColumn(dependencyGraph)
while (currentColumn >= this.start.col) {
yield simpleCellAddress(this.start.sheet, currentColumn, currentRow)
currentColumn -= 1
}
currentRow += 1
}
}
} else {
if (bottom > 0) {
let currentRow = this.effectiveEndRow(dependencyGraph)
while (currentRow >= this.start.row) {
let currentColumn = this.start.col
while (currentColumn <= this.effectiveEndColumn(dependencyGraph)) {
yield simpleCellAddress(this.start.sheet, currentColumn, currentRow)
currentColumn += 1
}
currentRow -= 1
}
} else {
let currentRow = this.start.row
while (currentRow <= this.effectiveEndRow(dependencyGraph)) {
let currentColumn = this.start.col
while (currentColumn <= this.effectiveEndColumn(dependencyGraph)) {
yield simpleCellAddress(this.start.sheet, currentColumn, currentRow)
currentColumn += 1
}
currentRow += 1
}
}
}
}
public getAddress(col: number, row: number): SimpleCellAddress {
if (col < 0 || row < 0 || row > this.height() - 1 || col > this.width() - 1) {
throw Error('Index out of bound')
}
return simpleCellAddress(this.start.sheet, this.start.col + col, this.start.row + row)
}
public exceedsSheetSizeLimits(maxColumns: number, maxRows: number): boolean {
return this.end.col >= maxColumns || this.end.row >= maxRows
}
public effectiveEndColumn(_dependencyGraph: DependencyGraph): number {
return this.end.col
}
public effectiveEndRow(_dependencyGraph: DependencyGraph): number {
return this.end.row
}
public effectiveWidth(_dependencyGraph: DependencyGraph): number {
return this.width()
}
public effectiveHeight(_dependencyGraph: DependencyGraph): number {
return this.height()
}
protected removeRows(rowStart: number, rowEnd: number) {
if (rowStart > this.end.row) {
return
}
if (rowEnd < this.start.row) {
const numberOfRows = rowEnd - rowStart + 1
return this.shiftByRows(-numberOfRows)
}
if (rowStart <= this.start.row) {
this.start.row = rowStart
}
this.end.row -= Math.min(rowEnd, this.end.row) - rowStart + 1
}
protected removeColumns(columnStart: number, columnEnd: number) {
if (columnStart > this.end.col) {
return
}
if (columnEnd < this.start.col) {
const numberOfColumns = columnEnd - columnStart + 1
return this.shiftByColumns(-numberOfColumns)
}
if (columnStart <= this.start.col) {
this.start.col = columnStart
}
this.end.col -= Math.min(columnEnd, this.end.col) - columnStart + 1
}
}
export class AbsoluteColumnRange extends AbsoluteCellRange {
constructor(sheet: number, columnStart: number, columnEnd: number) {
super(
simpleCellAddress(sheet, columnStart, 0),
simpleCellAddress(sheet, columnEnd, Number.POSITIVE_INFINITY),
)
}
public static fromColumnRange(x: ColumnRangeAst, baseAddress: SimpleCellAddress): AbsoluteColumnRange {
const start = x.start.toSimpleColumnAddress(baseAddress)
const end = x.end.toSimpleColumnAddress(baseAddress)
if (start.sheet !== end.sheet) {
throw new SheetsNotEqual(start.sheet, end.sheet)
}
return new AbsoluteColumnRange(start.sheet, start.col, end.col)
}
public shouldBeRemoved() {
return this.width() <= 0
}
public shiftByRows(_numberOfRows: number) {
return
}
public expandByRows(_numberOfRows: number) {
return
}
public shifted(byCols: number, _byRows: number): AbsoluteCellRange {
return new AbsoluteColumnRange(this.sheet, this.start.col + byCols, this.end.col + byCols)
}
public rangeWithSameHeight(startColumn: number, numberOfColumns: number): AbsoluteCellRange {
return new AbsoluteColumnRange(this.sheet, startColumn, startColumn + numberOfColumns - 1)
}
public exceedsSheetSizeLimits(maxColumns: number, _maxRows: number): boolean {
return this.end.col >= maxColumns
}
public effectiveEndRow(dependencyGraph: DependencyGraph): number {
return this.effectiveHeight(dependencyGraph) - 1
}
public effectiveHeight(dependencyGraph: DependencyGraph): number {
return dependencyGraph.getSheetHeight(this.sheet)
}
protected removeRows(_rowStart: number, _rowEnd: number) {
return
}
}
export class AbsoluteRowRange extends AbsoluteCellRange {
constructor(sheet: number, rowStart: number, rowEnd: number) {
super(
simpleCellAddress(sheet, 0, rowStart),
simpleCellAddress(sheet, Number.POSITIVE_INFINITY, rowEnd),
)
}
public static fromRowRangeAst(x: RowRangeAst, baseAddress: SimpleCellAddress): AbsoluteRowRange {
const start = x.start.toSimpleRowAddress(baseAddress)
const end = x.end.toSimpleRowAddress(baseAddress)
if (start.sheet !== end.sheet) {
throw new SheetsNotEqual(start.sheet, end.sheet)
}
return new AbsoluteRowRange(start.sheet, start.row, end.row)
}
public shouldBeRemoved() {
return this.height() <= 0
}
public shiftByColumns(_numberOfColumns: number) {
return
}
public expandByColumns(_numberOfColumns: number) {
return
}
public shifted(byCols: number, byRows: number): AbsoluteCellRange {
return new AbsoluteRowRange(this.sheet, this.start.row + byRows, this.end.row + byRows)
}
public rangeWithSameWidth(startRow: number, numberOfRows: number): AbsoluteCellRange {
return new AbsoluteRowRange(this.sheet, startRow, startRow + numberOfRows - 1)
}
public exceedsSheetSizeLimits(_maxColumns: number, maxRows: number): boolean {
return this.end.row >= maxRows
}
public effectiveEndColumn(dependencyGraph: DependencyGraph): number {
return this.effectiveWidth(dependencyGraph) - 1
}
public effectiveWidth(dependencyGraph: DependencyGraph): number {
return dependencyGraph.getSheetWidth(this.sheet)
}
protected removeColumns(_columnStart: number, _columnEnd: number) {
return
}
}