Repository navigation
Expand file tree
/
Copy pathelxml.js
More file actions
1421 lines (1324 loc) · 43.1 KB
/
Copy pathelxml.js
File metadata and controls
1421 lines (1324 loc) · 43.1 KB
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
var builder = require('xmlbuilder');
var fs = require('fs');
var _ = require('underscore');
var archiver = require('archiver');
var stringtable = require('./modules/stringtable');
/** @constant CELL_ALIGNMENT_H_CENTER */
exports.CELL_ALIGNMENT_H_CENTER = "general";
/** @constant CELL_ALIGNMENT_H_LEFT */
exports.CELL_ALIGNMENT_H_LEFT = "left";
/** @constant CELL_ALIGNMENT_H_CENTER */
exports.CELL_ALIGNMENT_H_CENTER = "center";
/** @constant CELL_ALIGNMENT_H_RIGHT */
exports.CELL_ALIGNMENT_H_RIGHT = "right";
/** @constant CELL_ALIGNMENT_H_FILL */
exports.CELL_ALIGNMENT_H_FILL = "fill";
/** @constant CELL_ALIGNMENT_H_JUSTIFY */
exports.CELL_ALIGNMENT_H_JUSTIFY = "justify";
/** @constant CELL_ALIGNMENT_H_CENTER_CONT */
exports.CELL_ALIGNMENT_H_CENTER_CONT = "centerContinuous";
/** @constant CELL_ALIGNMENT_H_DISTRIBUTED */
exports.CELL_ALIGNMENT_H_DISTRIBUTED = "distributed";
/** @constant CELL_ALIGNMENT_V_TOP */
exports.CELL_ALIGNMENT_V_TOP = "top";
/** @constant CELL_ALIGNMENT_V_BOTTOM */
exports.CELL_ALIGNMENT_V_BOTTOM = "left";
/** @constant CELL_ALIGNMENT_V_CENTER */
exports.CELL_ALIGNMENT_V_CENTER = "center";
/** @constant CELL_ALIGNMENT_V_JUSTIFY */
exports.CELL_ALIGNMENT_V_JUSTIFY = exports.CELL_ALIGNMENT_H_JUSTIFY;
/** @constant CELL_ALIGNMENT_V_DISTRIBUTED */
exports.CELL_ALIGNMENT_V_DISTRIBUTED = exports.CELL_ALIGNMENT_H_DISTRIBUTED;
/** @constant PATTERN_TYPE_NONE */
exports.PATTERN_TYPE_NONE = "none";
/** @constant PATTERN_TYPE_SOLID */
exports.PATTERN_TYPE_SOLID = "solid";
/** @constant PATTERN_TYPE_MEDIUM_GRAY */
exports.PATTERN_TYPE_MEDIUM_GRAY = "mediumGray";
/** @constant PATTERN_TYPE_DARK_GRAY */
exports.PATTERN_TYPE_DARK_GRAY = "darkGray";
/** @constant PATTERN_TYPE_LIGHT_GREY */
exports.PATTERN_TYPE_LIGHT_GREY = "lightGray";
/** @constant PATTERN_TYPE_DARK_HORIZONTAL */
exports.PATTERN_TYPE_DARK_HORIZONTAL = "darkHorizontal";
/** @constant PATTERN_TYPE_DARK_VERTICAL */
exports.PATTERN_TYPE_DARK_VERTICAL = "darkVertical";
/** @constant PATTERN_TYPE_DARK_DOWN */
exports.PATTERN_TYPE_DARK_DOWN = "darkDown";
/** @constant PATTERN_TYPE_DARK_DOWN */
exports.PATTERN_TYPE_DARK_UP = "darkUp";
/** @constant PATTERN_TYPE_DARK_GRID */
exports.PATTERN_TYPE_DARK_GRID = "darkGrid";
/** @constant PATTERN_TYPE_DARK_TRELLIS */
exports.PATTERN_TYPE_DARK_TRELLIS = "darkTrellis";
/** @constant PATTERN_TYPE_LIGHT_HORIZONTAL */
exports.PATTERN_TYPE_LIGHT_HORIZONTAL = "lightHorizontal";
/** @constant PATTERN_TYPE_LIGHT_VERTICAL */
exports.PATTERN_TYPE_LIGHT_VERTICAL = "lightVertical";
/** @constant PATTERN_TYPE_LIGHT_DOWN */
exports.PATTERN_TYPE_LIGHT_DOWN = "lightDown";
/** @constant PATTERN_TYPE_LIGHT_UP */
exports.PATTERN_TYPE_LIGHT_UP = "lightUp";
/** @constant PATTERN_TYPE_LIGHT_GRID */
exports.PATTERN_TYPE_LIGHT_GRID = "lightGrid";
/** @constant PATTERN_TYPE_LIGHT_TRELLIS */
exports.PATTERN_TYPE_LIGHT_TRELLIS = "lightTrellis";
/** @constant PATTERN_TYPE_GRAY125 */
exports.PATTERN_TYPE_GRAY125 = "gray125";
/** @constant PATTERN_TYPE_GRAY0625 */
exports.PATTERN_TYPE_GRAY0625 = "gray0625";
/** @constant BORDER_STYLE_NONE */
exports.BORDER_STYLE_NONE = "none";
/** @constant BORDER_STYLE_THIN */
exports.BORDER_STYLE_THIN = "thin";
/** @constant BORDER_STYLE_MEDIUM */
exports.BORDER_STYLE_MEDIUM = "medium";
/** @constant BORDER_STYLE_DASHED */
exports.BORDER_STYLE_DASHED = "dashed";
/** @constant BORDER_STYLE_DOTTED */
exports.BORDER_STYLE_DOTTED = "dotted";
/** @constant BORDER_STYLE_THICK */
exports.BORDER_STYLE_THICK = "thick";
/** @constant BORDER_STYLE_DOUBLE */
exports.BORDER_STYLE_DOUBLE = "double";
/** @constant BORDER_STYLE_HAIR */
exports.BORDER_STYLE_HAIR = "hair";
/** @constant BORDER_STYLE_MEDIUM_DASHED */
exports.BORDER_STYLE_MEDIUM_DASHED = "mediumDashed";
/** @constant BORDER_STYLE_DASH_DOT */
exports.BORDER_STYLE_DASH_DOT = "dashDot";
/** @constant BORDER_STYLE_MEDIUM_DASH_DOT */
exports.BORDER_STYLE_MEDIUM_DASH_DOT = "mediumDashDot";
/** @constant BORDER_STYLE_DASH_DOT_DOT */
exports.BORDER_STYLE_DASH_DOT_DOT = "dashDotDot";
/** @constant BORDER_STYLE_MEDIUM_DASH_DOT_DOT */
exports.BORDER_STYLE_MEDIUM_DASH_DOT_DOT = "mediumDashDotDot";
/** @constant BORDER_STYLE_SLANT_DASH_DOT */
exports.BORDER_STYLE_SLANT_DASH_DOT = "slantDashDot";
/**
* @constant CELL_TYPE_DATE
* @desc date type for cells
*/
exports.CELL_TYPE_DATE = "d";
/**
* @constant CELL_TYPE_NUMBER
* @desc number type for cells
*/
exports.CELL_TYPE_NUMBER = "n";
/**
* @constant CELL_TYPE_MSDATE
* @desc number type for cells
*/
exports.CELL_TYPE_MSDATE = "n";
/**
* @constant CELL_TYPE_BOOLEAN
* @desc boolean type for cells
*/
exports.CELL_TYPE_BOOLEAN = "b";
/**
* @constant CELL_TYPE_ERROR
* @desc error type for cells
*/
exports.CELL_TYPE_ERROR = "e";
/**
* @constant CELL_TYPE_STRING
* @desc inline string (not a shared string ) type for cells
*/
exports.CELL_TYPE_STRING = "inlineStr";
/**
* @constant CELL_TYPE_STRING_TAB
* @desc string which will be saved in a string table
*/
exports.CELL_TYPE_STRING_TAB = "s";
/**
* @constant CELL_TYPE_FORMULA
* @desc formula string type for cells
*/
exports.CELL_TYPE_FORMULA = "str";
/**
* @constant CELL_FORMULA_NORMAL
* @desc type of formula
*/
exports.CELL_FORMULA_NORMAL = "normal";
/**
* @constant AUTO_FILTER_COLOR_FONT
* @desc to filter by cell font color
*/
exports.AUTO_FILTER_COLOR_FONT = 0;
/**
* @constant AUTO_FILTER_COLOR_FILL
* @desc to filter by cell fill color
*/
exports.AUTO_FILTER_COLOR_FILL = 1;
/**
* Creates a {@linkcode Workbook} instance
*/
exports.createWorkbook = function() {
return new Workbook();
};
// all internal stuff below this line
// Note that all objects should have a 'save' method which creates the
// complete XML-element structure.
// This save method has one mandatory parameter: the parent XML-element
// schemas and content-types
var EXCEL_SCHEMA_MAIN = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
var EXCEL_SCHEMA_CONTENT_TYPES = "http://schemas.openxmlformats.org/package/2006/content-types";
var EXCEL_SCHEMA_FILE_REL = "http://schemas.openxmlformats.org/package/2006/relationships";
var EXCEL_SCHEMA_DOC_REL = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
var EXCEL_SCHEMA_REL_TYPE_WB = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
var EXCEL_SCHEMA_REL_STYLES = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
var EXCEL_SCHEMA_REL_SHEET = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet";
var EXCEL_SCHEMA_REL_STRTAB = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings";
var EXCEL_SCHEMA_STYLES = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
var EXCEL_TYPE_REL = "application/vnd.openxmlformats-package.relationships+xml";
var EXCEL_TYPE_WORKBOOK = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
var EXCEL_TYPE_SHEET = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml";
var EXCEL_TYPE_STYLES = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml";
var EXCEL_TYPE_STRINGTABLE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml";
// the A-Z array, gets filled as soon as it is needed
var COLUMN_IDS = null;
// creates the xf element
function _writeStyle(ele, style, id) {
var xf = ele.ele("xf");
xf.att("numFmtId", style.numFormat != null ? style.numFormat.id : 0);
xf.att("fontId", style.font != null ? style.font.id : 0);
xf.att("fillId", style.fill != null ? style.fill.id : 0);
xf.att("borderId", style.border != null ? style.border.id : 0);
if (id != null) {
xf.att("xfId",id);
}
if (style.numFormat != 0) {
xf.att("applyNumberFormat",1);
}
if (style.font != 0) {
xf.att("applyFont",1);
}
if (style.border != 0) {
xf.att("applyBorder",1);
}
if (style.fill != 0) {
xf.att("applyFill",1);
}
if (style.alignment != null) {
xf.att("applyAlignment",1);
style.alignment.save(xf);
}
}
// adds the styles from the given list as xf elements to ele
// if all is true every style is added otherwise only those which
// don't have a parent style
function _writeXF(ele, styles, all) {
styles.forEach(function(style) {
if (style.parentStyle == null || all == true) {
var id = style.id;
// use parent ID (which is a zero-based index) for derived styles
if (style.parentStyle != null) {
id = style.parentStyle.id;
}
// no ID (which is a zero-based index) for cellStyleXfs (all = false)
if (all == false) {
id = null;
}
_writeStyle(ele, style, id);
}
});
}
// adds cellStyle elements to cellStyles for all styles without a parent style
function _writeCellStyles(cellStyles, styles) {
styles.forEach(function(style) {
if (style.parentStyle == null) {
cellStyles.ele("cellStyle").att("name", style.name).att("xfId", style.id).att("builtinId",0);
}
});
}
/**
* The AutoFilter definition for a single column.
*
* @param colId
* @constructor
*/
function FilterColumn(colId) {
this.colId = colId;
this.filters = [];
this.hiddenButton = false;
this.showButton = true;
this.colorFilter = null;
}
FilterColumn.prototype = {
constructor : FilterColumn,
/**
* Adds a simple value filter.
* @param value
* @returns {FilterColumn}
*/
addFilter : function(value) {
this.filters.push(value);
return this;
},
/**
* Sets the color to filter for.
* @param fill use a PatternFill
* @param type the type, either AUTO_FILTER_COLOR_FILL or AUTO_FILTER_COLOR_FONT
* @returns {FilterColumn}
*/
setColorFilter : function(fill, type) {
this.colorFilter = {fill:fill, type:type};
return this;
},
setShowButton : function(flag) {
this.showButton = flag;
return this;
},
setHiddenButton : function(flag) {
this.hiddenButton = flag;
return this;
},
save : function(parent) {
var xFilterColumn = parent.ele("filterColumn");
if (this.filters.length != 0) {
var xFilters = xFilterColumn.ele("filters");
this.filters.forEach(function (filter) {
xFilters.ele("filter").att("val", filter);
});
}
xFilterColumn.att("colId", this.colId);
xFilterColumn.att("hiddenButton", this.hiddenButton);
xFilterColumn.att("showButton", this.showButton);
if (this.colorFilter != null) {
xFilterColumn.ele("colorFilter")
.att("dxfId", this.colorFilter.fill.getDxfsId())
.att("cellColor", this.colorFilter.type);
}
}
};
function AutoFilter(ref) {
this.ref = ref;
this.filters = [];
}
AutoFilter.prototype = {
constructor : AutoFilter,
/**
* Add a filter definition for the given column within the AutoFilter range
* NOTE: As far as I know there is no way to let Excel apply the filter upon loading the file.
*
* @param colId zero based ID
* @returns {FilterColumn}
*/
addFilter : function(colId) {
var fCol = new FilterColumn(colId);
this.filters.push(fCol);
return fCol;
},
save : function(parent) {
var xAutoFilter = parent.ele("autoFilter");
xAutoFilter.att("ref", this.ref);
this.filters.forEach(function(filter) {
filter.save(xAutoFilter);
});
}
};
/**
* @constructor
* @param r - red (0-255) {number}
* @param g - green (0-255) {number}
* @param b - blue (0-255) {number}
* @param a - alpha (0-255) {number}
* @param name - the name of the color {string}
*/
function Color(r, g, b, a, name) {
if (a == undefined) {
this.a = 255;
}
if (name == undefined) {
this.name = "COLOR";
}
this.a = a;
this.r = r;
this.g = g;
this.b = b;
this._auto = false;
this.index = -1;
}
/**
* @class
*/
Color.prototype = {
constructor : Color,
auto : function(auto) {
this._auto = auto;
return this;
},
/**
* @returns the internal color value as a ARGB hex string
*/
toHexARGB : function() {
var value = this.a;
value = value << 8;
value = value | this.r;
value = value << 8;
value = value | this.g;
value = value << 8;
value = value | this.b;
return (value+0x100000000).toString(16).substr(-8).toUpperCase();
},
// slight deviation from the pattern to make use of Color simpler
// see PatternFill.save
save : function(parent, name) {
var ele = null;
if (name != undefined) {
ele = parent.ele(name);
} else {
ele = parent.ele(this.name);
}
if (this.index != -1) {
ele.att("indexed", this.index);
} else if (this._auto == true) {
ele.att("auto", this._auto);
} else {
ele.att("rgb",this.toHexARGB());
}
}
};
// colum configuration
// min is the minimal column index, max is the maximal column index
// min=1 and max=1 -> first column
// min=1 and max=5 -> column 1 to 5
// width is the width (in whatever unit)
// bestFit (false by default) is optional
function Column(min, max, width, bestFit) {
this._min = min;
this._max = max;
this._width = width;
if (bestFit != undefined) {
this._bestFit = bestFit;
}
}
Column.prototype = {
constructor : Column,
min : function(_min) {
this._min = _min;
return this;
},
max : function(_max) {
this._max = _max;
return this;
},
width : function(_width) {
this._width = _width;
return this;
},
bestFit : function(_bestFit) {
this._bestFit = _bestFit;
return this;
},
save : function(parent) {
var col = parent.ele("col");
col.att("min", this._min);
col.att("max", this._max);
if (this._width > 0) {
col.att("width", this._width);
col.att("customWidth", "1");
}
if (this._bestFit != undefined) {
col.att("bestFit", this._bestFit);
}
}
};
function Font(opts, id) {
// if something is changed here -> update clone and Fonts.deriveFromDefault as well
this.name = opts.name;
this.size = opts.size;
this.bold = opts.bold;
this.italic = opts.italic;
this.underline = opts.underline;
this.color = opts.color;
this.id = id;
}
Font.prototype = {
constructor : Font,
save : function(parent) {
var font = parent.ele("font");
font.ele("sz").att("val",this.size);
font.ele("name").att("val",this.name);
if (this.bold) {
font.ele("b");
}
if (this.italic) {
font.ele("i");
}
if (this.underline) {
font.ele("u");
}
if (this.color != undefined) {
this.color.save(font,"color");
}
},
clone : function() {
var font = new Font({name:this.name, size:this.size, bold:this.bold, italic:this.italic, underline:this.underline, color:this.color}, this.id);
return font;
}
};
function Fonts() {
this.fontId = 0;
this.fonts = [];
// create a default font
this.defaultOpts = {bold: false, italic: false, underline: false, size: 11, name:"Calibri"};
this.addFont(this.defaultOpts);
}
Fonts.prototype = {
constructor : Fonts,
addFont : function(opts) {
var font = new Font(opts, this.fontId++);
this.fonts.push(font);
return font;
},
deriveFromDefault : function(opts) {
var defFont = this.fonts[0];
var newFont = defFont.clone();
newFont.id = this.fontId++;
opts = (opts == undefined ? {} : opts);
_.defaults(opts, this.defaultOpts);
newFont.bold = opts.bold;
newFont.italic = opts.italic;
newFont.underline = opts.underline;
newFont.size = opts.size;
newFont.name = opts.name;
newFont.color = opts.color;
this.fonts.push(newFont);
return newFont;
},
save : function(stylesheet) {
var ele = stylesheet.ele("fonts");
ele.att("count", this.fonts.length);
this.fonts.forEach(function(font) {
font.save(ele);
});
}
};
function CellAlignment(h, v, wrap) {
this.h = h;
this.v = v;
this.wrap = !!wrap;
}
CellAlignment.prototype = {
constructor : CellAlignment,
save : function(parent) {
var el = parent.ele("alignment");
if (this.h != null) {
el.att("horizontal", this.h);
}
if (this.v != null) {
el.att("vertical", this.v);
}
if (this.rotation != null) {
el.att("textRotation", this.rotation);
}
if (this.wrap) {
el.att("wrapText", "1");
}
}
};
/**
* @class
* @param style {string} one of the predefined border styles
* @param color {Color} the border color
* @desc use {@linkcode Workbook#createBorderPr} to create new instances
*/
function BorderPr(style, color) {
if (color == undefined) {
this.color = new Color(0,0,0,0);
this.color.auto = true;
} else {
this.color = color;
}
this.style = style;
}
BorderPr.prototype = {
constructor : BorderPr,
save : function(parent, name) {
var borderPr = parent.ele(name);
if (this.color != undefined && !this.color.auto) {
this.color.save(borderPr,"color");
}
borderPr.att("style", this.style);
}
};
/**
* @class
*/
function Border(opts, id) {
this.id = id;
this.top = opts.top;
this.right = opts.right;
this.bottom = opts.bottom;
this.left = opts.left;
}
Border.prototype = {
constructor : Border,
save : function(parent) {
var ele = parent.ele("border");
this.left != null ? this.left.save(ele, "left") : ele.ele("left");
this.right != null ? this.right.save(ele, "right") : ele.ele("right");
this.top != null ? this.top.save(ele, "top") : ele.ele("top");
this.bottom != null ? this.bottom.save(ele, "bottom") : ele.ele("bottom");
this.diagonal != null ? this.left.save(ele, "diagonal") : ele.ele("diagonal");
}
};
// Internal list of Borders
// The constructor adds a default border with no styles.
// Borders keeps track of the right internal ID for new Border objects.
function Borders() {
this.borderId = 0;
this.borders = [];
// create a default border
this.add();
}
Borders.prototype = {
constructor : Borders,
add : function(opts) {
opts = (opts == undefined ? {} : opts);
_.defaults(opts,{top:null, bottom:null, left:null, right:null});
var border = new Border(opts, this.borderId++);
this.borders.push(border);
return border;
},
createBorderPr : function(style, color) {
return new BorderPr(style, color);
},
save : function(stylesheet) {
var ele = stylesheet.ele("borders");
ele.att("count", this.borders.length);
this.borders.forEach(function(border) {
border.save(ele);
})
}
};
/**
* @class
*/
function PatternFill(opts, id) {
this.fgColor = opts.fgColor;
this.bgColor = opts.bgColor;
this.type = opts.type;
this.id = id;
this.dxfsId = 0;
}
PatternFill.prototype = {
constructor : PatternFill,
getId : function() {
return this.id;
},
getDxfsId : function() {
return this.dxfsId;
},
setDxfsId : function(id) {
this.dxfsId = id;
},
save : function(fills) {
var fill = fills.ele("fill");
var pf = fill.ele("patternFill");
if (this.fgColor != null) {
this.fgColor.save(pf, "fgColor");
}
if (this.bgColor != null) {
this.bgColor.save(pf, "bgColor");
}
pf.att("patternType", this.type);
}
};
function Fills() {
this.fillId = 0;
this.fills = [];
// create two default fills, looks like that this is required
this.addPatternFill({type: exports.PATTERN_TYPE_NONE});
this.addPatternFill({type: exports.PATTERN_TYPE_GRAY125});
}
Fills.prototype = {
constructor : Fills,
addPatternFill : function(opts) {
opts = (opts == undefined ? {} : opts);
_.defaults(opts, {fgColor: null, bgColor: null, type: exports.PATTERN_TYPE_NONE});
var fill = new PatternFill(opts, this.fillId++);
this.fills.push(fill);
return fill;
},
save : function(stylesheet) {
var ele = stylesheet.ele("fills");
ele.att("count", this.fills.length);
this.fills.forEach(function(fill) {
fill.save(ele);
});
},
saveDxfs : function(stylesheet) {
var dxfs = stylesheet.ele("dxfs");
dxfs.att("count", this.fills.length);
this.fills.forEach(function(fill, index) {
fill.save(dxfs.ele("dxf"));
fill.setDxfsId(index);
});
}
};
/**
* @class
*/
function NumberFormat(id, formatCode) {
this.id = id;
this.formatCode = formatCode;
}
NumberFormat.prototype = {
constructor : NumberFormat
};
function NumberFormats() {
this.formats = [];
this.numFmtId = 1;
}
NumberFormats.prototype = {
constructor : NumberFormats,
add : function(formatCode) {
var format = new NumberFormat(this.numFmtId++, formatCode);
this.formats.push(format);
return format;
}
};
/**
* @class
*/
function CellStyle(name, id) {
this.id = id;
this.parentStyle = null;
this.name = name;
this.numFormat = null;
this.font = null;
this.fill = null;
this.border = null;
this.alignment = new CellAlignment(null, null, false);
}
CellStyle.prototype = {
constructor : CellStyle,
setAlignment : function(h, v, w) {
this.alignment = new CellAlignment(h, v, w);
return this;
},
setWrapText : function(w) {
this.alignment = new CellAlignment(this.alignment.h, this.alignment.v, w);
return this;
},
setFont : function(font) {
this.font = font;
return this;
},
setBorder : function (border) {
this.border = border;
return this;
},
apply : function(style) {
this.parentStyle = style;
this.fill = style.fill;
this.font = style.font;
this.numFormat = style.numFormat;
this.border = style.border;
this.alignment = style.alignment;
}
};
function CellStyles() {
this.nextStyleId = 0;
this.styles = [];
}
CellStyles.prototype = {
constructor : CellStyles,
/**
* Creates a new CellStyle with the given name IF such a CellStyle is not already defined.
* The new or existing CellStyle is returned.
* Note that a new CellStyle is always returned IF name is null.
* @param name
* @returns {*|null}
*/
create : function(name) {
var style = this.getStyle(name);
if (style == null) {
style = new CellStyle(name, this.nextStyleId++);
this.styles.push(style);
}
return style;
},
/**
* Creates a new style based on an existing style.
* The new style gets a name like "elxmlStyle_id" where id is the unique style id.
*
* @param cellStyle the style to derive from
* @param opts options for the new style
* @returns {CellStyle} the new style
*/
derive : function(cellStyle, opts) {
opts = (opts == undefined ? {} : opts);
_.defaults(opts, {numFormat: null, fill: null, font: null, border: null});
var style = new CellStyle("elxmlStyle_" + this.nextStyleId, this.nextStyleId);
this.nextStyleId++;
style.apply(cellStyle);
if (opts.numFormat != null) {
style.numFormat = opts.numFormat;
}
if (opts.font != null) {
style.font = opts.font;
}
if (opts.fill != null) {
style.fill = opts.fill;
}
if (opts.border != null) {
style.border = opts.border;
}
this.styles.push(style);
return style;
},
countDirectStyles : function() {
var count = 0;
this.styles.forEach(function(style) {
if (style.parentStyle == null) {
count++;
}
});
return count;
},
/**
* Returns the current number of CellStyle instances managed by this.
* @returns {Number}
*/
count : function() {
return this.styles.length;
},
/**
* Returns the internal array of CellStyle instances.
* @returns {Array}
*/
getStyles : function() {
return this.styles;
},
/**
* Returns a style by name.
* @param name the name of the style to return, if null no style is returned
* @returns null if no style with the given name is found or name is null, otherwise an instance of CellStyle with the given name
*/
getStyle : function(name) {
var style = null;
if (name != null) {
this.styles.forEach(function (st) {
if (st.name == name) {
style = st;
}
});
}
return style;
}
};
/**
* @class
*/
function Cell(row, index, type) {
this.index = index;
this.row = row;
this.type = type;
this.style = null;
this.value = null;
this.formula = null;
}
Cell.prototype = {
constructor : Cell,
/**
* @param value {string} the cell value
* @return this
*/
setValue : function(value) {
this.value = value;
return this;
},
/**
* @param formula {string} the cell formula
* In case you set a formula, make sure to set the type to CELL_TYPE_FORMULA
* @return this
*/
setFormula : function(formula) {
this.formula = formula;
return this;
},
/**
* @param style {CellStyle} the cell style
* @return this
*/
setStyle : function(style) {
this.style = style;
return this;
},
save : function(row) {
var ele = row.ele("c").att("r", this.index + this.row.index);
if (this.type !== undefined) {
ele.att("t", this.type);
} else {
ele.att("t", "inlineStr");
}
if (this.formula != null) {
var f = ele.ele("f");
f.att("t", exports.CELL_FORMULA_NORMAL);
f.t(this.formula);
}
if (this.value != null) {
if (this.type == undefined || this.type == "inlineStr") {
ele.ele("is").ele("t").t(this.value);
} else if (this.type == "s") {
ele.ele("v").t(this.row.strTable.addString(this.value));
} else {
ele.ele("v").t(this.value);
}
}
if (this.style != null) {
ele.att("s", this.style.id);
}
}
};
/**
* @class
*/
function Row(index, opts, strTable) {
opts = (opts == undefined ? {} : opts);
_.defaults(opts, {height:-1});
this.height = opts.height;
this.index = index;
this.strTable = strTable;
this.cells = [];
}
Row.prototype = {
constructor : Row,
/**
* @param index {number} the column index (A-...)
* @param type {string} one of the predefined cell types
* @desc Adds a {@linkcode Cell} to this row.
*/
addCell : function(index, type) {
var cell = new Cell(this, index, type);
this.cells.push(cell);
return cell;
},
/**
* @param style {CellStyle} to apply for the cells of the row
* @desc Assigns the given style to all cells the row currently(!) holds.
*/
setStyleForAllCells : function(style) {
this.cells.forEach(function(cell) {
cell.setStyle(style);
})
},
save : function(sheetData) {
var ele = sheetData.ele("row");
ele.att("r", this.index);
if (this.height != -1) {
ele.att("ht", this.height);
ele.att("customHeight", 1);
}
this.cells.forEach(function(cell) {
cell.save(ele);
});
}
};
/**
* @class
* @param range {string} range identifier (eg. 'A1:B2')
* @desc
*/
function MergeCell(range) {
this.range = range;
}
MergeCell.prototype = {
constructor : MergeCell,
save : function(mergeCells) {
var ele = mergeCells.ele("mergeCell");
ele.att("ref", this.range);
}
};
/**
* @class
* @param id {number} the sheet id
* @param name {string} the sheet name
* @param strTable the string table, used to reuse strings
* @desc Don't use this constructor, use {@linkcode Workbook#addSheet} instead.
*/
function Sheet(id, name, strTable) {
this.id = id;
this.name = name;
this.rows = [];
this.cols = [];
this.merges = [];
this.strTable = strTable;
this.autoFilter = null;