Lines 27-32
Link Here
|
27 |
import java.util.Stack; |
27 |
import java.util.Stack; |
28 |
|
28 |
|
29 |
import org.apache.poi.hssf.record.formula.Ptg; |
29 |
import org.apache.poi.hssf.record.formula.Ptg; |
|
|
30 |
import org.apache.poi.util.HexDump; |
30 |
import org.apache.poi.util.LittleEndian; |
31 |
import org.apache.poi.util.LittleEndian; |
31 |
|
32 |
|
32 |
/** |
33 |
/** |
Lines 43-48
Link Here
|
43 |
{ |
44 |
{ |
44 |
|
45 |
|
45 |
public static final boolean EXPERIMENTAL_FORMULA_SUPPORT_ENABLED=true; |
46 |
public static final boolean EXPERIMENTAL_FORMULA_SUPPORT_ENABLED=true; |
|
|
47 |
public static final byte STRING_VALUE = 0x00; |
48 |
public static final byte BOOL_VALUE = 0x01; |
49 |
public static final byte ERROR_VALUE = 0x02; |
50 |
public static final byte BLANK_VALUE = 0x03; |
51 |
public static final byte NUMERIC_VALUE = Byte.MAX_VALUE; // non std code |
52 |
|
53 |
public static final byte ERROR_CODE_NULL = 0x00; // #NULL! Intersection of two cell ranges is empty |
54 |
public static final byte ERROR_CODE_DIVZERO = 0x07; // #DIV/0! Error |
55 |
public static final byte ERROR_CODE_VALUE = 0x0F; // #VALUE! Wrong type of operand |
56 |
public static final byte ERROR_CODE_REF = 0x17; // #REF! Illegal or deleted cell reference |
57 |
public static final byte ERROR_CODE_NAME = 0x1D; // #NAME? Wrong function or range name |
58 |
public static final byte ERROR_CODE_NUM = 0x24; // #NUM! Value range overflow |
59 |
public static final byte ERROR_CODE_NA = 0x2A ; // #N/A! Argument or function not available |
46 |
|
60 |
|
47 |
public static final short sid = |
61 |
public static final short sid = |
48 |
0x06; // docs say 406...because of a bug Microsoft support site article #Q184647) |
62 |
0x06; // docs say 406...because of a bug Microsoft support site article #Q184647) |
Lines 51-62
Link Here
|
51 |
private int field_1_row; |
65 |
private int field_1_row; |
52 |
private short field_2_column; |
66 |
private short field_2_column; |
53 |
private short field_3_xf; |
67 |
private short field_3_xf; |
|
|
68 |
|
69 |
// stored formula result can be numeric/string/bool/error/<blank> |
54 |
private double field_4_value; |
70 |
private double field_4_value; |
|
|
71 |
//private String field_4_string; Commented since string is stored externally |
72 |
private boolean field_4_bool; |
73 |
private byte field_4_errtype; |
74 |
|
55 |
private short field_5_options; |
75 |
private short field_5_options; |
56 |
private int field_6_zero; |
76 |
private int field_6_zero; |
57 |
private short field_7_expression_len; |
77 |
private short field_7_expression_len; |
58 |
private Stack field_8_parsed_expr; |
78 |
private Stack field_8_parsed_expr; |
59 |
|
79 |
|
|
|
80 |
private byte value_type_id; // |
81 |
|
60 |
/** |
82 |
/** |
61 |
* Since the NaN support seems sketchy (different constants) we'll store and spit it out directly |
83 |
* Since the NaN support seems sketchy (different constants) we'll store and spit it out directly |
62 |
*/ |
84 |
*/ |
Lines 107-118
Link Here
|
107 |
field_1_row = LittleEndian.getUShort(data, 0 + offset); |
129 |
field_1_row = LittleEndian.getUShort(data, 0 + offset); |
108 |
field_2_column = LittleEndian.getShort(data, 2 + offset); |
130 |
field_2_column = LittleEndian.getShort(data, 2 + offset); |
109 |
field_3_xf = LittleEndian.getShort(data, 4 + offset); |
131 |
field_3_xf = LittleEndian.getShort(data, 4 + offset); |
110 |
field_4_value = LittleEndian.getDouble(data, 6 + offset); |
|
|
111 |
field_5_options = LittleEndian.getShort(data, 14 + offset); |
132 |
field_5_options = LittleEndian.getShort(data, 14 + offset); |
|
|
133 |
field_4_value = LittleEndian.getDouble(data, 6 + offset); |
112 |
|
134 |
|
|
|
135 |
value_data = new byte[8]; |
136 |
System.arraycopy(data, offset+6, value_data, 0, 8); |
137 |
|
113 |
if (Double.isNaN(field_4_value)) { |
138 |
if (Double.isNaN(field_4_value)) { |
114 |
value_data = new byte[8]; |
139 |
value_type_id = value_data[0]; |
115 |
System.arraycopy(data, offset+6, value_data, 0, 8); |
140 |
switch (value_type_id) { |
|
|
141 |
case ERROR_VALUE: |
142 |
field_4_errtype = value_data[2]; |
143 |
break; |
144 |
case BOOL_VALUE: |
145 |
field_4_bool = (value_data[2] == 1); |
146 |
break; |
147 |
case STRING_VALUE: |
148 |
// do nothing since string is stored outside this record |
149 |
break; |
150 |
default: //blank |
151 |
} |
152 |
} |
153 |
else { |
154 |
value_type_id = NUMERIC_VALUE; |
116 |
} |
155 |
} |
117 |
|
156 |
|
118 |
field_6_zero = LittleEndian.getInt(data, 16 + offset); |
157 |
field_6_zero = LittleEndian.getInt(data, 16 + offset); |
Lines 165-179
Link Here
|
165 |
} |
204 |
} |
166 |
|
205 |
|
167 |
/** |
206 |
/** |
168 |
* set the calculated value of the formula |
207 |
* set the calculated numeric value of the formula |
169 |
* |
208 |
* |
170 |
* @param value calculated value |
209 |
* @param value calculated value |
171 |
*/ |
210 |
*/ |
172 |
|
211 |
|
173 |
public void setValue(double value) |
212 |
public void setValue(double value) { |
174 |
{ |
213 |
if (Double.isNaN(value)) { |
175 |
field_4_value = value; |
214 |
setValue(ERROR_CODE_DIVZERO); |
|
|
215 |
} |
216 |
else if (Double.isInfinite(value)) { |
217 |
setValue(ERROR_CODE_NUM); |
218 |
} |
219 |
else { |
220 |
value_type_id = NUMERIC_VALUE; |
221 |
field_4_value = value; |
222 |
} |
223 |
} |
224 |
|
225 |
/** |
226 |
* set the calculated boolean value of the formula |
227 |
* |
228 |
* @param value calculated value |
229 |
*/ |
230 |
public void setValue(boolean value) { |
231 |
field_4_value = Double.NaN; |
232 |
value_type_id = BOOL_VALUE; |
233 |
field_4_bool = value; |
234 |
|
235 |
if (value_data==null) value_data = new byte[8]; |
236 |
value_data[0] = value_type_id; |
237 |
value_data[2] = (byte) (value ? 1 : 0); |
238 |
value_data[6] = (byte) 0xFF; value_data[7] = (byte) 0xFF; |
239 |
} |
240 |
|
241 |
/** |
242 |
* set the calculated string value of the formula |
243 |
* |
244 |
* @param value value is ignored. calculated value |
245 |
* is stored outside the formula record. |
246 |
*/ |
247 |
public void setValue(String value) { |
248 |
field_4_value = Double.NaN; |
249 |
value_type_id = STRING_VALUE; |
250 |
|
251 |
if (value_data==null) value_data = new byte[8]; |
252 |
value_data[0] = value_type_id; |
253 |
value_data[6] = (byte) 0xFF; value_data[7] = (byte) 0xFF; |
254 |
} |
255 |
|
256 |
/** |
257 |
* set the error code as result of the formula |
258 |
* Use the error codes defined in this class. |
259 |
* @param value calculated value |
260 |
*/ |
261 |
public void setValue(byte value) { |
262 |
field_4_value = Double.NaN; |
263 |
value_type_id = ERROR_VALUE; |
264 |
field_4_errtype = value; |
265 |
|
266 |
if (value_data==null) value_data = new byte[8]; |
267 |
value_data[0] = value_type_id; |
268 |
value_data[2] = value; |
269 |
value_data[6] = (byte) 0xFF; value_data[7] = (byte) 0xFF; |
270 |
} |
271 |
|
272 |
/** |
273 |
* set the calculated value of the formula as blank |
274 |
* |
275 |
* @param value calculated value |
276 |
*/ |
277 |
public void setValue() { |
278 |
field_4_value = Double.NaN; |
279 |
value_type_id = BLANK_VALUE; |
280 |
|
281 |
if (value_data==null) value_data = new byte[8]; |
282 |
value_data[0] = value_type_id; |
283 |
value_data[6] = (byte) 0xFF; value_data[7] = (byte) 0xFF; |
176 |
} |
284 |
} |
|
|
285 |
|
177 |
|
286 |
|
178 |
/** |
287 |
/** |
179 |
* set the option flags |
288 |
* set the option flags |
Lines 213-220
Link Here
|
213 |
} |
322 |
} |
214 |
|
323 |
|
215 |
/** |
324 |
/** |
|
|
325 |
* returned value indicates the type of formula |
326 |
* value. Depending on the returned value, the |
327 |
* appropriate getXYZValue() or getValue() method |
328 |
* should be called to get the actual value. |
329 |
* @see getValue(), getStringValue(), getBoolValue(), |
330 |
* getErrorCodeValue() |
331 |
* @return |
332 |
*/ |
333 |
public byte getValueType() { |
334 |
return value_type_id; |
335 |
} |
336 |
|
337 |
/** |
216 |
* get the calculated value of the formula |
338 |
* get the calculated value of the formula |
217 |
* |
339 |
* @see getValueType() |
218 |
* @return calculated value |
340 |
* @return calculated value |
219 |
*/ |
341 |
*/ |
220 |
|
342 |
|
Lines 222-228
Link Here
|
222 |
{ |
344 |
{ |
223 |
return field_4_value; |
345 |
return field_4_value; |
224 |
} |
346 |
} |
225 |
|
347 |
|
|
|
348 |
/** |
349 |
* get the calculated value of the formula |
350 |
* (to be used when the formula value is of type boolean) |
351 |
* @see getValueType() |
352 |
* @return |
353 |
*/ |
354 |
public boolean getBooleanValue() { |
355 |
return field_4_bool; |
356 |
} |
357 |
|
358 |
/** |
359 |
* get the calculated value of the formula |
360 |
* (to be used when the formula value is of type error) |
361 |
* @see getValueType() |
362 |
* @return |
363 |
*/ |
364 |
public byte getErrorCodeValue() { |
365 |
return field_4_errtype; |
366 |
} |
367 |
|
226 |
/** |
368 |
/** |
227 |
* get the option flags |
369 |
* get the option flags |
228 |
* |
370 |
* |
Lines 520-529
Link Here
|
520 |
.append("\n"); |
662 |
.append("\n"); |
521 |
buffer.append(" .xf = ") |
663 |
buffer.append(" .xf = ") |
522 |
.append(Integer.toHexString(getXFIndex())).append("\n"); |
664 |
.append(Integer.toHexString(getXFIndex())).append("\n"); |
523 |
if (Double.isNaN(this.getValue()) && value_data != null) |
665 |
if (Double.isNaN(this.getValue()) && value_data != null) { |
524 |
buffer.append(" .value (NaN) = ") |
666 |
buffer.append(" .value "); |
525 |
.append(org.apache.poi.util.HexDump.dump(value_data,0,0)) |
667 |
switch (value_type_id) { |
526 |
.append("\n"); |
668 |
case STRING_VALUE: |
|
|
669 |
buffer.append("(String) [string is stored outside formula record] "); |
670 |
// note: string value is not stored in this record. |
671 |
break; |
672 |
case ERROR_VALUE: |
673 |
buffer.append("(ErrorCode) = "); |
674 |
buffer.append(field_4_errtype); |
675 |
break; |
676 |
case BOOL_VALUE: |
677 |
buffer.append("(Bool) = "); |
678 |
buffer.append(field_4_bool); |
679 |
break; |
680 |
case BLANK_VALUE: |
681 |
buffer.append("(Blank) = "); |
682 |
break; |
683 |
default: |
684 |
buffer.append("(UNKNOWN) = "); |
685 |
buffer.append(HexDump.dump(value_data, 0, 0)); |
686 |
} |
687 |
buffer.append("\n"); |
688 |
} |
527 |
else |
689 |
else |
528 |
buffer.append(" .value = ").append(getValue()) |
690 |
buffer.append(" .value = ").append(getValue()) |
529 |
.append("\n"); |
691 |
.append("\n"); |
Lines 566-572
Link Here
|
566 |
rec.field_1_row = field_1_row; |
728 |
rec.field_1_row = field_1_row; |
567 |
rec.field_2_column = field_2_column; |
729 |
rec.field_2_column = field_2_column; |
568 |
rec.field_3_xf = field_3_xf; |
730 |
rec.field_3_xf = field_3_xf; |
569 |
rec.field_4_value = field_4_value; |
731 |
rec.field_4_value = field_4_value; // numeric |
|
|
732 |
rec.field_4_bool = field_4_bool; // boolean |
733 |
rec.field_4_errtype = field_4_errtype; // error code |
734 |
rec.value_type_id = value_type_id; // type indicator |
735 |
|
570 |
rec.field_5_options = field_5_options; |
736 |
rec.field_5_options = field_5_options; |
571 |
rec.field_6_zero = field_6_zero; |
737 |
rec.field_6_zero = field_6_zero; |
572 |
rec.field_7_expression_len = field_7_expression_len; |
738 |
rec.field_7_expression_len = field_7_expression_len; |