Parser.php 56 KB

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