PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/functions/PEAR/Spreadsheet/Excel/Writer/Parser.php

https://bitbucket.org/bertramtruong/phpipam
PHP | 1703 lines | 1500 code | 33 blank | 170 comment | 20 complexity | e55934f36468f623b1a8a17ec5787e84 MD5 | raw file

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

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