Parser.php 65 KB

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