dc3b3863ea283427879d83f3b4ba8def0da27cc0.svn-base 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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 ##VERSION##, ##DATE##
  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. $this->_advance(); // Skip the percentage operator once we've pre-built that tree
  1341. } else {
  1342. $result = $this->_createTree($this->_current_token, '', '');
  1343. }
  1344. $this->_advance();
  1345. return $result;
  1346. }
  1347. // if it's a function call
  1348. elseif (preg_match("/^[A-Z0-9\xc0-\xdc\.]+$/i",$this->_current_token))
  1349. {
  1350. $result = $this->_func();
  1351. return $result;
  1352. }
  1353. throw new PHPExcel_Writer_Exception("Syntax error: ".$this->_current_token.
  1354. ", lookahead: ".$this->_lookahead.
  1355. ", current char: ".$this->_current_char);
  1356. }
  1357. /**
  1358. * It parses a function call. It assumes the following rule:
  1359. * Func -> ( Expr [,Expr]* )
  1360. *
  1361. * @access private
  1362. * @return mixed The parsed ptg'd tree on success
  1363. */
  1364. function _func()
  1365. {
  1366. $num_args = 0; // number of arguments received
  1367. $function = strtoupper($this->_current_token);
  1368. $result = ''; // initialize result
  1369. $this->_advance();
  1370. $this->_advance(); // eat the "("
  1371. while ($this->_current_token != ')') {
  1372. /**/
  1373. if ($num_args > 0) {
  1374. if ($this->_current_token == "," or
  1375. $this->_current_token == ";")
  1376. {
  1377. $this->_advance(); // eat the "," or ";"
  1378. } else {
  1379. throw new PHPExcel_Writer_Exception("Syntax error: comma expected in ".
  1380. "function $function, arg #{$num_args}");
  1381. }
  1382. $result2 = $this->_condition();
  1383. $result = $this->_createTree('arg', $result, $result2);
  1384. } else { // first argument
  1385. $result2 = $this->_condition();
  1386. $result = $this->_createTree('arg', '', $result2);
  1387. }
  1388. ++$num_args;
  1389. }
  1390. if (!isset($this->_functions[$function])) {
  1391. throw new PHPExcel_Writer_Exception("Function $function() doesn't exist");
  1392. }
  1393. $args = $this->_functions[$function][1];
  1394. // If fixed number of args eg. TIME($i,$j,$k). Check that the number of args is valid.
  1395. if (($args >= 0) and ($args != $num_args)) {
  1396. throw new PHPExcel_Writer_Exception("Incorrect number of arguments in function $function() ");
  1397. }
  1398. $result = $this->_createTree($function, $result, $num_args);
  1399. $this->_advance(); // eat the ")"
  1400. return $result;
  1401. }
  1402. /**
  1403. * Creates a tree. In fact an array which may have one or two arrays (sub-trees)
  1404. * as elements.
  1405. *
  1406. * @access private
  1407. * @param mixed $value The value of this node.
  1408. * @param mixed $left The left array (sub-tree) or a final node.
  1409. * @param mixed $right The right array (sub-tree) or a final node.
  1410. * @return array A tree
  1411. */
  1412. function _createTree($value, $left, $right)
  1413. {
  1414. return array('value' => $value, 'left' => $left, 'right' => $right);
  1415. }
  1416. /**
  1417. * Builds a string containing the tree in reverse polish notation (What you
  1418. * would use in a HP calculator stack).
  1419. * The following tree:
  1420. *
  1421. * +
  1422. * / \
  1423. * 2 3
  1424. *
  1425. * produces: "23+"
  1426. *
  1427. * The following tree:
  1428. *
  1429. * +
  1430. * / \
  1431. * 3 *
  1432. * / \
  1433. * 6 A1
  1434. *
  1435. * produces: "36A1*+"
  1436. *
  1437. * In fact all operands, functions, references, etc... are written as ptg's
  1438. *
  1439. * @access public
  1440. * @param array $tree The optional tree to convert.
  1441. * @return string The tree in reverse polish notation
  1442. */
  1443. function toReversePolish($tree = array())
  1444. {
  1445. $polish = ""; // the string we are going to return
  1446. if (empty($tree)) { // If it's the first call use _parse_tree
  1447. $tree = $this->_parse_tree;
  1448. }
  1449. if (is_array($tree['left'])) {
  1450. $converted_tree = $this->toReversePolish($tree['left']);
  1451. $polish .= $converted_tree;
  1452. } elseif ($tree['left'] != '') { // It's a final node
  1453. $converted_tree = $this->_convert($tree['left']);
  1454. $polish .= $converted_tree;
  1455. }
  1456. if (is_array($tree['right'])) {
  1457. $converted_tree = $this->toReversePolish($tree['right']);
  1458. $polish .= $converted_tree;
  1459. } elseif ($tree['right'] != '') { // It's a final node
  1460. $converted_tree = $this->_convert($tree['right']);
  1461. $polish .= $converted_tree;
  1462. }
  1463. // if it's a function convert it here (so we can set it's arguments)
  1464. if (preg_match("/^[A-Z0-9\xc0-\xdc\.]+$/",$tree['value']) and
  1465. !preg_match('/^([A-Ia-i]?[A-Za-z])(\d+)$/',$tree['value']) and
  1466. !preg_match("/^[A-Ia-i]?[A-Za-z](\d+)\.\.[A-Ia-i]?[A-Za-z](\d+)$/",$tree['value']) and
  1467. !is_numeric($tree['value']) and
  1468. !isset($this->ptg[$tree['value']]))
  1469. {
  1470. // left subtree for a function is always an array.
  1471. if ($tree['left'] != '') {
  1472. $left_tree = $this->toReversePolish($tree['left']);
  1473. } else {
  1474. $left_tree = '';
  1475. }
  1476. // add it's left subtree and return.
  1477. return $left_tree.$this->_convertFunction($tree['value'], $tree['right']);
  1478. } else {
  1479. $converted_tree = $this->_convert($tree['value']);
  1480. }
  1481. $polish .= $converted_tree;
  1482. return $polish;
  1483. }
  1484. }