/
sheet.ts
1033 lines (944 loc) · 31.5 KB
/
sheet.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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { FORBIDDEN_IN_EXCEL_REGEX } from "../../constants";
import {
createDefaultRows,
deepCopy,
getUnquotedSheetName,
groupConsecutive,
includesAll,
isDefined,
isZoneInside,
isZoneValid,
range,
toCartesian,
} from "../../helpers/index";
import { _t } from "../../translation";
import {
Cell,
CellPosition,
Command,
CommandResult,
CoreCommand,
CreateSheetCommand,
Dimension,
ExcelWorkbookData,
FreezeColumnsCommand,
FreezeRowsCommand,
HeaderIndex,
PaneDivision,
RenameSheetCommand,
Row,
Sheet,
SheetData,
UID,
UnboundedZone,
UpdateCellPositionCommand,
WorkbookData,
Zone,
ZoneDimension,
} from "../../types/index";
import { CorePlugin } from "../core_plugin";
interface SheetState {
readonly sheets: Record<UID, Sheet | undefined>;
readonly orderedSheetIds: UID[];
readonly sheetIdsMapName: Record<string, UID | undefined>;
readonly cellPosition: Record<UID, CellPosition | undefined>;
}
export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
static getters = [
"getSheetName",
"tryGetSheetName",
"getSheet",
"tryGetSheet",
"getSheetIdByName",
"getSheetIds",
"getVisibleSheetIds",
"isSheetVisible",
"doesHeaderExist",
"doesHeadersExist",
"getCell",
"getCellPosition",
"getColsZone",
"getRowCells",
"getRowsZone",
"getNumberCols",
"getNumberRows",
"getNumberHeaders",
"getGridLinesVisibility",
"getNextSheetName",
"getSheetSize",
"getSheetZone",
"getPaneDivisions",
"checkZonesExistInSheet",
"getCommandZones",
"getUnboundedZone",
"checkElementsIncludeAllNonFrozenHeaders",
] as const;
readonly sheetIdsMapName: Record<string, UID | undefined> = {};
readonly orderedSheetIds: UID[] = [];
readonly sheets: Record<UID, Sheet | undefined> = {};
readonly cellPosition: Record<UID, CellPosition | undefined> = {};
// ---------------------------------------------------------------------------
// Command Handling
// ---------------------------------------------------------------------------
allowDispatch(cmd: CoreCommand) {
const genericChecks = this.chainValidations(
this.checkSheetExists,
this.checkZonesAreInSheet
)(cmd);
if (genericChecks !== CommandResult.Success) {
return genericChecks;
}
switch (cmd.type) {
case "HIDE_SHEET": {
if (this.getVisibleSheetIds().length === 1) {
return CommandResult.NotEnoughSheets;
}
return CommandResult.Success;
}
case "CREATE_SHEET": {
return this.checkValidations(cmd, this.checkSheetName, this.checkSheetPosition);
}
case "MOVE_SHEET":
try {
const currentIndex = this.orderedSheetIds.findIndex((id) => id === cmd.sheetId);
this.findIndexOfTargetSheet(currentIndex, cmd.delta);
return CommandResult.Success;
} catch (e) {
return CommandResult.WrongSheetMove;
}
case "RENAME_SHEET":
return this.isRenameAllowed(cmd);
case "DELETE_SHEET":
return this.orderedSheetIds.length > 1
? CommandResult.Success
: CommandResult.NotEnoughSheets;
case "ADD_COLUMNS_ROWS":
if (!this.doesHeaderExist(cmd.sheetId, cmd.dimension, cmd.base)) {
return CommandResult.InvalidHeaderIndex;
} else if (cmd.quantity <= 0) {
return CommandResult.InvalidQuantity;
}
return CommandResult.Success;
case "REMOVE_COLUMNS_ROWS": {
const min = Math.min(...cmd.elements);
const max = Math.max(...cmd.elements);
if (min < 0 || !this.doesHeaderExist(cmd.sheetId, cmd.dimension, max)) {
return CommandResult.InvalidHeaderIndex;
} else if (
this.checkElementsIncludeAllNonFrozenHeaders(cmd.sheetId, cmd.dimension, cmd.elements)
) {
return CommandResult.NotEnoughElements;
} else {
return CommandResult.Success;
}
}
case "FREEZE_ROWS": {
return this.checkValidations(
cmd,
this.checkRowFreezeQuantity,
this.checkRowFreezeOverlapMerge
);
}
case "FREEZE_COLUMNS": {
return this.checkValidations(
cmd,
this.checkColFreezeQuantity,
this.checkColFreezeOverlapMerge
);
}
default:
return CommandResult.Success;
}
}
handle(cmd: CoreCommand) {
switch (cmd.type) {
case "SET_GRID_LINES_VISIBILITY":
this.setGridLinesVisibility(cmd.sheetId, cmd.areGridLinesVisible);
break;
case "CREATE_SHEET":
const sheet = this.createSheet(
cmd.sheetId,
cmd.name || this.getNextSheetName(),
cmd.cols || 26,
cmd.rows || 100,
cmd.position
);
this.history.update("sheetIdsMapName", sheet.name, sheet.id);
break;
case "MOVE_SHEET":
this.moveSheet(cmd.sheetId, cmd.delta);
break;
case "RENAME_SHEET":
this.renameSheet(this.sheets[cmd.sheetId]!, cmd.name!);
break;
case "HIDE_SHEET":
this.hideSheet(cmd.sheetId);
break;
case "SHOW_SHEET":
this.showSheet(cmd.sheetId);
break;
case "DUPLICATE_SHEET":
this.duplicateSheet(cmd.sheetId, cmd.sheetIdTo);
break;
case "DELETE_SHEET":
this.deleteSheet(this.sheets[cmd.sheetId]!);
break;
case "REMOVE_COLUMNS_ROWS":
if (cmd.dimension === "COL") {
this.removeColumns(this.sheets[cmd.sheetId]!, [...cmd.elements]);
} else {
this.removeRows(this.sheets[cmd.sheetId]!, [...cmd.elements]);
}
break;
case "ADD_COLUMNS_ROWS":
if (cmd.dimension === "COL") {
this.addColumns(this.sheets[cmd.sheetId]!, cmd.base, cmd.position, cmd.quantity);
} else {
this.addRows(this.sheets[cmd.sheetId]!, cmd.base, cmd.position, cmd.quantity);
}
break;
case "UPDATE_CELL_POSITION":
this.updateCellPosition(cmd);
break;
case "FREEZE_COLUMNS":
this.setPaneDivisions(cmd.sheetId, cmd.quantity, "COL");
break;
case "FREEZE_ROWS":
this.setPaneDivisions(cmd.sheetId, cmd.quantity, "ROW");
break;
case "UNFREEZE_ROWS":
this.setPaneDivisions(cmd.sheetId, 0, "ROW");
break;
case "UNFREEZE_COLUMNS":
this.setPaneDivisions(cmd.sheetId, 0, "COL");
break;
case "UNFREEZE_COLUMNS_ROWS":
this.setPaneDivisions(cmd.sheetId, 0, "COL");
this.setPaneDivisions(cmd.sheetId, 0, "ROW");
}
}
// ---------------------------------------------------------------------------
// Import/Export
// ---------------------------------------------------------------------------
import(data: WorkbookData) {
// we need to fill the sheetIds mapping first, because otherwise formulas
// that depends on a sheet not already imported will not be able to be
// compiled
for (let sheet of data.sheets) {
this.sheetIdsMapName[sheet.name] = sheet.id;
}
for (let sheetData of data.sheets) {
const name = sheetData.name || _t("Sheet") + (Object.keys(this.sheets).length + 1);
const { colNumber, rowNumber } = this.getImportedSheetSize(sheetData);
const sheet: Sheet = {
id: sheetData.id,
name: name,
numberOfCols: colNumber,
rows: createDefaultRows(rowNumber),
areGridLinesVisible:
sheetData.areGridLinesVisible === undefined ? true : sheetData.areGridLinesVisible,
isVisible: sheetData.isVisible,
panes: {
xSplit: sheetData.panes?.xSplit || 0,
ySplit: sheetData.panes?.ySplit || 0,
},
};
this.orderedSheetIds.push(sheet.id);
this.sheets[sheet.id] = sheet;
}
}
private exportSheets(data: WorkbookData) {
data.sheets = this.orderedSheetIds.filter(isDefined).map((id) => {
const sheet = this.sheets[id]!;
const sheetData: SheetData = {
id: sheet.id,
name: sheet.name,
colNumber: sheet.numberOfCols,
rowNumber: this.getters.getNumberRows(sheet.id),
rows: {},
cols: {},
merges: [],
cells: {},
conditionalFormats: [],
figures: [],
tables: [],
areGridLinesVisible:
sheet.areGridLinesVisible === undefined ? true : sheet.areGridLinesVisible,
isVisible: sheet.isVisible,
};
if (sheet.panes.xSplit || sheet.panes.ySplit) {
sheetData.panes = sheet.panes;
}
return sheetData;
});
}
export(data: WorkbookData) {
this.exportSheets(data);
}
exportForExcel(data: ExcelWorkbookData) {
this.exportSheets(data);
}
// ---------------------------------------------------------------------------
// Getters
// ---------------------------------------------------------------------------
getGridLinesVisibility(sheetId: UID): boolean {
return this.getSheet(sheetId).areGridLinesVisible;
}
tryGetSheet(sheetId: UID): Sheet | undefined {
return this.sheets[sheetId];
}
getSheet(sheetId: UID): Sheet {
const sheet = this.sheets[sheetId];
if (!sheet) {
throw new Error(`Sheet ${sheetId} not found.`);
}
return sheet;
}
isSheetVisible(sheetId: UID): boolean {
return this.getSheet(sheetId).isVisible;
}
/**
* Return the sheet name. Throw if the sheet is not found.
*/
getSheetName(sheetId: UID): string {
return this.getSheet(sheetId).name;
}
/**
* Return the sheet name or undefined if the sheet doesn't exist.
*/
tryGetSheetName(sheetId: UID): string | undefined {
return this.tryGetSheet(sheetId)?.name;
}
getSheetIdByName(name: string | undefined): UID | undefined {
if (name) {
const unquotedName = getUnquotedSheetName(name);
for (const key in this.sheetIdsMapName) {
if (key.toUpperCase() === unquotedName.toUpperCase()) {
return this.sheetIdsMapName[key];
}
}
}
return undefined;
}
getSheetIds(): UID[] {
return this.orderedSheetIds;
}
getVisibleSheetIds(): UID[] {
return this.orderedSheetIds.filter(this.isSheetVisible.bind(this));
}
doesHeaderExist(sheetId: UID, dimension: Dimension, index: number) {
return dimension === "COL"
? index >= 0 && index < this.getNumberCols(sheetId)
: index >= 0 && index < this.getNumberRows(sheetId);
}
doesHeadersExist(sheetId: UID, dimension: Dimension, headerIndexes: HeaderIndex[]): boolean {
return headerIndexes.every((index) => this.doesHeaderExist(sheetId, dimension, index));
}
getCell({ sheetId, col, row }: CellPosition): Cell | undefined {
const sheet = this.tryGetSheet(sheetId);
const cellId = sheet?.rows[row]?.cells[col];
if (cellId === undefined) {
return undefined;
}
return this.getters.getCellById(cellId);
}
getColsZone(sheetId: UID, start: HeaderIndex, end: HeaderIndex): Zone {
return {
top: 0,
bottom: this.getNumberRows(sheetId) - 1,
left: start,
right: end,
};
}
getRowCells(sheetId: UID, row: HeaderIndex): UID[] {
return Object.values(this.getSheet(sheetId).rows[row]?.cells).filter(isDefined);
}
getRowsZone(sheetId: UID, start: HeaderIndex, end: HeaderIndex): Zone {
return {
top: start,
bottom: end,
left: 0,
right: this.getSheet(sheetId).numberOfCols - 1,
};
}
getCellPosition(cellId: UID): CellPosition {
const cell = this.cellPosition[cellId];
if (!cell) {
throw new Error(`asking for a cell position that doesn't exist, cell id: ${cellId}`);
}
return cell;
}
getNumberCols(sheetId: UID) {
return this.getSheet(sheetId).numberOfCols;
}
getNumberRows(sheetId: UID) {
return this.getSheet(sheetId).rows.length;
}
getNumberHeaders(sheetId: UID, dimension: Dimension) {
return dimension === "COL" ? this.getNumberCols(sheetId) : this.getNumberRows(sheetId);
}
getNextSheetName(baseName = "Sheet"): string {
let i = 1;
const names = this.orderedSheetIds.map(this.getSheetName.bind(this));
let name = `${baseName}${i}`;
while (names.includes(name)) {
name = `${baseName}${i}`;
i++;
}
return name;
}
getSheetSize(sheetId: UID): ZoneDimension {
return {
numberOfRows: this.getNumberRows(sheetId),
numberOfCols: this.getNumberCols(sheetId),
};
}
getSheetZone(sheetId: UID): Zone {
return {
top: 0,
left: 0,
bottom: this.getNumberRows(sheetId) - 1,
right: this.getNumberCols(sheetId) - 1,
};
}
getUnboundedZone(sheetId: UID, zone: Zone | UnboundedZone): UnboundedZone {
const isFullRow = zone.left === 0 && zone.right === this.getNumberCols(sheetId) - 1;
const isFullCol = zone.top === 0 && zone.bottom === this.getNumberRows(sheetId) - 1;
return {
...zone,
bottom: isFullCol ? undefined : zone.bottom,
// cannot be unbounded in the 2 dimensions at once
right: isFullRow && !isFullCol ? undefined : zone.right,
};
}
getPaneDivisions(sheetId: UID): Readonly<PaneDivision> {
return this.getSheet(sheetId).panes;
}
private setPaneDivisions(sheetId: UID, base: HeaderIndex, dimension: Dimension) {
const panes = { ...this.getPaneDivisions(sheetId) };
if (dimension === "COL") {
panes.xSplit = base;
} else if (dimension === "ROW") {
panes.ySplit = base;
}
this.history.update("sheets", sheetId, "panes", panes);
}
/**
* Checks if all non-frozen header indices are present in the provided elements of selected rows/columns.
* This validation ensures that all rows or columns cannot be deleted when frozen panes exist.
*/
checkElementsIncludeAllNonFrozenHeaders(
sheetId: UID,
dimension: Dimension,
elements: HeaderIndex[]
): boolean {
const paneDivisions = this.getters.getPaneDivisions(sheetId);
const startIndex = dimension === "ROW" ? paneDivisions.ySplit : paneDivisions.xSplit;
const endIndex = this.getters.getNumberHeaders(sheetId, dimension);
if (!startIndex) {
return false;
}
const indicesToCheck = range(startIndex, endIndex);
return includesAll(elements, indicesToCheck);
}
// ---------------------------------------------------------------------------
// Row/Col manipulation
// ---------------------------------------------------------------------------
getCommandZones(cmd: Command): Zone[] {
const zones: Zone[] = [];
if ("zone" in cmd) {
zones.push(cmd.zone);
}
if ("target" in cmd) {
zones.push(...cmd.target);
}
if ("ranges" in cmd) {
zones.push(
...cmd.ranges.map((rangeData) => this.getters.getRangeFromRangeData(rangeData).zone)
);
}
if ("col" in cmd && "row" in cmd) {
zones.push({ top: cmd.row, left: cmd.col, bottom: cmd.row, right: cmd.col });
}
return zones;
}
/**
* Check if zones in the command are well formed and
* not outside the sheet.
*/
checkZonesExistInSheet(sheetId: UID, zones: Zone[]): CommandResult {
if (!zones.every(isZoneValid)) return CommandResult.InvalidRange;
if (zones.length) {
const sheetZone = this.getSheetZone(sheetId);
return zones.every((zone) => isZoneInside(zone, sheetZone))
? CommandResult.Success
: CommandResult.TargetOutOfSheet;
}
return CommandResult.Success;
}
private updateCellPosition(cmd: UpdateCellPositionCommand) {
const { sheetId, cellId, col, row } = cmd;
if (cellId) {
this.setNewPosition(cellId, sheetId, col, row);
} else {
this.clearPosition(sheetId, col, row);
}
}
/**
* Set the cell at a new position and clear its previous position.
*/
private setNewPosition(cellId: UID, sheetId: UID, col: HeaderIndex, row: HeaderIndex) {
const currentPosition = this.cellPosition[cellId];
if (currentPosition) {
this.clearPosition(sheetId, currentPosition.col, currentPosition.row);
}
this.history.update("cellPosition", cellId, {
row: row,
col: col,
sheetId: sheetId,
});
this.history.update("sheets", sheetId, "rows", row, "cells", col, cellId);
}
/**
* Remove the cell at the given position (if there's one)
*/
private clearPosition(sheetId: UID, col: HeaderIndex, row: HeaderIndex) {
const cellId = this.sheets[sheetId]?.rows[row].cells[col];
if (cellId) {
this.history.update("cellPosition", cellId, undefined);
this.history.update("sheets", sheetId, "rows", row, "cells", col, undefined);
}
}
private setGridLinesVisibility(sheetId: UID, areGridLinesVisible: boolean) {
this.history.update("sheets", sheetId, "areGridLinesVisible", areGridLinesVisible);
}
private createSheet(
id: UID,
name: string,
colNumber: number,
rowNumber: number,
position: number
): Sheet {
const sheet: Sheet = {
id,
name,
numberOfCols: colNumber,
rows: createDefaultRows(rowNumber),
areGridLinesVisible: true,
isVisible: true,
panes: {
xSplit: 0,
ySplit: 0,
},
};
const orderedSheetIds = this.orderedSheetIds.slice();
orderedSheetIds.splice(position, 0, sheet.id);
const sheets = this.sheets;
this.history.update("orderedSheetIds", orderedSheetIds);
this.history.update("sheets", Object.assign({}, sheets, { [sheet.id]: sheet }));
return sheet;
}
private moveSheet(sheetId: UID, delta: number) {
const orderedSheetIds = this.orderedSheetIds.slice();
const currentIndex = orderedSheetIds.findIndex((id) => id === sheetId);
const sheet = orderedSheetIds.splice(currentIndex, 1);
let index = this.findIndexOfTargetSheet(currentIndex, delta);
orderedSheetIds.splice(index, 0, sheet[0]);
this.history.update("orderedSheetIds", orderedSheetIds);
}
private findIndexOfTargetSheet(currentIndex: HeaderIndex, deltaIndex: number): number {
while (deltaIndex != 0 && 0 <= currentIndex && currentIndex <= this.orderedSheetIds.length) {
if (deltaIndex > 0) {
currentIndex++;
if (this.isSheetVisible(this.orderedSheetIds[currentIndex])) {
deltaIndex--;
}
} else if (deltaIndex < 0) {
currentIndex--;
if (this.isSheetVisible(this.orderedSheetIds[currentIndex])) {
deltaIndex++;
}
}
}
if (deltaIndex === 0) {
return currentIndex;
}
throw new Error(_t("There is not enough visible sheets"));
}
private checkSheetName(cmd: RenameSheetCommand | CreateSheetCommand): CommandResult {
const originalSheetName = this.getters.tryGetSheetName(cmd.sheetId);
if (originalSheetName !== undefined && cmd.name === originalSheetName) {
return CommandResult.UnchangedSheetName;
}
const { orderedSheetIds, sheets } = this;
const name = cmd.name && cmd.name.trim().toLowerCase();
if (
orderedSheetIds.find((id) => sheets[id]?.name.toLowerCase() === name && id !== cmd.sheetId)
) {
return CommandResult.DuplicatedSheetName;
}
if (FORBIDDEN_IN_EXCEL_REGEX.test(name!)) {
return CommandResult.ForbiddenCharactersInSheetName;
}
return CommandResult.Success;
}
private checkSheetPosition(cmd: CreateSheetCommand) {
const { orderedSheetIds } = this;
if (cmd.position > orderedSheetIds.length || cmd.position < 0) {
return CommandResult.WrongSheetPosition;
}
return CommandResult.Success;
}
private checkRowFreezeQuantity(cmd: FreezeRowsCommand): CommandResult {
return cmd.quantity >= 1 && cmd.quantity < this.getNumberRows(cmd.sheetId)
? CommandResult.Success
: CommandResult.InvalidFreezeQuantity;
}
private checkColFreezeQuantity(cmd: FreezeColumnsCommand): CommandResult {
return cmd.quantity >= 1 && cmd.quantity < this.getNumberCols(cmd.sheetId)
? CommandResult.Success
: CommandResult.InvalidFreezeQuantity;
}
private checkRowFreezeOverlapMerge(cmd: FreezeRowsCommand): CommandResult {
const merges = this.getters.getMerges(cmd.sheetId);
for (let merge of merges) {
if (merge.top < cmd.quantity && cmd.quantity <= merge.bottom) {
return CommandResult.MergeOverlap;
}
}
return CommandResult.Success;
}
private checkColFreezeOverlapMerge(cmd: FreezeColumnsCommand): CommandResult {
const merges = this.getters.getMerges(cmd.sheetId);
for (let merge of merges) {
if (merge.left < cmd.quantity && cmd.quantity <= merge.right) {
return CommandResult.MergeOverlap;
}
}
return CommandResult.Success;
}
private isRenameAllowed(cmd: RenameSheetCommand): CommandResult {
const name = cmd.name && cmd.name.trim().toLowerCase();
if (!name) {
return CommandResult.MissingSheetName;
}
return this.checkSheetName(cmd);
}
private renameSheet(sheet: Sheet, name: string) {
const oldName = sheet.name;
this.history.update("sheets", sheet.id, "name", name.trim());
const sheetIdsMapName = Object.assign({}, this.sheetIdsMapName);
delete sheetIdsMapName[oldName];
sheetIdsMapName[name] = sheet.id;
this.history.update("sheetIdsMapName", sheetIdsMapName);
}
private hideSheet(sheetId: UID) {
this.history.update("sheets", sheetId, "isVisible", false);
}
private showSheet(sheetId: UID) {
this.history.update("sheets", sheetId, "isVisible", true);
}
private duplicateSheet(fromId: UID, toId: UID) {
const sheet = this.getSheet(fromId);
const toName = this.getDuplicateSheetName(sheet.name);
const newSheet: Sheet = deepCopy(sheet);
newSheet.id = toId;
newSheet.name = toName;
for (let col = 0; col <= newSheet.numberOfCols; col++) {
for (let row = 0; row <= newSheet.rows.length; row++) {
if (newSheet.rows[row]) {
newSheet.rows[row].cells[col] = undefined;
}
}
}
const orderedSheetIds = this.orderedSheetIds.slice();
const currentIndex = orderedSheetIds.indexOf(fromId);
orderedSheetIds.splice(currentIndex + 1, 0, newSheet.id);
this.history.update("orderedSheetIds", orderedSheetIds);
this.history.update("sheets", Object.assign({}, this.sheets, { [newSheet.id]: newSheet }));
for (const cell of Object.values(this.getters.getCells(fromId))) {
const { col, row } = this.getCellPosition(cell.id);
this.dispatch("UPDATE_CELL", {
sheetId: newSheet.id,
col,
row,
content: cell.content,
format: cell.format,
style: cell.style,
});
}
const sheetIdsMapName = Object.assign({}, this.sheetIdsMapName);
sheetIdsMapName[newSheet.name] = newSheet.id;
this.history.update("sheetIdsMapName", sheetIdsMapName);
}
private getDuplicateSheetName(sheetName: string) {
let i = 1;
const names = this.orderedSheetIds.map(this.getSheetName.bind(this));
const baseName = _t("Copy of %s", sheetName);
let name = baseName.toString();
while (names.includes(name)) {
name = `${baseName} (${i})`;
i++;
}
return name;
}
private deleteSheet(sheet: Sheet) {
const name = sheet.name;
const sheets = Object.assign({}, this.sheets);
delete sheets[sheet.id];
this.history.update("sheets", sheets);
const orderedSheetIds = this.orderedSheetIds.slice();
const currentIndex = orderedSheetIds.indexOf(sheet.id);
orderedSheetIds.splice(currentIndex, 1);
this.history.update("orderedSheetIds", orderedSheetIds);
const sheetIdsMapName = Object.assign({}, this.sheetIdsMapName);
delete sheetIdsMapName[name];
this.history.update("sheetIdsMapName", sheetIdsMapName);
}
/**
* Delete column. This requires a lot of handling:
* - Update all the formulas in all sheets
* - Move the cells
* - Update the cols/rows (size, number, (cells), ...)
* - Reevaluate the cells
*
* @param sheet ID of the sheet on which deletion should be applied
* @param columns Columns to delete
*/
private removeColumns(sheet: Sheet, columns: HeaderIndex[]) {
// This is necessary because we have to delete elements in correct order:
// begin with the end.
columns.sort((a, b) => b - a);
for (let column of columns) {
// Move the cells.
this.moveCellOnColumnsDeletion(sheet, column);
}
const numberOfCols = this.sheets[sheet.id]!.numberOfCols;
this.history.update("sheets", sheet.id, "numberOfCols", numberOfCols - columns.length);
const count = columns.filter((col) => col < sheet.panes.xSplit).length;
if (count) {
this.setPaneDivisions(sheet.id, sheet.panes.xSplit - count, "COL");
}
}
/**
* Delete row. This requires a lot of handling:
* - Update the merges
* - Update all the formulas in all sheets
* - Move the cells
* - Update the cols/rows (size, number, (cells), ...)
* - Reevaluate the cells
*
* @param sheet ID of the sheet on which deletion should be applied
* @param rows Rows to delete
*/
private removeRows(sheet: Sheet, rows: HeaderIndex[]) {
// This is necessary because we have to delete elements in correct order:
// begin with the end.
rows.sort((a, b) => b - a);
for (let group of groupConsecutive(rows)) {
// indexes are sorted in the descending order
const from = group[group.length - 1];
const to = group[0];
// Move the cells.
this.moveCellOnRowsDeletion(sheet, from, to);
// Effectively delete the rows
this.updateRowsStructureOnDeletion(sheet, from, to);
}
const count = rows.filter((row) => row < sheet.panes.ySplit).length;
if (count) {
this.setPaneDivisions(sheet.id, sheet.panes.ySplit - count, "ROW");
}
}
private addColumns(
sheet: Sheet,
column: HeaderIndex,
position: "before" | "after",
quantity: number
) {
const index = position === "before" ? column : column + 1;
// Move the cells.
this.moveCellsOnAddition(sheet, index, quantity, "columns");
const numberOfCols = this.sheets[sheet.id]!.numberOfCols;
this.history.update("sheets", sheet.id, "numberOfCols", numberOfCols + quantity);
if (index < sheet.panes.xSplit) {
this.setPaneDivisions(sheet.id, sheet.panes.xSplit + quantity, "COL");
}
}
private addRows(sheet: Sheet, row: HeaderIndex, position: "before" | "after", quantity: number) {
const index = position === "before" ? row : row + 1;
this.addEmptyRows(sheet, quantity);
// Move the cells.
this.moveCellsOnAddition(sheet, index, quantity, "rows");
if (index < sheet.panes.ySplit) {
this.setPaneDivisions(sheet.id, sheet.panes.ySplit + quantity, "ROW");
}
}
private moveCellOnColumnsDeletion(sheet: Sheet, deletedColumn: number) {
for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
for (let i in row.cells) {
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
if (colIndex === deletedColumn) {
this.dispatch("CLEAR_CELL", {
sheetId: sheet.id,
col: colIndex,
row: rowIndex,
});
}
if (colIndex > deletedColumn) {
this.dispatch("UPDATE_CELL_POSITION", {
sheetId: sheet.id,
cellId: cellId,
col: colIndex - 1,
row: rowIndex,
});
}
}
}
}
}
/**
* Move the cells after a column or rows insertion
*/
private moveCellsOnAddition(
sheet: Sheet,
addedElement: HeaderIndex,
quantity: number,
dimension: "rows" | "columns"
) {
const commands: UpdateCellPositionCommand[] = [];
for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
if (dimension !== "rows" || rowIndex >= addedElement) {
for (let i in row.cells) {
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
if (dimension === "rows" || colIndex >= addedElement) {
commands.push({
type: "UPDATE_CELL_POSITION",
sheetId: sheet.id,
cellId: cellId,
col: colIndex + (dimension === "columns" ? quantity : 0),
row: rowIndex + (dimension === "rows" ? quantity : 0),
});
}
}
}
}
}
for (let cmd of commands.reverse()) {
this.dispatch(cmd.type, cmd);
}
}
/**
* Move all the cells that are from the row under `deleteToRow` up to `deleteFromRow`
*
* b.e.
* move vertically with delete from 3 and delete to 5 will first clear all the cells from lines 3 to 5,
* then take all the row starting at index 6 and add them back at index 3
*
*/
private moveCellOnRowsDeletion(
sheet: Sheet,
deleteFromRow: HeaderIndex,
deleteToRow: HeaderIndex
) {
const numberRows = deleteToRow - deleteFromRow + 1;
for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
if (rowIndex >= deleteFromRow && rowIndex <= deleteToRow) {
for (let i in row.cells) {
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
this.dispatch("CLEAR_CELL", {
sheetId: sheet.id,
col: colIndex,
row: rowIndex,
});
}
}
}
if (rowIndex > deleteToRow) {
for (let i in row.cells) {
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
this.dispatch("UPDATE_CELL_POSITION", {
sheetId: sheet.id,
cellId: cellId,
col: colIndex,
row: rowIndex - numberRows,
});
}
}
}
}
}
private updateRowsStructureOnDeletion(
sheet: Sheet,
deleteFromRow: HeaderIndex,
deleteToRow: HeaderIndex
) {
const rows: Row[] = [];
const cellsQueue = sheet.rows.map((row) => row.cells).reverse();
for (let i in sheet.rows) {
const row = Number(i);
if (row >= deleteFromRow && row <= deleteToRow) {
continue;
}
rows.push({
cells: cellsQueue.pop()!,
});
}
this.history.update("sheets", sheet.id, "rows", rows);
}
/**
* Add empty rows at the end of the rows
*
* @param sheet Sheet
* @param quantity Number of rows to add
*/
private addEmptyRows(sheet: Sheet, quantity: number) {
const rows: Row[] = sheet.rows.slice();
for (let i = 0; i < quantity; i++) {
rows.push({
cells: {},
});
}
this.history.update("sheets", sheet.id, "rows", rows);
}
private getImportedSheetSize(data: SheetData): { rowNumber: number; colNumber: number } {