PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/include/php_writeexcel/class.writeexcel_formula.inc.php

https://bitbucket.org/jhunsinfotech/blue-blues
PHP | 1617 lines | 1448 code | 34 blank | 135 comment | 22 complexity | b12bf25f09dcef4073c657843d07fba7 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * Copyleft 2002 Johann Hanne
  4. *
  5. * This is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this software; if not, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* This file contains source from the PEAR::Spreadsheet class Parser.php file version 0.4 .
  21. The raiseError was replaced by triggerError function.
  22. The PEAR::isError was imported to keep compatibility to PEAR::Spreadsheet class
  23. Imported and adapted by Andreas Brodowski 2003 (andreas.brodowski@oscar-gmbh.com).
  24. There should be no license rights in question because the Parser.php from PEAR class is
  25. published under GNU License the same way like this class.
  26. Changes: 03/08/27 Added SPREADSHEET_EXCEL_WRITER_SCOLON for arg seperation in excel functions
  27. */
  28. /*
  29. * This is the Spreadsheet::WriteExcel Perl package ported to PHP
  30. * Spreadsheet::WriteExcel was written by John McNamara, jmcnamara@cpan.org
  31. */
  32. define('SPREADSHEET_EXCEL_WRITER_ADD',"+");
  33. // @const SPREADSHEET_EXCEL_WRITER_ADD token identifier for character "+"
  34. define('SPREADSHEET_EXCEL_WRITER_SUB',"-");
  35. // @const SPREADSHEET_EXCEL_WRITER_SUB token identifier for character "-"
  36. define('SPREADSHEET_EXCEL_WRITER_MUL',"*");
  37. // @const SPREADSHEET_EXCEL_WRITER_MUL token identifier for character "*"
  38. define('SPREADSHEET_EXCEL_WRITER_DIV',"/");
  39. // @const SPREADSHEET_EXCEL_WRITER_DIV token identifier for character "/"
  40. define('SPREADSHEET_EXCEL_WRITER_OPEN',"(");
  41. // @const SPREADSHEET_EXCEL_WRITER_OPEN token identifier for character "("
  42. define('SPREADSHEET_EXCEL_WRITER_CLOSE',")");
  43. // @const SPREADSHEET_EXCEL_WRITER_CLOSE token identifier for character ")"
  44. define('SPREADSHEET_EXCEL_WRITER_COMA',",");
  45. // @const SPREADSHEET_EXCEL_WRITER_COMA token identifier for character ","
  46. define('SPREADSHEET_EXCEL_WRITER_SCOLON',";");
  47. // @const SPREADSHEET_EXCEL_WRITER_SCOLON token identifier for character ";"
  48. define('SPREADSHEET_EXCEL_WRITER_GT',">");
  49. // @const SPREADSHEET_EXCEL_WRITER_GT token identifier for character ">"
  50. define('SPREADSHEET_EXCEL_WRITER_LT',"<");
  51. // @const SPREADSHEET_EXCEL_WRITER_LT token identifier for character "<"
  52. define('SPREADSHEET_EXCEL_WRITER_LE',"<=");
  53. // @const SPREADSHEET_EXCEL_WRITER_LE token identifier for character "<="
  54. define('SPREADSHEET_EXCEL_WRITER_GE',">=");
  55. // @const SPREADSHEET_EXCEL_WRITER_GE token identifier for character ">="
  56. define('SPREADSHEET_EXCEL_WRITER_EQ',"=");
  57. // @const SPREADSHEET_EXCEL_WRITER_EQ token identifier for character "="
  58. define('SPREADSHEET_EXCEL_WRITER_NE',"<>");
  59. // @const SPREADSHEET_EXCEL_WRITER_NE token identifier for character "<>"
  60. class writeexcel_formula {
  61. ###############################################################################
  62. #
  63. # Class data.
  64. #
  65. var $parser;
  66. var $ptg;
  67. var $_functions;
  68. var $_current_char;
  69. var $_current_token;
  70. var $_lookahead;
  71. var $_debug;
  72. var $_byte_order;
  73. var $_volatile;
  74. var $_workbook;
  75. var $_ext_sheets;
  76. var $_formula;
  77. ###############################################################################
  78. #
  79. # new()
  80. #
  81. # Constructor
  82. #
  83. function writeexcel_formula($byte_order) {
  84. $this->parser = false;
  85. $this->ptg = array();
  86. $this->_functions = array();
  87. $this->_debug = 0;
  88. $this->_byte_order = $byte_order;
  89. $this->_volatile = 0;
  90. $this->_workbook = "";
  91. //$this->_ext_sheets = array();
  92. //Changes made to accomodate PHP5
  93. $this->_ext_sheets ;
  94. $this->_current_token = '';
  95. $this->_lookahead = '';
  96. $this->_current_char = 0;
  97. $this->_formula = '';
  98. }
  99. ###############################################################################
  100. #
  101. # _init_parser()
  102. #
  103. # There is a small overhead involved in generating the parser. Therefore, the
  104. # initialisation is delayed until a formula is required. TODO: use a pre-
  105. # compiled header.
  106. #
  107. function _init_parser() {
  108. $this->_initializeHashes();
  109. if ($this->_debug) {
  110. print "Init_parser.\n\n";
  111. }
  112. }
  113. ###############################################################################
  114. #
  115. # parse_formula()
  116. #
  117. # This is the only public method. It takes a textual description of a formula
  118. # and returns a RPN encoded byte string.
  119. #
  120. function parse_formula() {
  121. $_=func_get_args();
  122. # Initialise the parser if this is the first call
  123. if ($this->parser===false) {
  124. $this->_init_parser();
  125. }
  126. $formula = array_shift($_);
  127. //$str;
  128. //$tokens;
  129. if ($this->_debug) {
  130. print "$formula\n";
  131. }
  132. # Build the parse tree for the formula
  133. $this->_formula = $formula;
  134. $this->_current_char = 0;
  135. $this->_lookahead = $this->_formula{1};
  136. $this->_advance($formula);
  137. $parsetree = $this->_condition();
  138. $str = $this->toReversePolish($parsetree);
  139. return $str;
  140. }
  141. function set_ext_sheet($key, $value) {
  142. $this->_ext_sheets->$key = $value;
  143. }
  144. function isError($data) {
  145. return (bool)(is_object($data) &&
  146. (get_class($data) == 'pear_error' ||
  147. is_subclass_of($data, 'pear_error')));
  148. }
  149. /**
  150. * Class for parsing Excel formulas
  151. *
  152. * @author Xavier Noguer <xnoguer@rezebra.com>
  153. * @category FileFormats
  154. * @package Spreadsheet_Excel_Writer
  155. */
  156. /**
  157. * Initialize the ptg and function hashes.
  158. *
  159. * @access private
  160. */
  161. function _initializeHashes()
  162. {
  163. // The Excel ptg indices
  164. $this->ptg = array(
  165. 'ptgExp' => 0x01,
  166. 'ptgTbl' => 0x02,
  167. 'ptgAdd' => 0x03,
  168. 'ptgSub' => 0x04,
  169. 'ptgMul' => 0x05,
  170. 'ptgDiv' => 0x06,
  171. 'ptgPower' => 0x07, 'ptgConcat' => 0x08,
  172. 'ptgLT' => 0x09,
  173. 'ptgLE' => 0x0A,
  174. 'ptgEQ' => 0x0B,
  175. 'ptgGE' => 0x0C,
  176. 'ptgGT' => 0x0D,
  177. 'ptgNE' => 0x0E,
  178. 'ptgIsect' => 0x0F,
  179. 'ptgUnion' => 0x10,
  180. 'ptgRange' => 0x11,
  181. 'ptgUplus' => 0x12,
  182. 'ptgUminus' => 0x13,
  183. 'ptgPercent' => 0x14,
  184. 'ptgParen' => 0x15,
  185. 'ptgMissArg' => 0x16,
  186. 'ptgStr' => 0x17,
  187. 'ptgAttr' => 0x19,
  188. 'ptgSheet' => 0x1A,
  189. 'ptgEndSheet' => 0x1B,
  190. 'ptgErr' => 0x1C,
  191. 'ptgBool' => 0x1D,
  192. 'ptgInt' => 0x1E,
  193. 'ptgNum' => 0x1F,
  194. 'ptgArray' => 0x20,
  195. 'ptgFunc' => 0x21,
  196. 'ptgFuncVar' => 0x22,
  197. 'ptgName' => 0x23,
  198. 'ptgRef' => 0x24,
  199. 'ptgArea' => 0x25,
  200. 'ptgMemArea' => 0x26,
  201. 'ptgMemErr' => 0x27,
  202. 'ptgMemNoMem' => 0x28,
  203. 'ptgMemFunc' => 0x29,
  204. 'ptgRefErr' => 0x2A,
  205. 'ptgAreaErr' => 0x2B,
  206. 'ptgRefN' => 0x2C,
  207. 'ptgAreaN' => 0x2D,
  208. 'ptgMemAreaN' => 0x2E,
  209. 'ptgMemNoMemN' => 0x2F,
  210. 'ptgNameX' => 0x39,
  211. 'ptgRef3d' => 0x3A,
  212. 'ptgArea3d' => 0x3B,
  213. 'ptgRefErr3d' => 0x3C,
  214. 'ptgAreaErr3d' => 0x3D,
  215. 'ptgArrayV' => 0x40,
  216. 'ptgFuncV' => 0x41,
  217. 'ptgFuncVarV' => 0x42,
  218. 'ptgNameV' => 0x43,
  219. 'ptgRefV' => 0x44,
  220. 'ptgAreaV' => 0x45,
  221. 'ptgMemAreaV' => 0x46,
  222. 'ptgMemErrV' => 0x47,
  223. 'ptgMemNoMemV' => 0x48,
  224. 'ptgMemFuncV' => 0x49,
  225. 'ptgRefErrV' => 0x4A,
  226. 'ptgAreaErrV' => 0x4B,
  227. 'ptgRefNV' => 0x4C,
  228. 'ptgAreaNV' => 0x4D,
  229. 'ptgMemAreaNV' => 0x4E,
  230. 'ptgMemNoMemN' => 0x4F,
  231. 'ptgFuncCEV' => 0x58,
  232. 'ptgNameXV' => 0x59,
  233. 'ptgRef3dV' => 0x5A,
  234. 'ptgArea3dV' => 0x5B, 'ptgRefErr3dV' => 0x5C,
  235. 'ptgAreaErr3d' => 0x5D,
  236. 'ptgArrayA' => 0x60,
  237. 'ptgFuncA' => 0x61,
  238. 'ptgFuncVarA' => 0x62,
  239. 'ptgNameA' => 0x63, 'ptgRefA' => 0x64,
  240. 'ptgAreaA' => 0x65,
  241. 'ptgMemAreaA' => 0x66,
  242. 'ptgMemErrA' => 0x67,
  243. 'ptgMemNoMemA' => 0x68,
  244. 'ptgMemFuncA' => 0x69,
  245. 'ptgRefErrA' => 0x6A,
  246. 'ptgAreaErrA' => 0x6B,
  247. 'ptgRefNA' => 0x6C,
  248. 'ptgAreaNA' => 0x6D,
  249. 'ptgMemAreaNA' => 0x6E,
  250. 'ptgMemNoMemN' => 0x6F,
  251. 'ptgFuncCEA' => 0x78,
  252. 'ptgNameXA' => 0x79,
  253. 'ptgRef3dA' => 0x7A,
  254. 'ptgArea3dA' => 0x7B,
  255. 'ptgRefErr3dA' => 0x7C,
  256. 'ptgAreaErr3d' => 0x7D
  257. );
  258. // Thanks to Michael Meeks and Gnumeric for the initial arg values.
  259. //
  260. // The following hash was generated by "function_locale.pl" in the distro.
  261. // Refer to function_locale.pl for non-English function names.
  262. //
  263. // The array elements are as follow:
  264. // ptg: The Excel function ptg code.
  265. // args: The number of arguments that the function takes:
  266. // >=0 is a fixed number of arguments.
  267. // -1 is a variable number of arguments.
  268. // class: The reference, value or array class of the function args.
  269. // vol: The function is volatile.
  270. //
  271. $this->_functions = array(
  272. // function ptg args class vol
  273. 'COUNT' => array( 0, -1, 0, 0 ),
  274. 'IF' => array( 1, -1, 1, 0 ),
  275. 'ISNA' => array( 2, 1, 1, 0 ),
  276. 'ISERROR' => array( 3, 1, 1, 0 ),
  277. 'SUM' => array( 4, -1, 0, 0 ),
  278. 'AVERAGE' => array( 5, -1, 0, 0 ),
  279. 'MIN' => array( 6, -1, 0, 0 ),
  280. 'MAX' => array( 7, -1, 0, 0 ),
  281. 'ROW' => array( 8, -1, 0, 0 ),
  282. 'COLUMN' => array( 9, -1, 0, 0 ),
  283. 'NA' => array( 10, 0, 0, 0 ),
  284. 'NPV' => array( 11, -1, 1, 0 ),
  285. 'STDEV' => array( 12, -1, 0, 0 ),
  286. 'DOLLAR' => array( 13, -1, 1, 0 ),
  287. 'FIXED' => array( 14, -1, 1, 0 ),
  288. 'SIN' => array( 15, 1, 1, 0 ),
  289. 'COS' => array( 16, 1, 1, 0 ),
  290. 'TAN' => array( 17, 1, 1, 0 ),
  291. 'ATAN' => array( 18, 1, 1, 0 ),
  292. 'PI' => array( 19, 0, 1, 0 ),
  293. 'SQRT' => array( 20, 1, 1, 0 ),
  294. 'EXP' => array( 21, 1, 1, 0 ),
  295. 'LN' => array( 22, 1, 1, 0 ),
  296. 'LOG10' => array( 23, 1, 1, 0 ),
  297. 'ABS' => array( 24, 1, 1, 0 ),
  298. 'INT' => array( 25, 1, 1, 0 ),
  299. 'SIGN' => array( 26, 1, 1, 0 ),
  300. 'ROUND' => array( 27, 2, 1, 0 ),
  301. 'LOOKUP' => array( 28, -1, 0, 0 ),
  302. 'INDEX' => array( 29, -1, 0, 1 ),
  303. 'REPT' => array( 30, 2, 1, 0 ),
  304. 'MID' => array( 31, 3, 1, 0 ),
  305. 'LEN' => array( 32, 1, 1, 0 ),
  306. 'VALUE' => array( 33, 1, 1, 0 ),
  307. 'TRUE' => array( 34, 0, 1, 0 ),
  308. 'FALSE' => array( 35, 0, 1, 0 ),
  309. 'AND' => array( 36, -1, 0, 0 ),
  310. 'OR' => array( 37, -1, 0, 0 ),
  311. 'NOT' => array( 38, 1, 1, 0 ),
  312. 'MOD' => array( 39, 2, 1, 0 ),
  313. 'DCOUNT' => array( 40, 3, 0, 0 ),
  314. 'DSUM' => array( 41, 3, 0, 0 ),
  315. 'DAVERAGE' => array( 42, 3, 0, 0 ),
  316. 'DMIN' => array( 43, 3, 0, 0 ),
  317. 'DMAX' => array( 44, 3, 0, 0 ),
  318. 'DSTDEV' => array( 45, 3, 0, 0 ),
  319. 'VAR' => array( 46, -1, 0, 0 ),
  320. 'DVAR' => array( 47, 3, 0, 0 ),
  321. 'TEXT' => array( 48, 2, 1, 0 ),
  322. 'LINEST' => array( 49, -1, 0, 0 ),
  323. 'TREND' => array( 50, -1, 0, 0 ),
  324. 'LOGEST' => array( 51, -1, 0, 0 ),
  325. 'GROWTH' => array( 52, -1, 0, 0 ),
  326. 'PV' => array( 56, -1, 1, 0 ),
  327. 'FV' => array( 57, -1, 1, 0 ),
  328. 'NPER' => array( 58, -1, 1, 0 ),
  329. 'PMT' => array( 59, -1, 1, 0 ),
  330. 'RATE' => array( 60, -1, 1, 0 ),
  331. 'MIRR' => array( 61, 3, 0, 0 ),
  332. 'IRR' => array( 62, -1, 0, 0 ),
  333. 'RAND' => array( 63, 0, 1, 1 ),
  334. 'MATCH' => array( 64, -1, 0, 0 ),
  335. 'DATE' => array( 65, 3, 1, 0 ),
  336. 'TIME' => array( 66, 3, 1, 0 ),
  337. 'DAY' => array( 67, 1, 1, 0 ),
  338. 'MONTH' => array( 68, 1, 1, 0 ),
  339. 'YEAR' => array( 69, 1, 1, 0 ),
  340. 'WEEKDAY' => array( 70, -1, 1, 0 ),
  341. 'HOUR' => array( 71, 1, 1, 0 ),
  342. 'MINUTE' => array( 72, 1, 1, 0 ),
  343. 'SECOND' => array( 73, 1, 1, 0 ),
  344. 'NOW' => array( 74, 0, 1, 1 ),
  345. 'AREAS' => array( 75, 1, 0, 1 ),
  346. 'ROWS' => array( 76, 1, 0, 1 ),
  347. 'COLUMNS' => array( 77, 1, 0, 1 ),
  348. 'OFFSET' => array( 78, -1, 0, 1 ),
  349. 'SEARCH' => array( 82, -1, 1, 0 ),
  350. 'TRANSPOSE' => array( 83, 1, 1, 0 ),
  351. 'TYPE' => array( 86, 1, 1, 0 ),
  352. 'ATAN2' => array( 97, 2, 1, 0 ),
  353. 'ASIN' => array( 98, 1, 1, 0 ),
  354. 'ACOS' => array( 99, 1, 1, 0 ),
  355. 'CHOOSE' => array( 100, -1, 1, 0 ),
  356. 'HLOOKUP' => array( 101, -1, 0, 0 ),
  357. 'VLOOKUP' => array( 102, -1, 0, 0 ),
  358. 'ISREF' => array( 105, 1, 0, 0 ),
  359. 'LOG' => array( 109, -1, 1, 0 ),
  360. 'CHAR' => array( 111, 1, 1, 0 ),
  361. 'LOWER' => array( 112, 1, 1, 0 ),
  362. 'UPPER' => array( 113, 1, 1, 0 ),
  363. 'PROPER' => array( 114, 1, 1, 0 ),
  364. 'LEFT' => array( 115, -1, 1, 0 ),
  365. 'RIGHT' => array( 116, -1, 1, 0 ),
  366. 'EXACT' => array( 117, 2, 1, 0 ),
  367. 'TRIM' => array( 118, 1, 1, 0 ),
  368. 'REPLACE' => array( 119, 4, 1, 0 ),
  369. 'SUBSTITUTE' => array( 120, -1, 1, 0 ),
  370. 'CODE' => array( 121, 1, 1, 0 ),
  371. 'FIND' => array( 124, -1, 1, 0 ),
  372. 'CELL' => array( 125, -1, 0, 1 ),
  373. 'ISERR' => array( 126, 1, 1, 0 ),
  374. 'ISTEXT' => array( 127, 1, 1, 0 ),
  375. 'ISNUMBER' => array( 128, 1, 1, 0 ),
  376. 'ISBLANK' => array( 129, 1, 1, 0 ),
  377. 'T' => array( 130, 1, 0, 0 ),
  378. 'N' => array( 131, 1, 0, 0 ),
  379. 'DATEVALUE' => array( 140, 1, 1, 0 ),
  380. 'TIMEVALUE' => array( 141, 1, 1, 0 ),
  381. 'SLN' => array( 142, 3, 1, 0 ),
  382. 'SYD' => array( 143, 4, 1, 0 ),
  383. 'DDB' => array( 144, -1, 1, 0 ),
  384. 'INDIRECT' => array( 148, -1, 1, 1 ),
  385. 'CALL' => array( 150, -1, 1, 0 ),
  386. 'CLEAN' => array( 162, 1, 1, 0 ),
  387. 'MDETERM' => array( 163, 1, 2, 0 ),
  388. 'MINVERSE' => array( 164, 1, 2, 0 ),
  389. 'MMULT' => array( 165, 2, 2, 0 ),
  390. 'IPMT' => array( 167, -1, 1, 0 ),
  391. 'PPMT' => array( 168, -1, 1, 0 ),
  392. 'COUNTA' => array( 169, -1, 0, 0 ),
  393. 'PRODUCT' => array( 183, -1, 0, 0 ),
  394. 'FACT' => array( 184, 1, 1, 0 ),
  395. 'DPRODUCT' => array( 189, 3, 0, 0 ),
  396. 'ISNONTEXT' => array( 190, 1, 1, 0 ),
  397. 'STDEVP' => array( 193, -1, 0, 0 ),
  398. 'VARP' => array( 194, -1, 0, 0 ),
  399. 'DSTDEVP' => array( 195, 3, 0, 0 ),
  400. 'DVARP' => array( 196, 3, 0, 0 ),
  401. 'TRUNC' => array( 197, -1, 1, 0 ),
  402. 'ISLOGICAL' => array( 198, 1, 1, 0 ),
  403. 'DCOUNTA' => array( 199, 3, 0, 0 ),
  404. 'ROUNDUP' => array( 212, 2, 1, 0 ),
  405. 'ROUNDDOWN' => array( 213, 2, 1, 0 ),
  406. 'RANK' => array( 216, -1, 0, 0 ),
  407. 'ADDRESS' => array( 219, -1, 1, 0 ),
  408. 'DAYS360' => array( 220, -1, 1, 0 ),
  409. 'TODAY' => array( 221, 0, 1, 1 ),
  410. 'VDB' => array( 222, -1, 1, 0 ),
  411. 'MEDIAN' => array( 227, -1, 0, 0 ),
  412. 'SUMPRODUCT' => array( 228, -1, 2, 0 ),
  413. 'SINH' => array( 229, 1, 1, 0 ),
  414. 'COSH' => array( 230, 1, 1, 0 ),
  415. 'TANH' => array( 231, 1, 1, 0 ),
  416. 'ASINH' => array( 232, 1, 1, 0 ),
  417. 'ACOSH' => array( 233, 1, 1, 0 ),
  418. 'ATANH' => array( 234, 1, 1, 0 ),
  419. 'DGET' => array( 235, 3, 0, 0 ),
  420. 'INFO' => array( 244, 1, 1, 1 ),
  421. 'DB' => array( 247, -1, 1, 0 ),
  422. 'FREQUENCY' => array( 252, 2, 0, 0 ),
  423. 'ERROR.TYPE' => array( 261, 1, 1, 0 ),
  424. 'REGISTER.ID' => array( 267, -1, 1, 0 ),
  425. 'AVEDEV' => array( 269, -1, 0, 0 ),
  426. 'BETADIST' => array( 270, -1, 1, 0 ),
  427. 'GAMMALN' => array( 271, 1, 1, 0 ),
  428. 'BETAINV' => array( 272, -1, 1, 0 ),
  429. 'BINOMDIST' => array( 273, 4, 1, 0 ),
  430. 'CHIDIST' => array( 274, 2, 1, 0 ),
  431. 'CHIINV' => array( 275, 2, 1, 0 ),
  432. 'COMBIN' => array( 276, 2, 1, 0 ),
  433. 'CONFIDENCE' => array( 277, 3, 1, 0 ),
  434. 'CRITBINOM' => array( 278, 3, 1, 0 ),
  435. 'EVEN' => array( 279, 1, 1, 0 ),
  436. 'EXPONDIST' => array( 280, 3, 1, 0 ),
  437. 'FDIST' => array( 281, 3, 1, 0 ),
  438. 'FINV' => array( 282, 3, 1, 0 ),
  439. 'FISHER' => array( 283, 1, 1, 0 ),
  440. 'FISHERINV' => array( 284, 1, 1, 0 ),
  441. 'FLOOR' => array( 285, 2, 1, 0 ),
  442. 'GAMMADIST' => array( 286, 4, 1, 0 ),
  443. 'GAMMAINV' => array( 287, 3, 1, 0 ),
  444. 'CEILING' => array( 288, 2, 1, 0 ),
  445. 'HYPGEOMDIST' => array( 289, 4, 1, 0 ),
  446. 'LOGNORMDIST' => array( 290, 3, 1, 0 ),
  447. 'LOGINV' => array( 291, 3, 1, 0 ),
  448. 'NEGBINOMDIST' => array( 292, 3, 1, 0 ),
  449. 'NORMDIST' => array( 293, 4, 1, 0 ),
  450. 'NORMSDIST' => array( 294, 1, 1, 0 ),
  451. 'NORMINV' => array( 295, 3, 1, 0 ),
  452. 'NORMSINV' => array( 296, 1, 1, 0 ),
  453. 'STANDARDIZE' => array( 297, 3, 1, 0 ),
  454. 'ODD' => array( 298, 1, 1, 0 ),
  455. 'PERMUT' => array( 299, 2, 1, 0 ),
  456. 'POISSON' => array( 300, 3, 1, 0 ),
  457. 'TDIST' => array( 301, 3, 1, 0 ),
  458. 'WEIBULL' => array( 302, 4, 1, 0 ),
  459. 'SUMXMY2' => array( 303, 2, 2, 0 ),
  460. 'SUMX2MY2' => array( 304, 2, 2, 0 ),
  461. 'SUMX2PY2' => array( 305, 2, 2, 0 ),
  462. 'CHITEST' => array( 306, 2, 2, 0 ),
  463. 'CORREL' => array( 307, 2, 2, 0 ),
  464. 'COVAR' => array( 308, 2, 2, 0 ),
  465. 'FORECAST' => array( 309, 3, 2, 0 ),
  466. 'FTEST' => array( 310, 2, 2, 0 ),
  467. 'INTERCEPT' => array( 311, 2, 2, 0 ),
  468. 'PEARSON' => array( 312, 2, 2, 0 ),
  469. 'RSQ' => array( 313, 2, 2, 0 ),
  470. 'STEYX' => array( 314, 2, 2, 0 ),
  471. 'SLOPE' => array( 315, 2, 2, 0 ),
  472. 'TTEST' => array( 316, 4, 2, 0 ),
  473. 'PROB' => array( 317, -1, 2, 0 ),
  474. 'DEVSQ' => array( 318, -1, 0, 0 ),
  475. 'GEOMEAN' => array( 319, -1, 0, 0 ),
  476. 'HARMEAN' => array( 320, -1, 0, 0 ),
  477. 'SUMSQ' => array( 321, -1, 0, 0 ),
  478. 'KURT' => array( 322, -1, 0, 0 ),
  479. 'SKEW' => array( 323, -1, 0, 0 ),
  480. 'ZTEST' => array( 324, -1, 0, 0 ),
  481. 'LARGE' => array( 325, 2, 0, 0 ),
  482. 'SMALL' => array( 326, 2, 0, 0 ),
  483. 'QUARTILE' => array( 327, 2, 0, 0 ),
  484. 'PERCENTILE' => array( 328, 2, 0, 0 ),
  485. 'PERCENTRANK' => array( 329, -1, 0, 0 ),
  486. 'MODE' => array( 330, -1, 2, 0 ),
  487. 'TRIMMEAN' => array( 331, 2, 0, 0 ),
  488. 'TINV' => array( 332, 2, 1, 0 ),
  489. 'CONCATENATE' => array( 336, -1, 1, 0 ),
  490. 'POWER' => array( 337, 2, 1, 0 ),
  491. 'RADIANS' => array( 342, 1, 1, 0 ),
  492. 'DEGREES' => array( 343, 1, 1, 0 ),
  493. 'SUBTOTAL' => array( 344, -1, 0, 0 ),
  494. 'SUMIF' => array( 345, -1, 0, 0 ),
  495. 'COUNTIF' => array( 346, 2, 0, 0 ),
  496. 'COUNTBLANK' => array( 347, 1, 0, 0 ),
  497. 'ROMAN' => array( 354, -1, 1, 0 )
  498. );
  499. }
  500. /**
  501. * Convert a token to the proper ptg value.
  502. *
  503. * @access private
  504. * @param mixed $token The token to convert.
  505. * @return mixed the converted token on success. PEAR_Error if the token
  506. * is not recognized
  507. */
  508. function _convert($token)
  509. {
  510. if (preg_match("/^\"[^\"]{0,255}\"$/", $token))
  511. {
  512. return $this->_convertString($token);
  513. }
  514. elseif (is_numeric($token))
  515. {
  516. return $this->_convertNumber($token);
  517. }
  518. // match references like A1 or $A$1
  519. elseif (preg_match('/^\$?([A-Ia-i]?[A-Za-z])\$?(\d+)$/',$token))
  520. {
  521. return $this->_convertRef2d($token);
  522. }
  523. // match external references like Sheet1:Sheet2!A1
  524. elseif (preg_match("/^[A-Za-z0-9_]+(\:[A-Za-z0-9_]+)?\![A-Ia-i]?[A-Za-z](\d+)$/",$token))
  525. {
  526. return $this->_convertRef3d($token);
  527. }
  528. // match ranges like A1:B2
  529. elseif (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)\:(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/",$token))
  530. {
  531. return $this->_convertRange2d($token);
  532. }
  533. // match ranges like A1..B2
  534. elseif (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/",$token))
  535. {
  536. return $this->_convertRange2d($token);
  537. }
  538. // match external ranges like Sheet1:Sheet2!A1:B2
  539. elseif (preg_match("/^[A-Za-z0-9_]+(\:[A-Za-z0-9_]+)?\!([A-Ia-i]?[A-Za-z])?(\d+)\:([A-Ia-i]?[A-Za-z])?(\d+)$/",$token))
  540. {
  541. return $this->_convertRange3d($token);
  542. }
  543. // match external ranges like 'Sheet1:Sheet2'!A1:B2
  544. elseif (preg_match("/^'[A-Za-z0-9_ ]+(\:[A-Za-z0-9_ ]+)?'\!([A-Ia-i]?[A-Za-z])?(\d+)\:([A-Ia-i]?[A-Za-z])?(\d+)$/",$token))
  545. {
  546. return $this->_convertRange3d($token);
  547. }
  548. elseif (isset($this->ptg[$token])) // operators (including parentheses)
  549. {
  550. return pack("C", $this->ptg[$token]);
  551. }
  552. // commented so argument number can be processed correctly. See toReversePolish().
  553. /*elseif (preg_match("/[A-Z0-9\xc0-\xdc\.]+/",$token))
  554. {
  555. return($this->_convertFunction($token,$this->_func_args));
  556. }*/
  557. // if it's an argument, ignore the token (the argument remains)
  558. elseif ($token == 'arg')
  559. {
  560. return '';
  561. }
  562. // TODO: use real error codes
  563. trigger_error("Unknown token $token", E_USER_ERROR);
  564. }
  565. /**
  566. * Convert a number token to ptgInt or ptgNum
  567. *
  568. * @access private
  569. * @param mixed $num an integer or double for conversion to its ptg value
  570. */
  571. function _convertNumber($num)
  572. {
  573. // Integer in the range 0..2**16-1
  574. if ((preg_match("/^\d+$/",$num)) and ($num <= 65535)) {
  575. return(pack("Cv", $this->ptg['ptgInt'], $num));
  576. }
  577. else { // A float
  578. if ($this->_byte_order) { // if it's Big Endian
  579. $num = strrev($num);
  580. }
  581. return pack("Cd", $this->ptg['ptgNum'], $num);
  582. }
  583. }
  584. /**
  585. * Convert a string token to ptgStr
  586. *
  587. * @access private
  588. * @param string $string A string for conversion to its ptg value
  589. */
  590. function _convertString($string)
  591. {
  592. // chop away beggining and ending quotes
  593. $string = substr($string, 1, strlen($string) - 2);
  594. return pack("CC", $this->ptg['ptgStr'], strlen($string)).$string;
  595. }
  596. /**
  597. * Convert a function to a ptgFunc or ptgFuncVarV depending on the number of
  598. * args that it takes.
  599. *
  600. * @access private
  601. * @param string $token The name of the function for convertion to ptg value.
  602. * @param integer $num_args The number of arguments the function receives.
  603. * @return string The packed ptg for the function
  604. */
  605. function _convertFunction($token, $num_args)
  606. {
  607. $args = $this->_functions[$token][1];
  608. $volatile = $this->_functions[$token][3];
  609. // Fixed number of args eg. TIME($i,$j,$k).
  610. if ($args >= 0) {
  611. return pack("Cv", $this->ptg['ptgFuncV'], $this->_functions[$token][0]);
  612. }
  613. // Variable number of args eg. SUM($i,$j,$k, ..).
  614. if ($args == -1) {
  615. return pack("CCv", $this->ptg['ptgFuncVarV'], $num_args, $this->_functions[$token][0]);
  616. }
  617. }
  618. /**
  619. * Convert an Excel range such as A1:D4 to a ptgRefV.
  620. *
  621. * @access private
  622. * @param string $range An Excel range in the A1:A2 or A1..A2 format.
  623. */
  624. function _convertRange2d($range)
  625. {
  626. $class = 2; // as far as I know, this is magick.
  627. // Split the range into 2 cell refs
  628. if (preg_match("/^([A-Ia-i]?[A-Za-z])(\d+)\:([A-Ia-i]?[A-Za-z])(\d+)$/",$range)) {
  629. list($cell1, $cell2) = split(':', $range);
  630. }
  631. elseif (preg_match("/^([A-Ia-i]?[A-Za-z])(\d+)\.\.([A-Ia-i]?[A-Za-z])(\d+)$/",$range)) {
  632. list($cell1, $cell2) = split('\.\.', $range);
  633. }
  634. else {
  635. // TODO: use real error codes
  636. trigger_error("Unknown range separator", E_USER_ERROR);
  637. }
  638. // Convert the cell references
  639. $cell_array1 = $this->_cellToPackedRowcol($cell1);
  640. if ($this->isError($cell_array1)) {
  641. return $cell_array1;
  642. }
  643. list($row1, $col1) = $cell_array1;
  644. $cell_array2 = $this->_cellToPackedRowcol($cell2);
  645. if ($this->isError($cell_array2)) {
  646. return $cell_array2;
  647. }
  648. list($row2, $col2) = $cell_array2;
  649. // The ptg value depends on the class of the ptg.
  650. if ($class == 0) {
  651. $ptgArea = pack("C", $this->ptg['ptgArea']);
  652. }
  653. elseif ($class == 1) {
  654. $ptgArea = pack("C", $this->ptg['ptgAreaV']);
  655. }
  656. elseif ($class == 2) {
  657. $ptgArea = pack("C", $this->ptg['ptgAreaA']);
  658. }
  659. else {
  660. // TODO: use real error codes
  661. trigger_error("Unknown class $class", E_USER_ERROR);
  662. }
  663. return $ptgArea . $row1 . $row2 . $col1. $col2;
  664. }
  665. /**
  666. * Convert an Excel 3d range such as "Sheet1!A1:D4" or "Sheet1:Sheet2!A1:D4" to
  667. * a ptgArea3dV.
  668. *
  669. * @access private
  670. * @param string $token An Excel range in the Sheet1!A1:A2 format.
  671. */
  672. function _convertRange3d($token)
  673. {
  674. $class = 2; // as far as I know, this is magick.
  675. // Split the ref at the ! symbol
  676. list($ext_ref, $range) = split('!', $token);
  677. // Convert the external reference part
  678. $ext_ref = $this->_packExtRef($ext_ref);
  679. if ($this->isError($ext_ref)) {
  680. return $ext_ref;
  681. }
  682. // Split the range into 2 cell refs
  683. list($cell1, $cell2) = split(':', $range);
  684. // Convert the cell references
  685. if (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?(\d+)$/", $cell1))
  686. {
  687. $cell_array1 = $this->_cellToPackedRowcol($cell1);
  688. if ($this->isError($cell_array1)) {
  689. return $cell_array1;
  690. }
  691. list($row1, $col1) = $cell_array1;
  692. $cell_array2 = $this->_cellToPackedRowcol($cell2);
  693. if ($this->isError($cell_array2)) {
  694. return $cell_array2;
  695. }
  696. list($row2, $col2) = $cell_array2;
  697. }
  698. else { // It's a columns range (like 26:27)
  699. $cells_array = $this->_rangeToPackedRange($cell1.':'.$cell2);
  700. if ($this->isError($cells_array)) {
  701. return $cells_array;
  702. }
  703. list($row1, $col1, $row2, $col2) = $cells_array;
  704. }
  705. // The ptg value depends on the class of the ptg.
  706. if ($class == 0) {
  707. $ptgArea = pack("C", $this->ptg['ptgArea3d']);
  708. }
  709. elseif ($class == 1) {
  710. $ptgArea = pack("C", $this->ptg['ptgArea3dV']);
  711. }
  712. elseif ($class == 2) {
  713. $ptgArea = pack("C", $this->ptg['ptgArea3dA']);
  714. }
  715. else {
  716. trigger_error("Unknown class $class", E_USER_ERROR);
  717. }
  718. return $ptgArea . $ext_ref . $row1 . $row2 . $col1. $col2;
  719. }
  720. /**
  721. * Convert an Excel reference such as A1, $B2, C$3 or $D$4 to a ptgRefV.
  722. *
  723. * @access private
  724. * @param string $cell An Excel cell reference
  725. * @return string The cell in packed() format with the corresponding ptg
  726. */
  727. function _convertRef2d($cell)
  728. {
  729. $class = 2; // as far as I know, this is magick.
  730. // Convert the cell reference
  731. $cell_array = $this->_cellToPackedRowcol($cell);
  732. if ($this->isError($cell_array)) {
  733. return $cell_array;
  734. }
  735. list($row, $col) = $cell_array;
  736. // The ptg value depends on the class of the ptg.
  737. if ($class == 0) {
  738. $ptgRef = pack("C", $this->ptg['ptgRef']);
  739. }
  740. elseif ($class == 1) {
  741. $ptgRef = pack("C", $this->ptg['ptgRefV']);
  742. }
  743. elseif ($class == 2) {
  744. $ptgRef = pack("C", $this->ptg['ptgRefA']);
  745. }
  746. else {
  747. // TODO: use real error codes
  748. trigger_error("Unknown class $class",E_USER_ERROR);
  749. }
  750. return $ptgRef.$row.$col;
  751. }
  752. /**
  753. * Convert an Excel 3d reference such as "Sheet1!A1" or "Sheet1:Sheet2!A1" to a
  754. * ptgRef3dV.
  755. *
  756. * @access private
  757. * @param string $cell An Excel cell reference
  758. * @return string The cell in packed() format with the corresponding ptg
  759. */
  760. function _convertRef3d($cell)
  761. {
  762. $class = 2; // as far as I know, this is magick.
  763. // Split the ref at the ! symbol
  764. list($ext_ref, $cell) = split('!', $cell);
  765. // Convert the external reference part
  766. $ext_ref = $this->_packExtRef($ext_ref);
  767. if ($this->isError($ext_ref)) {
  768. return $ext_ref;
  769. }
  770. // Convert the cell reference part
  771. list($row, $col) = $this->_cellToPackedRowcol($cell);
  772. // The ptg value depends on the class of the ptg.
  773. if ($class == 0) {
  774. $ptgRef = pack("C", $this->ptg['ptgRef3d']);
  775. } elseif ($class == 1) {
  776. $ptgRef = pack("C", $this->ptg['ptgRef3dV']);
  777. } elseif ($class == 2) {
  778. $ptgRef = pack("C", $this->ptg['ptgRef3dA']);
  779. }
  780. else {
  781. trigger_error("Unknown class $class", E_USER_ERROR);
  782. }
  783. return $ptgRef . $ext_ref. $row . $col;
  784. }
  785. /**
  786. * Convert the sheet name part of an external reference, for example "Sheet1" or
  787. * "Sheet1:Sheet2", to a packed structure.
  788. *
  789. * @access private
  790. * @param string $ext_ref The name of the external reference
  791. * @return string The reference index in packed() format
  792. */
  793. function _packExtRef($ext_ref) {
  794. $ext_ref = preg_replace("/^'/", '', $ext_ref); // Remove leading ' if any.
  795. $ext_ref = preg_replace("/'$/", '', $ext_ref); // Remove trailing ' if any.
  796. // Check if there is a sheet range eg., Sheet1:Sheet2.
  797. if (preg_match("/:/", $ext_ref))
  798. {
  799. list($sheet_name1, $sheet_name2) = split(':', $ext_ref);
  800. $sheet1 = $this->_getSheetIndex($sheet_name1);
  801. if ($sheet1 == -1) {
  802. trigger_error("Unknown sheet name $sheet_name1 in formula",E_USER_ERROR);
  803. }
  804. $sheet2 = $this->_getSheetIndex($sheet_name2);
  805. if ($sheet2 == -1) {
  806. trigger_error("Unknown sheet name $sheet_name2 in formula",E_USER_ERROR);
  807. }
  808. // Reverse max and min sheet numbers if necessary
  809. if ($sheet1 > $sheet2) {
  810. list($sheet1, $sheet2) = array($sheet2, $sheet1);
  811. }
  812. }
  813. else { // Single sheet name only.
  814. $sheet1 = $this->_getSheetIndex($ext_ref);
  815. if ($sheet1 == -1) {
  816. trigger_error("Unknown sheet name $ext_ref in formula",E_USER_ERROR);
  817. }
  818. $sheet2 = $sheet1;
  819. }
  820. // References are stored relative to 0xFFFF.
  821. $offset = -1 - $sheet1;
  822. return pack('vdvv', $offset, 0x00, $sheet1, $sheet2);
  823. }
  824. /**
  825. * Look up the index that corresponds to an external sheet name. The hash of
  826. * sheet names is updated by the addworksheet() method of the
  827. * Spreadsheet_Excel_Writer_Workbook class.
  828. *
  829. * @access private
  830. * @return integer
  831. */
  832. function _getSheetIndex($sheet_name)
  833. {
  834. if (!isset($this->_ext_sheets[$sheet_name])) {
  835. return -1;
  836. }
  837. else {
  838. return $this->_ext_sheets[$sheet_name];
  839. }
  840. }
  841. /**
  842. * This method is used to update the array of sheet names. It is
  843. * called by the addWorksheet() method of the Spreadsheet_Excel_Writer_Workbook class.
  844. *
  845. * @access private
  846. * @param string $name The name of the worksheet being added
  847. * @param integer $index The index of the worksheet being added
  848. */
  849. function setExtSheet($name, $index)
  850. {
  851. $this->_ext_sheets[$name] = $index;
  852. }
  853. /**
  854. * pack() row and column into the required 3 byte format.
  855. *
  856. * @access private
  857. * @param string $cell The Excel cell reference to be packed
  858. * @return array Array containing the row and column in packed() format
  859. */
  860. function _cellToPackedRowcol($cell)
  861. {
  862. $cell = strtoupper($cell);
  863. list($row, $col, $row_rel, $col_rel) = $this->_cellToRowcol($cell);
  864. if ($col >= 256) {
  865. trigger_error("Column in: $cell greater than 255", E_USER_ERROR);
  866. }
  867. if ($row >= 16384) {
  868. trigger_error("Row in: $cell greater than 16384 ", E_USER_ERROR);
  869. }
  870. // Set the high bits to indicate if row or col are relative.
  871. $row |= $col_rel << 14;
  872. $row |= $row_rel << 15;
  873. $row = pack('v', $row);
  874. $col = pack('C', $col);
  875. return array($row, $col);
  876. }
  877. /**
  878. * pack() row range into the required 3 byte format.
  879. * Just using maximun col/rows, which is probably not the correct solution
  880. *
  881. * @access private
  882. * @param string $range The Excel range to be packed
  883. * @return array Array containing (row1,col1,row2,col2) in packed() format
  884. */
  885. function _rangeToPackedRange($range)
  886. {
  887. preg_match('/(\$)?(\d+)\:(\$)?(\d+)/', $range, $match);
  888. // return absolute rows if there is a $ in the ref
  889. $row1_rel = empty($match[1]) ? 1 : 0;
  890. $row1 = $match[2];
  891. $row2_rel = empty($match[3]) ? 1 : 0;
  892. $row2 = $match[4];
  893. // Convert 1-index to zero-index
  894. $row1--;
  895. $row2--;
  896. // Trick poor inocent Excel
  897. $col1 = 0;
  898. $col2 = 16383; // maximum possible value for Excel 5 (change this!!!)
  899. //list($row, $col, $row_rel, $col_rel) = $this->_cellToRowcol($cell);
  900. if (($row1 >= 16384) or ($row2 >= 16384)) {
  901. trigger_error("Row in: $range greater than 16384 ",E_USER_ERROR);
  902. }
  903. // Set the high bits to indicate if rows are relative.
  904. $row1 |= $row1_rel << 14;
  905. $row2 |= $row2_rel << 15;
  906. $row1 = pack('v', $row1);
  907. $row2 = pack('v', $row2);
  908. $col1 = pack('C', $col1);
  909. $col2 = pack('C', $col2);
  910. return array($row1, $col1, $row2, $col2);
  911. }
  912. /**
  913. * Convert an Excel cell reference such as A1 or $B2 or C$3 or $D$4 to a zero
  914. * indexed row and column number. Also returns two (0,1) values to indicate
  915. * whether the row or column are relative references.
  916. *
  917. * @access private
  918. * @param string $cell The Excel cell reference in A1 format.
  919. * @return array
  920. */
  921. function _cellToRowcol($cell)
  922. {
  923. preg_match('/(\$)?([A-I]?[A-Z])(\$)?(\d+)/',$cell,$match);
  924. // return absolute column if there is a $ in the ref
  925. $col_rel = empty($match[1]) ? 1 : 0;
  926. $col_ref = $match[2];
  927. $row_rel = empty($match[3]) ? 1 : 0;
  928. $row = $match[4];
  929. // Convert base26 column string to a number.
  930. $expn = strlen($col_ref) - 1;
  931. $col = 0;
  932. for ($i=0; $i < strlen($col_ref); $i++)
  933. {
  934. $col += (ord($col_ref{$i}) - ord('A') + 1) * pow(26, $expn);
  935. $expn--;
  936. }
  937. // Convert 1-index to zero-index
  938. $row--;
  939. $col--;
  940. return array($row, $col, $row_rel, $col_rel);
  941. }
  942. /**
  943. * Advance to the next valid token.
  944. *
  945. * @access private
  946. */
  947. function _advance()
  948. {
  949. $i = $this->_current_char;
  950. // eat up white spaces
  951. if ($i < strlen($this->_formula))
  952. {
  953. while ($this->_formula{$i} == " ") {
  954. $i++;
  955. }
  956. if ($i < strlen($this->_formula) - 1) {
  957. $this->_lookahead = $this->_formula{$i+1};
  958. }
  959. $token = "";
  960. }
  961. while ($i < strlen($this->_formula))
  962. {
  963. $token .= $this->_formula{$i};
  964. if ($i < strlen($this->_formula) - 1) {
  965. $this->_lookahead = $this->_formula{$i+1};
  966. }
  967. else {
  968. $this->_lookahead = '';
  969. }
  970. if ($this->_match($token) != '')
  971. {
  972. //if ($i < strlen($this->_formula) - 1) {
  973. // $this->_lookahead = $this->_formula{$i+1};
  974. //}
  975. $this->_current_char = $i + 1;
  976. $this->_current_token = $token;
  977. return 1;
  978. }
  979. if ($i < strlen($this->_formula) - 2) {
  980. $this->_lookahead = $this->_formula{$i+2};
  981. }
  982. else {
  983. // if we run out of characters _lookahead becomes empty
  984. $this->_lookahead = '';
  985. }
  986. $i++;
  987. }
  988. //die("Lexical error ".$this->_current_char);
  989. }
  990. /**
  991. * Checks if it's a valid token.
  992. *
  993. * @access private
  994. * @param mixed $token The token to check.
  995. * @return mixed The checked token or false on failure
  996. */
  997. function _match($token)
  998. {
  999. switch($token)
  1000. {
  1001. case SPREADSHEET_EXCEL_WRITER_ADD:
  1002. return($token);
  1003. break;
  1004. case SPREADSHEET_EXCEL_WRITER_SUB:
  1005. return($token);
  1006. break;
  1007. case SPREADSHEET_EXCEL_WRITER_MUL:
  1008. return($token);
  1009. break;
  1010. case SPREADSHEET_EXCEL_WRITER_DIV:
  1011. return($token);
  1012. break;
  1013. case SPREADSHEET_EXCEL_WRITER_OPEN:
  1014. return($token);
  1015. break;
  1016. case SPREADSHEET_EXCEL_WRITER_CLOSE:
  1017. return($token);
  1018. break;
  1019. case SPREADSHEET_EXCEL_WRITER_SCOLON:
  1020. return($token);
  1021. break;
  1022. case SPREADSHEET_EXCEL_WRITER_COMA:
  1023. return($token);
  1024. break;
  1025. case SPREADSHEET_EXCEL_WRITER_GT:
  1026. if ($this->_lookahead == '=') { // it's a GE token
  1027. break;
  1028. }
  1029. return($token);
  1030. break;
  1031. case SPREADSHEET_EXCEL_WRITER_LT:
  1032. // it's a LE or a NE token
  1033. if (($this->_lookahead == '=') or ($this->_lookahead == '>')) {
  1034. break;
  1035. }
  1036. return($token);
  1037. break;
  1038. case SPREADSHEET_EXCEL_WRITER_GE:
  1039. return($token);
  1040. break;
  1041. case SPREADSHEET_EXCEL_WRITER_LE:
  1042. return($token);
  1043. break;
  1044. case SPREADSHEET_EXCEL_WRITER_EQ:
  1045. return($token);
  1046. break;
  1047. case SPREADSHEET_EXCEL_WRITER_NE:
  1048. return($token);
  1049. break;
  1050. default:
  1051. // if it's a reference
  1052. if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$token) and
  1053. !ereg("[0-9]",$this->_lookahead) and
  1054. ($this->_lookahead != ':') and ($this->_lookahead != '.') and
  1055. ($this->_lookahead != '!'))
  1056. {
  1057. return $token;
  1058. }
  1059. // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1)
  1060. elseif (preg_match("/^[A-Za-z0-9_]+(\:[A-Za-z0-9_]+)?\![A-Ia-i]?[A-Za-z][0-9]+$/",$token) and
  1061. !ereg("[0-9]",$this->_lookahead) and
  1062. ($this->_lookahead != ':') and ($this->_lookahead != '.'))
  1063. {
  1064. return $token;
  1065. }
  1066. // if it's a range (A1:A2)
  1067. elseif (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token) and
  1068. !ereg("[0-9]",$this->_lookahead))
  1069. {
  1070. return $token;
  1071. }
  1072. // if it's a range (A1..A2)
  1073. elseif (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$token) and
  1074. !ereg("[0-9]",$this->_lookahead))
  1075. {
  1076. return $token;
  1077. }
  1078. // If it's an external range like Sheet1:Sheet2!A1:B2
  1079. elseif (preg_match("/^[A-Za-z0-9_]+(\:[A-Za-z0-9_]+)?\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/",$token) and
  1080. !ereg("[0-9]",$this->_lookahead))
  1081. {
  1082. return $token;
  1083. }
  1084. // If it's an external range like 'Sheet1:Sheet2'!A1:B2
  1085. elseif (preg_match("/^'[A-Za-z0-9_ ]+(\:[A-Za-z0-9_ ]+)?'\!([A-Ia-i]?[A-Za-z])?[0-9]+:([A-Ia-i]?[A-Za-z])?[0-9]+$/",$token) and
  1086. !ereg("[0-9]",$this->_lookahead))
  1087. {
  1088. return $token;
  1089. }
  1090. // If it's a number (check that it's not a sheet name or range)
  1091. elseif (is_numeric($token) and
  1092. (!is_numeric($token.$this->_lookahead) or ($this->_lookahead == '')) and
  1093. ($this->_lookahead != '!') and ($this->_lookahead != ':'))
  1094. {
  1095. return $token;
  1096. }
  1097. // If it's a string (of maximum 255 characters)
  1098. elseif (ereg("^\"[^\"]{0,255}\"$",$token))
  1099. {
  1100. return $token;
  1101. }
  1102. // if it's a function call
  1103. elseif (eregi("^[A-Z0-9\xc0-\xdc\.]+$",$token) and ($this->_lookahead == "(")) {
  1104. return $token;
  1105. }
  1106. return '';
  1107. }
  1108. }
  1109. /**
  1110. * The parsing method. It parses a formula.
  1111. *
  1112. * @access public
  1113. * @param string $formula The formula to parse, without the initial equal sign (=).
  1114. */
  1115. function parse($formula)
  1116. {
  1117. $this->_current_char = 0;
  1118. $this->_formula = $formula;
  1119. $this->_lookahead = $formula{1};
  1120. $this->_advance();
  1121. $this->_parse_tree = $this->_condition();
  1122. if ($this->isError($this->_parse_tree)) {
  1123. return $this->_parse_tree;
  1124. }
  1125. }
  1126. /**
  1127. * It parses a condition. It assumes the following rule:
  1128. * Cond -> Expr [(">" | "<") Expr]
  1129. *
  1130. * @access private
  1131. * @return mixed The parsed ptg'd tree
  1132. */
  1133. function _condition()
  1134. {
  1135. $result = $this->_expression();
  1136. if ($this->isError($result)) {
  1137. return $result;
  1138. }
  1139. if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_LT)
  1140. {
  1141. $this->_advance();
  1142. $result2 = $this->_expression();
  1143. if ($this->isError($result2)) {
  1144. return $result2;
  1145. }
  1146. $result = $this->_createTree('ptgLT', $result, $result2);
  1147. }
  1148. elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_GT)
  1149. {
  1150. $this->_advance();
  1151. $result2 = $this->_expression();
  1152. if ($this->isError($result2)) {
  1153. return $result2;
  1154. }
  1155. $result = $this->_createTree('ptgGT', $result, $result2);
  1156. }
  1157. elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_LE)
  1158. {
  1159. $this->_advance();
  1160. $result2 = $this->_expression();
  1161. if ($this->isError($result2)) {
  1162. return $result2;
  1163. }
  1164. $result = $this->_createTree('ptgLE', $result, $result2);
  1165. }
  1166. elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_GE)
  1167. {
  1168. $this->_advance();
  1169. $result2 = $this->_expression();
  1170. if ($this->isError($result2)) {
  1171. return $result2;
  1172. }
  1173. $result = $this->_createTree('ptgGE', $result, $result2);
  1174. }
  1175. elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_EQ)
  1176. {
  1177. $this->_advance();
  1178. $result2 = $this->_expression();
  1179. if ($this->isError($result2)) {
  1180. return $result2;
  1181. }
  1182. $result = $this->_createTree('ptgEQ', $result, $result2);
  1183. }
  1184. elseif ($this->_current_token == SPREADSHEET_EXCEL_WRITER_NE)
  1185. {
  1186. $this->_advance();
  1187. $result2 = $this->_expression();
  1188. if ($this->isError($result2)) {
  1189. return $result2;
  1190. }
  1191. $result = $this->_createTree('ptgNE', $result, $result2);
  1192. }
  1193. return $result;
  1194. }
  1195. /**
  1196. * It parses a expression. It assumes the following rule:
  1197. * Expr -> Term [("+" | "-") Term]
  1198. *
  1199. * @access private
  1200. * @return mixed The parsed ptg'd tree
  1201. */
  1202. function _expression()
  1203. {
  1204. // If it's a string return a string node
  1205. if (ereg("^\"[^\"]{0,255}\"$", $this->_current_token))
  1206. {
  1207. $result = $this->_createTree($this->_current_token, '', '');
  1208. $this->_advance();
  1209. return $result;
  1210. }
  1211. $result = $this->_term();
  1212. if ($this->isError($result)) {
  1213. return $result;
  1214. }
  1215. while (($this->_current_token == SPREADSHEET_EXCEL_WRITER_ADD) or
  1216. ($this->_current_token == SPREADSHEET_EXCEL_WRITER_SUB))
  1217. {
  1218. if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_ADD)
  1219. {
  1220. $this->_advance();
  1221. $result2 = $this->_term();
  1222. if ($this->isError($result2)) {
  1223. return $result2;
  1224. }
  1225. $result = $this->_createTree('ptgAdd', $result, $result2);
  1226. }
  1227. else
  1228. {
  1229. $this->_advance();
  1230. $result2 = $this->_term();
  1231. if ($this->isError($result2)) {
  1232. return $result2;
  1233. }
  1234. $result = $this->_createTree('ptgSub', $result, $result2);
  1235. }
  1236. }
  1237. return $result;
  1238. }
  1239. /**
  1240. * This function just introduces a ptgParen element in the tree, so that Excel
  1241. * doesn't get confused when working with a parenthesized formula afterwards.
  1242. *
  1243. * @access private
  1244. * @see _fact()
  1245. * @return mixed The parsed ptg'd tree
  1246. */
  1247. function _parenthesizedExpression()
  1248. {
  1249. $result = $this->_createTree('ptgParen', $this->_expression(), '');
  1250. return $result;
  1251. }
  1252. /**
  1253. * It parses a term. It assumes the following rule:
  1254. * Term -> Fact [("*" | "/") Fact]
  1255. *
  1256. * @access private
  1257. * @return mixed The parsed ptg'd tree
  1258. */
  1259. function _term()
  1260. {
  1261. $result = $this->_fact();
  1262. if ($this->isError($result)) {
  1263. return $result;
  1264. }
  1265. while (($this->_current_token == SPREADSHEET_EXCEL_WRITER_MUL) or
  1266. ($this->_current_token == SPREADSHEET_EXCEL_WRITER_DIV)) {
  1267. if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_MUL)
  1268. {
  1269. $this->_advance();
  1270. $result2 = $this->_fact();
  1271. if ($this->isError($result2)) {
  1272. return $result2;
  1273. }
  1274. $result = $this->_createTree('ptgMul', $result, $result2);
  1275. }
  1276. else
  1277. {
  1278. $this->_advance();
  1279. $result2 = $this->_fact();
  1280. if ($this->isError($result2)) {
  1281. return $result2;
  1282. }
  1283. $result = $this->_createTree('ptgDiv', $result, $result2);
  1284. }
  1285. }
  1286. return $result;
  1287. }
  1288. /**
  1289. * It parses a factor. It assumes the following rule:
  1290. * Fact -> ( Expr )
  1291. * | CellRef
  1292. * | CellRange
  1293. * | Number
  1294. * | Function
  1295. *
  1296. * @access private
  1297. * @return mixed The parsed ptg'd tree
  1298. */
  1299. function _fact()
  1300. {
  1301. if ($this->_current_token == SPREADSHEET_EXCEL_WRITER_OPEN)
  1302. {
  1303. $this->_advance(); // eat the "("
  1304. $result = $this->_parenthesizedExpression();
  1305. if ($this->_current_token != SPREADSHEET_EXCEL_WRITER_CLOSE) {
  1306. trigger_error("')' token expected.",E_USER_ERROR);
  1307. }
  1308. $this->_advance(); // eat the ")"
  1309. return $result;
  1310. }
  1311. if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/',$this->_current_token))
  1312. {
  1313. // if it's a reference
  1314. $result = $this->_createTree($this->_current_token, '', '');
  1315. $this->_advance();
  1316. return $result;
  1317. }
  1318. elseif (preg_match("/^[A-Za-z0-9_]+(\:[A-Za-z0-9_]+)?\![A-Ia-i]?[A-Za-z][0-9]+$/",$this->_current_token))
  1319. {
  1320. // If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1)
  1321. $result = $this->_createTree($this->_current_token, '', '');
  1322. $this->_advance();
  1323. return $result;
  1324. }
  1325. elseif (preg_match("/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/",$t

Large files files are truncated, but you can click here to view the full file