PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/core/vendors/excel/writer/parser.php

https://bitbucket.org/gio_jgs/botica
PHP | 1686 lines | 1488 code | 31 blank | 167 comment | 18 complexity | 8cd4a36d9b74191b0b0b8cf07ae77188 MD5 | raw file
Possible License(s): GPL-3.0

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

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

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