File Source for Parser.php

PHPExcel_Writer_Excel5

# Source for file Parser.php

Documentation is available at Parser.php

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

Documentation generated on Sun, 27 Feb 2011 16:33:09 -0800 by phpDocumentor 1.4.3