PageRenderTime 73ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/admin/pear/Spreadsheet/Excel/Writer/Parser.php

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

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