PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/add-ons/PHPExcel/PHPExcel/Writer/Excel5/Parser.php

https://github.com/jcplat/console-seolan
PHP | 1575 lines | 1402 code | 21 blank | 152 comment | 11 complexity | b49cfc0296907a89baf814b9f616ed75 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause

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

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

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