PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Writer/Excel5/Parser.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 1583 lines | 1410 code | 18 blank | 155 comment | 14 complexity | 449f0be61d71d172df4406ac5ac00e44 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT

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

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