Skip to content

Commit f0564f1

Browse files
committed
Finalising shared formulas
1 parent 7ab00f2 commit f0564f1

27 files changed

+744
-520
lines changed

.eslintrc

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"func-names": ["off", "never"],
55
"linebreak-style": ["off"],
66
"no-var": ["off"],
7+
"arrow-parens": ["off"],
8+
"quote-props": ["off"],
9+
"no-use-before-define": ["off"],
10+
"no-underscore-dangle": ["off"],
711
"vars-on-top": ["off"],
812
"no-param-reassign": ["off"],
913
"prefer-arrow-callback": ["off"],

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014-2017 guyonroche
3+
Copyright (c) 2014-2017 Guyon Roche
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

lib/doc/cell.js

+39-86
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* THE SOFTWARE.
2121
*
2222
*/
23+
2324
'use strict';
2425

2526
var colCache = require('./../utils/col-cache');
@@ -36,13 +37,6 @@ var Cell = module.exports = function(row, column, address) {
3637
throw new Error('A Cell needs a Row');
3738
}
3839

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-
4640
this._row = row;
4741
this._column = column;
4842

@@ -110,19 +104,19 @@ Cell.prototype = {
110104
},
111105

112106
_mergeStyle: function(rowStyle, colStyle, style) {
113-
var numFmt = rowStyle && rowStyle.numFmt || colStyle && colStyle.numFmt;
107+
var numFmt = (rowStyle && rowStyle.numFmt) || (colStyle && colStyle.numFmt);
114108
if (numFmt) style.numFmt = numFmt;
115109

116-
var font = rowStyle && rowStyle.font || colStyle && colStyle.font;
110+
var font = (rowStyle && rowStyle.font) || (colStyle && colStyle.font);
117111
if (font) style.font = font;
118112

119-
var alignment = rowStyle && rowStyle.alignment || colStyle && colStyle.alignment;
113+
var alignment = (rowStyle && rowStyle.alignment) || (colStyle && colStyle.alignment);
120114
if (alignment) style.alignment = alignment;
121115

122-
var border = rowStyle && rowStyle.border || colStyle && colStyle.border;
116+
var border = (rowStyle && rowStyle.border) || (colStyle && colStyle.border);
123117
if (border) style.border = border;
124118

125-
var fill = rowStyle && rowStyle.fill || colStyle && colStyle.fill;
119+
var fill = (rowStyle && rowStyle.fill) || (colStyle && colStyle.fill);
126120
if (fill) style.fill = fill;
127121

128122
return style;
@@ -171,31 +165,33 @@ Cell.prototype = {
171165
this._mergeCount--;
172166
},
173167
get isMerged() {
174-
return (this._mergeCount > 0) || (this.type == Cell.Types.Merge);
168+
return (this._mergeCount > 0) || (this.type === Cell.Types.Merge);
175169
},
176170
merge: function(master) {
177171
this._value.release();
178172
this._value = Value.create(Cell.Types.Merge, this, master);
179173
this.style = master.style;
180174
},
181175
unmerge: function() {
182-
if (this.type == Cell.Types.Merge) {
176+
if (this.type === Cell.Types.Merge) {
183177
this._value.release();
184178
this._value = Value.create(Cell.Types.Null, this);
185179
this.style = this._mergeStyle(this._row.style, this._column.style, {});
186180
}
187181
},
188182
isMergedTo: function(master) {
189-
if (this._value.type != Cell.Types.Merge) return false;
183+
if (this._value.type !== Cell.Types.Merge) return false;
190184
return this._value.isMergedTo(master);
191185
},
192186
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
195191
},
196192

197193
get isHyperlink() {
198-
return this._value.type == Cell.Types.Hyperlink;
194+
return this._value.type === Cell.Types.Hyperlink;
199195
},
200196
get hyperlink() {
201197
return this._value.hyperlink;
@@ -209,7 +205,8 @@ Cell.prototype = {
209205
set value(v) {
210206
// special case - merge cells set their master's value
211207
if (this.type === Cell.Types.Merge) {
212-
return this._value.master.value = v;
208+
this._value.master.value = v;
209+
return;
213210
}
214211

215212
this._value.release();
@@ -226,7 +223,7 @@ Cell.prototype = {
226223

227224
_upgradeToHyperlink: function(hyperlink) {
228225
// 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) {
230227
this._value = Value.create(Cell.Types.Hyperlink, this, {
231228
text: this._value._value,
232229
hyperlink: hyperlink
@@ -321,6 +318,7 @@ NullValue.prototype = {
321318
return null;
322319
},
323320
set value(value) {
321+
// nothing to do
324322
},
325323
get type() {
326324
return Cell.Types.Null;
@@ -605,18 +603,23 @@ var FormulaValue = function(cell, value) {
605603
address: cell.address,
606604
type: Cell.Types.Formula,
607605
formula: value ? value.formula : undefined,
606+
sharedFormula: value ? value.sharedFormula : undefined,
608607
result: value ? value.result : undefined
609608
};
610609
};
611610
FormulaValue.prototype = {
612611
get value() {
613-
return {
612+
return this.model.formula ? {
614613
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,
616618
};
617619
},
618620
set value(value) {
619621
this.model.formula = value.formula;
622+
this.model.sharedFormula = value.sharedFormula;
620623
this.model.result = value.result;
621624
},
622625
validate: function(value) {
@@ -633,6 +636,7 @@ FormulaValue.prototype = {
633636
}
634637
},
635638
get dependencies() {
639+
// find all the ranges and cells mentioned in the formula
636640
var ranges = this.formula.match(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}:[A-Z]{1,3}\d{1,4}/g);
637641
var cells = this.formula.replace(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}:[A-Z]{1,3}\d{1,4}/g, '')
638642
.match(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}/g);
@@ -660,19 +664,19 @@ FormulaValue.prototype = {
660664
var v = this.model.result;
661665
if ((v === null) || (v === undefined)) {
662666
return Enums.ValueType.Null;
663-
} else if ((v instanceof String) || (typeof v == 'string')) {
667+
} else if ((v instanceof String) || (typeof v === 'string')) {
664668
return Enums.ValueType.String;
665-
} else if (typeof v == 'number') {
669+
} else if (typeof v === 'number') {
666670
return Enums.ValueType.Number;
667671
} else if (v instanceof Date) {
668672
return Enums.ValueType.Date;
669673
} else if (v.text && v.hyperlink) {
670674
return Enums.ValueType.Hyperlink;
671675
} else if (v.formula) {
672676
return Enums.ValueType.Formula;
673-
} else {
674-
return Enums.ValueType.Null;
675677
}
678+
679+
return Enums.ValueType.Null;
676680
},
677681
get address() {
678682
return this.model.address;
@@ -836,56 +840,6 @@ JSONValue.prototype = {
836840
}
837841
};
838842

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-
889843
// Value is a place to hold common static Value type functions
890844
var Value = {
891845
getType: function(value) {
@@ -901,19 +855,19 @@ var Value = {
901855
return Cell.Types.Date;
902856
} else if (value.text && value.hyperlink) {
903857
return Cell.Types.Hyperlink;
904-
} else if (value.formula) {
858+
} else if (value.formula || value.sharedFormula) {
905859
return Cell.Types.Formula;
906860
} else if (value.richText) {
907861
return Cell.Types.RichText;
908862
} else if (value.sharedString) {
909863
return Cell.Types.SharedString;
910864
} else if (value.error) {
911865
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);
916866
}
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);
917871
},
918872

919873
// map valueType to constructor
@@ -930,14 +884,13 @@ var Value = {
930884
{t:Cell.Types.RichText, f:RichTextValue},
931885
{t:Cell.Types.Boolean, f:BooleanValue},
932886
{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; }, []),
935888

936889
create: function(type, cell, value) {
937-
var t = this.types[type];
938-
if (!t) {
890+
var T = this.types[type];
891+
if (!T) {
939892
throw new Error('Could not create Value of type ' + type);
940893
}
941-
return new t(cell, value);
894+
return new T(cell, value);
942895
}
943896
};

lib/doc/enums.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ module.exports = {
3535
RichText: 8,
3636
Boolean: 9,
3737
Error: 10,
38-
SharedFormula: 11
3938
},
4039
RelationshipType: {
4140
None: 0,
@@ -45,14 +44,14 @@ module.exports = {
4544
SharedStrings: 4,
4645
Styles: 5,
4746
Theme: 6,
48-
Hyperlink: 7
47+
Hyperlink: 7,
4948
},
5049
DocumentType: {
51-
Xlsx: 1
50+
Xlsx: 1,
5251
},
5352
ReadingOrder: {
5453
RightToLeft: 1,
55-
LeftToRight: 2
54+
LeftToRight: 2,
5655
},
5756
ErrorValue: {
5857
NotApplicable: '#N/A',
@@ -61,6 +60,6 @@ module.exports = {
6160
DivZero: '#DIV/0!',
6261
Null: '#NULL!',
6362
Value: '#VALUE!',
64-
Num: '#NUM!'
63+
Num: '#NUM!',
6564
},
6665
};

lib/doc/range.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,10 @@ Range.prototype = {
132132
if (sheetName) {
133133
if (/^[a-zA-Z0-9]*$/.test(sheetName)) {
134134
return sheetName + '!';
135-
} else {
136-
return '\'' + sheetName + '\'!';
137135
}
138-
} else {
139-
return '';
136+
return '\'' + sheetName + '\'!';
140137
}
138+
return '';
141139
},
142140

143141
expand: function(top, left, bottom, right) {

0 commit comments

Comments
 (0)