28 define(
'SPREADSHEET_EXCEL_WRITER_ADD',
"+");
33 define(
'SPREADSHEET_EXCEL_WRITER_SUB',
"-");
38 define(
'SPREADSHEET_EXCEL_WRITER_MUL',
"*");
43 define(
'SPREADSHEET_EXCEL_WRITER_DIV',
"/");
48 define(
'SPREADSHEET_EXCEL_WRITER_OPEN',
"(");
53 define(
'SPREADSHEET_EXCEL_WRITER_CLOSE',
")");
58 define(
'SPREADSHEET_EXCEL_WRITER_COMA',
",");
63 define(
'SPREADSHEET_EXCEL_WRITER_SEMICOLON',
";");
68 define(
'SPREADSHEET_EXCEL_WRITER_GT',
">");
73 define(
'SPREADSHEET_EXCEL_WRITER_LT',
"<");
78 define(
'SPREADSHEET_EXCEL_WRITER_LE',
"<=");
83 define(
'SPREADSHEET_EXCEL_WRITER_GE',
">=");
88 define(
'SPREADSHEET_EXCEL_WRITER_EQ',
"=");
93 define(
'SPREADSHEET_EXCEL_WRITER_NE',
"<>");
98 define(
'SPREADSHEET_EXCEL_WRITER_CONCAT',
"&");
100 require_once
'PEAR.php';
174 $this->_current_char = 0;
175 $this->_BIFF_version = $biff_version;
176 $this->_current_token =
'';
177 $this->_formula =
'';
178 $this->_lookahead =
'';
179 $this->_parse_tree =
'';
181 $this->_byte_order = $byte_order;
182 $this->_ext_sheets = array();
183 $this->_references = array();
214 'ptgPercent' => 0x14,
216 'ptgMissArg' => 0x16,
220 'ptgEndSheet' => 0x1B,
227 'ptgFuncVar' => 0x22,
231 'ptgMemArea' => 0x26,
233 'ptgMemNoMem' => 0x28,
234 'ptgMemFunc' => 0x29,
236 'ptgAreaErr' => 0x2B,
239 'ptgMemAreaN' => 0x2E,
240 'ptgMemNoMemN' => 0x2F,
244 'ptgRefErr3d' => 0x3C,
245 'ptgAreaErr3d' => 0x3D,
248 'ptgFuncVarV' => 0x42,
252 'ptgMemAreaV' => 0x46,
253 'ptgMemErrV' => 0x47,
254 'ptgMemNoMemV' => 0x48,
255 'ptgMemFuncV' => 0x49,
256 'ptgRefErrV' => 0x4A,
257 'ptgAreaErrV' => 0x4B,
260 'ptgMemAreaNV' => 0x4E,
261 'ptgMemNoMemN' => 0x4F,
262 'ptgFuncCEV' => 0x58,
265 'ptgArea3dV' => 0x5B,
266 'ptgRefErr3dV' => 0x5C,
267 'ptgAreaErr3d' => 0x5D,
270 'ptgFuncVarA' => 0x62,
274 'ptgMemAreaA' => 0x66,
275 'ptgMemErrA' => 0x67,
276 'ptgMemNoMemA' => 0x68,
277 'ptgMemFuncA' => 0x69,
278 'ptgRefErrA' => 0x6A,
279 'ptgAreaErrA' => 0x6B,
282 'ptgMemAreaNA' => 0x6E,
283 'ptgMemNoMemN' => 0x6F,
284 'ptgFuncCEA' => 0x78,
287 'ptgArea3dA' => 0x7B,
288 'ptgRefErr3dA' => 0x7C,
289 'ptgAreaErr3d' => 0x7D
305 $this->_functions = array(
307 'COUNT' => array( 0, -1, 0, 0 ),
308 'IF' => array( 1, -1, 1, 0 ),
309 'ISNA' => array( 2, 1, 1, 0 ),
310 'ISERROR' => array( 3, 1, 1, 0 ),
311 'SUM' => array( 4, -1, 0, 0 ),
312 'AVERAGE' => array( 5, -1, 0, 0 ),
313 'MIN' => array( 6, -1, 0, 0 ),
314 'MAX' => array( 7, -1, 0, 0 ),
315 'ROW' => array( 8, -1, 0, 0 ),
316 'COLUMN' => array( 9, -1, 0, 0 ),
317 'NA' => array( 10, 0, 0, 0 ),
318 'NPV' => array( 11, -1, 1, 0 ),
319 'STDEV' => array( 12, -1, 0, 0 ),
320 'DOLLAR' => array( 13, -1, 1, 0 ),
321 'FIXED' => array( 14, -1, 1, 0 ),
322 'SIN' => array( 15, 1, 1, 0 ),
323 'COS' => array( 16, 1, 1, 0 ),
324 'TAN' => array( 17, 1, 1, 0 ),
325 'ATAN' => array( 18, 1, 1, 0 ),
326 'PI' => array( 19, 0, 1, 0 ),
327 'SQRT' => array( 20, 1, 1, 0 ),
328 'EXP' => array( 21, 1, 1, 0 ),
329 'LN' => array( 22, 1, 1, 0 ),
330 'LOG10' => array( 23, 1, 1, 0 ),
331 'ABS' => array( 24, 1, 1, 0 ),
332 'INT' => array( 25, 1, 1, 0 ),
333 'SIGN' => array( 26, 1, 1, 0 ),
334 'ROUND' => array( 27, 2, 1, 0 ),
335 'LOOKUP' => array( 28, -1, 0, 0 ),
336 'INDEX' => array( 29, -1, 0, 1 ),
337 'REPT' => array( 30, 2, 1, 0 ),
338 'MID' => array( 31, 3, 1, 0 ),
339 'LEN' => array( 32, 1, 1, 0 ),
340 'VALUE' => array( 33, 1, 1, 0 ),
341 'TRUE' => array( 34, 0, 1, 0 ),
342 'FALSE' => array( 35, 0, 1, 0 ),
343 'AND' => array( 36, -1, 0, 0 ),
344 'OR' => array( 37, -1, 0, 0 ),
345 'NOT' => array( 38, 1, 1, 0 ),
346 'MOD' => array( 39, 2, 1, 0 ),
347 'DCOUNT' => array( 40, 3, 0, 0 ),
348 'DSUM' => array( 41, 3, 0, 0 ),
349 'DAVERAGE' => array( 42, 3, 0, 0 ),
350 'DMIN' => array( 43, 3, 0, 0 ),
351 'DMAX' => array( 44, 3, 0, 0 ),
352 'DSTDEV' => array( 45, 3, 0, 0 ),
353 'VAR' => array( 46, -1, 0, 0 ),
354 'DVAR' => array( 47, 3, 0, 0 ),
355 'TEXT' => array( 48, 2, 1, 0 ),
356 'LINEST' => array( 49, -1, 0, 0 ),
357 'TREND' => array( 50, -1, 0, 0 ),
358 'LOGEST' => array( 51, -1, 0, 0 ),
359 'GROWTH' => array( 52, -1, 0, 0 ),
360 'PV' => array( 56, -1, 1, 0 ),
361 'FV' => array( 57, -1, 1, 0 ),
362 'NPER' => array( 58, -1, 1, 0 ),
363 'PMT' => array( 59, -1, 1, 0 ),
364 'RATE' => array( 60, -1, 1, 0 ),
365 'MIRR' => array( 61, 3, 0, 0 ),
366 'IRR' => array( 62, -1, 0, 0 ),
367 'RAND' => array( 63, 0, 1, 1 ),
368 'MATCH' => array( 64, -1, 0, 0 ),
369 'DATE' => array( 65, 3, 1, 0 ),
370 'TIME' => array( 66, 3, 1, 0 ),
371 'DAY' => array( 67, 1, 1, 0 ),
372 'MONTH' => array( 68, 1, 1, 0 ),
373 'YEAR' => array( 69, 1, 1, 0 ),
374 'WEEKDAY' => array( 70, -1, 1, 0 ),
375 'HOUR' => array( 71, 1, 1, 0 ),
376 'MINUTE' => array( 72, 1, 1, 0 ),
377 'SECOND' => array( 73, 1, 1, 0 ),
378 'NOW' => array( 74, 0, 1, 1 ),
379 'AREAS' => array( 75, 1, 0, 1 ),
380 'ROWS' => array( 76, 1, 0, 1 ),
381 'COLUMNS' => array( 77, 1, 0, 1 ),
382 'OFFSET' => array( 78, -1, 0, 1 ),
383 'SEARCH' => array( 82, -1, 1, 0 ),
384 'TRANSPOSE' => array( 83, 1, 1, 0 ),
385 'TYPE' => array( 86, 1, 1, 0 ),
386 'ATAN2' => array( 97, 2, 1, 0 ),
387 'ASIN' => array( 98, 1, 1, 0 ),
388 'ACOS' => array( 99, 1, 1, 0 ),
389 'CHOOSE' => array( 100, -1, 1, 0 ),
390 'HLOOKUP' => array( 101, -1, 0, 0 ),
391 'VLOOKUP' => array( 102, -1, 0, 0 ),
392 'ISREF' => array( 105, 1, 0, 0 ),
393 'LOG' => array( 109, -1, 1, 0 ),
394 'CHAR' => array( 111, 1, 1, 0 ),
395 'LOWER' => array( 112, 1, 1, 0 ),
396 'UPPER' => array( 113, 1, 1, 0 ),
397 'PROPER' => array( 114, 1, 1, 0 ),
398 'LEFT' => array( 115, -1, 1, 0 ),
399 'RIGHT' => array( 116, -1, 1, 0 ),
400 'EXACT' => array( 117, 2, 1, 0 ),
401 'TRIM' => array( 118, 1, 1, 0 ),
402 'REPLACE' => array( 119, 4, 1, 0 ),
403 'SUBSTITUTE' => array( 120, -1, 1, 0 ),
404 'CODE' => array( 121, 1, 1, 0 ),
405 'FIND' => array( 124, -1, 1, 0 ),
406 'CELL' => array( 125, -1, 0, 1 ),
407 'ISERR' => array( 126, 1, 1, 0 ),
408 'ISTEXT' => array( 127, 1, 1, 0 ),
409 'ISNUMBER' => array( 128, 1, 1, 0 ),
410 'ISBLANK' => array( 129, 1, 1, 0 ),
411 'T' => array( 130, 1, 0, 0 ),
412 'N' => array( 131, 1, 0, 0 ),
413 'DATEVALUE' => array( 140, 1, 1, 0 ),
414 'TIMEVALUE' => array( 141, 1, 1, 0 ),
415 'SLN' => array( 142, 3, 1, 0 ),
416 'SYD' => array( 143, 4, 1, 0 ),
417 'DDB' => array( 144, -1, 1, 0 ),
418 'INDIRECT' => array( 148, -1, 1, 1 ),
419 'CALL' => array( 150, -1, 1, 0 ),
420 'CLEAN' => array( 162, 1, 1, 0 ),
421 'MDETERM' => array( 163, 1, 2, 0 ),
422 'MINVERSE' => array( 164, 1, 2, 0 ),
423 'MMULT' => array( 165, 2, 2, 0 ),
424 'IPMT' => array( 167, -1, 1, 0 ),
425 'PPMT' => array( 168, -1, 1, 0 ),
426 'COUNTA' => array( 169, -1, 0, 0 ),
427 'PRODUCT' => array( 183, -1, 0, 0 ),
428 'FACT' => array( 184, 1, 1, 0 ),
429 'DPRODUCT' => array( 189, 3, 0, 0 ),
430 'ISNONTEXT' => array( 190, 1, 1, 0 ),
431 'STDEVP' => array( 193, -1, 0, 0 ),
432 'VARP' => array( 194, -1, 0, 0 ),
433 'DSTDEVP' => array( 195, 3, 0, 0 ),
434 'DVARP' => array( 196, 3, 0, 0 ),
435 'TRUNC' => array( 197, -1, 1, 0 ),
436 'ISLOGICAL' => array( 198, 1, 1, 0 ),
437 'DCOUNTA' => array( 199, 3, 0, 0 ),
438 'ROUNDUP' => array( 212, 2, 1, 0 ),
439 'ROUNDDOWN' => array( 213, 2, 1, 0 ),
440 'RANK' => array( 216, -1, 0, 0 ),
441 'ADDRESS' => array( 219, -1, 1, 0 ),
442 'DAYS360' => array( 220, -1, 1, 0 ),
443 'TODAY' => array( 221, 0, 1, 1 ),
444 'VDB' => array( 222, -1, 1, 0 ),
445 'MEDIAN' => array( 227, -1, 0, 0 ),
446 'SUMPRODUCT' => array( 228, -1, 2, 0 ),
447 'SINH' => array( 229, 1, 1, 0 ),
448 'COSH' => array( 230, 1, 1, 0 ),
449 'TANH' => array( 231, 1, 1, 0 ),
450 'ASINH' => array( 232, 1, 1, 0 ),
451 'ACOSH' => array( 233, 1, 1, 0 ),
452 'ATANH' => array( 234, 1, 1, 0 ),
453 'DGET' => array( 235, 3, 0, 0 ),
454 'INFO' => array( 244, 1, 1, 1 ),
455 'DB' => array( 247, -1, 1, 0 ),
456 'FREQUENCY' => array( 252, 2, 0, 0 ),
457 'ERROR.TYPE' => array( 261, 1, 1, 0 ),
458 'REGISTER.ID' => array( 267, -1, 1, 0 ),
459 'AVEDEV' => array( 269, -1, 0, 0 ),
460 'BETADIST' => array( 270, -1, 1, 0 ),
461 'GAMMALN' => array( 271, 1, 1, 0 ),
462 'BETAINV' => array( 272, -1, 1, 0 ),
463 'BINOMDIST' => array( 273, 4, 1, 0 ),
464 'CHIDIST' => array( 274, 2, 1, 0 ),
465 'CHIINV' => array( 275, 2, 1, 0 ),
466 'COMBIN' => array( 276, 2, 1, 0 ),
467 'CONFIDENCE' => array( 277, 3, 1, 0 ),
468 'CRITBINOM' => array( 278, 3, 1, 0 ),
469 'EVEN' => array( 279, 1, 1, 0 ),
470 'EXPONDIST' => array( 280, 3, 1, 0 ),
471 'FDIST' => array( 281, 3, 1, 0 ),
472 'FINV' => array( 282, 3, 1, 0 ),
473 'FISHER' => array( 283, 1, 1, 0 ),
474 'FISHERINV' => array( 284, 1, 1, 0 ),
475 'FLOOR' => array( 285, 2, 1, 0 ),
476 'GAMMADIST' => array( 286, 4, 1, 0 ),
477 'GAMMAINV' => array( 287, 3, 1, 0 ),
478 'CEILING' => array( 288, 2, 1, 0 ),
479 'HYPGEOMDIST' => array( 289, 4, 1, 0 ),
480 'LOGNORMDIST' => array( 290, 3, 1, 0 ),
481 'LOGINV' => array( 291, 3, 1, 0 ),
482 'NEGBINOMDIST' => array( 292, 3, 1, 0 ),
483 'NORMDIST' => array( 293, 4, 1, 0 ),
484 'NORMSDIST' => array( 294, 1, 1, 0 ),
485 'NORMINV' => array( 295, 3, 1, 0 ),
486 'NORMSINV' => array( 296, 1, 1, 0 ),
487 'STANDARDIZE' => array( 297, 3, 1, 0 ),
488 'ODD' => array( 298, 1, 1, 0 ),
489 'PERMUT' => array( 299, 2, 1, 0 ),
490 'POISSON' => array( 300, 3, 1, 0 ),
491 'TDIST' => array( 301, 3, 1, 0 ),
492 'WEIBULL' => array( 302, 4, 1, 0 ),
493 'SUMXMY2' => array( 303, 2, 2, 0 ),
494 'SUMX2MY2' => array( 304, 2, 2, 0 ),
495 'SUMX2PY2' => array( 305, 2, 2, 0 ),
496 'CHITEST' => array( 306, 2, 2, 0 ),
497 'CORREL' => array( 307, 2, 2, 0 ),
498 'COVAR' => array( 308, 2, 2, 0 ),
499 'FORECAST' => array( 309, 3, 2, 0 ),
500 'FTEST' => array( 310, 2, 2, 0 ),
501 'INTERCEPT' => array( 311, 2, 2, 0 ),
502 'PEARSON' => array( 312, 2, 2, 0 ),
503 'RSQ' => array( 313, 2, 2, 0 ),
504 'STEYX' => array( 314, 2, 2, 0 ),
505 'SLOPE' => array( 315, 2, 2, 0 ),
506 'TTEST' => array( 316, 4, 2, 0 ),
507 'PROB' => array( 317, -1, 2, 0 ),
508 'DEVSQ' => array( 318, -1, 0, 0 ),
509 'GEOMEAN' => array( 319, -1, 0, 0 ),
510 'HARMEAN' => array( 320, -1, 0, 0 ),
511 'SUMSQ' => array( 321, -1, 0, 0 ),
512 'KURT' => array( 322, -1, 0, 0 ),
513 'SKEW' => array( 323, -1, 0, 0 ),
514 'ZTEST' => array( 324, -1, 0, 0 ),
515 'LARGE' => array( 325, 2, 0, 0 ),
516 'SMALL' => array( 326, 2, 0, 0 ),
517 'QUARTILE' => array( 327, 2, 0, 0 ),
518 'PERCENTILE' => array( 328, 2, 0, 0 ),
519 'PERCENTRANK' => array( 329, -1, 0, 0 ),
520 'MODE' => array( 330, -1, 2, 0 ),
521 'TRIMMEAN' => array( 331, 2, 0, 0 ),
522 'TINV' => array( 332, 2, 1, 0 ),
523 'CONCATENATE' => array( 336, -1, 1, 0 ),
524 'POWER' => array( 337, 2, 1, 0 ),
525 'RADIANS' => array( 342, 1, 1, 0 ),
526 'DEGREES' => array( 343, 1, 1, 0 ),
527 'SUBTOTAL' => array( 344, -1, 0, 0 ),
528 'SUMIF' => array( 345, -1, 0, 0 ),
529 'COUNTIF' => array( 346, 2, 0, 0 ),
530 'COUNTBLANK' => array( 347, 1, 0, 0 ),
531 'ROMAN' => array( 354, -1, 1, 0 )
545 if (preg_match(
"/^\"[^\"]{0,255}\"$/", $token)) {
548 } elseif (is_numeric($token)) {
552 } elseif (preg_match(
'/^\$?([A-Ia-i]?[A-Za-z])\$?(\d+)$/',$token)) {
556 } elseif (preg_match(
"/^\w+(\:\w+)?\![A-Ia-i]?[A-Za-z](\d+)$/u",$token)) {
560 } elseif (preg_match(
"/^'[\w -]+(\:[\w -]+)?'\![A-Ia-i]?[A-Za-z](\d+)$/u",$token)) {
564 } elseif (preg_match(
"/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)\:(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/",$token)) {
568 } elseif (preg_match(
"/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/",$token)) {
572 } elseif (preg_match(
"/^\w+(\:\w+)?\!([A-Ia-i]?[A-Za-z])?(\d+)\:([A-Ia-i]?[A-Za-z])?(\d+)$/u",$token)) {
576 } elseif (preg_match(
"/^'[\w -]+(\:[\w -]+)?'\!([A-Ia-i]?[A-Za-z])?(\d+)\:([A-Ia-i]?[A-Za-z])?(\d+)$/u",$token)) {
580 } elseif (isset($this->ptg[$token])) {
581 return pack(
"C", $this->ptg[$token]);
590 } elseif ($token ==
'arg') {
594 return $this->
raiseError(
"Unknown token $token");
606 if ((preg_match(
"/^\d+$/", $num)) and ($num <= 65535)) {
607 return pack(
"Cv", $this->ptg[
'ptgInt'], $num);
609 if ($this->_byte_order) {
612 return pack(
"Cd", $this->ptg[
'ptgNum'], $num);
627 $string = substr($string, 1, strlen($string) - 2);
628 if (strlen($string) > 255) {
629 return $this->
raiseError(
"String is too long");
632 if ($this->_BIFF_version == 0x0500) {
633 return pack(
"CC", $this->ptg[
'ptgStr'], strlen($string)).$string;
634 } elseif ($this->_BIFF_version == 0x0600) {
636 return pack(
"CCC", $this->ptg[
'ptgStr'], strlen($string), $encoding).$string;
651 $args = $this->_functions[$token][1];
652 $volatile = $this->_functions[$token][3];
656 return pack(
"Cv", $this->ptg[
'ptgFuncV'], $this->_functions[$token][0]);
660 return pack(
"CCv", $this->ptg[
'ptgFuncVarV'], $num_args, $this->_functions[$token][0]);
675 if (preg_match(
"/^([A-Ia-i]?[A-Za-z])(\d+)\:([A-Ia-i]?[A-Za-z])(\d+)$/", $range)) {
676 list($cell1, $cell2) = split(
':', $range);
677 } elseif (preg_match(
"/^([A-Ia-i]?[A-Za-z])(\d+)\.\.([A-Ia-i]?[A-Za-z])(\d+)$/", $range)) {
678 list($cell1, $cell2) = split(
'\.\.', $range);
690 list($row1, $col1) = $cell_array1;
695 list($row2, $col2) = $cell_array2;
699 $ptgArea = pack(
"C", $this->ptg[
'ptgArea']);
700 } elseif ($class == 1) {
701 $ptgArea = pack(
"C", $this->ptg[
'ptgAreaV']);
702 } elseif ($class == 2) {
703 $ptgArea = pack(
"C", $this->ptg[
'ptgAreaA']);
708 return $ptgArea . $row1 . $row2 . $col1. $col2;
724 list($ext_ref, $range) = split(
'!', $token);
727 if ($this->_BIFF_version == 0x0500) {
732 } elseif ($this->_BIFF_version == 0x0600) {
740 list($cell1, $cell2) = split(
':', $range);
743 if (preg_match(
"/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/", $cell1)) {
748 list($row1, $col1) = $cell_array1;
753 list($row2, $col2) = $cell_array2;
759 list($row1, $col1, $row2, $col2) = $cells_array;
764 $ptgArea = pack(
"C", $this->ptg[
'ptgArea3d']);
765 } elseif ($class == 1) {
766 $ptgArea = pack(
"C", $this->ptg[
'ptgArea3dV']);
767 } elseif ($class == 2) {
768 $ptgArea = pack(
"C", $this->ptg[
'ptgArea3dA']);
773 return $ptgArea . $ext_ref . $row1 . $row2 . $col1. $col2;
792 list(
$row, $col) = $cell_array;
796 $ptgRef = pack(
"C", $this->ptg[
'ptgRef']);
797 } elseif ($class == 1) {
798 $ptgRef = pack(
"C", $this->ptg[
'ptgRefV']);
799 } elseif ($class == 2) {
800 $ptgRef = pack(
"C", $this->ptg[
'ptgRefA']);
803 return $this->
raiseError(
"Unknown class $class");
805 return $ptgRef.$row.$col;
821 list($ext_ref, $cell) = split(
'!', $cell);
824 if ($this->_BIFF_version == 0x0500) {
829 } elseif ($this->_BIFF_version == 0x0600) {
841 $ptgRef = pack(
"C", $this->ptg[
'ptgRef3d']);
842 } elseif ($class == 1) {
843 $ptgRef = pack(
"C", $this->ptg[
'ptgRef3dV']);
844 } elseif ($class == 2) {
845 $ptgRef = pack(
"C", $this->ptg[
'ptgRef3dA']);
850 return $ptgRef . $ext_ref.
$row . $col;
863 $ext_ref = preg_replace(
"/^'/",
'', $ext_ref);
864 $ext_ref = preg_replace(
"/'$/",
'', $ext_ref);
867 if (preg_match(
"/:/", $ext_ref)) {
868 list($sheet_name1, $sheet_name2) = split(
':', $ext_ref);
872 return $this->
raiseError(
"Unknown sheet name $sheet_name1 in formula");
876 return $this->
raiseError(
"Unknown sheet name $sheet_name2 in formula");
880 if ($sheet1 > $sheet2) {
881 list($sheet1, $sheet2) = array($sheet2, $sheet1);
886 return $this->
raiseError(
"Unknown sheet name $ext_ref in formula");
892 $offset = -1 - $sheet1;
894 return pack(
'vdvv', $offset, 0x00, $sheet1, $sheet2);
909 $ext_ref = preg_replace(
"/^'/",
'', $ext_ref);
910 $ext_ref = preg_replace(
"/'$/",
'', $ext_ref);
913 if (preg_match(
"/:/", $ext_ref)) {
914 list($sheet_name1, $sheet_name2) = split(
':', $ext_ref);
918 return $this->
raiseError(
"Unknown sheet name $sheet_name1 in formula");
922 return $this->
raiseError(
"Unknown sheet name $sheet_name2 in formula");
926 if ($sheet1 > $sheet2) {
927 list($sheet1, $sheet2) = array($sheet2, $sheet1);
932 return $this->
raiseError(
"Unknown sheet name $ext_ref in formula");
938 $supbook_index = 0x00;
939 $ref = pack(
'vvv', $supbook_index, $sheet1, $sheet2);
940 $total_references = count($this->_references);
942 for ($i = 0; $i < $total_references; $i++) {
943 if ($ref == $this->_references[$i]) {
950 $this->_references[$total_references] = $ref;
951 $index = $total_references;
954 return pack(
'v', $index);
967 if (!isset($this->_ext_sheets[$sheet_name])) {
970 return $this->_ext_sheets[$sheet_name];
986 $this->_ext_sheets[$name] = $index;
998 $cell = strtoupper($cell);
1001 return $this->
raiseError(
"Column in: $cell greater than 255");
1004 if (
$row >= 16384) {
1005 return $this->
raiseError(
"Row in: $cell greater than 16384 ");
1009 if ($this->_BIFF_version == 0x0500) {
1010 $row |= $col_rel << 14;
1011 $row |= $row_rel << 15;
1012 $col = pack(
'C', $col);
1013 } elseif ($this->_BIFF_version == 0x0600) {
1014 $col |= $col_rel << 14;
1015 $col |= $row_rel << 15;
1016 $col = pack(
'v', $col);
1020 return array(
$row, $col);
1033 preg_match(
'/(\$)?(\d+)\:(\$)?(\d+)/', $range, $match);
1035 $row1_rel = empty($match[1]) ? 1 : 0;
1037 $row2_rel = empty($match[3]) ? 1 : 0;
1047 if (($row1 >= 16384) or ($row2 >= 16384)) {
1048 return $this->
raiseError(
"Row in: $range greater than 16384 ");
1052 if ($this->_BIFF_version == 0x0500) {
1053 $row1 |= $row1_rel << 14;
1054 $row2 |= $row2_rel << 15;
1055 $col1 = pack(
'C', $col1);
1056 $col2 = pack(
'C', $col2);
1057 } elseif ($this->_BIFF_version == 0x0600) {
1058 $col1 |= $row1_rel << 15;
1059 $col2 |= $row2_rel << 15;
1060 $col1 = pack(
'v', $col1);
1061 $col2 = pack(
'v', $col2);
1063 $row1 = pack(
'v', $row1);
1064 $row2 = pack(
'v', $row2);
1066 return array($row1, $col1, $row2, $col2);
1080 preg_match(
'/(\$)?([A-I]?[A-Z])(\$)?(\d+)/',$cell,$match);
1082 $col_rel = empty($match[1]) ? 1 : 0;
1083 $col_ref = $match[2];
1084 $row_rel = empty($match[3]) ? 1 : 0;
1088 $expn = strlen($col_ref) - 1;
1090 $col_ref_length = strlen($col_ref);
1091 for ($i = 0; $i < $col_ref_length; $i++) {
1092 $col += (ord($col_ref{$i}) - ord(
'A') + 1) * pow(26, $expn);
1100 return array(
$row, $col, $row_rel, $col_rel);
1111 $formula_length = strlen($this->_formula);
1113 if ($i < $formula_length) {
1114 while ($this->_formula{$i} ==
" ") {
1118 if ($i < ($formula_length - 1)) {
1119 $this->_lookahead = $this->_formula{$i+1};
1124 while ($i < $formula_length) {
1125 $token .= $this->_formula{$i};
1126 if ($i < ($formula_length - 1)) {
1127 $this->_lookahead = $this->_formula{$i+1};
1129 $this->_lookahead =
'';
1132 if ($this->
_match($token) !=
'') {
1136 $this->_current_char = $i + 1;
1137 $this->_current_token = $token;
1141 if ($i < ($formula_length - 2)) {
1142 $this->_lookahead = $this->_formula{$i+2};
1144 $this->_lookahead =
'';
1186 if ($this->_lookahead ==
'=') {
1193 if (($this->_lookahead ==
'=') or ($this->_lookahead ==
'>')) {
1215 if (preg_match(
'/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$token) and
1216 !preg_match(
"/[0-9]/",$this->_lookahead) and
1217 ($this->_lookahead !=
':') and ($this->_lookahead !=
'.') and
1218 ($this->_lookahead !=
'!'))
1223 elseif (preg_match(
"/^\w+(\:\w+)?\![A-Ia-i]?[A-Za-z][0-9]+$/u",$token) and
1224 !preg_match(
"/[0-9]/",$this->_lookahead) and
1225 ($this->_lookahead !=
':') and ($this->_lookahead !=
'.'))
1230 elseif (preg_match(
"/^'[\w -]+(\:[\w -]+)?'\![A-Ia-i]?[A-Za-z][0-9]+$/u",$token) and
1231 !preg_match(
"/[0-9]/",$this->_lookahead) and
1232 ($this->_lookahead !=
':') and ($this->_lookahead !=
'.'))
1237 elseif (preg_match(
"/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token) and
1238 !preg_match(
"/[0-9]/",$this->_lookahead))
1243 elseif (preg_match(
"/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token) and
1244 !preg_match(
"/[0-9]/",$this->_lookahead))
1249 elseif (preg_match(
"/^\w+(\:\w+)?\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/u",$token) and
1250 !preg_match(
"/[0-9]/",$this->_lookahead))
1255 elseif (preg_match(
"/^'[\w -]+(\:[\w -]+)?'\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/u",$token) and
1256 !preg_match(
"/[0-9]/",$this->_lookahead))
1261 elseif (is_numeric($token) and
1262 (!is_numeric($token.$this->_lookahead) or ($this->_lookahead ==
'')) and
1263 ($this->_lookahead !=
'!') and ($this->_lookahead !=
':'))
1268 elseif (preg_match(
"/^\"[^\"]{0,255}\"$/",$token))
1273 elseif (preg_match(
"/^[A-Z0-9\xc0-\xdc\.]+$/i",$token) and ($this->_lookahead ==
"("))
1291 $this->_current_char = 0;
1292 $this->_formula = $formula;
1293 $this->_lookahead = $formula{1};
1380 if (preg_match(
"/^\"[^\"]{0,255}\"$/", $this->_current_token)) {
1400 $result2 = $this->
_term();
1407 $result2 = $this->
_term();
1449 $result2 = $this->
_fact();
1456 $result2 = $this->
_fact();
1483 return $this->
raiseError(
"')' token expected.");
1489 if (preg_match(
'/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$this->_current_token))
1496 elseif (preg_match(
"/^\w+(\:\w+)?\![A-Ia-i]?[A-Za-z][0-9]+$/u",$this->_current_token))
1503 elseif (preg_match(
"/^'[\w -]+(\:[\w -]+)?'\![A-Ia-i]?[A-Za-z][0-9]+$/u",$this->_current_token))
1510 elseif (preg_match(
"/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$this->_current_token) or
1511 preg_match(
"/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$this->_current_token))
1518 elseif (preg_match(
"/^\w+(\:\w+)?\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/u",$this->_current_token))
1525 elseif (preg_match(
"/^'[\w -]+(\:[\w -]+)?'\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/u",$this->_current_token))
1531 elseif (is_numeric($this->_current_token))
1538 elseif (preg_match(
"/^[A-Z0-9\xc0-\xdc\.]+$/i",$this->_current_token))
1543 return $this->
raiseError(
"Syntax error: ".$this->_current_token.
1544 ", lookahead: ".$this->_lookahead.
1545 ", current char: ".$this->_current_char);
1558 $function = strtoupper($this->_current_token);
1562 while ($this->_current_token !=
')') {
1564 if ($num_args > 0) {
1570 return $this->
raiseError(
"Syntax error: comma expected in ".
1571 "function $function, arg #{$num_args}");
1587 if (!isset($this->_functions[$function])) {
1588 return $this->
raiseError(
"Function $function() doesn't exist");
1590 $args = $this->_functions[$function][1];
1592 if (($args >= 0) and ($args != $num_args)) {
1593 return $this->
raiseError(
"Incorrect number of arguments in function $function() ");
1613 return array(
'value' => $value,
'left' => $left,
'right' => $right);
1649 if (is_array($tree[
'left'])) {
1652 return $converted_tree;
1654 $polish .= $converted_tree;
1655 } elseif ($tree[
'left'] !=
'') {
1656 $converted_tree = $this->
_convert($tree[
'left']);
1658 return $converted_tree;
1660 $polish .= $converted_tree;
1662 if (is_array($tree[
'right'])) {
1665 return $converted_tree;
1667 $polish .= $converted_tree;
1668 } elseif ($tree[
'right'] !=
'') {
1669 $converted_tree = $this->
_convert($tree[
'right']);
1671 return $converted_tree;
1673 $polish .= $converted_tree;
1676 if (preg_match(
"/^[A-Z0-9\xc0-\xdc\.]+$/",$tree[
'value']) and
1677 !preg_match(
'/^([A-Ia-i]?[A-Za-z])(\d+)$/',$tree[
'value']) and
1678 !preg_match(
"/^[A-Ia-i]?[A-Za-z](\d+)\.\.[A-Ia-i]?[A-Za-z](\d+)$/",$tree[
'value']) and
1679 !is_numeric($tree[
'value']) and
1680 !isset($this->ptg[$tree[
'value']]))
1683 if ($tree[
'left'] !=
'') {
1692 return $left_tree.$this->_convertFunction($tree[
'value'], $tree[
'right']);
1694 $converted_tree = $this->
_convert($tree[
'value']);
1696 return $converted_tree;
1699 $polish .= $converted_tree;
_advance()
Advance to the next valid token.
_initializeHashes()
Initialize the ptg and function hashes.
_getRefIndex($ext_ref)
Look up the REF index that corresponds to an external sheet name (or range).
parse($formula)
The parsing method.
setExtSheet($name, $index)
This method is used to update the array of sheet names.
_expression()
It parses a expression.
const SPREADSHEET_EXCEL_WRITER_LT
SPREADSHEET_EXCEL_WRITER_LT token identifier for character "<"
_convertRange2d($range, $class=0)
Convert an Excel range such as A1:D4 to a ptgRefV.
const SPREADSHEET_EXCEL_WRITER_EQ
SPREADSHEET_EXCEL_WRITER_EQ token identifier for character "="
const SPREADSHEET_EXCEL_WRITER_GE
SPREADSHEET_EXCEL_WRITER_GE token identifier for character ">="
toReversePolish($tree=array())
Builds a string containing the tree in reverse polish notation (What you would use in a HP calculator...
_match($token)
Checks if it's a valid token.
_convert($token)
Convert a token to the proper ptg value.
const SPREADSHEET_EXCEL_WRITER_COMA
SPREADSHEET_EXCEL_WRITER_COMA token identifier for character ","
_fact()
It parses a factor.
_parenthesizedExpression()
This function just introduces a ptgParen element in the tree, so that Excel doesn't get confused when...
_convertNumber($num)
Convert a number token to ptgInt or ptgNum.
const SPREADSHEET_EXCEL_WRITER_DIV
SPREADSHEET_EXCEL_WRITER_DIV token identifier for character "/"
_convertFunction($token, $num_args)
Convert a function to a ptgFunc or ptgFuncVarV depending on the number of args that it takes...
_convertString($string)
Convert a string token to ptgStr.
_cellToPackedRowcol($cell)
pack() row and column into the required 3 or 4 byte format.
_packExtRef($ext_ref)
Convert the sheet name part of an external reference, for example "Sheet1" or "Sheet1:Sheet2", to a packed structure.
const SPREADSHEET_EXCEL_WRITER_MUL
SPREADSHEET_EXCEL_WRITER_MUL token identifier for character "*"
const SPREADSHEET_EXCEL_WRITER_SUB
SPREADSHEET_EXCEL_WRITER_SUB token identifier for character "-"
_condition()
It parses a condition.
const SPREADSHEET_EXCEL_WRITER_CLOSE
SPREADSHEET_EXCEL_WRITER_CLOSE token identifier for character ")"
const SPREADSHEET_EXCEL_WRITER_LE
SPREADSHEET_EXCEL_WRITER_LE token identifier for character "<="
_createTree($value, $left, $right)
Creates a tree.
_convertRef3d($cell)
Convert an Excel 3d reference such as "Sheet1!A1" or "Sheet1:Sheet2!A1" to a ptgRef3d.
const SPREADSHEET_EXCEL_WRITER_ADD
Class for parsing Excel formulas.
_cellToRowcol($cell)
Convert an Excel cell reference such as A1 or $B2 or C$3 or $D$4 to a zero indexed row and column num...
const SPREADSHEET_EXCEL_WRITER_GT
SPREADSHEET_EXCEL_WRITER_GT token identifier for character ">"
_getSheetIndex($sheet_name)
Look up the index that corresponds to an external sheet name.
_rangeToPackedRange($range)
pack() row range into the required 3 or 4 byte format.
_convertRef2d($cell)
Convert an Excel reference such as A1, $B2, C$3 or $D$4 to a ptgRefV.
_func()
It parses a function call.
const SPREADSHEET_EXCEL_WRITER_OPEN
SPREADSHEET_EXCEL_WRITER_OPEN token identifier for character "("
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
const SPREADSHEET_EXCEL_WRITER_NE
SPREADSHEET_EXCEL_WRITER_NE token identifier for character "<>"
_convertRange3d($token)
Convert an Excel 3d range such as "Sheet1!A1:D4" or "Sheet1:Sheet2!A1:D4" to a ptgArea3d.
const SPREADSHEET_EXCEL_WRITER_SEMICOLON
SPREADSHEET_EXCEL_WRITER_SEMICOLON token identifier for character ";"
const SPREADSHEET_EXCEL_WRITER_CONCAT
isError($data, $code=null)
Tell whether a value is a PEAR error.
Spreadsheet_Excel_Writer_Parser($byte_order, $biff_version)
The class constructor.