-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathworkbook.ts
2154 lines (2018 loc) · 88.4 KB
/
workbook.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 { Component, Property, NotifyPropertyChanges, INotifyPropertyChanged, Collection, Complex, EmitType } from '@syncfusion/ej2-base';
import { initSheet, getSheet, getSheetIndexFromId, getSheetIndex, Sheet, moveSheet, duplicateSheet } from './sheet';
import { Event, ModuleDeclaration, merge, L10n, isNullOrUndefined } from '@syncfusion/ej2-base';
import { WorkbookModel } from './workbook-model';
import { getWorkbookRequiredModules } from '../common/module';
import { SheetModel, CellModel, ColumnModel, RowModel, getData, RangeModel, isHiddenRow, isHiddenCol, OpenSettingsModel } from './index';
import { OpenOptions, BeforeOpenEventArgs, OpenFailureArgs, UndoRedoEventArgs } from '../../spreadsheet/common/interface';
import { DefineName, CellStyle, updateRowColCount, getIndexesFromAddress, localeData, workbookLocale, getUpdatedRange } from '../common/index';
import { BorderType, getSheetIndexFromAddress, CalculationMode } from '../common/index';
import * as events from '../common/event';
import { CellStyleModel, DefineNameModel, HyperlinkModel, insertModel, InsertDeleteModelArgs, getAddressInfo } from '../common/index';
import { setCellFormat, sheetCreated, deleteModel, ModelType, ProtectSettingsModel, ValidationModel, setLockCells } from '../common/index';
import { BeforeSaveEventArgs, SaveCompleteEventArgs, BeforeCellFormatArgs, UnprotectArgs, ExtendedRange, SerializationOptions } from '../common/interface';
import { SaveOptions, SetCellFormatArgs, ClearOptions, AutoFillSettings, AutoFillDirection, AutoFillType, dateToInt } from '../common/index';
import { SortOptions, BeforeSortEventArgs, SortEventArgs, FindOptions, CellInfoEventArgs, ConditionalFormatModel } from '../common/index';
import { FilterEventArgs, FilterOptions, BeforeFilterEventArgs, ChartModel, getCellIndexes, getCellAddress } from '../common/index';
import { setMerge, MergeType, MergeArgs, ImageModel, FilterCollectionModel, SortCollectionModel, dataChanged } from '../common/index';
import { getCell, skipDefaultValue, setCell, wrap as wrapText, OpenSettings } from './cell';
import { DataBind, setRow, setColumn, InsertDeleteEventArgs, NumberFormatArgs, parseLocaleNumber, refreshRibbonIcons } from '../index';
import { WorkbookSave, WorkbookFormula, WorkbookOpen, WorkbookSort, WorkbookFilter, WorkbookImage } from '../integrations/index';
import { WorkbookChart } from '../integrations/index';
import { WorkbookNumberFormat, getFormatFromType } from '../integrations/number-format';
import { WorkbookEdit, WorkbookCellFormat, WorkbookHyperlink, WorkbookInsert, WorkbookProtectSheet, WorkbookAutoFill } from '../actions/index';
import { WorkbookDataValidation, WorkbookMerge, addListValidationDropdown, checkColumnValidation } from '../index';
import { ServiceLocator } from '../services/index';
import { setLinkModel, setImage, setChart, setAutoFill, BeforeCellUpdateArgs, updateCell, isNumber } from '../common/index';
import { deleteChart, finiteAlert, formulaBarOperation } from '../../spreadsheet/common/event';
import { beginAction, WorkbookFindAndReplace, getRangeIndexes, workbookEditOperation, clearCFRule, CFArgs, setCFRule } from '../index';
import { WorkbookConditionalFormat } from '../actions/conditional-formatting';
import { AutoFillSettingsModel } from '../..';
import { CheckCellValidArgs, setVisibleMergeIndex, calculateFormula, dataSourceChanged } from '../common/index';
import { IFormulaColl } from '../../calculate/common/interface';
/**
* Represents the Workbook.
*/
@NotifyPropertyChanges
export class Workbook extends Component<HTMLElement> implements INotifyPropertyChanged {
/**
* Configures sheets and its options.
*
* {% codeBlock src='spreadsheet/sheets/index.md' %}{% endcodeBlock %}
*
* @default []
*/
@Collection<SheetModel>([], Sheet)
public sheets: SheetModel[];
/**
* Specifies the active sheet index in the workbook.
*
* {% codeBlock src='spreadsheet/activeSheetIndex/index.md' %}{% endcodeBlock %}
*
* @default 0
* @asptype int
*/
@Property(0)
public activeSheetIndex: number;
/**
* Defines the height of the Spreadsheet. It accepts height as pixels, number, and percentage.
*
* {% codeBlock src='spreadsheet/height/index.md' %}{% endcodeBlock %}
*
* @default '100%'
*/
@Property('100%')
public height: string | number;
/**
* It allows to enable/disable find and replace with its functionalities.
*
* @default true
*/
@Property(true)
public allowFindAndReplace: boolean;
/**
* It stores the filtered range collection.
*
* @hidden
*/
@Property()
public filterCollection: FilterCollectionModel[];
/**
* It stores the filtered range collection.
*
* @hidden
*/
@Property()
public sortCollection: SortCollectionModel[];
/** @hidden */
public isEdit: boolean = false;
/**
* Defines the width of the Spreadsheet. It accepts width as pixels, number, and percentage.
*
* {% codeBlock src='spreadsheet/width/index.md' %}{% endcodeBlock %}
*
* @default '100%'
*/
@Property('100%')
public width: string | number;
/**
* It shows or hides the ribbon in spreadsheet.
*
* @default true
*/
@Property(true)
public showRibbon: boolean;
/**
* It shows or hides the formula bar and its features.
*
* @default true
*/
@Property(true)
public showFormulaBar: boolean;
/**
* It shows or hides the sheets tabs, this is used to navigate among the sheets and create or delete sheets by UI interaction.
*
* @default true
*/
@Property(true)
public showSheetTabs: boolean;
/**
* It allows you to add new data or update existing cell data. If it is false, it will act as read only mode.
*
* @default true
*/
@Property(true)
public allowEditing: boolean;
/**
* It allows you to open an Excel file (.xlsx, .xls, and .csv) in Spreadsheet.
*
* @default true
*/
@Property(true)
public allowOpen: boolean;
/**
* It allows you to save Spreadsheet with all data as Excel file (.xlsx, .xls, and .csv).
*
* @default true
*/
@Property(true)
public allowSave: boolean;
/**
* It allows to enable/disable sort and its functionalities.
*
* @default true
*/
@Property(true)
public allowSorting: boolean;
/**
* It allows to enable/disable filter and its functionalities.
*
* @default true
*/
@Property(true)
public allowFiltering: boolean;
/**
* It allows formatting a raw number into different types of formats (number, currency, accounting, percentage, short date,
* long date, time, fraction, scientific, and text) with built-in format codes.
*
* @default true
*/
@Property(true)
public allowNumberFormatting: boolean;
/**
* It allows you to apply styles (font size, font weight, font family, fill color, and more) to the spreadsheet cells.
*
* @default true
*/
@Property(true)
public allowCellFormatting: boolean;
/**
* It allows to enable/disable Hyperlink and its functionalities.
*
* @default true
*/
@Property(true)
public allowHyperlink: boolean;
/**
* Enables or disables the ability to add or show notes in the Spreadsheet. If the property is set to false, the Spreadsheet will not add notes in the cells and the notes in the existing cells will not be visible.
*
* @default true
*/
@Property(true)
public enableNotes: boolean;
/**
* It allows you to insert rows, columns, and sheets into the spreadsheet.
*
* @default true
*/
@Property(true)
public allowInsert: boolean;
/**
* It allows you to delete rows, columns, and sheets from a spreadsheet.
*
* @default true
*/
@Property(true)
public allowDelete: boolean;
/**
* It allows you to merge the range of cells.
*
* @default true
*/
@Property(true)
public allowMerge: boolean;
/**
* It allows you to apply data validation to the spreadsheet cells.
*
* @default true
*/
@Property(true)
public allowDataValidation: boolean;
/**
* It allows you to insert the image in a spreadsheet.
*
* @default true
*/
@Property(true)
public allowImage: boolean;
/**
* It allows you to insert the chart in a spreadsheet.
*
* @default true
*/
@Property(true)
public allowChart: boolean;
/**
* It allows to enable/disable AutoFill functionalities.
*
* @default true
*/
@Property(true)
public allowAutoFill: boolean;
/**
* Enables or disables the printing functionality in the spreadsheet.
*
* @default true
*/
@Property(true)
public allowPrint: boolean;
/**
* Specifies the mode of calculation within the spreadsheet.
* Setting the calculation mode to `Manual` can enhance performance,
* particularly when working with multiple sheets at the same time.
*
* * `Automatic`: Calculations are performed automatically whenever a cell value changes.
* * `Manual`: Calculations are performed only when explicitly triggered, improving performance
* when loading or working with large spreadsheets.
*
* @default 'Automatic'
*/
@Property('Automatic')
public calculationMode: CalculationMode;
/**
* Configures the auto fill settings.
*
* The autoFillSettings `fillType` property has FOUR types and it is described below:
*
* * CopyCells: To update the copied cells for the selected range.
* * FillSeries: To update the filled series for the selected range.
* * FillFormattingOnly: To fill the formats only for the selected range.
* * FillWithoutFormatting: To fill without the format for the selected range.
*
* {% codeBlock src='spreadsheet/autoFillSettings/index.md' %}{% endcodeBlock %}
*
* > The `allowAutoFill` property should be `true`.
*
* @default { fillType: 'FillSeries', showFillOptions: true }
*/
@Complex<AutoFillSettingsModel>({}, AutoFillSettings)
public autoFillSettings: AutoFillSettingsModel;
/**
* It allows you to apply conditional formatting to the sheet.
*
* @default true
*/
@Property(true)
public allowConditionalFormat: boolean;
/**
* Specifies the cell style options.
*
* {% codeBlock src='spreadsheet/cellStyle/index.md' %}{% endcodeBlock %}
*
* @default {}
*/
@Complex<CellStyleModel>({}, CellStyle)
public cellStyle: CellStyleModel;
/**
* Specifies the service URL to open excel file in spreadsheet.
*
* @default ''
*/
@Property('')
public openUrl: string;
/**
* Specifies the options for configuration when opening a document.
*
* {% codeBlock src='spreadsheet/openSettings/index.md' %}{% endcodeBlock %}
*
* @default {}
*/
@Complex(<OpenSettingsModel>{}, OpenSettings)
public openSettings: OpenSettingsModel;
/**
* Specifies the service URL to save spreadsheet as Excel file.
*
* @default ''
*/
@Property('')
public saveUrl: string;
/**
* Specifies the password.
*
* @default ''
*/
@Property('')
public password: string;
/**
* Specifies to protect the workbook.
*
* @default false
*/
@Property(false)
public isProtected: boolean;
/**
* Specifies the name of a range and uses it in a formula for calculation.
*
* {% codeBlock src='spreadsheet/definedNames/index.md' %}{% endcodeBlock %}
*
* @default []
*/
@Collection([], DefineName)
public definedNames: DefineNameModel[];
/**
* Triggers before opening an Excel file.
* ```html
* <div id='Spreadsheet'></div>
* ```
* ```typescript
* new Spreadsheet({
* beforeOpen: (args: BeforeOpenEventArgs) => {
* }
* ...
* }, '#Spreadsheet');
* ```
*
* @event beforeOpen
*/
@Event()
public beforeOpen: EmitType<BeforeOpenEventArgs>;
/**
* Triggers when the opened Excel file fails to load.
* ```html
* <div id='Spreadsheet'></div>
* ```
* ```typescript
* new Spreadsheet({
* openFailure: (args: OpenFailureArgs) => {
* }
* ...
* }, '#Spreadsheet');
* ```
*
* @event openFailure
*/
@Event()
public openFailure: EmitType<OpenFailureArgs>;
/**
* Triggers before saving the Spreadsheet as Excel file.
* ```html
* <div id='Spreadsheet'></div>
* ```
* ```typescript
* new Spreadsheet({
* beforeSave: (args: BeforeSaveEventArgs) => {
* }
* ...
* }, '#Spreadsheet');
* ```
*
* @event beforeSave
*/
@Event()
public beforeSave: EmitType<BeforeSaveEventArgs>;
/**
* Triggers after saving the Spreadsheet as Excel file.
* ```html
* <div id='Spreadsheet'></div>
* ```
* ```typescript
* new Spreadsheet({
* saveComplete: (args: SaveCompleteEventArgs) => {
* }
* ...
* }, '#Spreadsheet');
* ```
*
* @event saveComplete
*/
@Event()
public saveComplete: EmitType<SaveCompleteEventArgs>;
/**
* Triggers before the cell format applied to the cell.
* ```html
* <div id='Spreadsheet'></div>
* ```
* ```typescript
* new Spreadsheet({
* beforeCellFormat: (args: BeforeCellFormatArgs) => {
* }
* ...
* }, '#Spreadsheet');
* ```
*
* @event beforeCellFormat
*/
@Event()
public beforeCellFormat: EmitType<BeforeCellFormatArgs>;
/**
* Triggered every time a request is made to access cell information.
* ```html
* <div id='Spreadsheet'></div>
* ```
* ```typescript
* new Spreadsheet({
* queryCellInfo: (args: CellInfoEventArgs) => {
* }
* ...
* }, '#Spreadsheet');
* ```
*
* @event queryCellInfo
*/
@Event()
public queryCellInfo: EmitType<CellInfoEventArgs>;
/**
* Triggers before changing any cell properties.
* ```html
* <div id='Spreadsheet'></div>
* ```
* ```typescript
* new Spreadsheet({
* beforeCellUpdate: (args: BeforeCellUpdateArgs) => {
* }
* ...
* }, '#Spreadsheet');
* ```
*
* @event beforeCellUpdate
*/
@Event()
public beforeCellUpdate: EmitType<BeforeCellUpdateArgs>;
/**
* It allows to enable/disable freeze pane functionality in spreadsheet.
*
* @default true
*/
@Property(true)
public allowFreezePane: boolean;
/**
* Specifies the list separator which is used as the formula argument separator.
*
* @default ','
*/
@Property(',')
public listSeparator: string;
/** @hidden */
public commonCellStyle: CellStyleModel;
/**
* To generate sheet name based on sheet count.
*
* @hidden
*/
public sheetNameCount: number = 1;
/** @hidden */
public serviceLocator: ServiceLocator;
/**
* @hidden
*/
public dataValidationRange: string = '';
/**
* @hidden
*/
public isOpen: boolean = false;
/**
* @hidden
*/
public chartColl: ChartModel[] = [];
/**
* @hidden
*/
public isPrintingProcessing: boolean = false;
/**
* @hidden
*/
public currentPrintSheetIndex: number = 0;
/** @hidden */
public formulaRefCell: string;
/** @hidden */
public customFormulaCollection: Map<string, IFormulaColl> = new Map<string, IFormulaColl>();
/**
* Constructor for initializing the library.
*
* @param {WorkbookModel} options - Configures Workbook model.
*/
constructor(options: WorkbookModel) {
super(options);
Workbook.Inject(
DataBind, WorkbookSave, WorkbookOpen, WorkbookNumberFormat, WorkbookCellFormat, WorkbookEdit,
WorkbookFormula, WorkbookSort, WorkbookHyperlink, WorkbookFilter, WorkbookInsert, WorkbookFindAndReplace,
WorkbookDataValidation, WorkbookProtectSheet, WorkbookMerge, WorkbookConditionalFormat, WorkbookImage, WorkbookChart,
WorkbookAutoFill);
this.commonCellStyle = {};
if (options && options.cellStyle) { this.commonCellStyle = options.cellStyle; }
if (this.getModuleName() === 'workbook') {
this.serviceLocator = new ServiceLocator;
this.initWorkbookServices();
this.dataBind(); this.initEmptySheet();
}
}
/**
* For internal use only.
*
* @returns {void} - For internal use only.
* @hidden
*/
protected preRender(): void {
if (!Object.keys(this.commonCellStyle).length) {
this.commonCellStyle = skipDefaultValue(this.cellStyle, true);
}
if (this.getModuleName() === 'spreadsheet' && !this.refreshing) {
this.initEmptySheet();
}
}
private initWorkbookServices(): void {
this.serviceLocator.register(workbookLocale, new L10n(this.getModuleName(), localeData, this.locale));
}
/**
* For internal use only.
*
* @returns {void} - For internal use only.
* @hidden
*/
protected render(): void {
/** code snippets */
}
/**
* To provide the array of modules needed for workbook.
*
* @returns {ModuleDeclaration[]} - To provide the array of modules needed for workbook.
* @hidden
*/
public requiredModules(): ModuleDeclaration[] {
return getWorkbookRequiredModules(this);
}
/**
* Get the properties to be maintained in the persisted state.
*
* @returns {string} - Get the properties to be maintained in the persisted state.
* @hidden
*/
public getPersistData(): string {
return this.addOnPersist([]);
}
/**
* Applies the style (font family, font weight, background color, etc...) to the specified range of cells.
*
* {% codeBlock src='spreadsheet/cellFormat/index.md' %}{% endcodeBlock %}
*
* @param {CellStyleModel} style - Specifies the cell style.
* @param {string} range - Specifies the address for the range of cells.
* @returns {void} - Applies the style (font family, font weight, background color, etc...) to the specified range of cells.
*/
public cellFormat(style: CellStyleModel, range?: string): void {
const sheet: SheetModel = this.getActiveSheet();
if (sheet && (!sheet.isProtected || sheet.protectSettings.formatCells)) {
range = range || sheet.selectedRange;
this.notify(setCellFormat, { style: style, range: range, refreshRibbon: range.indexOf(sheet.activeCell) > -1 ? true : false });
}
}
/**
* Applies cell lock to the specified range of cells.
*
* {% codeBlock src='spreadsheet/lockCells/index.md' %}{% endcodeBlock %}
*
* @param {string} range - Specifies the address for the range of cells.
* @param {boolean} isLocked -Specifies the cell is locked or not.
* @returns {void} - To Applies cell lock to the specified range of cells.
*/
public lockCells(range?: string, isLocked?: boolean): void {
const sheet: SheetModel = this.getActiveSheet();
range = range || sheet.selectedRange;
this.notify(setLockCells, { range: range, isLocked: isLocked, triggerEvent: true });
}
/**
* @hidden
* @param {Workbook} cssProps - Specifies the cssProps.
* @param {number[]} indexes - Specifies the indexes.
* @returns {CellStyleModel} - To get Cell Style Value.
*/
public getCellStyleValue(cssProps: string[], indexes: number[]): CellStyleModel {
const cell: CellModel = getCell(indexes[0], indexes[1], this.getActiveSheet());
const style: CellStyleModel = {};
cssProps.forEach((cssProp: string): void => {
style[`${cssProp}`] = this.cellStyle[`${cssProp}`];
if (cell && cell.style && cell.style[`${cssProp}`]) {
style[`${cssProp}`] = cell.style[`${cssProp}`];
}
});
return style;
}
/**
* Applies the number format (number, currency, percentage, short date, etc...) to the specified range of cells.
*
* {% codeBlock src='spreadsheet/numberFormat/index.md' %}{% endcodeBlock %}
*
* @param {string} format - Specifies the number format code.
* @param {string} range - Specifies the address of the range of cells.
* @returns {void} - Applies the number format (number, currency, percentage, short date, etc...) to the specified range of cells.
*/
public numberFormat(format: string, range?: string): void {
this.notify(events.applyNumberFormatting, { format: format, range: range });
this.notify(events.localizedFormatAction, { action: 'addToCustomFormats', format: format });
}
/**
* Used to create new sheet.
*
* @hidden
* @param {number} index - Specifies the index.
* @param {SheetModel[]} sheets - Specifies the sheets.
* @returns {void} - To create new sheet.
*/
public createSheet(index: number = this.sheets.length, sheets: SheetModel[] = [{}]): void {
this.sheets.splice(index, 0, ...sheets);
initSheet(this, sheets);
this.notify(sheetCreated, { sheetIndex: index || 0, sheets: sheets });
this.notify(events.workbookFormulaOperation, {
action: 'registerSheet', sheetIndex: index || 0, sheetCount: index + sheets.length
});
}
/**
* Used to remove sheet.
*
* @hidden
* @param {number} idx - Specifies the index.
* @returns {void} - To remove sheet
*/
public removeSheet(idx: number): void {
this.sheets.splice(idx, 1);
}
/**
* Destroys the Workbook library.
*
* @returns {void} - To destroy sheet
*/
public destroy(): void {
this.notify(events.workbookDestroyed, null);
super.destroy();
}
/**
* Called internally if any of the property value changed.
*
* @param {WorkbookModel} newProp - To set the properties
* @param {WorkbookModel} oldProp - To get the properties
* @returns {void} - property value changed
* @hidden
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public onPropertyChanged(newProp: WorkbookModel, oldProp: WorkbookModel): void {
for (const prop of Object.keys(newProp)) {
switch (prop) {
case 'cellStyle':
merge(this.commonCellStyle, newProp.cellStyle);
break;
case 'sheets':
if (newProp.sheets === this.sheets) {
this.notify(events.workbookFormulaOperation, { action: 'unRegisterSheet', propertyChange: true });
this.sheetNameCount = 1;
this.notify(events.sheetsDestroyed, {});
initSheet(this);
this.notify(sheetCreated, null);
this.notify(events.workbookFormulaOperation, { action: 'registerSheet' });
} else {
initSheet(this);
}
break;
case 'listSeparator':
this.notify(events.workbookFormulaOperation, { action: 'setArgumentSeparator' });
break;
}
}
}
/**
* Not applicable for workbook.
*
* @hidden
* @param {string | HTMLElement} selector - Specifies the selector.
* @returns {void} - To append the element.
*/
public appendTo(selector: string | HTMLElement): void {
super.appendTo(selector);
}
/**
* Used to hide/show the rows in spreadsheet.
*
* @param {number} startIndex - Specifies the start row index.
* @param {number} endIndex - Specifies the end row index.
* @param {boolean} hide - To hide/show the rows in specified range.
* @returns {void} - To hide/show the rows in spreadsheet.
*/
public hideRow(startIndex: number, endIndex: number = startIndex, hide: boolean = true): void {
const sheet: SheetModel = this.getActiveSheet();
for (let i: number = startIndex; i <= endIndex; i++) {
setRow(sheet, i, { hidden: hide });
}
}
/**
* Used to hide/show the columns in spreadsheet.
*
* @param {number} startIndex - Specifies the start column index.
* @param {number} endIndex - Specifies the end column index.
* @param {boolean} hide - Set `true` / `false` to hide / show the columns.
* @returns {void} - To hide/show the columns in spreadsheet.
*/
public hideColumn(startIndex: number, endIndex: number = startIndex, hide: boolean = true): void {
const sheet: SheetModel = this.getActiveSheet();
for (let i: number = startIndex; i <= endIndex; i++) {
setColumn(sheet, i, { hidden: hide });
}
}
/**
* Sets the border to specified range of cells.
*
* {% codeBlock src='spreadsheet/setBorder/index.md' %}{% endcodeBlock %}
*
* @param {CellStyleModel} style - Specifies the style property which contains border value.
* @param {string} range - Specifies the range of cell reference. If not specified, it will considered the active cell reference.
* @param {BorderType} type - Specifies the range of cell reference. If not specified, it will considered the active cell reference.
* @param {boolean} isUndoRedo - Specifies is undo redo or not.
* @returns {void} - To Sets the border to specified range of cells.
*/
public setBorder(style: CellStyleModel, range?: string, type?: BorderType, isUndoRedo?: boolean): void {
this.notify(setCellFormat, <SetCellFormatArgs>{
style: style, borderType: type, range:
range || this.getActiveSheet().selectedRange, isUndoRedo: isUndoRedo
});
}
/**
* Used to insert rows in to the spreadsheet.
*
* {% codeBlock src='spreadsheet/insertRow/index.md' %}{% endcodeBlock %}
*
* @param {number | RowModel[]} startRow - Specifies the start row index / row model which needs to be inserted.
* @param {number} endRow - Specifies the end row index.
* @param {number | string} sheet - Specifies the sheet name or index in which the insert operation will perform. By default,
* active sheet will be considered.
* @returns {void} - To insert rows in to the spreadsheet.
*/
public insertRow(startRow?: number | RowModel[], endRow?: number, sheet?: number | string): void {
this.notify(insertModel, <InsertDeleteModelArgs>{ model: this.getSheetModel(sheet), start: startRow, end: endRow,
modelType: 'Row', insertType: 'below' });
}
/**
* Used to insert columns in to the spreadsheet.
*
* {% codeBlock src='spreadsheet/insertColumn/index.md' %}{% endcodeBlock %}
*
* @param {number | ColumnModel[]} startColumn - Specifies the start column index / column model which needs to be inserted.
* @param {number} endColumn - Specifies the end column index.
* @param {number | string} sheet - Specifies the sheet name or index in which the insert operation will perform. By default,
* active sheet will be considered.
* @returns {void} - To insert columns in to the spreadsheet.
*/
public insertColumn(startColumn?: number | ColumnModel[], endColumn?: number, sheet?: number | string): void {
this.notify(insertModel, <InsertDeleteModelArgs>{ model: this.getSheetModel(sheet), start: startColumn, end: endColumn,
modelType: 'Column', insertType: 'after' });
}
/**
* Used to insert sheets in to the spreadsheet.
*
* {% codeBlock src='spreadsheet/insertSheet/index.md' %}{% endcodeBlock %}
*
* @param {number | SheetModel[]} startSheet - Specifies the start sheet index / sheet model which needs to be inserted.
* @param {number} endSheet - Specifies the end sheet index.
* @returns {void} - To insert sheets in to the spreadsheet.
*/
public insertSheet(startSheet?: number | SheetModel[], endSheet?: number): void {
if (this.isProtected) {
return;
}
this.notify(insertModel, <InsertDeleteModelArgs>{ model: this, start: startSheet, end: endSheet, modelType: 'Sheet' });
}
/**
* Used to delete rows, columns and sheets from the spreadsheet.
*
* {% codeBlock src='spreadsheet/delete/index.md' %}{% endcodeBlock %}
*
* @param {number} startIndex - Specifies the start sheet / row / column index.
* @param {number} endIndex - Specifies the end sheet / row / column index.
* @param {ModelType} model - Specifies the delete model type. By default, the model is considered as `Sheet`. The possible values are,
* - Row: To delete rows.
* - Column: To delete columns.
* - Sheet: To delete sheets.
* @param {number | string} sheet - Specifies the sheet name or index in which the delete operation will perform. By default,
* active sheet will be considered. It is applicable only for model type Row and Column.
* @returns {void} - To delete rows, columns and sheets from the spreadsheet.
*/
public delete(startIndex?: number, endIndex?: number, model?: ModelType, sheet?: number | string): void {
if (this.isProtected) {
return;
}
startIndex = startIndex || 0; let sheetModel: SheetModel | WorkbookModel;
endIndex = isNullOrUndefined(endIndex) ? startIndex : endIndex;
if (!model || model === 'Sheet') {
// eslint-disable-next-line @typescript-eslint/no-this-alias
sheetModel = this;
if (Math.abs(endIndex - startIndex) >= this.sheets.length) {
return;
}
} else {
sheetModel = this.getSheetModel(sheet);
if (!sheetModel) { return; }
}
this.notify(deleteModel, <InsertDeleteModelArgs>{
model: sheetModel, start: startIndex, end: endIndex, modelType: model || 'Sheet'
});
}
/**
* Used to move the sheets to the specified position in the list of sheets.
*
* {% codeBlock src='spreadsheet/moveSheet/index.md' %}{% endcodeBlock %}
*
* @param {number} position - Specifies the position to move a sheet in the list of sheets.
* @param {number[]} sheetIndexes - Specifies the indexes of the sheet to be moved. By default, the active sheet will be moved.
* @returns {void} - Used to move the sheets to the specified position in the list of sheets.
*/
public moveSheet(position: number, sheetIndexes?: number[]): void {
if (this.isProtected) {
return;
}
moveSheet(this, position, sheetIndexes);
}
/**
* Used to make a duplicate/copy of the sheet in the spreadsheet.
*
* {% codeBlock src='spreadsheet/duplicateSheet/index.md' %}{% endcodeBlock %}
*
* @param {number} sheetIndex - Specifies the index of the sheet to be duplicated. By default, the active sheet will be duplicated.
* @returns {void} - Used to make a duplicate/copy of the sheet in the spreadsheet.
*/
public duplicateSheet(sheetIndex?: number): void {
if (this.isProtected) {
return;
}
duplicateSheet(this, sheetIndex);
}
private getSheetModel(sheet: number | string): SheetModel {
if (isNullOrUndefined(sheet)) {
return this.getActiveSheet();
} else {
const index: number = typeof sheet === 'string' ? getSheetIndex(this, sheet) : sheet;
if (isNullOrUndefined(index) || index >= this.sheets.length) {
return null;
}
return this.sheets[index as number];
}
}
/**
* Used to merge the range of cells.
*
* {% codeBlock src='spreadsheet/merge/index.md' %}{% endcodeBlock %}
*
* @param {string} range - Specifies the range of cells as address.
* @param {MergeType} type - Specifies the merge type. The possible values are,
* - All: Merge all the cells between provided range.
* - Horizontally: Merge the cells row-wise.
* - Vertically: Merge the cells column-wise.
* @returns {void} - To merge the range of cells.
*/
public merge(range?: string, type?: MergeType): void {
let sheetIdx: number; let sheet: SheetModel;
if (range) {
sheetIdx = this.isPrintingProcessing ? this.currentPrintSheetIndex : getSheetIndexFromAddress(this, range);
sheet = getSheet(this, sheetIdx);
} else {
sheet = this.getActiveSheet(); range = sheet.selectedRange; sheetIdx = this.activeSheetIndex;
}
this.notify(setMerge, <MergeArgs>{ merge: true, range: range, type: type || 'All', sheetIndex: sheetIdx, refreshRibbon:
range.indexOf(sheet.activeCell) > -1 ? true : false, preventRefresh: this.activeSheetIndex !== sheetIdx });
}
/**
* Used to split the merged cell into multiple cells.
*
* {% codeBlock src='spreadsheet/unMerge/index.md' %}{% endcodeBlock %}
*
* @param {string} range - Specifies the range of cells as address.
* @returns {void} - To split the merged cell into multiple cells.
*/
public unMerge(range?: string): void {
let sheetIdx: number; let sheet: SheetModel;
if (range) {
sheetIdx = getSheetIndexFromAddress(this, range); sheet = getSheet(this, sheetIdx);
} else {
sheet = this.getActiveSheet(); range = sheet.selectedRange; sheetIdx = this.activeSheetIndex;
}
this.notify(setMerge, <MergeArgs>{
merge: false, range: range, sheetIndex: sheetIdx, type: 'All',
refreshRibbon: range.indexOf(sheet.activeCell) > -1 ? true : false, preventRefresh: this.activeSheetIndex !== sheetIdx
});
}
/** Used to compute the specified expression/formula.
*
* {% codeBlock src='spreadsheet/computeExpression/index.md' %}{% endcodeBlock %}
*
* @param {string} formula - Specifies the formula(=SUM(A1:A3)) or expression(2+3).
* @returns {string | number} - to compute the specified expression/formula.
*/
public computeExpression(formula: string): string | number {