-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathArrayMapping.ts
147 lines (127 loc) · 4.2 KB
/
ArrayMapping.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
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {AbsoluteCellRange} from '../AbsoluteCellRange'
import {addressKey, SimpleCellAddress} from '../Cell'
import {Maybe} from '../Maybe'
import {ColumnsSpan, RowsSpan} from '../Span'
import {ArrayVertex} from './'
export class ArrayMapping {
public readonly arrayMapping: Map<string, ArrayVertex> = new Map()
public getArray(range: AbsoluteCellRange): Maybe<ArrayVertex> {
const array = this.getArrayByCorner(range.start)
if (array?.getRange().sameAs(range)) {
return array
}
return
}
public getArrayByCorner(address: SimpleCellAddress): Maybe<ArrayVertex> {
return this.arrayMapping.get(addressKey(address))
}
public setArray(range: AbsoluteCellRange, vertex: ArrayVertex) {
this.arrayMapping.set(addressKey(range.start), vertex)
}
public removeArray(range: string | AbsoluteCellRange) {
if (typeof range === 'string') {
this.arrayMapping.delete(range)
} else {
this.arrayMapping.delete(addressKey(range.start))
}
}
public count(): number {
return this.arrayMapping.size
}
public* arraysInRows(rowsSpan: RowsSpan): IterableIterator<[string, ArrayVertex]> {
for (const [mtxKey, mtx] of this.arrayMapping.entries()) {
if (mtx.spansThroughSheetRows(rowsSpan.sheet, rowsSpan.rowStart, rowsSpan.rowEnd)) {
yield [mtxKey, mtx]
}
}
}
public* arraysInCols(col: ColumnsSpan): IterableIterator<[string, ArrayVertex]> {
for (const [mtxKey, mtx] of this.arrayMapping.entries()) {
if (mtx.spansThroughSheetColumn(col.sheet, col.columnStart, col.columnEnd)) {
yield [mtxKey, mtx]
}
}
}
public isFormulaArrayInRow(sheet: number, row: number): boolean {
for (const mtx of this.arrayMapping.values()) {
if (mtx.spansThroughSheetRows(sheet, row)) {
return true
}
}
return false
}
public isFormulaArrayInAllRows(span: RowsSpan): boolean {
let result = true
for (const row of span.rows()) {
if (!this.isFormulaArrayInRow(span.sheet, row)) {
result = false
}
}
return result
}
public isFormulaArrayInColumn(sheet: number, column: number): boolean {
for (const mtx of this.arrayMapping.values()) {
if (mtx.spansThroughSheetColumn(sheet, column)) {
return true
}
}
return false
}
public isFormulaArrayInAllColumns(span: ColumnsSpan): boolean {
let result = true
for (const col of span.columns()) {
if (!this.isFormulaArrayInColumn(span.sheet, col)) {
result = false
}
}
return result
}
public isFormulaArrayInRange(range: AbsoluteCellRange) {
for (const mtx of this.arrayMapping.values()) {
if (mtx.getRange().doesOverlap(range)) {
return true
}
}
return false
}
public isFormulaArrayAtAddress(address: SimpleCellAddress) {
for (const mtx of this.arrayMapping.values()) {
if (mtx.getRange().addressInRange(address)) {
return true
}
}
return false
}
public moveArrayVerticesAfterRowByRows(sheet: number, row: number, numberOfRows: number) {
this.updateArrayVerticesInSheet(sheet, (key: string, vertex: ArrayVertex) => {
const range = vertex.getRange()
return row <= range.start.row ? [range.shifted(0, numberOfRows), vertex] : undefined
})
}
public moveArrayVerticesAfterColumnByColumns(sheet: number, column: number, numberOfColumns: number) {
this.updateArrayVerticesInSheet(sheet, (key: string, vertex: ArrayVertex) => {
const range = vertex.getRange()
return column <= range.start.col ? [range.shifted(numberOfColumns, 0), vertex] : undefined
})
}
private updateArrayVerticesInSheet(sheet: number, fn: (key: string, vertex: ArrayVertex) => Maybe<[AbsoluteCellRange, ArrayVertex]>) {
const updated = Array<[AbsoluteCellRange, ArrayVertex]>()
for (const [key, vertex] of this.arrayMapping.entries()) {
if (vertex.sheet !== sheet) {
continue
}
const result = fn(key, vertex)
if (result !== undefined) {
this.removeArray(key)
updated.push(result)
}
}
updated.forEach(([range, array]) => {
this.setArray(range, array)
})
}
}