20
20
* THE SOFTWARE.
21
21
*
22
22
*/
23
+
23
24
'use strict' ;
24
25
25
26
var colCache = require ( './../utils/col-cache' ) ;
@@ -36,13 +37,6 @@ var Cell = module.exports = function(row, column, address) {
36
37
throw new Error ( 'A Cell needs a Row' ) ;
37
38
}
38
39
39
- //if (!(row instanceof Row)) {
40
- // throw new Error('Expected row to be Row, was ' + typeof row);
41
- //}
42
- //if (!(column instanceof Column)) {
43
- // throw new Error('Expected col to be Column, was ' + typeof column);
44
- //}
45
-
46
40
this . _row = row ;
47
41
this . _column = column ;
48
42
@@ -110,19 +104,19 @@ Cell.prototype = {
110
104
} ,
111
105
112
106
_mergeStyle : function ( rowStyle , colStyle , style ) {
113
- var numFmt = rowStyle && rowStyle . numFmt || colStyle && colStyle . numFmt ;
107
+ var numFmt = ( rowStyle && rowStyle . numFmt ) || ( colStyle && colStyle . numFmt ) ;
114
108
if ( numFmt ) style . numFmt = numFmt ;
115
109
116
- var font = rowStyle && rowStyle . font || colStyle && colStyle . font ;
110
+ var font = ( rowStyle && rowStyle . font ) || ( colStyle && colStyle . font ) ;
117
111
if ( font ) style . font = font ;
118
112
119
- var alignment = rowStyle && rowStyle . alignment || colStyle && colStyle . alignment ;
113
+ var alignment = ( rowStyle && rowStyle . alignment ) || ( colStyle && colStyle . alignment ) ;
120
114
if ( alignment ) style . alignment = alignment ;
121
115
122
- var border = rowStyle && rowStyle . border || colStyle && colStyle . border ;
116
+ var border = ( rowStyle && rowStyle . border ) || ( colStyle && colStyle . border ) ;
123
117
if ( border ) style . border = border ;
124
118
125
- var fill = rowStyle && rowStyle . fill || colStyle && colStyle . fill ;
119
+ var fill = ( rowStyle && rowStyle . fill ) || ( colStyle && colStyle . fill ) ;
126
120
if ( fill ) style . fill = fill ;
127
121
128
122
return style ;
@@ -171,31 +165,33 @@ Cell.prototype = {
171
165
this . _mergeCount -- ;
172
166
} ,
173
167
get isMerged ( ) {
174
- return ( this . _mergeCount > 0 ) || ( this . type == Cell . Types . Merge ) ;
168
+ return ( this . _mergeCount > 0 ) || ( this . type === Cell . Types . Merge ) ;
175
169
} ,
176
170
merge : function ( master ) {
177
171
this . _value . release ( ) ;
178
172
this . _value = Value . create ( Cell . Types . Merge , this , master ) ;
179
173
this . style = master . style ;
180
174
} ,
181
175
unmerge : function ( ) {
182
- if ( this . type == Cell . Types . Merge ) {
176
+ if ( this . type === Cell . Types . Merge ) {
183
177
this . _value . release ( ) ;
184
178
this . _value = Value . create ( Cell . Types . Null , this ) ;
185
179
this . style = this . _mergeStyle ( this . _row . style , this . _column . style , { } ) ;
186
180
}
187
181
} ,
188
182
isMergedTo : function ( master ) {
189
- if ( this . _value . type != Cell . Types . Merge ) return false ;
183
+ if ( this . _value . type !== Cell . Types . Merge ) return false ;
190
184
return this . _value . isMergedTo ( master ) ;
191
185
} ,
192
186
get master ( ) {
193
- if ( this . type === Cell . Types . Merge ) return this . _value . master ;
194
- else return this ; // an unmerged cell is its own master
187
+ if ( this . type === Cell . Types . Merge ) {
188
+ return this . _value . master ;
189
+ }
190
+ return this ; // an unmerged cell is its own master
195
191
} ,
196
192
197
193
get isHyperlink ( ) {
198
- return this . _value . type == Cell . Types . Hyperlink ;
194
+ return this . _value . type === Cell . Types . Hyperlink ;
199
195
} ,
200
196
get hyperlink ( ) {
201
197
return this . _value . hyperlink ;
@@ -209,7 +205,8 @@ Cell.prototype = {
209
205
set value ( v ) {
210
206
// special case - merge cells set their master's value
211
207
if ( this . type === Cell . Types . Merge ) {
212
- return this . _value . master . value = v ;
208
+ this . _value . master . value = v ;
209
+ return ;
213
210
}
214
211
215
212
this . _value . release ( ) ;
@@ -226,7 +223,7 @@ Cell.prototype = {
226
223
227
224
_upgradeToHyperlink : function ( hyperlink ) {
228
225
// if this cell is a string, turn it into a Hyperlink
229
- if ( this . type == Cell . Types . String ) {
226
+ if ( this . type === Cell . Types . String ) {
230
227
this . _value = Value . create ( Cell . Types . Hyperlink , this , {
231
228
text : this . _value . _value ,
232
229
hyperlink : hyperlink
@@ -321,6 +318,7 @@ NullValue.prototype = {
321
318
return null ;
322
319
} ,
323
320
set value ( value ) {
321
+ // nothing to do
324
322
} ,
325
323
get type ( ) {
326
324
return Cell . Types . Null ;
@@ -605,18 +603,23 @@ var FormulaValue = function(cell, value) {
605
603
address : cell . address ,
606
604
type : Cell . Types . Formula ,
607
605
formula : value ? value . formula : undefined ,
606
+ sharedFormula : value ? value . sharedFormula : undefined ,
608
607
result : value ? value . result : undefined
609
608
} ;
610
609
} ;
611
610
FormulaValue . prototype = {
612
611
get value ( ) {
613
- return {
612
+ return this . model . formula ? {
614
613
formula : this . model . formula ,
615
- result : this . model . result
614
+ result : this . model . result ,
615
+ } : {
616
+ sharedFormula : this . model . sharedFormula ,
617
+ result : this . model . result ,
616
618
} ;
617
619
} ,
618
620
set value ( value ) {
619
621
this . model . formula = value . formula ;
622
+ this . model . sharedFormula = value . sharedFormula ;
620
623
this . model . result = value . result ;
621
624
} ,
622
625
validate : function ( value ) {
@@ -633,6 +636,7 @@ FormulaValue.prototype = {
633
636
}
634
637
} ,
635
638
get dependencies ( ) {
639
+ // find all the ranges and cells mentioned in the formula
636
640
var ranges = this . formula . match ( / ( [ a - z A - Z 0 - 9 ] + ! ) ? [ A - Z ] { 1 , 3 } \d { 1 , 4 } : [ A - Z ] { 1 , 3 } \d { 1 , 4 } / g) ;
637
641
var cells = this . formula . replace ( / ( [ a - z A - Z 0 - 9 ] + ! ) ? [ A - Z ] { 1 , 3 } \d { 1 , 4 } : [ A - Z ] { 1 , 3 } \d { 1 , 4 } / g, '' )
638
642
. match ( / ( [ a - z A - Z 0 - 9 ] + ! ) ? [ A - Z ] { 1 , 3 } \d { 1 , 4 } / g) ;
@@ -660,19 +664,19 @@ FormulaValue.prototype = {
660
664
var v = this . model . result ;
661
665
if ( ( v === null ) || ( v === undefined ) ) {
662
666
return Enums . ValueType . Null ;
663
- } else if ( ( v instanceof String ) || ( typeof v == 'string' ) ) {
667
+ } else if ( ( v instanceof String ) || ( typeof v === 'string' ) ) {
664
668
return Enums . ValueType . String ;
665
- } else if ( typeof v == 'number' ) {
669
+ } else if ( typeof v === 'number' ) {
666
670
return Enums . ValueType . Number ;
667
671
} else if ( v instanceof Date ) {
668
672
return Enums . ValueType . Date ;
669
673
} else if ( v . text && v . hyperlink ) {
670
674
return Enums . ValueType . Hyperlink ;
671
675
} else if ( v . formula ) {
672
676
return Enums . ValueType . Formula ;
673
- } else {
674
- return Enums . ValueType . Null ;
675
677
}
678
+
679
+ return Enums . ValueType . Null ;
676
680
} ,
677
681
get address ( ) {
678
682
return this . model . address ;
@@ -836,56 +840,6 @@ JSONValue.prototype = {
836
840
}
837
841
} ;
838
842
839
- var SharedFormulaValue = function ( cell , value ) {
840
- this . model = {
841
- address : cell . address ,
842
- type : Cell . Types . SharedFormula ,
843
- sharedFormula : value ? value . sharedFormula : undefined ,
844
- result : value ? value . result : undefined
845
- } ;
846
- } ;
847
- SharedFormulaValue . prototype = {
848
- get value ( ) {
849
- return {
850
- sharedFormula : this . model . sharedFormula ,
851
- result : this . model . result
852
- } ;
853
- } ,
854
- set value ( value ) {
855
- this . model . sharedFormula = value . sharedFormula ;
856
- this . model . result = value . result ;
857
- } ,
858
- get sharedFormula ( ) {
859
- return this . model . sharedFormula ;
860
- } ,
861
- set sharedFormula ( value ) {
862
- this . model . sharedFormula = value ;
863
- } ,
864
- get result ( ) {
865
- return this . model . result ;
866
- } ,
867
- set result ( value ) {
868
- this . model . result = value ;
869
- } ,
870
- get type ( ) {
871
- return Cell . Types . SharedFormula ;
872
- } ,
873
- get address ( ) {
874
- return this . model . address ;
875
- } ,
876
- set address ( value ) {
877
- this . model . address = value ;
878
- } ,
879
- toCsvString : function ( ) {
880
- return '' + ( this . model . result || '' ) ;
881
- } ,
882
- release : function ( ) {
883
- } ,
884
- toString : function ( ) {
885
- return this . model . result ? this . model . result . toString ( ) : '' ;
886
- }
887
- } ;
888
-
889
843
// Value is a place to hold common static Value type functions
890
844
var Value = {
891
845
getType : function ( value ) {
@@ -901,19 +855,19 @@ var Value = {
901
855
return Cell . Types . Date ;
902
856
} else if ( value . text && value . hyperlink ) {
903
857
return Cell . Types . Hyperlink ;
904
- } else if ( value . formula ) {
858
+ } else if ( value . formula || value . sharedFormula ) {
905
859
return Cell . Types . Formula ;
906
860
} else if ( value . richText ) {
907
861
return Cell . Types . RichText ;
908
862
} else if ( value . sharedString ) {
909
863
return Cell . Types . SharedString ;
910
864
} else if ( value . error ) {
911
865
return Cell . Types . Error ;
912
- } else {
913
- return Cell . Types . JSON ;
914
- //console.log('Error: value=' + value + ', type=' + typeof value)
915
- // throw new Error('I could not understand type of value: ' + JSON.stringify(value) + ' - typeof: ' + typeof value);
916
866
}
867
+
868
+ return Cell . Types . JSON ;
869
+ // console.log('Error: value=' + value + ', type=' + typeof value)
870
+ // throw new Error('I could not understand type of value: ' + JSON.stringify(value) + ' - typeof: ' + typeof value);
917
871
} ,
918
872
919
873
// map valueType to constructor
@@ -930,14 +884,13 @@ var Value = {
930
884
{ t :Cell . Types . RichText , f :RichTextValue } ,
931
885
{ t :Cell . Types . Boolean , f :BooleanValue } ,
932
886
{ t :Cell . Types . Error , f :ErrorValue } ,
933
- { t :Cell . Types . SharedFormula , f :SharedFormulaValue } ,
934
- ] . reduce ( function ( p , t ) { p [ t . t ] = t . f ; return p ; } , [ ] ) ,
887
+ ] . reduce ( ( p , t ) => { p [ t . t ] = t . f ; return p ; } , [ ] ) ,
935
888
936
889
create : function ( type , cell , value ) {
937
- var t = this . types [ type ] ;
938
- if ( ! t ) {
890
+ var T = this . types [ type ] ;
891
+ if ( ! T ) {
939
892
throw new Error ( 'Could not create Value of type ' + type ) ;
940
893
}
941
- return new t ( cell , value ) ;
894
+ return new T ( cell , value ) ;
942
895
}
943
896
} ;
0 commit comments