Calculation.php 181 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Engine\CyclicReferenceStack;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Engine\Logger;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Token\Stack;
  6. use PhpOffice\PhpSpreadsheet\Cell\Cell;
  7. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  8. use PhpOffice\PhpSpreadsheet\NamedRange;
  9. use PhpOffice\PhpSpreadsheet\Shared;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  12. class Calculation
  13. {
  14. /** Constants */
  15. /** Regular Expressions */
  16. // Numeric operand
  17. const CALCULATION_REGEXP_NUMBER = '[-+]?\d*\.?\d+(e[-+]?\d+)?';
  18. // String operand
  19. const CALCULATION_REGEXP_STRING = '"(?:[^"]|"")*"';
  20. // Opening bracket
  21. const CALCULATION_REGEXP_OPENBRACE = '\(';
  22. // Function (allow for the old @ symbol that could be used to prefix a function, but we'll ignore it)
  23. const CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\.)?([A-Z][A-Z0-9\.]*)[\s]*\(';
  24. // Cell reference (cell or range of cells, with or without a sheet reference)
  25. const CALCULATION_REGEXP_CELLREF = '((([^\s,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?\$?([a-z]{1,3})\$?(\d{1,7})';
  26. // Named Range of cells
  27. const CALCULATION_REGEXP_NAMEDRANGE = '((([^\s,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?([_A-Z][_A-Z0-9\.]*)';
  28. // Error
  29. const CALCULATION_REGEXP_ERROR = '\#[A-Z][A-Z0_\/]*[!\?]?';
  30. /** constants */
  31. const RETURN_ARRAY_AS_ERROR = 'error';
  32. const RETURN_ARRAY_AS_VALUE = 'value';
  33. const RETURN_ARRAY_AS_ARRAY = 'array';
  34. private static $returnArrayAsType = self::RETURN_ARRAY_AS_VALUE;
  35. /**
  36. * Instance of this class.
  37. *
  38. * @var Calculation
  39. */
  40. private static $instance;
  41. /**
  42. * Instance of the spreadsheet this Calculation Engine is using.
  43. *
  44. * @var Spreadsheet
  45. */
  46. private $spreadsheet;
  47. /**
  48. * Calculation cache.
  49. *
  50. * @var array
  51. */
  52. private $calculationCache = [];
  53. /**
  54. * Calculation cache enabled.
  55. *
  56. * @var bool
  57. */
  58. private $calculationCacheEnabled = true;
  59. /**
  60. * List of operators that can be used within formulae
  61. * The true/false value indicates whether it is a binary operator or a unary operator.
  62. *
  63. * @var array
  64. */
  65. private static $operators = [
  66. '+' => true, '-' => true, '*' => true, '/' => true,
  67. '^' => true, '&' => true, '%' => false, '~' => false,
  68. '>' => true, '<' => true, '=' => true, '>=' => true,
  69. '<=' => true, '<>' => true, '|' => true, ':' => true,
  70. ];
  71. /**
  72. * List of binary operators (those that expect two operands).
  73. *
  74. * @var array
  75. */
  76. private static $binaryOperators = [
  77. '+' => true, '-' => true, '*' => true, '/' => true,
  78. '^' => true, '&' => true, '>' => true, '<' => true,
  79. '=' => true, '>=' => true, '<=' => true, '<>' => true,
  80. '|' => true, ':' => true,
  81. ];
  82. /**
  83. * The debug log generated by the calculation engine.
  84. *
  85. * @var Logger
  86. */
  87. private $debugLog;
  88. /**
  89. * Flag to determine how formula errors should be handled
  90. * If true, then a user error will be triggered
  91. * If false, then an exception will be thrown.
  92. *
  93. * @var bool
  94. */
  95. public $suppressFormulaErrors = false;
  96. /**
  97. * Error message for any error that was raised/thrown by the calculation engine.
  98. *
  99. * @var string
  100. */
  101. public $formulaError;
  102. /**
  103. * An array of the nested cell references accessed by the calculation engine, used for the debug log.
  104. *
  105. * @var array of string
  106. */
  107. private $cyclicReferenceStack;
  108. private $cellStack = [];
  109. /**
  110. * Current iteration counter for cyclic formulae
  111. * If the value is 0 (or less) then cyclic formulae will throw an exception,
  112. * otherwise they will iterate to the limit defined here before returning a result.
  113. *
  114. * @var int
  115. */
  116. private $cyclicFormulaCounter = 1;
  117. private $cyclicFormulaCell = '';
  118. /**
  119. * Number of iterations for cyclic formulae.
  120. *
  121. * @var int
  122. */
  123. public $cyclicFormulaCount = 1;
  124. /**
  125. * Epsilon Precision used for comparisons in calculations.
  126. *
  127. * @var float
  128. */
  129. private $delta = 0.1e-12;
  130. /**
  131. * The current locale setting.
  132. *
  133. * @var string
  134. */
  135. private static $localeLanguage = 'en_us'; // US English (default locale)
  136. /**
  137. * List of available locale settings
  138. * Note that this is read for the locale subdirectory only when requested.
  139. *
  140. * @var string[]
  141. */
  142. private static $validLocaleLanguages = [
  143. 'en', // English (default language)
  144. ];
  145. /**
  146. * Locale-specific argument separator for function arguments.
  147. *
  148. * @var string
  149. */
  150. private static $localeArgumentSeparator = ',';
  151. private static $localeFunctions = [];
  152. /**
  153. * Locale-specific translations for Excel constants (True, False and Null).
  154. *
  155. * @var string[]
  156. */
  157. public static $localeBoolean = [
  158. 'TRUE' => 'TRUE',
  159. 'FALSE' => 'FALSE',
  160. 'NULL' => 'NULL',
  161. ];
  162. /**
  163. * Excel constant string translations to their PHP equivalents
  164. * Constant conversion from text name/value to actual (datatyped) value.
  165. *
  166. * @var string[]
  167. */
  168. private static $excelConstants = [
  169. 'TRUE' => true,
  170. 'FALSE' => false,
  171. 'NULL' => null,
  172. ];
  173. // PhpSpreadsheet functions
  174. private static $phpSpreadsheetFunctions = [
  175. 'ABS' => [
  176. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  177. 'functionCall' => 'abs',
  178. 'argumentCount' => '1',
  179. ],
  180. 'ACCRINT' => [
  181. 'category' => Category::CATEGORY_FINANCIAL,
  182. 'functionCall' => [Financial::class, 'ACCRINT'],
  183. 'argumentCount' => '4-7',
  184. ],
  185. 'ACCRINTM' => [
  186. 'category' => Category::CATEGORY_FINANCIAL,
  187. 'functionCall' => [Financial::class, 'ACCRINTM'],
  188. 'argumentCount' => '3-5',
  189. ],
  190. 'ACOS' => [
  191. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  192. 'functionCall' => 'acos',
  193. 'argumentCount' => '1',
  194. ],
  195. 'ACOSH' => [
  196. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  197. 'functionCall' => 'acosh',
  198. 'argumentCount' => '1',
  199. ],
  200. 'ACOT' => [
  201. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  202. 'functionCall' => [MathTrig::class, 'ACOT'],
  203. 'argumentCount' => '1',
  204. ],
  205. 'ACOTH' => [
  206. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  207. 'functionCall' => [MathTrig::class, 'ACOTH'],
  208. 'argumentCount' => '1',
  209. ],
  210. 'ADDRESS' => [
  211. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  212. 'functionCall' => [LookupRef::class, 'cellAddress'],
  213. 'argumentCount' => '2-5',
  214. ],
  215. 'AMORDEGRC' => [
  216. 'category' => Category::CATEGORY_FINANCIAL,
  217. 'functionCall' => [Financial::class, 'AMORDEGRC'],
  218. 'argumentCount' => '6,7',
  219. ],
  220. 'AMORLINC' => [
  221. 'category' => Category::CATEGORY_FINANCIAL,
  222. 'functionCall' => [Financial::class, 'AMORLINC'],
  223. 'argumentCount' => '6,7',
  224. ],
  225. 'AND' => [
  226. 'category' => Category::CATEGORY_LOGICAL,
  227. 'functionCall' => [Logical::class, 'logicalAnd'],
  228. 'argumentCount' => '1+',
  229. ],
  230. 'AREAS' => [
  231. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  232. 'functionCall' => [Functions::class, 'DUMMY'],
  233. 'argumentCount' => '1',
  234. ],
  235. 'ASC' => [
  236. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  237. 'functionCall' => [Functions::class, 'DUMMY'],
  238. 'argumentCount' => '1',
  239. ],
  240. 'ASIN' => [
  241. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  242. 'functionCall' => 'asin',
  243. 'argumentCount' => '1',
  244. ],
  245. 'ASINH' => [
  246. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  247. 'functionCall' => 'asinh',
  248. 'argumentCount' => '1',
  249. ],
  250. 'ATAN' => [
  251. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  252. 'functionCall' => 'atan',
  253. 'argumentCount' => '1',
  254. ],
  255. 'ATAN2' => [
  256. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  257. 'functionCall' => [MathTrig::class, 'ATAN2'],
  258. 'argumentCount' => '2',
  259. ],
  260. 'ATANH' => [
  261. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  262. 'functionCall' => 'atanh',
  263. 'argumentCount' => '1',
  264. ],
  265. 'AVEDEV' => [
  266. 'category' => Category::CATEGORY_STATISTICAL,
  267. 'functionCall' => [Statistical::class, 'AVEDEV'],
  268. 'argumentCount' => '1+',
  269. ],
  270. 'AVERAGE' => [
  271. 'category' => Category::CATEGORY_STATISTICAL,
  272. 'functionCall' => [Statistical::class, 'AVERAGE'],
  273. 'argumentCount' => '1+',
  274. ],
  275. 'AVERAGEA' => [
  276. 'category' => Category::CATEGORY_STATISTICAL,
  277. 'functionCall' => [Statistical::class, 'AVERAGEA'],
  278. 'argumentCount' => '1+',
  279. ],
  280. 'AVERAGEIF' => [
  281. 'category' => Category::CATEGORY_STATISTICAL,
  282. 'functionCall' => [Statistical::class, 'AVERAGEIF'],
  283. 'argumentCount' => '2,3',
  284. ],
  285. 'AVERAGEIFS' => [
  286. 'category' => Category::CATEGORY_STATISTICAL,
  287. 'functionCall' => [Functions::class, 'DUMMY'],
  288. 'argumentCount' => '3+',
  289. ],
  290. 'BAHTTEXT' => [
  291. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  292. 'functionCall' => [Functions::class, 'DUMMY'],
  293. 'argumentCount' => '1',
  294. ],
  295. 'BESSELI' => [
  296. 'category' => Category::CATEGORY_ENGINEERING,
  297. 'functionCall' => [Engineering::class, 'BESSELI'],
  298. 'argumentCount' => '2',
  299. ],
  300. 'BESSELJ' => [
  301. 'category' => Category::CATEGORY_ENGINEERING,
  302. 'functionCall' => [Engineering::class, 'BESSELJ'],
  303. 'argumentCount' => '2',
  304. ],
  305. 'BESSELK' => [
  306. 'category' => Category::CATEGORY_ENGINEERING,
  307. 'functionCall' => [Engineering::class, 'BESSELK'],
  308. 'argumentCount' => '2',
  309. ],
  310. 'BESSELY' => [
  311. 'category' => Category::CATEGORY_ENGINEERING,
  312. 'functionCall' => [Engineering::class, 'BESSELY'],
  313. 'argumentCount' => '2',
  314. ],
  315. 'BETADIST' => [
  316. 'category' => Category::CATEGORY_STATISTICAL,
  317. 'functionCall' => [Statistical::class, 'BETADIST'],
  318. 'argumentCount' => '3-5',
  319. ],
  320. 'BETAINV' => [
  321. 'category' => Category::CATEGORY_STATISTICAL,
  322. 'functionCall' => [Statistical::class, 'BETAINV'],
  323. 'argumentCount' => '3-5',
  324. ],
  325. 'BIN2DEC' => [
  326. 'category' => Category::CATEGORY_ENGINEERING,
  327. 'functionCall' => [Engineering::class, 'BINTODEC'],
  328. 'argumentCount' => '1',
  329. ],
  330. 'BIN2HEX' => [
  331. 'category' => Category::CATEGORY_ENGINEERING,
  332. 'functionCall' => [Engineering::class, 'BINTOHEX'],
  333. 'argumentCount' => '1,2',
  334. ],
  335. 'BIN2OCT' => [
  336. 'category' => Category::CATEGORY_ENGINEERING,
  337. 'functionCall' => [Engineering::class, 'BINTOOCT'],
  338. 'argumentCount' => '1,2',
  339. ],
  340. 'BINOMDIST' => [
  341. 'category' => Category::CATEGORY_STATISTICAL,
  342. 'functionCall' => [Statistical::class, 'BINOMDIST'],
  343. 'argumentCount' => '4',
  344. ],
  345. 'BITAND' => [
  346. 'category' => Category::CATEGORY_ENGINEERING,
  347. 'functionCall' => [Engineering::class, 'BITAND'],
  348. 'argumentCount' => '2',
  349. ],
  350. 'BITOR' => [
  351. 'category' => Category::CATEGORY_ENGINEERING,
  352. 'functionCall' => [Engineering::class, 'BITOR'],
  353. 'argumentCount' => '2',
  354. ],
  355. 'BITXOR' => [
  356. 'category' => Category::CATEGORY_ENGINEERING,
  357. 'functionCall' => [Engineering::class, 'BITOR'],
  358. 'argumentCount' => '2',
  359. ],
  360. 'BITLSHIFT' => [
  361. 'category' => Category::CATEGORY_ENGINEERING,
  362. 'functionCall' => [Engineering::class, 'BITLSHIFT'],
  363. 'argumentCount' => '2',
  364. ],
  365. 'BITRSHIFT' => [
  366. 'category' => Category::CATEGORY_ENGINEERING,
  367. 'functionCall' => [Engineering::class, 'BITRSHIFT'],
  368. 'argumentCount' => '2',
  369. ],
  370. 'CEILING' => [
  371. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  372. 'functionCall' => [MathTrig::class, 'CEILING'],
  373. 'argumentCount' => '2',
  374. ],
  375. 'CELL' => [
  376. 'category' => Category::CATEGORY_INFORMATION,
  377. 'functionCall' => [Functions::class, 'DUMMY'],
  378. 'argumentCount' => '1,2',
  379. ],
  380. 'CHAR' => [
  381. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  382. 'functionCall' => [TextData::class, 'CHARACTER'],
  383. 'argumentCount' => '1',
  384. ],
  385. 'CHIDIST' => [
  386. 'category' => Category::CATEGORY_STATISTICAL,
  387. 'functionCall' => [Statistical::class, 'CHIDIST'],
  388. 'argumentCount' => '2',
  389. ],
  390. 'CHIINV' => [
  391. 'category' => Category::CATEGORY_STATISTICAL,
  392. 'functionCall' => [Statistical::class, 'CHIINV'],
  393. 'argumentCount' => '2',
  394. ],
  395. 'CHITEST' => [
  396. 'category' => Category::CATEGORY_STATISTICAL,
  397. 'functionCall' => [Functions::class, 'DUMMY'],
  398. 'argumentCount' => '2',
  399. ],
  400. 'CHOOSE' => [
  401. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  402. 'functionCall' => [LookupRef::class, 'CHOOSE'],
  403. 'argumentCount' => '2+',
  404. ],
  405. 'CLEAN' => [
  406. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  407. 'functionCall' => [TextData::class, 'TRIMNONPRINTABLE'],
  408. 'argumentCount' => '1',
  409. ],
  410. 'CODE' => [
  411. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  412. 'functionCall' => [TextData::class, 'ASCIICODE'],
  413. 'argumentCount' => '1',
  414. ],
  415. 'COLUMN' => [
  416. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  417. 'functionCall' => [LookupRef::class, 'COLUMN'],
  418. 'argumentCount' => '-1',
  419. 'passByReference' => [true],
  420. ],
  421. 'COLUMNS' => [
  422. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  423. 'functionCall' => [LookupRef::class, 'COLUMNS'],
  424. 'argumentCount' => '1',
  425. ],
  426. 'COMBIN' => [
  427. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  428. 'functionCall' => [MathTrig::class, 'COMBIN'],
  429. 'argumentCount' => '2',
  430. ],
  431. 'COMPLEX' => [
  432. 'category' => Category::CATEGORY_ENGINEERING,
  433. 'functionCall' => [Engineering::class, 'COMPLEX'],
  434. 'argumentCount' => '2,3',
  435. ],
  436. 'CONCAT' => [
  437. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  438. 'functionCall' => [TextData::class, 'CONCATENATE'],
  439. 'argumentCount' => '1+',
  440. ],
  441. 'CONCATENATE' => [
  442. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  443. 'functionCall' => [TextData::class, 'CONCATENATE'],
  444. 'argumentCount' => '1+',
  445. ],
  446. 'CONFIDENCE' => [
  447. 'category' => Category::CATEGORY_STATISTICAL,
  448. 'functionCall' => [Statistical::class, 'CONFIDENCE'],
  449. 'argumentCount' => '3',
  450. ],
  451. 'CONVERT' => [
  452. 'category' => Category::CATEGORY_ENGINEERING,
  453. 'functionCall' => [Engineering::class, 'CONVERTUOM'],
  454. 'argumentCount' => '3',
  455. ],
  456. 'CORREL' => [
  457. 'category' => Category::CATEGORY_STATISTICAL,
  458. 'functionCall' => [Statistical::class, 'CORREL'],
  459. 'argumentCount' => '2',
  460. ],
  461. 'COS' => [
  462. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  463. 'functionCall' => 'cos',
  464. 'argumentCount' => '1',
  465. ],
  466. 'COSH' => [
  467. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  468. 'functionCall' => 'cosh',
  469. 'argumentCount' => '1',
  470. ],
  471. 'COT' => [
  472. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  473. 'functionCall' => [MathTrig::class, 'COT'],
  474. 'argumentCount' => '1',
  475. ],
  476. 'COTH' => [
  477. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  478. 'functionCall' => [MathTrig::class, 'COTH'],
  479. 'argumentCount' => '1',
  480. ],
  481. 'COUNT' => [
  482. 'category' => Category::CATEGORY_STATISTICAL,
  483. 'functionCall' => [Statistical::class, 'COUNT'],
  484. 'argumentCount' => '1+',
  485. ],
  486. 'COUNTA' => [
  487. 'category' => Category::CATEGORY_STATISTICAL,
  488. 'functionCall' => [Statistical::class, 'COUNTA'],
  489. 'argumentCount' => '1+',
  490. ],
  491. 'COUNTBLANK' => [
  492. 'category' => Category::CATEGORY_STATISTICAL,
  493. 'functionCall' => [Statistical::class, 'COUNTBLANK'],
  494. 'argumentCount' => '1',
  495. ],
  496. 'COUNTIF' => [
  497. 'category' => Category::CATEGORY_STATISTICAL,
  498. 'functionCall' => [Statistical::class, 'COUNTIF'],
  499. 'argumentCount' => '2',
  500. ],
  501. 'COUNTIFS' => [
  502. 'category' => Category::CATEGORY_STATISTICAL,
  503. 'functionCall' => [Functions::class, 'DUMMY'],
  504. 'argumentCount' => '2+',
  505. ],
  506. 'COUPDAYBS' => [
  507. 'category' => Category::CATEGORY_FINANCIAL,
  508. 'functionCall' => [Financial::class, 'COUPDAYBS'],
  509. 'argumentCount' => '3,4',
  510. ],
  511. 'COUPDAYS' => [
  512. 'category' => Category::CATEGORY_FINANCIAL,
  513. 'functionCall' => [Financial::class, 'COUPDAYS'],
  514. 'argumentCount' => '3,4',
  515. ],
  516. 'COUPDAYSNC' => [
  517. 'category' => Category::CATEGORY_FINANCIAL,
  518. 'functionCall' => [Financial::class, 'COUPDAYSNC'],
  519. 'argumentCount' => '3,4',
  520. ],
  521. 'COUPNCD' => [
  522. 'category' => Category::CATEGORY_FINANCIAL,
  523. 'functionCall' => [Financial::class, 'COUPNCD'],
  524. 'argumentCount' => '3,4',
  525. ],
  526. 'COUPNUM' => [
  527. 'category' => Category::CATEGORY_FINANCIAL,
  528. 'functionCall' => [Financial::class, 'COUPNUM'],
  529. 'argumentCount' => '3,4',
  530. ],
  531. 'COUPPCD' => [
  532. 'category' => Category::CATEGORY_FINANCIAL,
  533. 'functionCall' => [Financial::class, 'COUPPCD'],
  534. 'argumentCount' => '3,4',
  535. ],
  536. 'COVAR' => [
  537. 'category' => Category::CATEGORY_STATISTICAL,
  538. 'functionCall' => [Statistical::class, 'COVAR'],
  539. 'argumentCount' => '2',
  540. ],
  541. 'CRITBINOM' => [
  542. 'category' => Category::CATEGORY_STATISTICAL,
  543. 'functionCall' => [Statistical::class, 'CRITBINOM'],
  544. 'argumentCount' => '3',
  545. ],
  546. 'CSC' => [
  547. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  548. 'functionCall' => [MathTrig::class, 'CSC'],
  549. 'argumentCount' => '1',
  550. ],
  551. 'CSCH' => [
  552. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  553. 'functionCall' => [MathTrig::class, 'CSCH'],
  554. 'argumentCount' => '1',
  555. ],
  556. 'CUBEKPIMEMBER' => [
  557. 'category' => Category::CATEGORY_CUBE,
  558. 'functionCall' => [Functions::class, 'DUMMY'],
  559. 'argumentCount' => '?',
  560. ],
  561. 'CUBEMEMBER' => [
  562. 'category' => Category::CATEGORY_CUBE,
  563. 'functionCall' => [Functions::class, 'DUMMY'],
  564. 'argumentCount' => '?',
  565. ],
  566. 'CUBEMEMBERPROPERTY' => [
  567. 'category' => Category::CATEGORY_CUBE,
  568. 'functionCall' => [Functions::class, 'DUMMY'],
  569. 'argumentCount' => '?',
  570. ],
  571. 'CUBERANKEDMEMBER' => [
  572. 'category' => Category::CATEGORY_CUBE,
  573. 'functionCall' => [Functions::class, 'DUMMY'],
  574. 'argumentCount' => '?',
  575. ],
  576. 'CUBESET' => [
  577. 'category' => Category::CATEGORY_CUBE,
  578. 'functionCall' => [Functions::class, 'DUMMY'],
  579. 'argumentCount' => '?',
  580. ],
  581. 'CUBESETCOUNT' => [
  582. 'category' => Category::CATEGORY_CUBE,
  583. 'functionCall' => [Functions::class, 'DUMMY'],
  584. 'argumentCount' => '?',
  585. ],
  586. 'CUBEVALUE' => [
  587. 'category' => Category::CATEGORY_CUBE,
  588. 'functionCall' => [Functions::class, 'DUMMY'],
  589. 'argumentCount' => '?',
  590. ],
  591. 'CUMIPMT' => [
  592. 'category' => Category::CATEGORY_FINANCIAL,
  593. 'functionCall' => [Financial::class, 'CUMIPMT'],
  594. 'argumentCount' => '6',
  595. ],
  596. 'CUMPRINC' => [
  597. 'category' => Category::CATEGORY_FINANCIAL,
  598. 'functionCall' => [Financial::class, 'CUMPRINC'],
  599. 'argumentCount' => '6',
  600. ],
  601. 'DATE' => [
  602. 'category' => Category::CATEGORY_DATE_AND_TIME,
  603. 'functionCall' => [DateTime::class, 'DATE'],
  604. 'argumentCount' => '3',
  605. ],
  606. 'DATEDIF' => [
  607. 'category' => Category::CATEGORY_DATE_AND_TIME,
  608. 'functionCall' => [DateTime::class, 'DATEDIF'],
  609. 'argumentCount' => '2,3',
  610. ],
  611. 'DATEVALUE' => [
  612. 'category' => Category::CATEGORY_DATE_AND_TIME,
  613. 'functionCall' => [DateTime::class, 'DATEVALUE'],
  614. 'argumentCount' => '1',
  615. ],
  616. 'DAVERAGE' => [
  617. 'category' => Category::CATEGORY_DATABASE,
  618. 'functionCall' => [Database::class, 'DAVERAGE'],
  619. 'argumentCount' => '3',
  620. ],
  621. 'DAY' => [
  622. 'category' => Category::CATEGORY_DATE_AND_TIME,
  623. 'functionCall' => [DateTime::class, 'DAYOFMONTH'],
  624. 'argumentCount' => '1',
  625. ],
  626. 'DAYS' => [
  627. 'category' => Category::CATEGORY_DATE_AND_TIME,
  628. 'functionCall' => [DateTime::class, 'DAYS'],
  629. 'argumentCount' => '2',
  630. ],
  631. 'DAYS360' => [
  632. 'category' => Category::CATEGORY_DATE_AND_TIME,
  633. 'functionCall' => [DateTime::class, 'DAYS360'],
  634. 'argumentCount' => '2,3',
  635. ],
  636. 'DB' => [
  637. 'category' => Category::CATEGORY_FINANCIAL,
  638. 'functionCall' => [Financial::class, 'DB'],
  639. 'argumentCount' => '4,5',
  640. ],
  641. 'DCOUNT' => [
  642. 'category' => Category::CATEGORY_DATABASE,
  643. 'functionCall' => [Database::class, 'DCOUNT'],
  644. 'argumentCount' => '3',
  645. ],
  646. 'DCOUNTA' => [
  647. 'category' => Category::CATEGORY_DATABASE,
  648. 'functionCall' => [Database::class, 'DCOUNTA'],
  649. 'argumentCount' => '3',
  650. ],
  651. 'DDB' => [
  652. 'category' => Category::CATEGORY_FINANCIAL,
  653. 'functionCall' => [Financial::class, 'DDB'],
  654. 'argumentCount' => '4,5',
  655. ],
  656. 'DEC2BIN' => [
  657. 'category' => Category::CATEGORY_ENGINEERING,
  658. 'functionCall' => [Engineering::class, 'DECTOBIN'],
  659. 'argumentCount' => '1,2',
  660. ],
  661. 'DEC2HEX' => [
  662. 'category' => Category::CATEGORY_ENGINEERING,
  663. 'functionCall' => [Engineering::class, 'DECTOHEX'],
  664. 'argumentCount' => '1,2',
  665. ],
  666. 'DEC2OCT' => [
  667. 'category' => Category::CATEGORY_ENGINEERING,
  668. 'functionCall' => [Engineering::class, 'DECTOOCT'],
  669. 'argumentCount' => '1,2',
  670. ],
  671. 'DEGREES' => [
  672. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  673. 'functionCall' => 'rad2deg',
  674. 'argumentCount' => '1',
  675. ],
  676. 'DELTA' => [
  677. 'category' => Category::CATEGORY_ENGINEERING,
  678. 'functionCall' => [Engineering::class, 'DELTA'],
  679. 'argumentCount' => '1,2',
  680. ],
  681. 'DEVSQ' => [
  682. 'category' => Category::CATEGORY_STATISTICAL,
  683. 'functionCall' => [Statistical::class, 'DEVSQ'],
  684. 'argumentCount' => '1+',
  685. ],
  686. 'DGET' => [
  687. 'category' => Category::CATEGORY_DATABASE,
  688. 'functionCall' => [Database::class, 'DGET'],
  689. 'argumentCount' => '3',
  690. ],
  691. 'DISC' => [
  692. 'category' => Category::CATEGORY_FINANCIAL,
  693. 'functionCall' => [Financial::class, 'DISC'],
  694. 'argumentCount' => '4,5',
  695. ],
  696. 'DMAX' => [
  697. 'category' => Category::CATEGORY_DATABASE,
  698. 'functionCall' => [Database::class, 'DMAX'],
  699. 'argumentCount' => '3',
  700. ],
  701. 'DMIN' => [
  702. 'category' => Category::CATEGORY_DATABASE,
  703. 'functionCall' => [Database::class, 'DMIN'],
  704. 'argumentCount' => '3',
  705. ],
  706. 'DOLLAR' => [
  707. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  708. 'functionCall' => [TextData::class, 'DOLLAR'],
  709. 'argumentCount' => '1,2',
  710. ],
  711. 'DOLLARDE' => [
  712. 'category' => Category::CATEGORY_FINANCIAL,
  713. 'functionCall' => [Financial::class, 'DOLLARDE'],
  714. 'argumentCount' => '2',
  715. ],
  716. 'DOLLARFR' => [
  717. 'category' => Category::CATEGORY_FINANCIAL,
  718. 'functionCall' => [Financial::class, 'DOLLARFR'],
  719. 'argumentCount' => '2',
  720. ],
  721. 'DPRODUCT' => [
  722. 'category' => Category::CATEGORY_DATABASE,
  723. 'functionCall' => [Database::class, 'DPRODUCT'],
  724. 'argumentCount' => '3',
  725. ],
  726. 'DSTDEV' => [
  727. 'category' => Category::CATEGORY_DATABASE,
  728. 'functionCall' => [Database::class, 'DSTDEV'],
  729. 'argumentCount' => '3',
  730. ],
  731. 'DSTDEVP' => [
  732. 'category' => Category::CATEGORY_DATABASE,
  733. 'functionCall' => [Database::class, 'DSTDEVP'],
  734. 'argumentCount' => '3',
  735. ],
  736. 'DSUM' => [
  737. 'category' => Category::CATEGORY_DATABASE,
  738. 'functionCall' => [Database::class, 'DSUM'],
  739. 'argumentCount' => '3',
  740. ],
  741. 'DURATION' => [
  742. 'category' => Category::CATEGORY_FINANCIAL,
  743. 'functionCall' => [Functions::class, 'DUMMY'],
  744. 'argumentCount' => '5,6',
  745. ],
  746. 'DVAR' => [
  747. 'category' => Category::CATEGORY_DATABASE,
  748. 'functionCall' => [Database::class, 'DVAR'],
  749. 'argumentCount' => '3',
  750. ],
  751. 'DVARP' => [
  752. 'category' => Category::CATEGORY_DATABASE,
  753. 'functionCall' => [Database::class, 'DVARP'],
  754. 'argumentCount' => '3',
  755. ],
  756. 'EDATE' => [
  757. 'category' => Category::CATEGORY_DATE_AND_TIME,
  758. 'functionCall' => [DateTime::class, 'EDATE'],
  759. 'argumentCount' => '2',
  760. ],
  761. 'EFFECT' => [
  762. 'category' => Category::CATEGORY_FINANCIAL,
  763. 'functionCall' => [Financial::class, 'EFFECT'],
  764. 'argumentCount' => '2',
  765. ],
  766. 'EOMONTH' => [
  767. 'category' => Category::CATEGORY_DATE_AND_TIME,
  768. 'functionCall' => [DateTime::class, 'EOMONTH'],
  769. 'argumentCount' => '2',
  770. ],
  771. 'ERF' => [
  772. 'category' => Category::CATEGORY_ENGINEERING,
  773. 'functionCall' => [Engineering::class, 'ERF'],
  774. 'argumentCount' => '1,2',
  775. ],
  776. 'ERF.PRECISE' => [
  777. 'category' => Category::CATEGORY_ENGINEERING,
  778. 'functionCall' => [Engineering::class, 'ERFPRECISE'],
  779. 'argumentCount' => '1',
  780. ],
  781. 'ERFC' => [
  782. 'category' => Category::CATEGORY_ENGINEERING,
  783. 'functionCall' => [Engineering::class, 'ERFC'],
  784. 'argumentCount' => '1',
  785. ],
  786. 'ERFC.PRECISE' => [
  787. 'category' => Category::CATEGORY_ENGINEERING,
  788. 'functionCall' => [Engineering::class, 'ERFC'],
  789. 'argumentCount' => '1',
  790. ],
  791. 'ERROR.TYPE' => [
  792. 'category' => Category::CATEGORY_INFORMATION,
  793. 'functionCall' => [Functions::class, 'errorType'],
  794. 'argumentCount' => '1',
  795. ],
  796. 'EVEN' => [
  797. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  798. 'functionCall' => [MathTrig::class, 'EVEN'],
  799. 'argumentCount' => '1',
  800. ],
  801. 'EXACT' => [
  802. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  803. 'functionCall' => [TextData::class, 'EXACT'],
  804. 'argumentCount' => '2',
  805. ],
  806. 'EXP' => [
  807. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  808. 'functionCall' => 'exp',
  809. 'argumentCount' => '1',
  810. ],
  811. 'EXPONDIST' => [
  812. 'category' => Category::CATEGORY_STATISTICAL,
  813. 'functionCall' => [Statistical::class, 'EXPONDIST'],
  814. 'argumentCount' => '3',
  815. ],
  816. 'FACT' => [
  817. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  818. 'functionCall' => [MathTrig::class, 'FACT'],
  819. 'argumentCount' => '1',
  820. ],
  821. 'FACTDOUBLE' => [
  822. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  823. 'functionCall' => [MathTrig::class, 'FACTDOUBLE'],
  824. 'argumentCount' => '1',
  825. ],
  826. 'FALSE' => [
  827. 'category' => Category::CATEGORY_LOGICAL,
  828. 'functionCall' => [Logical::class, 'FALSE'],
  829. 'argumentCount' => '0',
  830. ],
  831. 'FDIST' => [
  832. 'category' => Category::CATEGORY_STATISTICAL,
  833. 'functionCall' => [Functions::class, 'DUMMY'],
  834. 'argumentCount' => '3',
  835. ],
  836. 'FIND' => [
  837. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  838. 'functionCall' => [TextData::class, 'SEARCHSENSITIVE'],
  839. 'argumentCount' => '2,3',
  840. ],
  841. 'FINDB' => [
  842. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  843. 'functionCall' => [TextData::class, 'SEARCHSENSITIVE'],
  844. 'argumentCount' => '2,3',
  845. ],
  846. 'FINV' => [
  847. 'category' => Category::CATEGORY_STATISTICAL,
  848. 'functionCall' => [Functions::class, 'DUMMY'],
  849. 'argumentCount' => '3',
  850. ],
  851. 'FISHER' => [
  852. 'category' => Category::CATEGORY_STATISTICAL,
  853. 'functionCall' => [Statistical::class, 'FISHER'],
  854. 'argumentCount' => '1',
  855. ],
  856. 'FISHERINV' => [
  857. 'category' => Category::CATEGORY_STATISTICAL,
  858. 'functionCall' => [Statistical::class, 'FISHERINV'],
  859. 'argumentCount' => '1',
  860. ],
  861. 'FIXED' => [
  862. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  863. 'functionCall' => [TextData::class, 'FIXEDFORMAT'],
  864. 'argumentCount' => '1-3',
  865. ],
  866. 'FLOOR' => [
  867. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  868. 'functionCall' => [MathTrig::class, 'FLOOR'],
  869. 'argumentCount' => '2',
  870. ],
  871. 'FORECAST' => [
  872. 'category' => Category::CATEGORY_STATISTICAL,
  873. 'functionCall' => [Statistical::class, 'FORECAST'],
  874. 'argumentCount' => '3',
  875. ],
  876. 'FORMULATEXT' => [
  877. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  878. 'functionCall' => [LookupRef::class, 'FORMULATEXT'],
  879. 'argumentCount' => '1',
  880. 'passCellReference' => true,
  881. 'passByReference' => [true],
  882. ],
  883. 'FREQUENCY' => [
  884. 'category' => Category::CATEGORY_STATISTICAL,
  885. 'functionCall' => [Functions::class, 'DUMMY'],
  886. 'argumentCount' => '2',
  887. ],
  888. 'FTEST' => [
  889. 'category' => Category::CATEGORY_STATISTICAL,
  890. 'functionCall' => [Functions::class, 'DUMMY'],
  891. 'argumentCount' => '2',
  892. ],
  893. 'FV' => [
  894. 'category' => Category::CATEGORY_FINANCIAL,
  895. 'functionCall' => [Financial::class, 'FV'],
  896. 'argumentCount' => '3-5',
  897. ],
  898. 'FVSCHEDULE' => [
  899. 'category' => Category::CATEGORY_FINANCIAL,
  900. 'functionCall' => [Financial::class, 'FVSCHEDULE'],
  901. 'argumentCount' => '2',
  902. ],
  903. 'GAMMADIST' => [
  904. 'category' => Category::CATEGORY_STATISTICAL,
  905. 'functionCall' => [Statistical::class, 'GAMMADIST'],
  906. 'argumentCount' => '4',
  907. ],
  908. 'GAMMAINV' => [
  909. 'category' => Category::CATEGORY_STATISTICAL,
  910. 'functionCall' => [Statistical::class, 'GAMMAINV'],
  911. 'argumentCount' => '3',
  912. ],
  913. 'GAMMALN' => [
  914. 'category' => Category::CATEGORY_STATISTICAL,
  915. 'functionCall' => [Statistical::class, 'GAMMALN'],
  916. 'argumentCount' => '1',
  917. ],
  918. 'GCD' => [
  919. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  920. 'functionCall' => [MathTrig::class, 'GCD'],
  921. 'argumentCount' => '1+',
  922. ],
  923. 'GEOMEAN' => [
  924. 'category' => Category::CATEGORY_STATISTICAL,
  925. 'functionCall' => [Statistical::class, 'GEOMEAN'],
  926. 'argumentCount' => '1+',
  927. ],
  928. 'GESTEP' => [
  929. 'category' => Category::CATEGORY_ENGINEERING,
  930. 'functionCall' => [Engineering::class, 'GESTEP'],
  931. 'argumentCount' => '1,2',
  932. ],
  933. 'GETPIVOTDATA' => [
  934. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  935. 'functionCall' => [Functions::class, 'DUMMY'],
  936. 'argumentCount' => '2+',
  937. ],
  938. 'GROWTH' => [
  939. 'category' => Category::CATEGORY_STATISTICAL,
  940. 'functionCall' => [Statistical::class, 'GROWTH'],
  941. 'argumentCount' => '1-4',
  942. ],
  943. 'HARMEAN' => [
  944. 'category' => Category::CATEGORY_STATISTICAL,
  945. 'functionCall' => [Statistical::class, 'HARMEAN'],
  946. 'argumentCount' => '1+',
  947. ],
  948. 'HEX2BIN' => [
  949. 'category' => Category::CATEGORY_ENGINEERING,
  950. 'functionCall' => [Engineering::class, 'HEXTOBIN'],
  951. 'argumentCount' => '1,2',
  952. ],
  953. 'HEX2DEC' => [
  954. 'category' => Category::CATEGORY_ENGINEERING,
  955. 'functionCall' => [Engineering::class, 'HEXTODEC'],
  956. 'argumentCount' => '1',
  957. ],
  958. 'HEX2OCT' => [
  959. 'category' => Category::CATEGORY_ENGINEERING,
  960. 'functionCall' => [Engineering::class, 'HEXTOOCT'],
  961. 'argumentCount' => '1,2',
  962. ],
  963. 'HLOOKUP' => [
  964. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  965. 'functionCall' => [LookupRef::class, 'HLOOKUP'],
  966. 'argumentCount' => '3,4',
  967. ],
  968. 'HOUR' => [
  969. 'category' => Category::CATEGORY_DATE_AND_TIME,
  970. 'functionCall' => [DateTime::class, 'HOUROFDAY'],
  971. 'argumentCount' => '1',
  972. ],
  973. 'HYPERLINK' => [
  974. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  975. 'functionCall' => [LookupRef::class, 'HYPERLINK'],
  976. 'argumentCount' => '1,2',
  977. 'passCellReference' => true,
  978. ],
  979. 'HYPGEOMDIST' => [
  980. 'category' => Category::CATEGORY_STATISTICAL,
  981. 'functionCall' => [Statistical::class, 'HYPGEOMDIST'],
  982. 'argumentCount' => '4',
  983. ],
  984. 'IF' => [
  985. 'category' => Category::CATEGORY_LOGICAL,
  986. 'functionCall' => [Logical::class, 'statementIf'],
  987. 'argumentCount' => '1-3',
  988. ],
  989. 'IFERROR' => [
  990. 'category' => Category::CATEGORY_LOGICAL,
  991. 'functionCall' => [Logical::class, 'IFERROR'],
  992. 'argumentCount' => '2',
  993. ],
  994. 'IMABS' => [
  995. 'category' => Category::CATEGORY_ENGINEERING,
  996. 'functionCall' => [Engineering::class, 'IMABS'],
  997. 'argumentCount' => '1',
  998. ],
  999. 'IMAGINARY' => [
  1000. 'category' => Category::CATEGORY_ENGINEERING,
  1001. 'functionCall' => [Engineering::class, 'IMAGINARY'],
  1002. 'argumentCount' => '1',
  1003. ],
  1004. 'IMARGUMENT' => [
  1005. 'category' => Category::CATEGORY_ENGINEERING,
  1006. 'functionCall' => [Engineering::class, 'IMARGUMENT'],
  1007. 'argumentCount' => '1',
  1008. ],
  1009. 'IMCONJUGATE' => [
  1010. 'category' => Category::CATEGORY_ENGINEERING,
  1011. 'functionCall' => [Engineering::class, 'IMCONJUGATE'],
  1012. 'argumentCount' => '1',
  1013. ],
  1014. 'IMCOS' => [
  1015. 'category' => Category::CATEGORY_ENGINEERING,
  1016. 'functionCall' => [Engineering::class, 'IMCOS'],
  1017. 'argumentCount' => '1',
  1018. ],
  1019. 'IMCOSH' => [
  1020. 'category' => Category::CATEGORY_ENGINEERING,
  1021. 'functionCall' => [Engineering::class, 'IMCOSH'],
  1022. 'argumentCount' => '1',
  1023. ],
  1024. 'IMCOT' => [
  1025. 'category' => Category::CATEGORY_ENGINEERING,
  1026. 'functionCall' => [Engineering::class, 'IMCOT'],
  1027. 'argumentCount' => '1',
  1028. ],
  1029. 'IMCSC' => [
  1030. 'category' => Category::CATEGORY_ENGINEERING,
  1031. 'functionCall' => [Engineering::class, 'IMCSC'],
  1032. 'argumentCount' => '1',
  1033. ],
  1034. 'IMCSCH' => [
  1035. 'category' => Category::CATEGORY_ENGINEERING,
  1036. 'functionCall' => [Engineering::class, 'IMCSCH'],
  1037. 'argumentCount' => '1',
  1038. ],
  1039. 'IMDIV' => [
  1040. 'category' => Category::CATEGORY_ENGINEERING,
  1041. 'functionCall' => [Engineering::class, 'IMDIV'],
  1042. 'argumentCount' => '2',
  1043. ],
  1044. 'IMEXP' => [
  1045. 'category' => Category::CATEGORY_ENGINEERING,
  1046. 'functionCall' => [Engineering::class, 'IMEXP'],
  1047. 'argumentCount' => '1',
  1048. ],
  1049. 'IMLN' => [
  1050. 'category' => Category::CATEGORY_ENGINEERING,
  1051. 'functionCall' => [Engineering::class, 'IMLN'],
  1052. 'argumentCount' => '1',
  1053. ],
  1054. 'IMLOG10' => [
  1055. 'category' => Category::CATEGORY_ENGINEERING,
  1056. 'functionCall' => [Engineering::class, 'IMLOG10'],
  1057. 'argumentCount' => '1',
  1058. ],
  1059. 'IMLOG2' => [
  1060. 'category' => Category::CATEGORY_ENGINEERING,
  1061. 'functionCall' => [Engineering::class, 'IMLOG2'],
  1062. 'argumentCount' => '1',
  1063. ],
  1064. 'IMPOWER' => [
  1065. 'category' => Category::CATEGORY_ENGINEERING,
  1066. 'functionCall' => [Engineering::class, 'IMPOWER'],
  1067. 'argumentCount' => '2',
  1068. ],
  1069. 'IMPRODUCT' => [
  1070. 'category' => Category::CATEGORY_ENGINEERING,
  1071. 'functionCall' => [Engineering::class, 'IMPRODUCT'],
  1072. 'argumentCount' => '1+',
  1073. ],
  1074. 'IMREAL' => [
  1075. 'category' => Category::CATEGORY_ENGINEERING,
  1076. 'functionCall' => [Engineering::class, 'IMREAL'],
  1077. 'argumentCount' => '1',
  1078. ],
  1079. 'IMSEC' => [
  1080. 'category' => Category::CATEGORY_ENGINEERING,
  1081. 'functionCall' => [Engineering::class, 'IMSEC'],
  1082. 'argumentCount' => '1',
  1083. ],
  1084. 'IMSECH' => [
  1085. 'category' => Category::CATEGORY_ENGINEERING,
  1086. 'functionCall' => [Engineering::class, 'IMSECH'],
  1087. 'argumentCount' => '1',
  1088. ],
  1089. 'IMSIN' => [
  1090. 'category' => Category::CATEGORY_ENGINEERING,
  1091. 'functionCall' => [Engineering::class, 'IMSIN'],
  1092. 'argumentCount' => '1',
  1093. ],
  1094. 'IMSINH' => [
  1095. 'category' => Category::CATEGORY_ENGINEERING,
  1096. 'functionCall' => [Engineering::class, 'IMSINH'],
  1097. 'argumentCount' => '1',
  1098. ],
  1099. 'IMSQRT' => [
  1100. 'category' => Category::CATEGORY_ENGINEERING,
  1101. 'functionCall' => [Engineering::class, 'IMSQRT'],
  1102. 'argumentCount' => '1',
  1103. ],
  1104. 'IMSUB' => [
  1105. 'category' => Category::CATEGORY_ENGINEERING,
  1106. 'functionCall' => [Engineering::class, 'IMSUB'],
  1107. 'argumentCount' => '2',
  1108. ],
  1109. 'IMSUM' => [
  1110. 'category' => Category::CATEGORY_ENGINEERING,
  1111. 'functionCall' => [Engineering::class, 'IMSUM'],
  1112. 'argumentCount' => '1+',
  1113. ],
  1114. 'IMTAN' => [
  1115. 'category' => Category::CATEGORY_ENGINEERING,
  1116. 'functionCall' => [Engineering::class, 'IMTAN'],
  1117. 'argumentCount' => '1',
  1118. ],
  1119. 'INDEX' => [
  1120. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1121. 'functionCall' => [LookupRef::class, 'INDEX'],
  1122. 'argumentCount' => '1-4',
  1123. ],
  1124. 'INDIRECT' => [
  1125. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1126. 'functionCall' => [LookupRef::class, 'INDIRECT'],
  1127. 'argumentCount' => '1,2',
  1128. 'passCellReference' => true,
  1129. ],
  1130. 'INFO' => [
  1131. 'category' => Category::CATEGORY_INFORMATION,
  1132. 'functionCall' => [Functions::class, 'DUMMY'],
  1133. 'argumentCount' => '1',
  1134. ],
  1135. 'INT' => [
  1136. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1137. 'functionCall' => [MathTrig::class, 'INT'],
  1138. 'argumentCount' => '1',
  1139. ],
  1140. 'INTERCEPT' => [
  1141. 'category' => Category::CATEGORY_STATISTICAL,
  1142. 'functionCall' => [Statistical::class, 'INTERCEPT'],
  1143. 'argumentCount' => '2',
  1144. ],
  1145. 'INTRATE' => [
  1146. 'category' => Category::CATEGORY_FINANCIAL,
  1147. 'functionCall' => [Financial::class, 'INTRATE'],
  1148. 'argumentCount' => '4,5',
  1149. ],
  1150. 'IPMT' => [
  1151. 'category' => Category::CATEGORY_FINANCIAL,
  1152. 'functionCall' => [Financial::class, 'IPMT'],
  1153. 'argumentCount' => '4-6',
  1154. ],
  1155. 'IRR' => [
  1156. 'category' => Category::CATEGORY_FINANCIAL,
  1157. 'functionCall' => [Financial::class, 'IRR'],
  1158. 'argumentCount' => '1,2',
  1159. ],
  1160. 'ISBLANK' => [
  1161. 'category' => Category::CATEGORY_INFORMATION,
  1162. 'functionCall' => [Functions::class, 'isBlank'],
  1163. 'argumentCount' => '1',
  1164. ],
  1165. 'ISERR' => [
  1166. 'category' => Category::CATEGORY_INFORMATION,
  1167. 'functionCall' => [Functions::class, 'isErr'],
  1168. 'argumentCount' => '1',
  1169. ],
  1170. 'ISERROR' => [
  1171. 'category' => Category::CATEGORY_INFORMATION,
  1172. 'functionCall' => [Functions::class, 'isError'],
  1173. 'argumentCount' => '1',
  1174. ],
  1175. 'ISEVEN' => [
  1176. 'category' => Category::CATEGORY_INFORMATION,
  1177. 'functionCall' => [Functions::class, 'isEven'],
  1178. 'argumentCount' => '1',
  1179. ],
  1180. 'ISFORMULA' => [
  1181. 'category' => Category::CATEGORY_INFORMATION,
  1182. 'functionCall' => [Functions::class, 'isFormula'],
  1183. 'argumentCount' => '1',
  1184. 'passCellReference' => true,
  1185. 'passByReference' => [true],
  1186. ],
  1187. 'ISLOGICAL' => [
  1188. 'category' => Category::CATEGORY_INFORMATION,
  1189. 'functionCall' => [Functions::class, 'isLogical'],
  1190. 'argumentCount' => '1',
  1191. ],
  1192. 'ISNA' => [
  1193. 'category' => Category::CATEGORY_INFORMATION,
  1194. 'functionCall' => [Functions::class, 'isNa'],
  1195. 'argumentCount' => '1',
  1196. ],
  1197. 'ISNONTEXT' => [
  1198. 'category' => Category::CATEGORY_INFORMATION,
  1199. 'functionCall' => [Functions::class, 'isNonText'],
  1200. 'argumentCount' => '1',
  1201. ],
  1202. 'ISNUMBER' => [
  1203. 'category' => Category::CATEGORY_INFORMATION,
  1204. 'functionCall' => [Functions::class, 'isNumber'],
  1205. 'argumentCount' => '1',
  1206. ],
  1207. 'ISODD' => [
  1208. 'category' => Category::CATEGORY_INFORMATION,
  1209. 'functionCall' => [Functions::class, 'isOdd'],
  1210. 'argumentCount' => '1',
  1211. ],
  1212. 'ISOWEEKNUM' => [
  1213. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1214. 'functionCall' => [DateTime::class, 'ISOWEEKNUM'],
  1215. 'argumentCount' => '1',
  1216. ],
  1217. 'ISPMT' => [
  1218. 'category' => Category::CATEGORY_FINANCIAL,
  1219. 'functionCall' => [Financial::class, 'ISPMT'],
  1220. 'argumentCount' => '4',
  1221. ],
  1222. 'ISREF' => [
  1223. 'category' => Category::CATEGORY_INFORMATION,
  1224. 'functionCall' => [Functions::class, 'DUMMY'],
  1225. 'argumentCount' => '1',
  1226. ],
  1227. 'ISTEXT' => [
  1228. 'category' => Category::CATEGORY_INFORMATION,
  1229. 'functionCall' => [Functions::class, 'isText'],
  1230. 'argumentCount' => '1',
  1231. ],
  1232. 'JIS' => [
  1233. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1234. 'functionCall' => [Functions::class, 'DUMMY'],
  1235. 'argumentCount' => '1',
  1236. ],
  1237. 'KURT' => [
  1238. 'category' => Category::CATEGORY_STATISTICAL,
  1239. 'functionCall' => [Statistical::class, 'KURT'],
  1240. 'argumentCount' => '1+',
  1241. ],
  1242. 'LARGE' => [
  1243. 'category' => Category::CATEGORY_STATISTICAL,
  1244. 'functionCall' => [Statistical::class, 'LARGE'],
  1245. 'argumentCount' => '2',
  1246. ],
  1247. 'LCM' => [
  1248. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1249. 'functionCall' => [MathTrig::class, 'LCM'],
  1250. 'argumentCount' => '1+',
  1251. ],
  1252. 'LEFT' => [
  1253. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1254. 'functionCall' => [TextData::class, 'LEFT'],
  1255. 'argumentCount' => '1,2',
  1256. ],
  1257. 'LEFTB' => [
  1258. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1259. 'functionCall' => [TextData::class, 'LEFT'],
  1260. 'argumentCount' => '1,2',
  1261. ],
  1262. 'LEN' => [
  1263. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1264. 'functionCall' => [TextData::class, 'STRINGLENGTH'],
  1265. 'argumentCount' => '1',
  1266. ],
  1267. 'LENB' => [
  1268. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1269. 'functionCall' => [TextData::class, 'STRINGLENGTH'],
  1270. 'argumentCount' => '1',
  1271. ],
  1272. 'LINEST' => [
  1273. 'category' => Category::CATEGORY_STATISTICAL,
  1274. 'functionCall' => [Statistical::class, 'LINEST'],
  1275. 'argumentCount' => '1-4',
  1276. ],
  1277. 'LN' => [
  1278. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1279. 'functionCall' => 'log',
  1280. 'argumentCount' => '1',
  1281. ],
  1282. 'LOG' => [
  1283. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1284. 'functionCall' => [MathTrig::class, 'logBase'],
  1285. 'argumentCount' => '1,2',
  1286. ],
  1287. 'LOG10' => [
  1288. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1289. 'functionCall' => 'log10',
  1290. 'argumentCount' => '1',
  1291. ],
  1292. 'LOGEST' => [
  1293. 'category' => Category::CATEGORY_STATISTICAL,
  1294. 'functionCall' => [Statistical::class, 'LOGEST'],
  1295. 'argumentCount' => '1-4',
  1296. ],
  1297. 'LOGINV' => [
  1298. 'category' => Category::CATEGORY_STATISTICAL,
  1299. 'functionCall' => [Statistical::class, 'LOGINV'],
  1300. 'argumentCount' => '3',
  1301. ],
  1302. 'LOGNORMDIST' => [
  1303. 'category' => Category::CATEGORY_STATISTICAL,
  1304. 'functionCall' => [Statistical::class, 'LOGNORMDIST'],
  1305. 'argumentCount' => '3',
  1306. ],
  1307. 'LOOKUP' => [
  1308. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1309. 'functionCall' => [LookupRef::class, 'LOOKUP'],
  1310. 'argumentCount' => '2,3',
  1311. ],
  1312. 'LOWER' => [
  1313. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1314. 'functionCall' => [TextData::class, 'LOWERCASE'],
  1315. 'argumentCount' => '1',
  1316. ],
  1317. 'MATCH' => [
  1318. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1319. 'functionCall' => [LookupRef::class, 'MATCH'],
  1320. 'argumentCount' => '2,3',
  1321. ],
  1322. 'MAX' => [
  1323. 'category' => Category::CATEGORY_STATISTICAL,
  1324. 'functionCall' => [Statistical::class, 'MAX'],
  1325. 'argumentCount' => '1+',
  1326. ],
  1327. 'MAXA' => [
  1328. 'category' => Category::CATEGORY_STATISTICAL,
  1329. 'functionCall' => [Statistical::class, 'MAXA'],
  1330. 'argumentCount' => '1+',
  1331. ],
  1332. 'MAXIF' => [
  1333. 'category' => Category::CATEGORY_STATISTICAL,
  1334. 'functionCall' => [Statistical::class, 'MAXIF'],
  1335. 'argumentCount' => '2+',
  1336. ],
  1337. 'MDETERM' => [
  1338. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1339. 'functionCall' => [MathTrig::class, 'MDETERM'],
  1340. 'argumentCount' => '1',
  1341. ],
  1342. 'MDURATION' => [
  1343. 'category' => Category::CATEGORY_FINANCIAL,
  1344. 'functionCall' => [Functions::class, 'DUMMY'],
  1345. 'argumentCount' => '5,6',
  1346. ],
  1347. 'MEDIAN' => [
  1348. 'category' => Category::CATEGORY_STATISTICAL,
  1349. 'functionCall' => [Statistical::class, 'MEDIAN'],
  1350. 'argumentCount' => '1+',
  1351. ],
  1352. 'MEDIANIF' => [
  1353. 'category' => Category::CATEGORY_STATISTICAL,
  1354. 'functionCall' => [Functions::class, 'DUMMY'],
  1355. 'argumentCount' => '2+',
  1356. ],
  1357. 'MID' => [
  1358. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1359. 'functionCall' => [TextData::class, 'MID'],
  1360. 'argumentCount' => '3',
  1361. ],
  1362. 'MIDB' => [
  1363. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1364. 'functionCall' => [TextData::class, 'MID'],
  1365. 'argumentCount' => '3',
  1366. ],
  1367. 'MIN' => [
  1368. 'category' => Category::CATEGORY_STATISTICAL,
  1369. 'functionCall' => [Statistical::class, 'MIN'],
  1370. 'argumentCount' => '1+',
  1371. ],
  1372. 'MINA' => [
  1373. 'category' => Category::CATEGORY_STATISTICAL,
  1374. 'functionCall' => [Statistical::class, 'MINA'],
  1375. 'argumentCount' => '1+',
  1376. ],
  1377. 'MINIF' => [
  1378. 'category' => Category::CATEGORY_STATISTICAL,
  1379. 'functionCall' => [Statistical::class, 'MINIF'],
  1380. 'argumentCount' => '2+',
  1381. ],
  1382. 'MINUTE' => [
  1383. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1384. 'functionCall' => [DateTime::class, 'MINUTE'],
  1385. 'argumentCount' => '1',
  1386. ],
  1387. 'MINVERSE' => [
  1388. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1389. 'functionCall' => [MathTrig::class, 'MINVERSE'],
  1390. 'argumentCount' => '1',
  1391. ],
  1392. 'MIRR' => [
  1393. 'category' => Category::CATEGORY_FINANCIAL,
  1394. 'functionCall' => [Financial::class, 'MIRR'],
  1395. 'argumentCount' => '3',
  1396. ],
  1397. 'MMULT' => [
  1398. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1399. 'functionCall' => [MathTrig::class, 'MMULT'],
  1400. 'argumentCount' => '2',
  1401. ],
  1402. 'MOD' => [
  1403. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1404. 'functionCall' => [MathTrig::class, 'MOD'],
  1405. 'argumentCount' => '2',
  1406. ],
  1407. 'MODE' => [
  1408. 'category' => Category::CATEGORY_STATISTICAL,
  1409. 'functionCall' => [Statistical::class, 'MODE'],
  1410. 'argumentCount' => '1+',
  1411. ],
  1412. 'MODE.SNGL' => [
  1413. 'category' => Category::CATEGORY_STATISTICAL,
  1414. 'functionCall' => [Statistical::class, 'MODE'],
  1415. 'argumentCount' => '1+',
  1416. ],
  1417. 'MONTH' => [
  1418. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1419. 'functionCall' => [DateTime::class, 'MONTHOFYEAR'],
  1420. 'argumentCount' => '1',
  1421. ],
  1422. 'MROUND' => [
  1423. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1424. 'functionCall' => [MathTrig::class, 'MROUND'],
  1425. 'argumentCount' => '2',
  1426. ],
  1427. 'MULTINOMIAL' => [
  1428. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1429. 'functionCall' => [MathTrig::class, 'MULTINOMIAL'],
  1430. 'argumentCount' => '1+',
  1431. ],
  1432. 'N' => [
  1433. 'category' => Category::CATEGORY_INFORMATION,
  1434. 'functionCall' => [Functions::class, 'n'],
  1435. 'argumentCount' => '1',
  1436. ],
  1437. 'NA' => [
  1438. 'category' => Category::CATEGORY_INFORMATION,
  1439. 'functionCall' => [Functions::class, 'NA'],
  1440. 'argumentCount' => '0',
  1441. ],
  1442. 'NEGBINOMDIST' => [
  1443. 'category' => Category::CATEGORY_STATISTICAL,
  1444. 'functionCall' => [Statistical::class, 'NEGBINOMDIST'],
  1445. 'argumentCount' => '3',
  1446. ],
  1447. 'NETWORKDAYS' => [
  1448. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1449. 'functionCall' => [DateTime::class, 'NETWORKDAYS'],
  1450. 'argumentCount' => '2+',
  1451. ],
  1452. 'NOMINAL' => [
  1453. 'category' => Category::CATEGORY_FINANCIAL,
  1454. 'functionCall' => [Financial::class, 'NOMINAL'],
  1455. 'argumentCount' => '2',
  1456. ],
  1457. 'NORMDIST' => [
  1458. 'category' => Category::CATEGORY_STATISTICAL,
  1459. 'functionCall' => [Statistical::class, 'NORMDIST'],
  1460. 'argumentCount' => '4',
  1461. ],
  1462. 'NORMINV' => [
  1463. 'category' => Category::CATEGORY_STATISTICAL,
  1464. 'functionCall' => [Statistical::class, 'NORMINV'],
  1465. 'argumentCount' => '3',
  1466. ],
  1467. 'NORMSDIST' => [
  1468. 'category' => Category::CATEGORY_STATISTICAL,
  1469. 'functionCall' => [Statistical::class, 'NORMSDIST'],
  1470. 'argumentCount' => '1',
  1471. ],
  1472. 'NORMSINV' => [
  1473. 'category' => Category::CATEGORY_STATISTICAL,
  1474. 'functionCall' => [Statistical::class, 'NORMSINV'],
  1475. 'argumentCount' => '1',
  1476. ],
  1477. 'NOT' => [
  1478. 'category' => Category::CATEGORY_LOGICAL,
  1479. 'functionCall' => [Logical::class, 'NOT'],
  1480. 'argumentCount' => '1',
  1481. ],
  1482. 'NOW' => [
  1483. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1484. 'functionCall' => [DateTime::class, 'DATETIMENOW'],
  1485. 'argumentCount' => '0',
  1486. ],
  1487. 'NPER' => [
  1488. 'category' => Category::CATEGORY_FINANCIAL,
  1489. 'functionCall' => [Financial::class, 'NPER'],
  1490. 'argumentCount' => '3-5',
  1491. ],
  1492. 'NPV' => [
  1493. 'category' => Category::CATEGORY_FINANCIAL,
  1494. 'functionCall' => [Financial::class, 'NPV'],
  1495. 'argumentCount' => '2+',
  1496. ],
  1497. 'NUMBERVALUE' => [
  1498. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1499. 'functionCall' => [TextData::class, 'NUMBERVALUE'],
  1500. 'argumentCount' => '1+',
  1501. ],
  1502. 'OCT2BIN' => [
  1503. 'category' => Category::CATEGORY_ENGINEERING,
  1504. 'functionCall' => [Engineering::class, 'OCTTOBIN'],
  1505. 'argumentCount' => '1,2',
  1506. ],
  1507. 'OCT2DEC' => [
  1508. 'category' => Category::CATEGORY_ENGINEERING,
  1509. 'functionCall' => [Engineering::class, 'OCTTODEC'],
  1510. 'argumentCount' => '1',
  1511. ],
  1512. 'OCT2HEX' => [
  1513. 'category' => Category::CATEGORY_ENGINEERING,
  1514. 'functionCall' => [Engineering::class, 'OCTTOHEX'],
  1515. 'argumentCount' => '1,2',
  1516. ],
  1517. 'ODD' => [
  1518. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1519. 'functionCall' => [MathTrig::class, 'ODD'],
  1520. 'argumentCount' => '1',
  1521. ],
  1522. 'ODDFPRICE' => [
  1523. 'category' => Category::CATEGORY_FINANCIAL,
  1524. 'functionCall' => [Functions::class, 'DUMMY'],
  1525. 'argumentCount' => '8,9',
  1526. ],
  1527. 'ODDFYIELD' => [
  1528. 'category' => Category::CATEGORY_FINANCIAL,
  1529. 'functionCall' => [Functions::class, 'DUMMY'],
  1530. 'argumentCount' => '8,9',
  1531. ],
  1532. 'ODDLPRICE' => [
  1533. 'category' => Category::CATEGORY_FINANCIAL,
  1534. 'functionCall' => [Functions::class, 'DUMMY'],
  1535. 'argumentCount' => '7,8',
  1536. ],
  1537. 'ODDLYIELD' => [
  1538. 'category' => Category::CATEGORY_FINANCIAL,
  1539. 'functionCall' => [Functions::class, 'DUMMY'],
  1540. 'argumentCount' => '7,8',
  1541. ],
  1542. 'OFFSET' => [
  1543. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1544. 'functionCall' => [LookupRef::class, 'OFFSET'],
  1545. 'argumentCount' => '3-5',
  1546. 'passCellReference' => true,
  1547. 'passByReference' => [true],
  1548. ],
  1549. 'OR' => [
  1550. 'category' => Category::CATEGORY_LOGICAL,
  1551. 'functionCall' => [Logical::class, 'logicalOr'],
  1552. 'argumentCount' => '1+',
  1553. ],
  1554. 'PDURATION' => [
  1555. 'category' => Category::CATEGORY_FINANCIAL,
  1556. 'functionCall' => [Financial::class, 'PDURATION'],
  1557. 'argumentCount' => '3',
  1558. ],
  1559. 'PEARSON' => [
  1560. 'category' => Category::CATEGORY_STATISTICAL,
  1561. 'functionCall' => [Statistical::class, 'CORREL'],
  1562. 'argumentCount' => '2',
  1563. ],
  1564. 'PERCENTILE' => [
  1565. 'category' => Category::CATEGORY_STATISTICAL,
  1566. 'functionCall' => [Statistical::class, 'PERCENTILE'],
  1567. 'argumentCount' => '2',
  1568. ],
  1569. 'PERCENTRANK' => [
  1570. 'category' => Category::CATEGORY_STATISTICAL,
  1571. 'functionCall' => [Statistical::class, 'PERCENTRANK'],
  1572. 'argumentCount' => '2,3',
  1573. ],
  1574. 'PERMUT' => [
  1575. 'category' => Category::CATEGORY_STATISTICAL,
  1576. 'functionCall' => [Statistical::class, 'PERMUT'],
  1577. 'argumentCount' => '2',
  1578. ],
  1579. 'PHONETIC' => [
  1580. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1581. 'functionCall' => [Functions::class, 'DUMMY'],
  1582. 'argumentCount' => '1',
  1583. ],
  1584. 'PI' => [
  1585. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1586. 'functionCall' => 'pi',
  1587. 'argumentCount' => '0',
  1588. ],
  1589. 'PMT' => [
  1590. 'category' => Category::CATEGORY_FINANCIAL,
  1591. 'functionCall' => [Financial::class, 'PMT'],
  1592. 'argumentCount' => '3-5',
  1593. ],
  1594. 'POISSON' => [
  1595. 'category' => Category::CATEGORY_STATISTICAL,
  1596. 'functionCall' => [Statistical::class, 'POISSON'],
  1597. 'argumentCount' => '3',
  1598. ],
  1599. 'POWER' => [
  1600. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1601. 'functionCall' => [MathTrig::class, 'POWER'],
  1602. 'argumentCount' => '2',
  1603. ],
  1604. 'PPMT' => [
  1605. 'category' => Category::CATEGORY_FINANCIAL,
  1606. 'functionCall' => [Financial::class, 'PPMT'],
  1607. 'argumentCount' => '4-6',
  1608. ],
  1609. 'PRICE' => [
  1610. 'category' => Category::CATEGORY_FINANCIAL,
  1611. 'functionCall' => [Financial::class, 'PRICE'],
  1612. 'argumentCount' => '6,7',
  1613. ],
  1614. 'PRICEDISC' => [
  1615. 'category' => Category::CATEGORY_FINANCIAL,
  1616. 'functionCall' => [Financial::class, 'PRICEDISC'],
  1617. 'argumentCount' => '4,5',
  1618. ],
  1619. 'PRICEMAT' => [
  1620. 'category' => Category::CATEGORY_FINANCIAL,
  1621. 'functionCall' => [Financial::class, 'PRICEMAT'],
  1622. 'argumentCount' => '5,6',
  1623. ],
  1624. 'PROB' => [
  1625. 'category' => Category::CATEGORY_STATISTICAL,
  1626. 'functionCall' => [Functions::class, 'DUMMY'],
  1627. 'argumentCount' => '3,4',
  1628. ],
  1629. 'PRODUCT' => [
  1630. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1631. 'functionCall' => [MathTrig::class, 'PRODUCT'],
  1632. 'argumentCount' => '1+',
  1633. ],
  1634. 'PROPER' => [
  1635. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1636. 'functionCall' => [TextData::class, 'PROPERCASE'],
  1637. 'argumentCount' => '1',
  1638. ],
  1639. 'PV' => [
  1640. 'category' => Category::CATEGORY_FINANCIAL,
  1641. 'functionCall' => [Financial::class, 'PV'],
  1642. 'argumentCount' => '3-5',
  1643. ],
  1644. 'QUARTILE' => [
  1645. 'category' => Category::CATEGORY_STATISTICAL,
  1646. 'functionCall' => [Statistical::class, 'QUARTILE'],
  1647. 'argumentCount' => '2',
  1648. ],
  1649. 'QUOTIENT' => [
  1650. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1651. 'functionCall' => [MathTrig::class, 'QUOTIENT'],
  1652. 'argumentCount' => '2',
  1653. ],
  1654. 'RADIANS' => [
  1655. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1656. 'functionCall' => 'deg2rad',
  1657. 'argumentCount' => '1',
  1658. ],
  1659. 'RAND' => [
  1660. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1661. 'functionCall' => [MathTrig::class, 'RAND'],
  1662. 'argumentCount' => '0',
  1663. ],
  1664. 'RANDBETWEEN' => [
  1665. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1666. 'functionCall' => [MathTrig::class, 'RAND'],
  1667. 'argumentCount' => '2',
  1668. ],
  1669. 'RANK' => [
  1670. 'category' => Category::CATEGORY_STATISTICAL,
  1671. 'functionCall' => [Statistical::class, 'RANK'],
  1672. 'argumentCount' => '2,3',
  1673. ],
  1674. 'RATE' => [
  1675. 'category' => Category::CATEGORY_FINANCIAL,
  1676. 'functionCall' => [Financial::class, 'RATE'],
  1677. 'argumentCount' => '3-6',
  1678. ],
  1679. 'RECEIVED' => [
  1680. 'category' => Category::CATEGORY_FINANCIAL,
  1681. 'functionCall' => [Financial::class, 'RECEIVED'],
  1682. 'argumentCount' => '4-5',
  1683. ],
  1684. 'REPLACE' => [
  1685. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1686. 'functionCall' => [TextData::class, 'REPLACE'],
  1687. 'argumentCount' => '4',
  1688. ],
  1689. 'REPLACEB' => [
  1690. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1691. 'functionCall' => [TextData::class, 'REPLACE'],
  1692. 'argumentCount' => '4',
  1693. ],
  1694. 'REPT' => [
  1695. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1696. 'functionCall' => 'str_repeat',
  1697. 'argumentCount' => '2',
  1698. ],
  1699. 'RIGHT' => [
  1700. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1701. 'functionCall' => [TextData::class, 'RIGHT'],
  1702. 'argumentCount' => '1,2',
  1703. ],
  1704. 'RIGHTB' => [
  1705. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1706. 'functionCall' => [TextData::class, 'RIGHT'],
  1707. 'argumentCount' => '1,2',
  1708. ],
  1709. 'ROMAN' => [
  1710. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1711. 'functionCall' => [MathTrig::class, 'ROMAN'],
  1712. 'argumentCount' => '1,2',
  1713. ],
  1714. 'ROUND' => [
  1715. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1716. 'functionCall' => 'round',
  1717. 'argumentCount' => '2',
  1718. ],
  1719. 'ROUNDDOWN' => [
  1720. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1721. 'functionCall' => [MathTrig::class, 'ROUNDDOWN'],
  1722. 'argumentCount' => '2',
  1723. ],
  1724. 'ROUNDUP' => [
  1725. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1726. 'functionCall' => [MathTrig::class, 'ROUNDUP'],
  1727. 'argumentCount' => '2',
  1728. ],
  1729. 'ROW' => [
  1730. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1731. 'functionCall' => [LookupRef::class, 'ROW'],
  1732. 'argumentCount' => '-1',
  1733. 'passByReference' => [true],
  1734. ],
  1735. 'ROWS' => [
  1736. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1737. 'functionCall' => [LookupRef::class, 'ROWS'],
  1738. 'argumentCount' => '1',
  1739. ],
  1740. 'RRI' => [
  1741. 'category' => Category::CATEGORY_FINANCIAL,
  1742. 'functionCall' => [Financial::class, 'RRI'],
  1743. 'argumentCount' => '3',
  1744. ],
  1745. 'RSQ' => [
  1746. 'category' => Category::CATEGORY_STATISTICAL,
  1747. 'functionCall' => [Statistical::class, 'RSQ'],
  1748. 'argumentCount' => '2',
  1749. ],
  1750. 'RTD' => [
  1751. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1752. 'functionCall' => [Functions::class, 'DUMMY'],
  1753. 'argumentCount' => '1+',
  1754. ],
  1755. 'SEARCH' => [
  1756. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1757. 'functionCall' => [TextData::class, 'SEARCHINSENSITIVE'],
  1758. 'argumentCount' => '2,3',
  1759. ],
  1760. 'SEARCHB' => [
  1761. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1762. 'functionCall' => [TextData::class, 'SEARCHINSENSITIVE'],
  1763. 'argumentCount' => '2,3',
  1764. ],
  1765. 'SEC' => [
  1766. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1767. 'functionCall' => [MathTrig::class, 'SEC'],
  1768. 'argumentCount' => '1',
  1769. ],
  1770. 'SECH' => [
  1771. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1772. 'functionCall' => [MathTrig::class, 'SECH'],
  1773. 'argumentCount' => '1',
  1774. ],
  1775. 'SECOND' => [
  1776. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1777. 'functionCall' => [DateTime::class, 'SECOND'],
  1778. 'argumentCount' => '1',
  1779. ],
  1780. 'SERIESSUM' => [
  1781. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1782. 'functionCall' => [MathTrig::class, 'SERIESSUM'],
  1783. 'argumentCount' => '4',
  1784. ],
  1785. 'SIGN' => [
  1786. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1787. 'functionCall' => [MathTrig::class, 'SIGN'],
  1788. 'argumentCount' => '1',
  1789. ],
  1790. 'SIN' => [
  1791. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1792. 'functionCall' => 'sin',
  1793. 'argumentCount' => '1',
  1794. ],
  1795. 'SINH' => [
  1796. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1797. 'functionCall' => 'sinh',
  1798. 'argumentCount' => '1',
  1799. ],
  1800. 'SKEW' => [
  1801. 'category' => Category::CATEGORY_STATISTICAL,
  1802. 'functionCall' => [Statistical::class, 'SKEW'],
  1803. 'argumentCount' => '1+',
  1804. ],
  1805. 'SLN' => [
  1806. 'category' => Category::CATEGORY_FINANCIAL,
  1807. 'functionCall' => [Financial::class, 'SLN'],
  1808. 'argumentCount' => '3',
  1809. ],
  1810. 'SLOPE' => [
  1811. 'category' => Category::CATEGORY_STATISTICAL,
  1812. 'functionCall' => [Statistical::class, 'SLOPE'],
  1813. 'argumentCount' => '2',
  1814. ],
  1815. 'SMALL' => [
  1816. 'category' => Category::CATEGORY_STATISTICAL,
  1817. 'functionCall' => [Statistical::class, 'SMALL'],
  1818. 'argumentCount' => '2',
  1819. ],
  1820. 'SQRT' => [
  1821. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1822. 'functionCall' => 'sqrt',
  1823. 'argumentCount' => '1',
  1824. ],
  1825. 'SQRTPI' => [
  1826. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1827. 'functionCall' => [MathTrig::class, 'SQRTPI'],
  1828. 'argumentCount' => '1',
  1829. ],
  1830. 'STANDARDIZE' => [
  1831. 'category' => Category::CATEGORY_STATISTICAL,
  1832. 'functionCall' => [Statistical::class, 'STANDARDIZE'],
  1833. 'argumentCount' => '3',
  1834. ],
  1835. 'STDEV' => [
  1836. 'category' => Category::CATEGORY_STATISTICAL,
  1837. 'functionCall' => [Statistical::class, 'STDEV'],
  1838. 'argumentCount' => '1+',
  1839. ],
  1840. 'STDEV.S' => [
  1841. 'category' => Category::CATEGORY_STATISTICAL,
  1842. 'functionCall' => [Statistical::class, 'STDEV'],
  1843. 'argumentCount' => '1+',
  1844. ],
  1845. 'STDEV.P' => [
  1846. 'category' => Category::CATEGORY_STATISTICAL,
  1847. 'functionCall' => [Statistical::class, 'STDEVP'],
  1848. 'argumentCount' => '1+',
  1849. ],
  1850. 'STDEVA' => [
  1851. 'category' => Category::CATEGORY_STATISTICAL,
  1852. 'functionCall' => [Statistical::class, 'STDEVA'],
  1853. 'argumentCount' => '1+',
  1854. ],
  1855. 'STDEVP' => [
  1856. 'category' => Category::CATEGORY_STATISTICAL,
  1857. 'functionCall' => [Statistical::class, 'STDEVP'],
  1858. 'argumentCount' => '1+',
  1859. ],
  1860. 'STDEVPA' => [
  1861. 'category' => Category::CATEGORY_STATISTICAL,
  1862. 'functionCall' => [Statistical::class, 'STDEVPA'],
  1863. 'argumentCount' => '1+',
  1864. ],
  1865. 'STEYX' => [
  1866. 'category' => Category::CATEGORY_STATISTICAL,
  1867. 'functionCall' => [Statistical::class, 'STEYX'],
  1868. 'argumentCount' => '2',
  1869. ],
  1870. 'SUBSTITUTE' => [
  1871. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1872. 'functionCall' => [TextData::class, 'SUBSTITUTE'],
  1873. 'argumentCount' => '3,4',
  1874. ],
  1875. 'SUBTOTAL' => [
  1876. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1877. 'functionCall' => [MathTrig::class, 'SUBTOTAL'],
  1878. 'argumentCount' => '2+',
  1879. 'passCellReference' => true,
  1880. ],
  1881. 'SUM' => [
  1882. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1883. 'functionCall' => [MathTrig::class, 'SUM'],
  1884. 'argumentCount' => '1+',
  1885. ],
  1886. 'SUMIF' => [
  1887. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1888. 'functionCall' => [MathTrig::class, 'SUMIF'],
  1889. 'argumentCount' => '2,3',
  1890. ],
  1891. 'SUMIFS' => [
  1892. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1893. 'functionCall' => [MathTrig::class, 'SUMIFS'],
  1894. 'argumentCount' => '3+',
  1895. ],
  1896. 'SUMPRODUCT' => [
  1897. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1898. 'functionCall' => [MathTrig::class, 'SUMPRODUCT'],
  1899. 'argumentCount' => '1+',
  1900. ],
  1901. 'SUMSQ' => [
  1902. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1903. 'functionCall' => [MathTrig::class, 'SUMSQ'],
  1904. 'argumentCount' => '1+',
  1905. ],
  1906. 'SUMX2MY2' => [
  1907. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1908. 'functionCall' => [MathTrig::class, 'SUMX2MY2'],
  1909. 'argumentCount' => '2',
  1910. ],
  1911. 'SUMX2PY2' => [
  1912. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1913. 'functionCall' => [MathTrig::class, 'SUMX2PY2'],
  1914. 'argumentCount' => '2',
  1915. ],
  1916. 'SUMXMY2' => [
  1917. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1918. 'functionCall' => [MathTrig::class, 'SUMXMY2'],
  1919. 'argumentCount' => '2',
  1920. ],
  1921. 'SYD' => [
  1922. 'category' => Category::CATEGORY_FINANCIAL,
  1923. 'functionCall' => [Financial::class, 'SYD'],
  1924. 'argumentCount' => '4',
  1925. ],
  1926. 'T' => [
  1927. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1928. 'functionCall' => [TextData::class, 'RETURNSTRING'],
  1929. 'argumentCount' => '1',
  1930. ],
  1931. 'TAN' => [
  1932. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1933. 'functionCall' => 'tan',
  1934. 'argumentCount' => '1',
  1935. ],
  1936. 'TANH' => [
  1937. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  1938. 'functionCall' => 'tanh',
  1939. 'argumentCount' => '1',
  1940. ],
  1941. 'TBILLEQ' => [
  1942. 'category' => Category::CATEGORY_FINANCIAL,
  1943. 'functionCall' => [Financial::class, 'TBILLEQ'],
  1944. 'argumentCount' => '3',
  1945. ],
  1946. 'TBILLPRICE' => [
  1947. 'category' => Category::CATEGORY_FINANCIAL,
  1948. 'functionCall' => [Financial::class, 'TBILLPRICE'],
  1949. 'argumentCount' => '3',
  1950. ],
  1951. 'TBILLYIELD' => [
  1952. 'category' => Category::CATEGORY_FINANCIAL,
  1953. 'functionCall' => [Financial::class, 'TBILLYIELD'],
  1954. 'argumentCount' => '3',
  1955. ],
  1956. 'TDIST' => [
  1957. 'category' => Category::CATEGORY_STATISTICAL,
  1958. 'functionCall' => [Statistical::class, 'TDIST'],
  1959. 'argumentCount' => '3',
  1960. ],
  1961. 'TEXT' => [
  1962. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1963. 'functionCall' => [TextData::class, 'TEXTFORMAT'],
  1964. 'argumentCount' => '2',
  1965. ],
  1966. 'TEXTJOIN' => [
  1967. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  1968. 'functionCall' => [TextData::class, 'TEXTJOIN'],
  1969. 'argumentCount' => '3+',
  1970. ],
  1971. 'TIME' => [
  1972. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1973. 'functionCall' => [DateTime::class, 'TIME'],
  1974. 'argumentCount' => '3',
  1975. ],
  1976. 'TIMEVALUE' => [
  1977. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1978. 'functionCall' => [DateTime::class, 'TIMEVALUE'],
  1979. 'argumentCount' => '1',
  1980. ],
  1981. 'TINV' => [
  1982. 'category' => Category::CATEGORY_STATISTICAL,
  1983. 'functionCall' => [Statistical::class, 'TINV'],
  1984. 'argumentCount' => '2',
  1985. ],
  1986. 'TODAY' => [
  1987. 'category' => Category::CATEGORY_DATE_AND_TIME,
  1988. 'functionCall' => [DateTime::class, 'DATENOW'],
  1989. 'argumentCount' => '0',
  1990. ],
  1991. 'TRANSPOSE' => [
  1992. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  1993. 'functionCall' => [LookupRef::class, 'TRANSPOSE'],
  1994. 'argumentCount' => '1',
  1995. ],
  1996. 'TREND' => [
  1997. 'category' => Category::CATEGORY_STATISTICAL,
  1998. 'functionCall' => [Statistical::class, 'TREND'],
  1999. 'argumentCount' => '1-4',
  2000. ],
  2001. 'TRIM' => [
  2002. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  2003. 'functionCall' => [TextData::class, 'TRIMSPACES'],
  2004. 'argumentCount' => '1',
  2005. ],
  2006. 'TRIMMEAN' => [
  2007. 'category' => Category::CATEGORY_STATISTICAL,
  2008. 'functionCall' => [Statistical::class, 'TRIMMEAN'],
  2009. 'argumentCount' => '2',
  2010. ],
  2011. 'TRUE' => [
  2012. 'category' => Category::CATEGORY_LOGICAL,
  2013. 'functionCall' => [Logical::class, 'TRUE'],
  2014. 'argumentCount' => '0',
  2015. ],
  2016. 'TRUNC' => [
  2017. 'category' => Category::CATEGORY_MATH_AND_TRIG,
  2018. 'functionCall' => [MathTrig::class, 'TRUNC'],
  2019. 'argumentCount' => '1,2',
  2020. ],
  2021. 'TTEST' => [
  2022. 'category' => Category::CATEGORY_STATISTICAL,
  2023. 'functionCall' => [Functions::class, 'DUMMY'],
  2024. 'argumentCount' => '4',
  2025. ],
  2026. 'TYPE' => [
  2027. 'category' => Category::CATEGORY_INFORMATION,
  2028. 'functionCall' => [Functions::class, 'TYPE'],
  2029. 'argumentCount' => '1',
  2030. ],
  2031. 'UNICHAR' => [
  2032. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  2033. 'functionCall' => [TextData::class, 'CHARACTER'],
  2034. 'argumentCount' => '1',
  2035. ],
  2036. 'UNICODE' => [
  2037. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  2038. 'functionCall' => [TextData::class, 'ASCIICODE'],
  2039. 'argumentCount' => '1',
  2040. ],
  2041. 'UPPER' => [
  2042. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  2043. 'functionCall' => [TextData::class, 'UPPERCASE'],
  2044. 'argumentCount' => '1',
  2045. ],
  2046. 'USDOLLAR' => [
  2047. 'category' => Category::CATEGORY_FINANCIAL,
  2048. 'functionCall' => [Functions::class, 'DUMMY'],
  2049. 'argumentCount' => '2',
  2050. ],
  2051. 'VALUE' => [
  2052. 'category' => Category::CATEGORY_TEXT_AND_DATA,
  2053. 'functionCall' => [TextData::class, 'VALUE'],
  2054. 'argumentCount' => '1',
  2055. ],
  2056. 'VAR' => [
  2057. 'category' => Category::CATEGORY_STATISTICAL,
  2058. 'functionCall' => [Statistical::class, 'VARFunc'],
  2059. 'argumentCount' => '1+',
  2060. ],
  2061. 'VAR.P' => [
  2062. 'category' => Category::CATEGORY_STATISTICAL,
  2063. 'functionCall' => [Statistical::class, 'VARP'],
  2064. 'argumentCount' => '1+',
  2065. ],
  2066. 'VAR.S' => [
  2067. 'category' => Category::CATEGORY_STATISTICAL,
  2068. 'functionCall' => [Statistical::class, 'VARFunc'],
  2069. 'argumentCount' => '1+',
  2070. ],
  2071. 'VARA' => [
  2072. 'category' => Category::CATEGORY_STATISTICAL,
  2073. 'functionCall' => [Statistical::class, 'VARA'],
  2074. 'argumentCount' => '1+',
  2075. ],
  2076. 'VARP' => [
  2077. 'category' => Category::CATEGORY_STATISTICAL,
  2078. 'functionCall' => [Statistical::class, 'VARP'],
  2079. 'argumentCount' => '1+',
  2080. ],
  2081. 'VARPA' => [
  2082. 'category' => Category::CATEGORY_STATISTICAL,
  2083. 'functionCall' => [Statistical::class, 'VARPA'],
  2084. 'argumentCount' => '1+',
  2085. ],
  2086. 'VDB' => [
  2087. 'category' => Category::CATEGORY_FINANCIAL,
  2088. 'functionCall' => [Functions::class, 'DUMMY'],
  2089. 'argumentCount' => '5-7',
  2090. ],
  2091. 'VLOOKUP' => [
  2092. 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
  2093. 'functionCall' => [LookupRef::class, 'VLOOKUP'],
  2094. 'argumentCount' => '3,4',
  2095. ],
  2096. 'WEEKDAY' => [
  2097. 'category' => Category::CATEGORY_DATE_AND_TIME,
  2098. 'functionCall' => [DateTime::class, 'WEEKDAY'],
  2099. 'argumentCount' => '1,2',
  2100. ],
  2101. 'WEEKNUM' => [
  2102. 'category' => Category::CATEGORY_DATE_AND_TIME,
  2103. 'functionCall' => [DateTime::class, 'WEEKNUM'],
  2104. 'argumentCount' => '1,2',
  2105. ],
  2106. 'WEIBULL' => [
  2107. 'category' => Category::CATEGORY_STATISTICAL,
  2108. 'functionCall' => [Statistical::class, 'WEIBULL'],
  2109. 'argumentCount' => '4',
  2110. ],
  2111. 'WORKDAY' => [
  2112. 'category' => Category::CATEGORY_DATE_AND_TIME,
  2113. 'functionCall' => [DateTime::class, 'WORKDAY'],
  2114. 'argumentCount' => '2+',
  2115. ],
  2116. 'XIRR' => [
  2117. 'category' => Category::CATEGORY_FINANCIAL,
  2118. 'functionCall' => [Financial::class, 'XIRR'],
  2119. 'argumentCount' => '2,3',
  2120. ],
  2121. 'XNPV' => [
  2122. 'category' => Category::CATEGORY_FINANCIAL,
  2123. 'functionCall' => [Financial::class, 'XNPV'],
  2124. 'argumentCount' => '3',
  2125. ],
  2126. 'XOR' => [
  2127. 'category' => Category::CATEGORY_LOGICAL,
  2128. 'functionCall' => [Logical::class, 'logicalXor'],
  2129. 'argumentCount' => '1+',
  2130. ],
  2131. 'YEAR' => [
  2132. 'category' => Category::CATEGORY_DATE_AND_TIME,
  2133. 'functionCall' => [DateTime::class, 'YEAR'],
  2134. 'argumentCount' => '1',
  2135. ],
  2136. 'YEARFRAC' => [
  2137. 'category' => Category::CATEGORY_DATE_AND_TIME,
  2138. 'functionCall' => [DateTime::class, 'YEARFRAC'],
  2139. 'argumentCount' => '2,3',
  2140. ],
  2141. 'YIELD' => [
  2142. 'category' => Category::CATEGORY_FINANCIAL,
  2143. 'functionCall' => [Functions::class, 'DUMMY'],
  2144. 'argumentCount' => '6,7',
  2145. ],
  2146. 'YIELDDISC' => [
  2147. 'category' => Category::CATEGORY_FINANCIAL,
  2148. 'functionCall' => [Financial::class, 'YIELDDISC'],
  2149. 'argumentCount' => '4,5',
  2150. ],
  2151. 'YIELDMAT' => [
  2152. 'category' => Category::CATEGORY_FINANCIAL,
  2153. 'functionCall' => [Financial::class, 'YIELDMAT'],
  2154. 'argumentCount' => '5,6',
  2155. ],
  2156. 'ZTEST' => [
  2157. 'category' => Category::CATEGORY_STATISTICAL,
  2158. 'functionCall' => [Statistical::class, 'ZTEST'],
  2159. 'argumentCount' => '2-3',
  2160. ],
  2161. ];
  2162. // Internal functions used for special control purposes
  2163. private static $controlFunctions = [
  2164. 'MKMATRIX' => [
  2165. 'argumentCount' => '*',
  2166. 'functionCall' => 'self::mkMatrix',
  2167. ],
  2168. ];
  2169. public function __construct(Spreadsheet $spreadsheet = null)
  2170. {
  2171. $this->delta = 1 * pow(10, 0 - ini_get('precision'));
  2172. $this->spreadsheet = $spreadsheet;
  2173. $this->cyclicReferenceStack = new CyclicReferenceStack();
  2174. $this->debugLog = new Logger($this->cyclicReferenceStack);
  2175. }
  2176. private static function loadLocales()
  2177. {
  2178. $localeFileDirectory = __DIR__ . '/locale/';
  2179. foreach (glob($localeFileDirectory . '*', GLOB_ONLYDIR) as $filename) {
  2180. $filename = substr($filename, strlen($localeFileDirectory));
  2181. if ($filename != 'en') {
  2182. self::$validLocaleLanguages[] = $filename;
  2183. }
  2184. }
  2185. }
  2186. /**
  2187. * Get an instance of this class.
  2188. *
  2189. * @param Spreadsheet $spreadsheet Injected spreadsheet for working with a PhpSpreadsheet Spreadsheet object,
  2190. * or NULL to create a standalone claculation engine
  2191. *
  2192. * @return Calculation
  2193. */
  2194. public static function getInstance(Spreadsheet $spreadsheet = null)
  2195. {
  2196. if ($spreadsheet !== null) {
  2197. $instance = $spreadsheet->getCalculationEngine();
  2198. if (isset($instance)) {
  2199. return $instance;
  2200. }
  2201. }
  2202. if (!isset(self::$instance) || (self::$instance === null)) {
  2203. self::$instance = new self();
  2204. }
  2205. return self::$instance;
  2206. }
  2207. /**
  2208. * Flush the calculation cache for any existing instance of this class
  2209. * but only if a Calculation instance exists.
  2210. */
  2211. public function flushInstance()
  2212. {
  2213. $this->clearCalculationCache();
  2214. }
  2215. /**
  2216. * Get the Logger for this calculation engine instance.
  2217. *
  2218. * @return Logger
  2219. */
  2220. public function getDebugLog()
  2221. {
  2222. return $this->debugLog;
  2223. }
  2224. /**
  2225. * __clone implementation. Cloning should not be allowed in a Singleton!
  2226. *
  2227. * @throws Exception
  2228. */
  2229. final public function __clone()
  2230. {
  2231. throw new Exception('Cloning the calculation engine is not allowed!');
  2232. }
  2233. /**
  2234. * Return the locale-specific translation of TRUE.
  2235. *
  2236. * @return string locale-specific translation of TRUE
  2237. */
  2238. public static function getTRUE()
  2239. {
  2240. return self::$localeBoolean['TRUE'];
  2241. }
  2242. /**
  2243. * Return the locale-specific translation of FALSE.
  2244. *
  2245. * @return string locale-specific translation of FALSE
  2246. */
  2247. public static function getFALSE()
  2248. {
  2249. return self::$localeBoolean['FALSE'];
  2250. }
  2251. /**
  2252. * Set the Array Return Type (Array or Value of first element in the array).
  2253. *
  2254. * @param string $returnType Array return type
  2255. *
  2256. * @return bool Success or failure
  2257. */
  2258. public static function setArrayReturnType($returnType)
  2259. {
  2260. if (($returnType == self::RETURN_ARRAY_AS_VALUE) ||
  2261. ($returnType == self::RETURN_ARRAY_AS_ERROR) ||
  2262. ($returnType == self::RETURN_ARRAY_AS_ARRAY)) {
  2263. self::$returnArrayAsType = $returnType;
  2264. return true;
  2265. }
  2266. return false;
  2267. }
  2268. /**
  2269. * Return the Array Return Type (Array or Value of first element in the array).
  2270. *
  2271. * @return string $returnType Array return type
  2272. */
  2273. public static function getArrayReturnType()
  2274. {
  2275. return self::$returnArrayAsType;
  2276. }
  2277. /**
  2278. * Is calculation caching enabled?
  2279. *
  2280. * @return bool
  2281. */
  2282. public function getCalculationCacheEnabled()
  2283. {
  2284. return $this->calculationCacheEnabled;
  2285. }
  2286. /**
  2287. * Enable/disable calculation cache.
  2288. *
  2289. * @param bool $pValue
  2290. */
  2291. public function setCalculationCacheEnabled($pValue)
  2292. {
  2293. $this->calculationCacheEnabled = $pValue;
  2294. $this->clearCalculationCache();
  2295. }
  2296. /**
  2297. * Enable calculation cache.
  2298. */
  2299. public function enableCalculationCache()
  2300. {
  2301. $this->setCalculationCacheEnabled(true);
  2302. }
  2303. /**
  2304. * Disable calculation cache.
  2305. */
  2306. public function disableCalculationCache()
  2307. {
  2308. $this->setCalculationCacheEnabled(false);
  2309. }
  2310. /**
  2311. * Clear calculation cache.
  2312. */
  2313. public function clearCalculationCache()
  2314. {
  2315. $this->calculationCache = [];
  2316. }
  2317. /**
  2318. * Clear calculation cache for a specified worksheet.
  2319. *
  2320. * @param string $worksheetName
  2321. */
  2322. public function clearCalculationCacheForWorksheet($worksheetName)
  2323. {
  2324. if (isset($this->calculationCache[$worksheetName])) {
  2325. unset($this->calculationCache[$worksheetName]);
  2326. }
  2327. }
  2328. /**
  2329. * Rename calculation cache for a specified worksheet.
  2330. *
  2331. * @param string $fromWorksheetName
  2332. * @param string $toWorksheetName
  2333. */
  2334. public function renameCalculationCacheForWorksheet($fromWorksheetName, $toWorksheetName)
  2335. {
  2336. if (isset($this->calculationCache[$fromWorksheetName])) {
  2337. $this->calculationCache[$toWorksheetName] = &$this->calculationCache[$fromWorksheetName];
  2338. unset($this->calculationCache[$fromWorksheetName]);
  2339. }
  2340. }
  2341. /**
  2342. * Get the currently defined locale code.
  2343. *
  2344. * @return string
  2345. */
  2346. public function getLocale()
  2347. {
  2348. return self::$localeLanguage;
  2349. }
  2350. /**
  2351. * Set the locale code.
  2352. *
  2353. * @param string $locale The locale to use for formula translation, eg: 'en_us'
  2354. *
  2355. * @return bool
  2356. */
  2357. public function setLocale($locale)
  2358. {
  2359. // Identify our locale and language
  2360. $language = $locale = strtolower($locale);
  2361. if (strpos($locale, '_') !== false) {
  2362. list($language) = explode('_', $locale);
  2363. }
  2364. if (count(self::$validLocaleLanguages) == 1) {
  2365. self::loadLocales();
  2366. }
  2367. // Test whether we have any language data for this language (any locale)
  2368. if (in_array($language, self::$validLocaleLanguages)) {
  2369. // initialise language/locale settings
  2370. self::$localeFunctions = [];
  2371. self::$localeArgumentSeparator = ',';
  2372. self::$localeBoolean = ['TRUE' => 'TRUE', 'FALSE' => 'FALSE', 'NULL' => 'NULL'];
  2373. // Default is English, if user isn't requesting english, then read the necessary data from the locale files
  2374. if ($locale != 'en_us') {
  2375. // Search for a file with a list of function names for locale
  2376. $functionNamesFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'functions';
  2377. if (!file_exists($functionNamesFile)) {
  2378. // If there isn't a locale specific function file, look for a language specific function file
  2379. $functionNamesFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . 'functions';
  2380. if (!file_exists($functionNamesFile)) {
  2381. return false;
  2382. }
  2383. }
  2384. // Retrieve the list of locale or language specific function names
  2385. $localeFunctions = file($functionNamesFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  2386. foreach ($localeFunctions as $localeFunction) {
  2387. list($localeFunction) = explode('##', $localeFunction); // Strip out comments
  2388. if (strpos($localeFunction, '=') !== false) {
  2389. list($fName, $lfName) = explode('=', $localeFunction);
  2390. $fName = trim($fName);
  2391. $lfName = trim($lfName);
  2392. if ((isset(self::$phpSpreadsheetFunctions[$fName])) && ($lfName != '') && ($fName != $lfName)) {
  2393. self::$localeFunctions[$fName] = $lfName;
  2394. }
  2395. }
  2396. }
  2397. // Default the TRUE and FALSE constants to the locale names of the TRUE() and FALSE() functions
  2398. if (isset(self::$localeFunctions['TRUE'])) {
  2399. self::$localeBoolean['TRUE'] = self::$localeFunctions['TRUE'];
  2400. }
  2401. if (isset(self::$localeFunctions['FALSE'])) {
  2402. self::$localeBoolean['FALSE'] = self::$localeFunctions['FALSE'];
  2403. }
  2404. $configFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'config';
  2405. if (!file_exists($configFile)) {
  2406. $configFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . 'config';
  2407. }
  2408. if (file_exists($configFile)) {
  2409. $localeSettings = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  2410. foreach ($localeSettings as $localeSetting) {
  2411. list($localeSetting) = explode('##', $localeSetting); // Strip out comments
  2412. if (strpos($localeSetting, '=') !== false) {
  2413. list($settingName, $settingValue) = explode('=', $localeSetting);
  2414. $settingName = strtoupper(trim($settingName));
  2415. switch ($settingName) {
  2416. case 'ARGUMENTSEPARATOR':
  2417. self::$localeArgumentSeparator = trim($settingValue);
  2418. break;
  2419. }
  2420. }
  2421. }
  2422. }
  2423. }
  2424. self::$functionReplaceFromExcel = self::$functionReplaceToExcel =
  2425. self::$functionReplaceFromLocale = self::$functionReplaceToLocale = null;
  2426. self::$localeLanguage = $locale;
  2427. return true;
  2428. }
  2429. return false;
  2430. }
  2431. /**
  2432. * @param string $fromSeparator
  2433. * @param string $toSeparator
  2434. * @param string $formula
  2435. * @param bool $inBraces
  2436. *
  2437. * @return string
  2438. */
  2439. public static function translateSeparator($fromSeparator, $toSeparator, $formula, &$inBraces)
  2440. {
  2441. $strlen = mb_strlen($formula);
  2442. for ($i = 0; $i < $strlen; ++$i) {
  2443. $chr = mb_substr($formula, $i, 1);
  2444. switch ($chr) {
  2445. case '{':
  2446. $inBraces = true;
  2447. break;
  2448. case '}':
  2449. $inBraces = false;
  2450. break;
  2451. case $fromSeparator:
  2452. if (!$inBraces) {
  2453. $formula = mb_substr($formula, 0, $i) . $toSeparator . mb_substr($formula, $i + 1);
  2454. }
  2455. }
  2456. }
  2457. return $formula;
  2458. }
  2459. /**
  2460. * @param string[] $from
  2461. * @param string[] $to
  2462. * @param string $formula
  2463. * @param string $fromSeparator
  2464. * @param string $toSeparator
  2465. *
  2466. * @return string
  2467. */
  2468. private static function translateFormula(array $from, array $to, $formula, $fromSeparator, $toSeparator)
  2469. {
  2470. // Convert any Excel function names to the required language
  2471. if (self::$localeLanguage !== 'en_us') {
  2472. $inBraces = false;
  2473. // If there is the possibility of braces within a quoted string, then we don't treat those as matrix indicators
  2474. if (strpos($formula, '"') !== false) {
  2475. // So instead we skip replacing in any quoted strings by only replacing in every other array element after we've exploded
  2476. // the formula
  2477. $temp = explode('"', $formula);
  2478. $i = false;
  2479. foreach ($temp as &$value) {
  2480. // Only count/replace in alternating array entries
  2481. if ($i = !$i) {
  2482. $value = preg_replace($from, $to, $value);
  2483. $value = self::translateSeparator($fromSeparator, $toSeparator, $value, $inBraces);
  2484. }
  2485. }
  2486. unset($value);
  2487. // Then rebuild the formula string
  2488. $formula = implode('"', $temp);
  2489. } else {
  2490. // If there's no quoted strings, then we do a simple count/replace
  2491. $formula = preg_replace($from, $to, $formula);
  2492. $formula = self::translateSeparator($fromSeparator, $toSeparator, $formula, $inBraces);
  2493. }
  2494. }
  2495. return $formula;
  2496. }
  2497. private static $functionReplaceFromExcel = null;
  2498. private static $functionReplaceToLocale = null;
  2499. public function _translateFormulaToLocale($formula)
  2500. {
  2501. if (self::$functionReplaceFromExcel === null) {
  2502. self::$functionReplaceFromExcel = [];
  2503. foreach (array_keys(self::$localeFunctions) as $excelFunctionName) {
  2504. self::$functionReplaceFromExcel[] = '/(@?[^\w\.])' . preg_quote($excelFunctionName, '/') . '([\s]*\()/Ui';
  2505. }
  2506. foreach (array_keys(self::$localeBoolean) as $excelBoolean) {
  2507. self::$functionReplaceFromExcel[] = '/(@?[^\w\.])' . preg_quote($excelBoolean, '/') . '([^\w\.])/Ui';
  2508. }
  2509. }
  2510. if (self::$functionReplaceToLocale === null) {
  2511. self::$functionReplaceToLocale = [];
  2512. foreach (self::$localeFunctions as $localeFunctionName) {
  2513. self::$functionReplaceToLocale[] = '$1' . trim($localeFunctionName) . '$2';
  2514. }
  2515. foreach (self::$localeBoolean as $localeBoolean) {
  2516. self::$functionReplaceToLocale[] = '$1' . trim($localeBoolean) . '$2';
  2517. }
  2518. }
  2519. return self::translateFormula(self::$functionReplaceFromExcel, self::$functionReplaceToLocale, $formula, ',', self::$localeArgumentSeparator);
  2520. }
  2521. private static $functionReplaceFromLocale = null;
  2522. private static $functionReplaceToExcel = null;
  2523. public function _translateFormulaToEnglish($formula)
  2524. {
  2525. if (self::$functionReplaceFromLocale === null) {
  2526. self::$functionReplaceFromLocale = [];
  2527. foreach (self::$localeFunctions as $localeFunctionName) {
  2528. self::$functionReplaceFromLocale[] = '/(@?[^\w\.])' . preg_quote($localeFunctionName, '/') . '([\s]*\()/Ui';
  2529. }
  2530. foreach (self::$localeBoolean as $excelBoolean) {
  2531. self::$functionReplaceFromLocale[] = '/(@?[^\w\.])' . preg_quote($excelBoolean, '/') . '([^\w\.])/Ui';
  2532. }
  2533. }
  2534. if (self::$functionReplaceToExcel === null) {
  2535. self::$functionReplaceToExcel = [];
  2536. foreach (array_keys(self::$localeFunctions) as $excelFunctionName) {
  2537. self::$functionReplaceToExcel[] = '$1' . trim($excelFunctionName) . '$2';
  2538. }
  2539. foreach (array_keys(self::$localeBoolean) as $excelBoolean) {
  2540. self::$functionReplaceToExcel[] = '$1' . trim($excelBoolean) . '$2';
  2541. }
  2542. }
  2543. return self::translateFormula(self::$functionReplaceFromLocale, self::$functionReplaceToExcel, $formula, self::$localeArgumentSeparator, ',');
  2544. }
  2545. public static function localeFunc($function)
  2546. {
  2547. if (self::$localeLanguage !== 'en_us') {
  2548. $functionName = trim($function, '(');
  2549. if (isset(self::$localeFunctions[$functionName])) {
  2550. $brace = ($functionName != $function);
  2551. $function = self::$localeFunctions[$functionName];
  2552. if ($brace) {
  2553. $function .= '(';
  2554. }
  2555. }
  2556. }
  2557. return $function;
  2558. }
  2559. /**
  2560. * Wrap string values in quotes.
  2561. *
  2562. * @param mixed $value
  2563. *
  2564. * @return mixed
  2565. */
  2566. public static function wrapResult($value)
  2567. {
  2568. if (is_string($value)) {
  2569. // Error values cannot be "wrapped"
  2570. if (preg_match('/^' . self::CALCULATION_REGEXP_ERROR . '$/i', $value, $match)) {
  2571. // Return Excel errors "as is"
  2572. return $value;
  2573. }
  2574. // Return strings wrapped in quotes
  2575. return '"' . $value . '"';
  2576. // Convert numeric errors to NaN error
  2577. } elseif ((is_float($value)) && ((is_nan($value)) || (is_infinite($value)))) {
  2578. return Functions::NAN();
  2579. }
  2580. return $value;
  2581. }
  2582. /**
  2583. * Remove quotes used as a wrapper to identify string values.
  2584. *
  2585. * @param mixed $value
  2586. *
  2587. * @return mixed
  2588. */
  2589. public static function unwrapResult($value)
  2590. {
  2591. if (is_string($value)) {
  2592. if ((isset($value[0])) && ($value[0] == '"') && (substr($value, -1) == '"')) {
  2593. return substr($value, 1, -1);
  2594. }
  2595. // Convert numeric errors to NAN error
  2596. } elseif ((is_float($value)) && ((is_nan($value)) || (is_infinite($value)))) {
  2597. return Functions::NAN();
  2598. }
  2599. return $value;
  2600. }
  2601. /**
  2602. * Calculate cell value (using formula from a cell ID)
  2603. * Retained for backward compatibility.
  2604. *
  2605. * @param Cell $pCell Cell to calculate
  2606. *
  2607. * @throws Exception
  2608. *
  2609. * @return mixed
  2610. */
  2611. public function calculate(Cell $pCell = null)
  2612. {
  2613. try {
  2614. return $this->calculateCellValue($pCell);
  2615. } catch (\Exception $e) {
  2616. throw new Exception($e->getMessage());
  2617. }
  2618. }
  2619. /**
  2620. * Calculate the value of a cell formula.
  2621. *
  2622. * @param Cell $pCell Cell to calculate
  2623. * @param bool $resetLog Flag indicating whether the debug log should be reset or not
  2624. *
  2625. * @throws \PhpOffice\PhpSpreadsheet\Exception
  2626. *
  2627. * @return mixed
  2628. */
  2629. public function calculateCellValue(Cell $pCell = null, $resetLog = true)
  2630. {
  2631. if ($pCell === null) {
  2632. return null;
  2633. }
  2634. $returnArrayAsType = self::$returnArrayAsType;
  2635. if ($resetLog) {
  2636. // Initialise the logging settings if requested
  2637. $this->formulaError = null;
  2638. $this->debugLog->clearLog();
  2639. $this->cyclicReferenceStack->clear();
  2640. $this->cyclicFormulaCounter = 1;
  2641. self::$returnArrayAsType = self::RETURN_ARRAY_AS_ARRAY;
  2642. }
  2643. // Execute the calculation for the cell formula
  2644. $this->cellStack[] = [
  2645. 'sheet' => $pCell->getWorksheet()->getTitle(),
  2646. 'cell' => $pCell->getCoordinate(),
  2647. ];
  2648. try {
  2649. $result = self::unwrapResult($this->_calculateFormulaValue($pCell->getValue(), $pCell->getCoordinate(), $pCell));
  2650. $cellAddress = array_pop($this->cellStack);
  2651. $this->spreadsheet->getSheetByName($cellAddress['sheet'])->getCell($cellAddress['cell']);
  2652. } catch (\Exception $e) {
  2653. $cellAddress = array_pop($this->cellStack);
  2654. $this->spreadsheet->getSheetByName($cellAddress['sheet'])->getCell($cellAddress['cell']);
  2655. throw new Exception($e->getMessage());
  2656. }
  2657. if ((is_array($result)) && (self::$returnArrayAsType != self::RETURN_ARRAY_AS_ARRAY)) {
  2658. self::$returnArrayAsType = $returnArrayAsType;
  2659. $testResult = Functions::flattenArray($result);
  2660. if (self::$returnArrayAsType == self::RETURN_ARRAY_AS_ERROR) {
  2661. return Functions::VALUE();
  2662. }
  2663. // If there's only a single cell in the array, then we allow it
  2664. if (count($testResult) != 1) {
  2665. // If keys are numeric, then it's a matrix result rather than a cell range result, so we permit it
  2666. $r = array_keys($result);
  2667. $r = array_shift($r);
  2668. if (!is_numeric($r)) {
  2669. return Functions::VALUE();
  2670. }
  2671. if (is_array($result[$r])) {
  2672. $c = array_keys($result[$r]);
  2673. $c = array_shift($c);
  2674. if (!is_numeric($c)) {
  2675. return Functions::VALUE();
  2676. }
  2677. }
  2678. }
  2679. $result = array_shift($testResult);
  2680. }
  2681. self::$returnArrayAsType = $returnArrayAsType;
  2682. if ($result === null) {
  2683. return 0;
  2684. } elseif ((is_float($result)) && ((is_nan($result)) || (is_infinite($result)))) {
  2685. return Functions::NAN();
  2686. }
  2687. return $result;
  2688. }
  2689. /**
  2690. * Validate and parse a formula string.
  2691. *
  2692. * @param string $formula Formula to parse
  2693. *
  2694. * @return array|bool
  2695. */
  2696. public function parseFormula($formula)
  2697. {
  2698. // Basic validation that this is indeed a formula
  2699. // We return an empty array if not
  2700. $formula = trim($formula);
  2701. if ((!isset($formula[0])) || ($formula[0] != '=')) {
  2702. return [];
  2703. }
  2704. $formula = ltrim(substr($formula, 1));
  2705. if (!isset($formula[0])) {
  2706. return [];
  2707. }
  2708. // Parse the formula and return the token stack
  2709. return $this->_parseFormula($formula);
  2710. }
  2711. /**
  2712. * Calculate the value of a formula.
  2713. *
  2714. * @param string $formula Formula to parse
  2715. * @param string $cellID Address of the cell to calculate
  2716. * @param Cell $pCell Cell to calculate
  2717. *
  2718. * @throws \PhpOffice\PhpSpreadsheet\Exception
  2719. *
  2720. * @return mixed
  2721. */
  2722. public function calculateFormula($formula, $cellID = null, Cell $pCell = null)
  2723. {
  2724. // Initialise the logging settings
  2725. $this->formulaError = null;
  2726. $this->debugLog->clearLog();
  2727. $this->cyclicReferenceStack->clear();
  2728. if ($this->spreadsheet !== null && $cellID === null && $pCell === null) {
  2729. $cellID = 'A1';
  2730. $pCell = $this->spreadsheet->getActiveSheet()->getCell($cellID);
  2731. } else {
  2732. // Disable calculation cacheing because it only applies to cell calculations, not straight formulae
  2733. // But don't actually flush any cache
  2734. $resetCache = $this->getCalculationCacheEnabled();
  2735. $this->calculationCacheEnabled = false;
  2736. }
  2737. // Execute the calculation
  2738. try {
  2739. $result = self::unwrapResult($this->_calculateFormulaValue($formula, $cellID, $pCell));
  2740. } catch (\Exception $e) {
  2741. throw new Exception($e->getMessage());
  2742. }
  2743. if ($this->spreadsheet === null) {
  2744. // Reset calculation cacheing to its previous state
  2745. $this->calculationCacheEnabled = $resetCache;
  2746. }
  2747. return $result;
  2748. }
  2749. /**
  2750. * @param string $cellReference
  2751. * @param mixed $cellValue
  2752. *
  2753. * @return bool
  2754. */
  2755. public function getValueFromCache($cellReference, &$cellValue)
  2756. {
  2757. // Is calculation cacheing enabled?
  2758. // Is the value present in calculation cache?
  2759. $this->debugLog->writeDebugLog('Testing cache value for cell ', $cellReference);
  2760. if (($this->calculationCacheEnabled) && (isset($this->calculationCache[$cellReference]))) {
  2761. $this->debugLog->writeDebugLog('Retrieving value for cell ', $cellReference, ' from cache');
  2762. // Return the cached result
  2763. $cellValue = $this->calculationCache[$cellReference];
  2764. return true;
  2765. }
  2766. return false;
  2767. }
  2768. /**
  2769. * @param string $cellReference
  2770. * @param mixed $cellValue
  2771. */
  2772. public function saveValueToCache($cellReference, $cellValue)
  2773. {
  2774. if ($this->calculationCacheEnabled) {
  2775. $this->calculationCache[$cellReference] = $cellValue;
  2776. }
  2777. }
  2778. /**
  2779. * Parse a cell formula and calculate its value.
  2780. *
  2781. * @param string $formula The formula to parse and calculate
  2782. * @param string $cellID The ID (e.g. A3) of the cell that we are calculating
  2783. * @param Cell $pCell Cell to calculate
  2784. *
  2785. * @throws Exception
  2786. *
  2787. * @return mixed
  2788. */
  2789. public function _calculateFormulaValue($formula, $cellID = null, Cell $pCell = null)
  2790. {
  2791. $cellValue = null;
  2792. // Quote-Prefixed cell values cannot be formulae, but are treated as strings
  2793. if ($pCell !== null && $pCell->getStyle()->getQuotePrefix() === true) {
  2794. return self::wrapResult((string) $formula);
  2795. }
  2796. if (preg_match('/^=\s*cmd\s*\|/miu', $formula) !== 0) {
  2797. return self::wrapResult($formula);
  2798. }
  2799. // Basic validation that this is indeed a formula
  2800. // We simply return the cell value if not
  2801. $formula = trim($formula);
  2802. if ($formula[0] != '=') {
  2803. return self::wrapResult($formula);
  2804. }
  2805. $formula = ltrim(substr($formula, 1));
  2806. if (!isset($formula[0])) {
  2807. return self::wrapResult($formula);
  2808. }
  2809. $pCellParent = ($pCell !== null) ? $pCell->getWorksheet() : null;
  2810. $wsTitle = ($pCellParent !== null) ? $pCellParent->getTitle() : "\x00Wrk";
  2811. $wsCellReference = $wsTitle . '!' . $cellID;
  2812. if (($cellID !== null) && ($this->getValueFromCache($wsCellReference, $cellValue))) {
  2813. return $cellValue;
  2814. }
  2815. if (($wsTitle[0] !== "\x00") && ($this->cyclicReferenceStack->onStack($wsCellReference))) {
  2816. if ($this->cyclicFormulaCount <= 0) {
  2817. $this->cyclicFormulaCell = '';
  2818. return $this->raiseFormulaError('Cyclic Reference in Formula');
  2819. } elseif ($this->cyclicFormulaCell === $wsCellReference) {
  2820. ++$this->cyclicFormulaCounter;
  2821. if ($this->cyclicFormulaCounter >= $this->cyclicFormulaCount) {
  2822. $this->cyclicFormulaCell = '';
  2823. return $cellValue;
  2824. }
  2825. } elseif ($this->cyclicFormulaCell == '') {
  2826. if ($this->cyclicFormulaCounter >= $this->cyclicFormulaCount) {
  2827. return $cellValue;
  2828. }
  2829. $this->cyclicFormulaCell = $wsCellReference;
  2830. }
  2831. }
  2832. // Parse the formula onto the token stack and calculate the value
  2833. $this->cyclicReferenceStack->push($wsCellReference);
  2834. $cellValue = $this->processTokenStack($this->_parseFormula($formula, $pCell), $cellID, $pCell);
  2835. $this->cyclicReferenceStack->pop();
  2836. // Save to calculation cache
  2837. if ($cellID !== null) {
  2838. $this->saveValueToCache($wsCellReference, $cellValue);
  2839. }
  2840. // Return the calculated value
  2841. return $cellValue;
  2842. }
  2843. /**
  2844. * Ensure that paired matrix operands are both matrices and of the same size.
  2845. *
  2846. * @param mixed &$operand1 First matrix operand
  2847. * @param mixed &$operand2 Second matrix operand
  2848. * @param int $resize Flag indicating whether the matrices should be resized to match
  2849. * and (if so), whether the smaller dimension should grow or the
  2850. * larger should shrink.
  2851. * 0 = no resize
  2852. * 1 = shrink to fit
  2853. * 2 = extend to fit
  2854. *
  2855. * @return array
  2856. */
  2857. private static function checkMatrixOperands(&$operand1, &$operand2, $resize = 1)
  2858. {
  2859. // Examine each of the two operands, and turn them into an array if they aren't one already
  2860. // Note that this function should only be called if one or both of the operand is already an array
  2861. if (!is_array($operand1)) {
  2862. list($matrixRows, $matrixColumns) = self::getMatrixDimensions($operand2);
  2863. $operand1 = array_fill(0, $matrixRows, array_fill(0, $matrixColumns, $operand1));
  2864. $resize = 0;
  2865. } elseif (!is_array($operand2)) {
  2866. list($matrixRows, $matrixColumns) = self::getMatrixDimensions($operand1);
  2867. $operand2 = array_fill(0, $matrixRows, array_fill(0, $matrixColumns, $operand2));
  2868. $resize = 0;
  2869. }
  2870. list($matrix1Rows, $matrix1Columns) = self::getMatrixDimensions($operand1);
  2871. list($matrix2Rows, $matrix2Columns) = self::getMatrixDimensions($operand2);
  2872. if (($matrix1Rows == $matrix2Columns) && ($matrix2Rows == $matrix1Columns)) {
  2873. $resize = 1;
  2874. }
  2875. if ($resize == 2) {
  2876. // Given two matrices of (potentially) unequal size, convert the smaller in each dimension to match the larger
  2877. self::resizeMatricesExtend($operand1, $operand2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns);
  2878. } elseif ($resize == 1) {
  2879. // Given two matrices of (potentially) unequal size, convert the larger in each dimension to match the smaller
  2880. self::resizeMatricesShrink($operand1, $operand2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns);
  2881. }
  2882. return [$matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns];
  2883. }
  2884. /**
  2885. * Read the dimensions of a matrix, and re-index it with straight numeric keys starting from row 0, column 0.
  2886. *
  2887. * @param array &$matrix matrix operand
  2888. *
  2889. * @return int[] An array comprising the number of rows, and number of columns
  2890. */
  2891. public static function getMatrixDimensions(array &$matrix)
  2892. {
  2893. $matrixRows = count($matrix);
  2894. $matrixColumns = 0;
  2895. foreach ($matrix as $rowKey => $rowValue) {
  2896. if (!is_array($rowValue)) {
  2897. $matrix[$rowKey] = [$rowValue];
  2898. $matrixColumns = max(1, $matrixColumns);
  2899. } else {
  2900. $matrix[$rowKey] = array_values($rowValue);
  2901. $matrixColumns = max(count($rowValue), $matrixColumns);
  2902. }
  2903. }
  2904. $matrix = array_values($matrix);
  2905. return [$matrixRows, $matrixColumns];
  2906. }
  2907. /**
  2908. * Ensure that paired matrix operands are both matrices of the same size.
  2909. *
  2910. * @param mixed &$matrix1 First matrix operand
  2911. * @param mixed &$matrix2 Second matrix operand
  2912. * @param int $matrix1Rows Row size of first matrix operand
  2913. * @param int $matrix1Columns Column size of first matrix operand
  2914. * @param int $matrix2Rows Row size of second matrix operand
  2915. * @param int $matrix2Columns Column size of second matrix operand
  2916. */
  2917. private static function resizeMatricesShrink(&$matrix1, &$matrix2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns)
  2918. {
  2919. if (($matrix2Columns < $matrix1Columns) || ($matrix2Rows < $matrix1Rows)) {
  2920. if ($matrix2Rows < $matrix1Rows) {
  2921. for ($i = $matrix2Rows; $i < $matrix1Rows; ++$i) {
  2922. unset($matrix1[$i]);
  2923. }
  2924. }
  2925. if ($matrix2Columns < $matrix1Columns) {
  2926. for ($i = 0; $i < $matrix1Rows; ++$i) {
  2927. for ($j = $matrix2Columns; $j < $matrix1Columns; ++$j) {
  2928. unset($matrix1[$i][$j]);
  2929. }
  2930. }
  2931. }
  2932. }
  2933. if (($matrix1Columns < $matrix2Columns) || ($matrix1Rows < $matrix2Rows)) {
  2934. if ($matrix1Rows < $matrix2Rows) {
  2935. for ($i = $matrix1Rows; $i < $matrix2Rows; ++$i) {
  2936. unset($matrix2[$i]);
  2937. }
  2938. }
  2939. if ($matrix1Columns < $matrix2Columns) {
  2940. for ($i = 0; $i < $matrix2Rows; ++$i) {
  2941. for ($j = $matrix1Columns; $j < $matrix2Columns; ++$j) {
  2942. unset($matrix2[$i][$j]);
  2943. }
  2944. }
  2945. }
  2946. }
  2947. }
  2948. /**
  2949. * Ensure that paired matrix operands are both matrices of the same size.
  2950. *
  2951. * @param mixed &$matrix1 First matrix operand
  2952. * @param mixed &$matrix2 Second matrix operand
  2953. * @param int $matrix1Rows Row size of first matrix operand
  2954. * @param int $matrix1Columns Column size of first matrix operand
  2955. * @param int $matrix2Rows Row size of second matrix operand
  2956. * @param int $matrix2Columns Column size of second matrix operand
  2957. */
  2958. private static function resizeMatricesExtend(&$matrix1, &$matrix2, $matrix1Rows, $matrix1Columns, $matrix2Rows, $matrix2Columns)
  2959. {
  2960. if (($matrix2Columns < $matrix1Columns) || ($matrix2Rows < $matrix1Rows)) {
  2961. if ($matrix2Columns < $matrix1Columns) {
  2962. for ($i = 0; $i < $matrix2Rows; ++$i) {
  2963. $x = $matrix2[$i][$matrix2Columns - 1];
  2964. for ($j = $matrix2Columns; $j < $matrix1Columns; ++$j) {
  2965. $matrix2[$i][$j] = $x;
  2966. }
  2967. }
  2968. }
  2969. if ($matrix2Rows < $matrix1Rows) {
  2970. $x = $matrix2[$matrix2Rows - 1];
  2971. for ($i = 0; $i < $matrix1Rows; ++$i) {
  2972. $matrix2[$i] = $x;
  2973. }
  2974. }
  2975. }
  2976. if (($matrix1Columns < $matrix2Columns) || ($matrix1Rows < $matrix2Rows)) {
  2977. if ($matrix1Columns < $matrix2Columns) {
  2978. for ($i = 0; $i < $matrix1Rows; ++$i) {
  2979. $x = $matrix1[$i][$matrix1Columns - 1];
  2980. for ($j = $matrix1Columns; $j < $matrix2Columns; ++$j) {
  2981. $matrix1[$i][$j] = $x;
  2982. }
  2983. }
  2984. }
  2985. if ($matrix1Rows < $matrix2Rows) {
  2986. $x = $matrix1[$matrix1Rows - 1];
  2987. for ($i = 0; $i < $matrix2Rows; ++$i) {
  2988. $matrix1[$i] = $x;
  2989. }
  2990. }
  2991. }
  2992. }
  2993. /**
  2994. * Format details of an operand for display in the log (based on operand type).
  2995. *
  2996. * @param mixed $value First matrix operand
  2997. *
  2998. * @return mixed
  2999. */
  3000. private function showValue($value)
  3001. {
  3002. if ($this->debugLog->getWriteDebugLog()) {
  3003. $testArray = Functions::flattenArray($value);
  3004. if (count($testArray) == 1) {
  3005. $value = array_pop($testArray);
  3006. }
  3007. if (is_array($value)) {
  3008. $returnMatrix = [];
  3009. $pad = $rpad = ', ';
  3010. foreach ($value as $row) {
  3011. if (is_array($row)) {
  3012. $returnMatrix[] = implode($pad, array_map([$this, 'showValue'], $row));
  3013. $rpad = '; ';
  3014. } else {
  3015. $returnMatrix[] = $this->showValue($row);
  3016. }
  3017. }
  3018. return '{ ' . implode($rpad, $returnMatrix) . ' }';
  3019. } elseif (is_string($value) && (trim($value, '"') == $value)) {
  3020. return '"' . $value . '"';
  3021. } elseif (is_bool($value)) {
  3022. return ($value) ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
  3023. }
  3024. }
  3025. return Functions::flattenSingleValue($value);
  3026. }
  3027. /**
  3028. * Format type and details of an operand for display in the log (based on operand type).
  3029. *
  3030. * @param mixed $value First matrix operand
  3031. *
  3032. * @return null|string
  3033. */
  3034. private function showTypeDetails($value)
  3035. {
  3036. if ($this->debugLog->getWriteDebugLog()) {
  3037. $testArray = Functions::flattenArray($value);
  3038. if (count($testArray) == 1) {
  3039. $value = array_pop($testArray);
  3040. }
  3041. if ($value === null) {
  3042. return 'a NULL value';
  3043. } elseif (is_float($value)) {
  3044. $typeString = 'a floating point number';
  3045. } elseif (is_int($value)) {
  3046. $typeString = 'an integer number';
  3047. } elseif (is_bool($value)) {
  3048. $typeString = 'a boolean';
  3049. } elseif (is_array($value)) {
  3050. $typeString = 'a matrix';
  3051. } else {
  3052. if ($value == '') {
  3053. return 'an empty string';
  3054. } elseif ($value[0] == '#') {
  3055. return 'a ' . $value . ' error';
  3056. }
  3057. $typeString = 'a string';
  3058. }
  3059. return $typeString . ' with a value of ' . $this->showValue($value);
  3060. }
  3061. }
  3062. /**
  3063. * @param string $formula
  3064. *
  3065. * @return string
  3066. */
  3067. private function convertMatrixReferences($formula)
  3068. {
  3069. static $matrixReplaceFrom = ['{', ';', '}'];
  3070. static $matrixReplaceTo = ['MKMATRIX(MKMATRIX(', '),MKMATRIX(', '))'];
  3071. // Convert any Excel matrix references to the MKMATRIX() function
  3072. if (strpos($formula, '{') !== false) {
  3073. // If there is the possibility of braces within a quoted string, then we don't treat those as matrix indicators
  3074. if (strpos($formula, '"') !== false) {
  3075. // So instead we skip replacing in any quoted strings by only replacing in every other array element after we've exploded
  3076. // the formula
  3077. $temp = explode('"', $formula);
  3078. // Open and Closed counts used for trapping mismatched braces in the formula
  3079. $openCount = $closeCount = 0;
  3080. $i = false;
  3081. foreach ($temp as &$value) {
  3082. // Only count/replace in alternating array entries
  3083. if ($i = !$i) {
  3084. $openCount += substr_count($value, '{');
  3085. $closeCount += substr_count($value, '}');
  3086. $value = str_replace($matrixReplaceFrom, $matrixReplaceTo, $value);
  3087. }
  3088. }
  3089. unset($value);
  3090. // Then rebuild the formula string
  3091. $formula = implode('"', $temp);
  3092. } else {
  3093. // If there's no quoted strings, then we do a simple count/replace
  3094. $openCount = substr_count($formula, '{');
  3095. $closeCount = substr_count($formula, '}');
  3096. $formula = str_replace($matrixReplaceFrom, $matrixReplaceTo, $formula);
  3097. }
  3098. // Trap for mismatched braces and trigger an appropriate error
  3099. if ($openCount < $closeCount) {
  3100. if ($openCount > 0) {
  3101. return $this->raiseFormulaError("Formula Error: Mismatched matrix braces '}'");
  3102. }
  3103. return $this->raiseFormulaError("Formula Error: Unexpected '}' encountered");
  3104. } elseif ($openCount > $closeCount) {
  3105. if ($closeCount > 0) {
  3106. return $this->raiseFormulaError("Formula Error: Mismatched matrix braces '{'");
  3107. }
  3108. return $this->raiseFormulaError("Formula Error: Unexpected '{' encountered");
  3109. }
  3110. }
  3111. return $formula;
  3112. }
  3113. private static function mkMatrix(...$args)
  3114. {
  3115. return $args;
  3116. }
  3117. // Binary Operators
  3118. // These operators always work on two values
  3119. // Array key is the operator, the value indicates whether this is a left or right associative operator
  3120. private static $operatorAssociativity = [
  3121. '^' => 0, // Exponentiation
  3122. '*' => 0, '/' => 0, // Multiplication and Division
  3123. '+' => 0, '-' => 0, // Addition and Subtraction
  3124. '&' => 0, // Concatenation
  3125. '|' => 0, ':' => 0, // Intersect and Range
  3126. '>' => 0, '<' => 0, '=' => 0, '>=' => 0, '<=' => 0, '<>' => 0, // Comparison
  3127. ];
  3128. // Comparison (Boolean) Operators
  3129. // These operators work on two values, but always return a boolean result
  3130. private static $comparisonOperators = ['>' => true, '<' => true, '=' => true, '>=' => true, '<=' => true, '<>' => true];
  3131. // Operator Precedence
  3132. // This list includes all valid operators, whether binary (including boolean) or unary (such as %)
  3133. // Array key is the operator, the value is its precedence
  3134. private static $operatorPrecedence = [
  3135. ':' => 8, // Range
  3136. '|' => 7, // Intersect
  3137. '~' => 6, // Negation
  3138. '%' => 5, // Percentage
  3139. '^' => 4, // Exponentiation
  3140. '*' => 3, '/' => 3, // Multiplication and Division
  3141. '+' => 2, '-' => 2, // Addition and Subtraction
  3142. '&' => 1, // Concatenation
  3143. '>' => 0, '<' => 0, '=' => 0, '>=' => 0, '<=' => 0, '<>' => 0, // Comparison
  3144. ];
  3145. // Convert infix to postfix notation
  3146. /**
  3147. * @param string $formula
  3148. * @param null|\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell
  3149. *
  3150. * @return bool
  3151. */
  3152. private function _parseFormula($formula, Cell $pCell = null)
  3153. {
  3154. if (($formula = $this->convertMatrixReferences(trim($formula))) === false) {
  3155. return false;
  3156. }
  3157. // If we're using cell caching, then $pCell may well be flushed back to the cache (which detaches the parent worksheet),
  3158. // so we store the parent worksheet so that we can re-attach it when necessary
  3159. $pCellParent = ($pCell !== null) ? $pCell->getWorksheet() : null;
  3160. $regexpMatchString = '/^(' . self::CALCULATION_REGEXP_FUNCTION .
  3161. '|' . self::CALCULATION_REGEXP_CELLREF .
  3162. '|' . self::CALCULATION_REGEXP_NUMBER .
  3163. '|' . self::CALCULATION_REGEXP_STRING .
  3164. '|' . self::CALCULATION_REGEXP_OPENBRACE .
  3165. '|' . self::CALCULATION_REGEXP_NAMEDRANGE .
  3166. '|' . self::CALCULATION_REGEXP_ERROR .
  3167. ')/si';
  3168. // Start with initialisation
  3169. $index = 0;
  3170. $stack = new Stack();
  3171. $output = [];
  3172. $expectingOperator = false; // We use this test in syntax-checking the expression to determine when a
  3173. // - is a negation or + is a positive operator rather than an operation
  3174. $expectingOperand = false; // We use this test in syntax-checking the expression to determine whether an operand
  3175. // should be null in a function call
  3176. // The guts of the lexical parser
  3177. // Loop through the formula extracting each operator and operand in turn
  3178. while (true) {
  3179. $opCharacter = $formula[$index]; // Get the first character of the value at the current index position
  3180. if ((isset(self::$comparisonOperators[$opCharacter])) && (strlen($formula) > $index) && (isset(self::$comparisonOperators[$formula[$index + 1]]))) {
  3181. $opCharacter .= $formula[++$index];
  3182. }
  3183. // Find out if we're currently at the beginning of a number, variable, cell reference, function, parenthesis or operand
  3184. $isOperandOrFunction = preg_match($regexpMatchString, substr($formula, $index), $match);
  3185. if ($opCharacter == '-' && !$expectingOperator) { // Is it a negation instead of a minus?
  3186. $stack->push('Unary Operator', '~'); // Put a negation on the stack
  3187. ++$index; // and drop the negation symbol
  3188. } elseif ($opCharacter == '%' && $expectingOperator) {
  3189. $stack->push('Unary Operator', '%'); // Put a percentage on the stack
  3190. ++$index;
  3191. } elseif ($opCharacter == '+' && !$expectingOperator) { // Positive (unary plus rather than binary operator plus) can be discarded?
  3192. ++$index; // Drop the redundant plus symbol
  3193. } elseif ((($opCharacter == '~') || ($opCharacter == '|')) && (!$isOperandOrFunction)) { // We have to explicitly deny a tilde or pipe, because they are legal
  3194. return $this->raiseFormulaError("Formula Error: Illegal character '~'"); // on the stack but not in the input expression
  3195. } elseif ((isset(self::$operators[$opCharacter]) or $isOperandOrFunction) && $expectingOperator) { // Are we putting an operator on the stack?
  3196. while ($stack->count() > 0 &&
  3197. ($o2 = $stack->last()) &&
  3198. isset(self::$operators[$o2['value']]) &&
  3199. @(self::$operatorAssociativity[$opCharacter] ? self::$operatorPrecedence[$opCharacter] < self::$operatorPrecedence[$o2['value']] : self::$operatorPrecedence[$opCharacter] <= self::$operatorPrecedence[$o2['value']])) {
  3200. $output[] = $stack->pop(); // Swap operands and higher precedence operators from the stack to the output
  3201. }
  3202. $stack->push('Binary Operator', $opCharacter); // Finally put our current operator onto the stack
  3203. ++$index;
  3204. $expectingOperator = false;
  3205. } elseif ($opCharacter == ')' && $expectingOperator) { // Are we expecting to close a parenthesis?
  3206. $expectingOperand = false;
  3207. while (($o2 = $stack->pop()) && $o2['value'] != '(') { // Pop off the stack back to the last (
  3208. if ($o2 === null) {
  3209. return $this->raiseFormulaError('Formula Error: Unexpected closing brace ")"');
  3210. }
  3211. $output[] = $o2;
  3212. }
  3213. $d = $stack->last(2);
  3214. if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $d['value'], $matches)) { // Did this parenthesis just close a function?
  3215. $functionName = $matches[1]; // Get the function name
  3216. $d = $stack->pop();
  3217. $argumentCount = $d['value']; // See how many arguments there were (argument count is the next value stored on the stack)
  3218. $output[] = $d; // Dump the argument count on the output
  3219. $output[] = $stack->pop(); // Pop the function and push onto the output
  3220. if (isset(self::$controlFunctions[$functionName])) {
  3221. $expectedArgumentCount = self::$controlFunctions[$functionName]['argumentCount'];
  3222. $functionCall = self::$controlFunctions[$functionName]['functionCall'];
  3223. } elseif (isset(self::$phpSpreadsheetFunctions[$functionName])) {
  3224. $expectedArgumentCount = self::$phpSpreadsheetFunctions[$functionName]['argumentCount'];
  3225. $functionCall = self::$phpSpreadsheetFunctions[$functionName]['functionCall'];
  3226. } else { // did we somehow push a non-function on the stack? this should never happen
  3227. return $this->raiseFormulaError('Formula Error: Internal error, non-function on stack');
  3228. }
  3229. // Check the argument count
  3230. $argumentCountError = false;
  3231. if (is_numeric($expectedArgumentCount)) {
  3232. if ($expectedArgumentCount < 0) {
  3233. if ($argumentCount > abs($expectedArgumentCount)) {
  3234. $argumentCountError = true;
  3235. $expectedArgumentCountString = 'no more than ' . abs($expectedArgumentCount);
  3236. }
  3237. } else {
  3238. if ($argumentCount != $expectedArgumentCount) {
  3239. $argumentCountError = true;
  3240. $expectedArgumentCountString = $expectedArgumentCount;
  3241. }
  3242. }
  3243. } elseif ($expectedArgumentCount != '*') {
  3244. $isOperandOrFunction = preg_match('/(\d*)([-+,])(\d*)/', $expectedArgumentCount, $argMatch);
  3245. switch ($argMatch[2]) {
  3246. case '+':
  3247. if ($argumentCount < $argMatch[1]) {
  3248. $argumentCountError = true;
  3249. $expectedArgumentCountString = $argMatch[1] . ' or more ';
  3250. }
  3251. break;
  3252. case '-':
  3253. if (($argumentCount < $argMatch[1]) || ($argumentCount > $argMatch[3])) {
  3254. $argumentCountError = true;
  3255. $expectedArgumentCountString = 'between ' . $argMatch[1] . ' and ' . $argMatch[3];
  3256. }
  3257. break;
  3258. case ',':
  3259. if (($argumentCount != $argMatch[1]) && ($argumentCount != $argMatch[3])) {
  3260. $argumentCountError = true;
  3261. $expectedArgumentCountString = 'either ' . $argMatch[1] . ' or ' . $argMatch[3];
  3262. }
  3263. break;
  3264. }
  3265. }
  3266. if ($argumentCountError) {
  3267. return $this->raiseFormulaError("Formula Error: Wrong number of arguments for $functionName() function: $argumentCount given, " . $expectedArgumentCountString . ' expected');
  3268. }
  3269. }
  3270. ++$index;
  3271. } elseif ($opCharacter == ',') { // Is this the separator for function arguments?
  3272. while (($o2 = $stack->pop()) && $o2['value'] != '(') { // Pop off the stack back to the last (
  3273. if ($o2 === null) {
  3274. return $this->raiseFormulaError('Formula Error: Unexpected ,');
  3275. }
  3276. $output[] = $o2; // pop the argument expression stuff and push onto the output
  3277. }
  3278. // If we've a comma when we're expecting an operand, then what we actually have is a null operand;
  3279. // so push a null onto the stack
  3280. if (($expectingOperand) || (!$expectingOperator)) {
  3281. $output[] = ['type' => 'NULL Value', 'value' => self::$excelConstants['NULL'], 'reference' => null];
  3282. }
  3283. // make sure there was a function
  3284. $d = $stack->last(2);
  3285. if (!preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $d['value'], $matches)) {
  3286. return $this->raiseFormulaError('Formula Error: Unexpected ,');
  3287. }
  3288. $d = $stack->pop();
  3289. $stack->push($d['type'], ++$d['value'], $d['reference']); // increment the argument count
  3290. $stack->push('Brace', '('); // put the ( back on, we'll need to pop back to it again
  3291. $expectingOperator = false;
  3292. $expectingOperand = true;
  3293. ++$index;
  3294. } elseif ($opCharacter == '(' && !$expectingOperator) {
  3295. $stack->push('Brace', '(');
  3296. ++$index;
  3297. } elseif ($isOperandOrFunction && !$expectingOperator) { // do we now have a function/variable/number?
  3298. $expectingOperator = true;
  3299. $expectingOperand = false;
  3300. $val = $match[1];
  3301. $length = strlen($val);
  3302. if (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $val, $matches)) {
  3303. $val = preg_replace('/\s/u', '', $val);
  3304. if (isset(self::$phpSpreadsheetFunctions[strtoupper($matches[1])]) || isset(self::$controlFunctions[strtoupper($matches[1])])) { // it's a function
  3305. $stack->push('Function', strtoupper($val));
  3306. $ax = preg_match('/^\s*(\s*\))/ui', substr($formula, $index + $length), $amatch);
  3307. if ($ax) {
  3308. $stack->push('Operand Count for Function ' . strtoupper($val) . ')', 0);
  3309. $expectingOperator = true;
  3310. } else {
  3311. $stack->push('Operand Count for Function ' . strtoupper($val) . ')', 1);
  3312. $expectingOperator = false;
  3313. }
  3314. $stack->push('Brace', '(');
  3315. } else { // it's a var w/ implicit multiplication
  3316. $output[] = ['type' => 'Value', 'value' => $matches[1], 'reference' => null];
  3317. }
  3318. } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $val, $matches)) {
  3319. // Watch for this case-change when modifying to allow cell references in different worksheets...
  3320. // Should only be applied to the actual cell column, not the worksheet name
  3321. // If the last entry on the stack was a : operator, then we have a cell range reference
  3322. $testPrevOp = $stack->last(1);
  3323. if ($testPrevOp['value'] == ':') {
  3324. // If we have a worksheet reference, then we're playing with a 3D reference
  3325. if ($matches[2] == '') {
  3326. // Otherwise, we 'inherit' the worksheet reference from the start cell reference
  3327. // The start of the cell range reference should be the last entry in $output
  3328. $startCellRef = $output[count($output) - 1]['value'];
  3329. preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $startCellRef, $startMatches);
  3330. if ($startMatches[2] > '') {
  3331. $val = $startMatches[2] . '!' . $val;
  3332. }
  3333. } else {
  3334. return $this->raiseFormulaError('3D Range references are not yet supported');
  3335. }
  3336. }
  3337. $output[] = ['type' => 'Cell Reference', 'value' => $val, 'reference' => $val];
  3338. } else { // it's a variable, constant, string, number or boolean
  3339. // If the last entry on the stack was a : operator, then we may have a row or column range reference
  3340. $testPrevOp = $stack->last(1);
  3341. if ($testPrevOp['value'] == ':') {
  3342. $startRowColRef = $output[count($output) - 1]['value'];
  3343. list($rangeWS1, $startRowColRef) = Worksheet::extractSheetTitle($startRowColRef, true);
  3344. if ($rangeWS1 != '') {
  3345. $rangeWS1 .= '!';
  3346. }
  3347. list($rangeWS2, $val) = Worksheet::extractSheetTitle($val, true);
  3348. if ($rangeWS2 != '') {
  3349. $rangeWS2 .= '!';
  3350. } else {
  3351. $rangeWS2 = $rangeWS1;
  3352. }
  3353. if ((is_int($startRowColRef)) && (ctype_digit($val)) &&
  3354. ($startRowColRef <= 1048576) && ($val <= 1048576)) {
  3355. // Row range
  3356. $endRowColRef = ($pCellParent !== null) ? $pCellParent->getHighestColumn() : 'XFD'; // Max 16,384 columns for Excel2007
  3357. $output[count($output) - 1]['value'] = $rangeWS1 . 'A' . $startRowColRef;
  3358. $val = $rangeWS2 . $endRowColRef . $val;
  3359. } elseif ((ctype_alpha($startRowColRef)) && (ctype_alpha($val)) &&
  3360. (strlen($startRowColRef) <= 3) && (strlen($val) <= 3)) {
  3361. // Column range
  3362. $endRowColRef = ($pCellParent !== null) ? $pCellParent->getHighestRow() : 1048576; // Max 1,048,576 rows for Excel2007
  3363. $output[count($output) - 1]['value'] = $rangeWS1 . strtoupper($startRowColRef) . '1';
  3364. $val = $rangeWS2 . $val . $endRowColRef;
  3365. }
  3366. }
  3367. $localeConstant = false;
  3368. if ($opCharacter == '"') {
  3369. // UnEscape any quotes within the string
  3370. $val = self::wrapResult(str_replace('""', '"', self::unwrapResult($val)));
  3371. } elseif (is_numeric($val)) {
  3372. if ((strpos($val, '.') !== false) || (stripos($val, 'e') !== false) || ($val > PHP_INT_MAX) || ($val < -PHP_INT_MAX)) {
  3373. $val = (float) $val;
  3374. } else {
  3375. $val = (int) $val;
  3376. }
  3377. } elseif (isset(self::$excelConstants[trim(strtoupper($val))])) {
  3378. $excelConstant = trim(strtoupper($val));
  3379. $val = self::$excelConstants[$excelConstant];
  3380. } elseif (($localeConstant = array_search(trim(strtoupper($val)), self::$localeBoolean)) !== false) {
  3381. $val = self::$excelConstants[$localeConstant];
  3382. }
  3383. $details = ['type' => 'Value', 'value' => $val, 'reference' => null];
  3384. if ($localeConstant) {
  3385. $details['localeValue'] = $localeConstant;
  3386. }
  3387. $output[] = $details;
  3388. }
  3389. $index += $length;
  3390. } elseif ($opCharacter == '$') { // absolute row or column range
  3391. ++$index;
  3392. } elseif ($opCharacter == ')') { // miscellaneous error checking
  3393. if ($expectingOperand) {
  3394. $output[] = ['type' => 'NULL Value', 'value' => self::$excelConstants['NULL'], 'reference' => null];
  3395. $expectingOperand = false;
  3396. $expectingOperator = true;
  3397. } else {
  3398. return $this->raiseFormulaError("Formula Error: Unexpected ')'");
  3399. }
  3400. } elseif (isset(self::$operators[$opCharacter]) && !$expectingOperator) {
  3401. return $this->raiseFormulaError("Formula Error: Unexpected operator '$opCharacter'");
  3402. } else { // I don't even want to know what you did to get here
  3403. return $this->raiseFormulaError('Formula Error: An unexpected error occured');
  3404. }
  3405. // Test for end of formula string
  3406. if ($index == strlen($formula)) {
  3407. // Did we end with an operator?.
  3408. // Only valid for the % unary operator
  3409. if ((isset(self::$operators[$opCharacter])) && ($opCharacter != '%')) {
  3410. return $this->raiseFormulaError("Formula Error: Operator '$opCharacter' has no operands");
  3411. }
  3412. break;
  3413. }
  3414. // Ignore white space
  3415. while (($formula[$index] == "\n") || ($formula[$index] == "\r")) {
  3416. ++$index;
  3417. }
  3418. if ($formula[$index] == ' ') {
  3419. while ($formula[$index] == ' ') {
  3420. ++$index;
  3421. }
  3422. // If we're expecting an operator, but only have a space between the previous and next operands (and both are
  3423. // Cell References) then we have an INTERSECTION operator
  3424. if (($expectingOperator) && (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '.*/Ui', substr($formula, $index), $match)) &&
  3425. ($output[count($output) - 1]['type'] == 'Cell Reference')) {
  3426. while ($stack->count() > 0 &&
  3427. ($o2 = $stack->last()) &&
  3428. isset(self::$operators[$o2['value']]) &&
  3429. @(self::$operatorAssociativity[$opCharacter] ? self::$operatorPrecedence[$opCharacter] < self::$operatorPrecedence[$o2['value']] : self::$operatorPrecedence[$opCharacter] <= self::$operatorPrecedence[$o2['value']])) {
  3430. $output[] = $stack->pop(); // Swap operands and higher precedence operators from the stack to the output
  3431. }
  3432. $stack->push('Binary Operator', '|'); // Put an Intersect Operator on the stack
  3433. $expectingOperator = false;
  3434. }
  3435. }
  3436. }
  3437. while (($op = $stack->pop()) !== null) { // pop everything off the stack and push onto output
  3438. if ((is_array($op) && $op['value'] == '(') || ($op === '(')) {
  3439. return $this->raiseFormulaError("Formula Error: Expecting ')'"); // if there are any opening braces on the stack, then braces were unbalanced
  3440. }
  3441. $output[] = $op;
  3442. }
  3443. return $output;
  3444. }
  3445. private static function dataTestReference(&$operandData)
  3446. {
  3447. $operand = $operandData['value'];
  3448. if (($operandData['reference'] === null) && (is_array($operand))) {
  3449. $rKeys = array_keys($operand);
  3450. $rowKey = array_shift($rKeys);
  3451. $cKeys = array_keys(array_keys($operand[$rowKey]));
  3452. $colKey = array_shift($cKeys);
  3453. if (ctype_upper($colKey)) {
  3454. $operandData['reference'] = $colKey . $rowKey;
  3455. }
  3456. }
  3457. return $operand;
  3458. }
  3459. // evaluate postfix notation
  3460. /**
  3461. * @param mixed $tokens
  3462. * @param null|string $cellID
  3463. * @param null|Cell $pCell
  3464. *
  3465. * @return bool
  3466. */
  3467. private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
  3468. {
  3469. if ($tokens == false) {
  3470. return false;
  3471. }
  3472. // If we're using cell caching, then $pCell may well be flushed back to the cache (which detaches the parent cell collection),
  3473. // so we store the parent cell collection so that we can re-attach it when necessary
  3474. $pCellWorksheet = ($pCell !== null) ? $pCell->getWorksheet() : null;
  3475. $pCellParent = ($pCell !== null) ? $pCell->getParent() : null;
  3476. $stack = new Stack();
  3477. // Loop through each token in turn
  3478. foreach ($tokens as $tokenData) {
  3479. $token = $tokenData['value'];
  3480. // if the token is a binary operator, pop the top two values off the stack, do the operation, and push the result back on the stack
  3481. if (isset(self::$binaryOperators[$token])) {
  3482. // We must have two operands, error if we don't
  3483. if (($operand2Data = $stack->pop()) === null) {
  3484. return $this->raiseFormulaError('Internal error - Operand value missing from stack');
  3485. }
  3486. if (($operand1Data = $stack->pop()) === null) {
  3487. return $this->raiseFormulaError('Internal error - Operand value missing from stack');
  3488. }
  3489. $operand1 = self::dataTestReference($operand1Data);
  3490. $operand2 = self::dataTestReference($operand2Data);
  3491. // Log what we're doing
  3492. if ($token == ':') {
  3493. $this->debugLog->writeDebugLog('Evaluating Range ', $this->showValue($operand1Data['reference']), ' ', $token, ' ', $this->showValue($operand2Data['reference']));
  3494. } else {
  3495. $this->debugLog->writeDebugLog('Evaluating ', $this->showValue($operand1), ' ', $token, ' ', $this->showValue($operand2));
  3496. }
  3497. // Process the operation in the appropriate manner
  3498. switch ($token) {
  3499. // Comparison (Boolean) Operators
  3500. case '>': // Greater than
  3501. case '<': // Less than
  3502. case '>=': // Greater than or Equal to
  3503. case '<=': // Less than or Equal to
  3504. case '=': // Equality
  3505. case '<>': // Inequality
  3506. $this->executeBinaryComparisonOperation($cellID, $operand1, $operand2, $token, $stack);
  3507. break;
  3508. // Binary Operators
  3509. case ':': // Range
  3510. if (strpos($operand1Data['reference'], '!') !== false) {
  3511. list($sheet1, $operand1Data['reference']) = Worksheet::extractSheetTitle($operand1Data['reference'], true);
  3512. } else {
  3513. $sheet1 = ($pCellParent !== null) ? $pCellWorksheet->getTitle() : '';
  3514. }
  3515. list($sheet2, $operand2Data['reference']) = Worksheet::extractSheetTitle($operand2Data['reference'], true);
  3516. if (empty($sheet2)) {
  3517. $sheet2 = $sheet1;
  3518. }
  3519. if ($sheet1 == $sheet2) {
  3520. if ($operand1Data['reference'] === null) {
  3521. if ((trim($operand1Data['value']) != '') && (is_numeric($operand1Data['value']))) {
  3522. $operand1Data['reference'] = $pCell->getColumn() . $operand1Data['value'];
  3523. } elseif (trim($operand1Data['reference']) == '') {
  3524. $operand1Data['reference'] = $pCell->getCoordinate();
  3525. } else {
  3526. $operand1Data['reference'] = $operand1Data['value'] . $pCell->getRow();
  3527. }
  3528. }
  3529. if ($operand2Data['reference'] === null) {
  3530. if ((trim($operand2Data['value']) != '') && (is_numeric($operand2Data['value']))) {
  3531. $operand2Data['reference'] = $pCell->getColumn() . $operand2Data['value'];
  3532. } elseif (trim($operand2Data['reference']) == '') {
  3533. $operand2Data['reference'] = $pCell->getCoordinate();
  3534. } else {
  3535. $operand2Data['reference'] = $operand2Data['value'] . $pCell->getRow();
  3536. }
  3537. }
  3538. $oData = array_merge(explode(':', $operand1Data['reference']), explode(':', $operand2Data['reference']));
  3539. $oCol = $oRow = [];
  3540. foreach ($oData as $oDatum) {
  3541. $oCR = Coordinate::coordinateFromString($oDatum);
  3542. $oCol[] = Coordinate::columnIndexFromString($oCR[0]) - 1;
  3543. $oRow[] = $oCR[1];
  3544. }
  3545. $cellRef = Coordinate::stringFromColumnIndex(min($oCol) + 1) . min($oRow) . ':' . Coordinate::stringFromColumnIndex(max($oCol) + 1) . max($oRow);
  3546. if ($pCellParent !== null) {
  3547. $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($sheet1), false);
  3548. } else {
  3549. return $this->raiseFormulaError('Unable to access Cell Reference');
  3550. }
  3551. $stack->push('Cell Reference', $cellValue, $cellRef);
  3552. } else {
  3553. $stack->push('Error', Functions::REF(), null);
  3554. }
  3555. break;
  3556. case '+': // Addition
  3557. $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'plusEquals', $stack);
  3558. break;
  3559. case '-': // Subtraction
  3560. $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'minusEquals', $stack);
  3561. break;
  3562. case '*': // Multiplication
  3563. $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'arrayTimesEquals', $stack);
  3564. break;
  3565. case '/': // Division
  3566. $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'arrayRightDivide', $stack);
  3567. break;
  3568. case '^': // Exponential
  3569. $this->executeNumericBinaryOperation($operand1, $operand2, $token, 'power', $stack);
  3570. break;
  3571. case '&': // Concatenation
  3572. // If either of the operands is a matrix, we need to treat them both as matrices
  3573. // (converting the other operand to a matrix if need be); then perform the required
  3574. // matrix operation
  3575. if (is_bool($operand1)) {
  3576. $operand1 = ($operand1) ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
  3577. }
  3578. if (is_bool($operand2)) {
  3579. $operand2 = ($operand2) ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
  3580. }
  3581. if ((is_array($operand1)) || (is_array($operand2))) {
  3582. // Ensure that both operands are arrays/matrices
  3583. self::checkMatrixOperands($operand1, $operand2, 2);
  3584. try {
  3585. // Convert operand 1 from a PHP array to a matrix
  3586. $matrix = new Shared\JAMA\Matrix($operand1);
  3587. // Perform the required operation against the operand 1 matrix, passing in operand 2
  3588. $matrixResult = $matrix->concat($operand2);
  3589. $result = $matrixResult->getArray();
  3590. } catch (\Exception $ex) {
  3591. $this->debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
  3592. $result = '#VALUE!';
  3593. }
  3594. } else {
  3595. $result = '"' . str_replace('""', '"', self::unwrapResult($operand1) . self::unwrapResult($operand2)) . '"';
  3596. }
  3597. $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
  3598. $stack->push('Value', $result);
  3599. break;
  3600. case '|': // Intersect
  3601. $rowIntersect = array_intersect_key($operand1, $operand2);
  3602. $cellIntersect = $oCol = $oRow = [];
  3603. foreach (array_keys($rowIntersect) as $row) {
  3604. $oRow[] = $row;
  3605. foreach ($rowIntersect[$row] as $col => $data) {
  3606. $oCol[] = Coordinate::columnIndexFromString($col) - 1;
  3607. $cellIntersect[$row] = array_intersect_key($operand1[$row], $operand2[$row]);
  3608. }
  3609. }
  3610. $cellRef = Coordinate::stringFromColumnIndex(min($oCol) + 1) . min($oRow) . ':' . Coordinate::stringFromColumnIndex(max($oCol) + 1) . max($oRow);
  3611. $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($cellIntersect));
  3612. $stack->push('Value', $cellIntersect, $cellRef);
  3613. break;
  3614. }
  3615. // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
  3616. } elseif (($token === '~') || ($token === '%')) {
  3617. if (($arg = $stack->pop()) === null) {
  3618. return $this->raiseFormulaError('Internal error - Operand value missing from stack');
  3619. }
  3620. $arg = $arg['value'];
  3621. if ($token === '~') {
  3622. $this->debugLog->writeDebugLog('Evaluating Negation of ', $this->showValue($arg));
  3623. $multiplier = -1;
  3624. } else {
  3625. $this->debugLog->writeDebugLog('Evaluating Percentile of ', $this->showValue($arg));
  3626. $multiplier = 0.01;
  3627. }
  3628. if (is_array($arg)) {
  3629. self::checkMatrixOperands($arg, $multiplier, 2);
  3630. try {
  3631. $matrix1 = new Shared\JAMA\Matrix($arg);
  3632. $matrixResult = $matrix1->arrayTimesEquals($multiplier);
  3633. $result = $matrixResult->getArray();
  3634. } catch (\Exception $ex) {
  3635. $this->debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
  3636. $result = '#VALUE!';
  3637. }
  3638. $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
  3639. $stack->push('Value', $result);
  3640. } else {
  3641. $this->executeNumericBinaryOperation($multiplier, $arg, '*', 'arrayTimesEquals', $stack);
  3642. }
  3643. } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token, $matches)) {
  3644. $cellRef = null;
  3645. if (isset($matches[8])) {
  3646. if ($pCell === null) {
  3647. // We can't access the range, so return a REF error
  3648. $cellValue = Functions::REF();
  3649. } else {
  3650. $cellRef = $matches[6] . $matches[7] . ':' . $matches[9] . $matches[10];
  3651. if ($matches[2] > '') {
  3652. $matches[2] = trim($matches[2], "\"'");
  3653. if ((strpos($matches[2], '[') !== false) || (strpos($matches[2], ']') !== false)) {
  3654. // It's a Reference to an external spreadsheet (not currently supported)
  3655. return $this->raiseFormulaError('Unable to access External Workbook');
  3656. }
  3657. $matches[2] = trim($matches[2], "\"'");
  3658. $this->debugLog->writeDebugLog('Evaluating Cell Range ', $cellRef, ' in worksheet ', $matches[2]);
  3659. if ($pCellParent !== null) {
  3660. $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($matches[2]), false);
  3661. } else {
  3662. return $this->raiseFormulaError('Unable to access Cell Reference');
  3663. }
  3664. $this->debugLog->writeDebugLog('Evaluation Result for cells ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->showTypeDetails($cellValue));
  3665. } else {
  3666. $this->debugLog->writeDebugLog('Evaluating Cell Range ', $cellRef, ' in current worksheet');
  3667. if ($pCellParent !== null) {
  3668. $cellValue = $this->extractCellRange($cellRef, $pCellWorksheet, false);
  3669. } else {
  3670. return $this->raiseFormulaError('Unable to access Cell Reference');
  3671. }
  3672. $this->debugLog->writeDebugLog('Evaluation Result for cells ', $cellRef, ' is ', $this->showTypeDetails($cellValue));
  3673. }
  3674. }
  3675. } else {
  3676. if ($pCell === null) {
  3677. // We can't access the cell, so return a REF error
  3678. $cellValue = Functions::REF();
  3679. } else {
  3680. $cellRef = $matches[6] . $matches[7];
  3681. if ($matches[2] > '') {
  3682. $matches[2] = trim($matches[2], "\"'");
  3683. if ((strpos($matches[2], '[') !== false) || (strpos($matches[2], ']') !== false)) {
  3684. // It's a Reference to an external spreadsheet (not currently supported)
  3685. return $this->raiseFormulaError('Unable to access External Workbook');
  3686. }
  3687. $this->debugLog->writeDebugLog('Evaluating Cell ', $cellRef, ' in worksheet ', $matches[2]);
  3688. if ($pCellParent !== null) {
  3689. $cellSheet = $this->spreadsheet->getSheetByName($matches[2]);
  3690. if ($cellSheet && $cellSheet->cellExists($cellRef)) {
  3691. $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($matches[2]), false);
  3692. $pCell->attach($pCellParent);
  3693. } else {
  3694. $cellValue = null;
  3695. }
  3696. } else {
  3697. return $this->raiseFormulaError('Unable to access Cell Reference');
  3698. }
  3699. $this->debugLog->writeDebugLog('Evaluation Result for cell ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->showTypeDetails($cellValue));
  3700. } else {
  3701. $this->debugLog->writeDebugLog('Evaluating Cell ', $cellRef, ' in current worksheet');
  3702. if ($pCellParent->has($cellRef)) {
  3703. $cellValue = $this->extractCellRange($cellRef, $pCellWorksheet, false);
  3704. $pCell->attach($pCellParent);
  3705. } else {
  3706. $cellValue = null;
  3707. }
  3708. $this->debugLog->writeDebugLog('Evaluation Result for cell ', $cellRef, ' is ', $this->showTypeDetails($cellValue));
  3709. }
  3710. }
  3711. }
  3712. $stack->push('Value', $cellValue, $cellRef);
  3713. // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
  3714. } elseif (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $token, $matches)) {
  3715. $functionName = $matches[1];
  3716. $argCount = $stack->pop();
  3717. $argCount = $argCount['value'];
  3718. if ($functionName != 'MKMATRIX') {
  3719. $this->debugLog->writeDebugLog('Evaluating Function ', self::localeFunc($functionName), '() with ', (($argCount == 0) ? 'no' : $argCount), ' argument', (($argCount == 1) ? '' : 's'));
  3720. }
  3721. if ((isset(self::$phpSpreadsheetFunctions[$functionName])) || (isset(self::$controlFunctions[$functionName]))) { // function
  3722. if (isset(self::$phpSpreadsheetFunctions[$functionName])) {
  3723. $functionCall = self::$phpSpreadsheetFunctions[$functionName]['functionCall'];
  3724. $passByReference = isset(self::$phpSpreadsheetFunctions[$functionName]['passByReference']);
  3725. $passCellReference = isset(self::$phpSpreadsheetFunctions[$functionName]['passCellReference']);
  3726. } elseif (isset(self::$controlFunctions[$functionName])) {
  3727. $functionCall = self::$controlFunctions[$functionName]['functionCall'];
  3728. $passByReference = isset(self::$controlFunctions[$functionName]['passByReference']);
  3729. $passCellReference = isset(self::$controlFunctions[$functionName]['passCellReference']);
  3730. }
  3731. // get the arguments for this function
  3732. $args = $argArrayVals = [];
  3733. for ($i = 0; $i < $argCount; ++$i) {
  3734. $arg = $stack->pop();
  3735. $a = $argCount - $i - 1;
  3736. if (($passByReference) &&
  3737. (isset(self::$phpSpreadsheetFunctions[$functionName]['passByReference'][$a])) &&
  3738. (self::$phpSpreadsheetFunctions[$functionName]['passByReference'][$a])) {
  3739. if ($arg['reference'] === null) {
  3740. $args[] = $cellID;
  3741. if ($functionName != 'MKMATRIX') {
  3742. $argArrayVals[] = $this->showValue($cellID);
  3743. }
  3744. } else {
  3745. $args[] = $arg['reference'];
  3746. if ($functionName != 'MKMATRIX') {
  3747. $argArrayVals[] = $this->showValue($arg['reference']);
  3748. }
  3749. }
  3750. } else {
  3751. $args[] = self::unwrapResult($arg['value']);
  3752. if ($functionName != 'MKMATRIX') {
  3753. $argArrayVals[] = $this->showValue($arg['value']);
  3754. }
  3755. }
  3756. }
  3757. // Reverse the order of the arguments
  3758. krsort($args);
  3759. if (($passByReference) && ($argCount == 0)) {
  3760. $args[] = $cellID;
  3761. $argArrayVals[] = $this->showValue($cellID);
  3762. }
  3763. if ($functionName != 'MKMATRIX') {
  3764. if ($this->debugLog->getWriteDebugLog()) {
  3765. krsort($argArrayVals);
  3766. $this->debugLog->writeDebugLog('Evaluating ', self::localeFunc($functionName), '( ', implode(self::$localeArgumentSeparator . ' ', Functions::flattenArray($argArrayVals)), ' )');
  3767. }
  3768. }
  3769. // Process the argument with the appropriate function call
  3770. $args = $this->addCellReference($args, $passCellReference, $functionCall, $pCell);
  3771. if (!is_array($functionCall)) {
  3772. foreach ($args as &$arg) {
  3773. $arg = Functions::flattenSingleValue($arg);
  3774. }
  3775. unset($arg);
  3776. }
  3777. $result = call_user_func_array($functionCall, $args);
  3778. if ($functionName != 'MKMATRIX') {
  3779. $this->debugLog->writeDebugLog('Evaluation Result for ', self::localeFunc($functionName), '() function call is ', $this->showTypeDetails($result));
  3780. }
  3781. $stack->push('Value', self::wrapResult($result));
  3782. }
  3783. } else {
  3784. // if the token is a number, boolean, string or an Excel error, push it onto the stack
  3785. if (isset(self::$excelConstants[strtoupper($token)])) {
  3786. $excelConstant = strtoupper($token);
  3787. $stack->push('Constant Value', self::$excelConstants[$excelConstant]);
  3788. $this->debugLog->writeDebugLog('Evaluating Constant ', $excelConstant, ' as ', $this->showTypeDetails(self::$excelConstants[$excelConstant]));
  3789. } elseif ((is_numeric($token)) || ($token === null) || (is_bool($token)) || ($token == '') || ($token[0] == '"') || ($token[0] == '#')) {
  3790. $stack->push('Value', $token);
  3791. // if the token is a named range, push the named range name onto the stack
  3792. } elseif (preg_match('/^' . self::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $token, $matches)) {
  3793. $namedRange = $matches[6];
  3794. $this->debugLog->writeDebugLog('Evaluating Named Range ', $namedRange);
  3795. $cellValue = $this->extractNamedRange($namedRange, ((null !== $pCell) ? $pCellWorksheet : null), false);
  3796. $pCell->attach($pCellParent);
  3797. $this->debugLog->writeDebugLog('Evaluation Result for named range ', $namedRange, ' is ', $this->showTypeDetails($cellValue));
  3798. $stack->push('Named Range', $cellValue, $namedRange);
  3799. } else {
  3800. return $this->raiseFormulaError("undefined variable '$token'");
  3801. }
  3802. }
  3803. }
  3804. // when we're out of tokens, the stack should have a single element, the final result
  3805. if ($stack->count() != 1) {
  3806. return $this->raiseFormulaError('internal error');
  3807. }
  3808. $output = $stack->pop();
  3809. $output = $output['value'];
  3810. return $output;
  3811. }
  3812. private function validateBinaryOperand(&$operand, &$stack)
  3813. {
  3814. if (is_array($operand)) {
  3815. if ((count($operand, COUNT_RECURSIVE) - count($operand)) == 1) {
  3816. do {
  3817. $operand = array_pop($operand);
  3818. } while (is_array($operand));
  3819. }
  3820. }
  3821. // Numbers, matrices and booleans can pass straight through, as they're already valid
  3822. if (is_string($operand)) {
  3823. // We only need special validations for the operand if it is a string
  3824. // Start by stripping off the quotation marks we use to identify true excel string values internally
  3825. if ($operand > '' && $operand[0] == '"') {
  3826. $operand = self::unwrapResult($operand);
  3827. }
  3828. // If the string is a numeric value, we treat it as a numeric, so no further testing
  3829. if (!is_numeric($operand)) {
  3830. // If not a numeric, test to see if the value is an Excel error, and so can't be used in normal binary operations
  3831. if ($operand > '' && $operand[0] == '#') {
  3832. $stack->push('Value', $operand);
  3833. $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($operand));
  3834. return false;
  3835. } elseif (!Shared\StringHelper::convertToNumberIfFraction($operand)) {
  3836. // If not a numeric or a fraction, then it's a text string, and so can't be used in mathematical binary operations
  3837. $stack->push('Value', '#VALUE!');
  3838. $this->debugLog->writeDebugLog('Evaluation Result is a ', $this->showTypeDetails('#VALUE!'));
  3839. return false;
  3840. }
  3841. }
  3842. }
  3843. // return a true if the value of the operand is one that we can use in normal binary operations
  3844. return true;
  3845. }
  3846. /**
  3847. * @param null|string $cellID
  3848. * @param mixed $operand1
  3849. * @param mixed $operand2
  3850. * @param string $operation
  3851. * @param Stack $stack
  3852. * @param bool $recursingArrays
  3853. *
  3854. * @return bool
  3855. */
  3856. private function executeBinaryComparisonOperation($cellID, $operand1, $operand2, $operation, Stack &$stack, $recursingArrays = false)
  3857. {
  3858. // If we're dealing with matrix operations, we want a matrix result
  3859. if ((is_array($operand1)) || (is_array($operand2))) {
  3860. $result = [];
  3861. if ((is_array($operand1)) && (!is_array($operand2))) {
  3862. foreach ($operand1 as $x => $operandData) {
  3863. $this->debugLog->writeDebugLog('Evaluating Comparison ', $this->showValue($operandData), ' ', $operation, ' ', $this->showValue($operand2));
  3864. $this->executeBinaryComparisonOperation($cellID, $operandData, $operand2, $operation, $stack);
  3865. $r = $stack->pop();
  3866. $result[$x] = $r['value'];
  3867. }
  3868. } elseif ((!is_array($operand1)) && (is_array($operand2))) {
  3869. foreach ($operand2 as $x => $operandData) {
  3870. $this->debugLog->writeDebugLog('Evaluating Comparison ', $this->showValue($operand1), ' ', $operation, ' ', $this->showValue($operandData));
  3871. $this->executeBinaryComparisonOperation($cellID, $operand1, $operandData, $operation, $stack);
  3872. $r = $stack->pop();
  3873. $result[$x] = $r['value'];
  3874. }
  3875. } else {
  3876. if (!$recursingArrays) {
  3877. self::checkMatrixOperands($operand1, $operand2, 2);
  3878. }
  3879. foreach ($operand1 as $x => $operandData) {
  3880. $this->debugLog->writeDebugLog('Evaluating Comparison ', $this->showValue($operandData), ' ', $operation, ' ', $this->showValue($operand2[$x]));
  3881. $this->executeBinaryComparisonOperation($cellID, $operandData, $operand2[$x], $operation, $stack, true);
  3882. $r = $stack->pop();
  3883. $result[$x] = $r['value'];
  3884. }
  3885. }
  3886. // Log the result details
  3887. $this->debugLog->writeDebugLog('Comparison Evaluation Result is ', $this->showTypeDetails($result));
  3888. // And push the result onto the stack
  3889. $stack->push('Array', $result);
  3890. return true;
  3891. }
  3892. // Simple validate the two operands if they are string values
  3893. if (is_string($operand1) && $operand1 > '' && $operand1[0] == '"') {
  3894. $operand1 = self::unwrapResult($operand1);
  3895. }
  3896. if (is_string($operand2) && $operand2 > '' && $operand2[0] == '"') {
  3897. $operand2 = self::unwrapResult($operand2);
  3898. }
  3899. // Use case insensitive comparaison if not OpenOffice mode
  3900. if (Functions::getCompatibilityMode() != Functions::COMPATIBILITY_OPENOFFICE) {
  3901. if (is_string($operand1)) {
  3902. $operand1 = strtoupper($operand1);
  3903. }
  3904. if (is_string($operand2)) {
  3905. $operand2 = strtoupper($operand2);
  3906. }
  3907. }
  3908. $useLowercaseFirstComparison = is_string($operand1) && is_string($operand2) && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE;
  3909. // execute the necessary operation
  3910. switch ($operation) {
  3911. // Greater than
  3912. case '>':
  3913. if ($useLowercaseFirstComparison) {
  3914. $result = $this->strcmpLowercaseFirst($operand1, $operand2) > 0;
  3915. } else {
  3916. $result = ($operand1 > $operand2);
  3917. }
  3918. break;
  3919. // Less than
  3920. case '<':
  3921. if ($useLowercaseFirstComparison) {
  3922. $result = $this->strcmpLowercaseFirst($operand1, $operand2) < 0;
  3923. } else {
  3924. $result = ($operand1 < $operand2);
  3925. }
  3926. break;
  3927. // Equality
  3928. case '=':
  3929. if (is_numeric($operand1) && is_numeric($operand2)) {
  3930. $result = (abs($operand1 - $operand2) < $this->delta);
  3931. } else {
  3932. $result = strcmp($operand1, $operand2) == 0;
  3933. }
  3934. break;
  3935. // Greater than or equal
  3936. case '>=':
  3937. if (is_numeric($operand1) && is_numeric($operand2)) {
  3938. $result = ((abs($operand1 - $operand2) < $this->delta) || ($operand1 > $operand2));
  3939. } elseif ($useLowercaseFirstComparison) {
  3940. $result = $this->strcmpLowercaseFirst($operand1, $operand2) >= 0;
  3941. } else {
  3942. $result = strcmp($operand1, $operand2) >= 0;
  3943. }
  3944. break;
  3945. // Less than or equal
  3946. case '<=':
  3947. if (is_numeric($operand1) && is_numeric($operand2)) {
  3948. $result = ((abs($operand1 - $operand2) < $this->delta) || ($operand1 < $operand2));
  3949. } elseif ($useLowercaseFirstComparison) {
  3950. $result = $this->strcmpLowercaseFirst($operand1, $operand2) <= 0;
  3951. } else {
  3952. $result = strcmp($operand1, $operand2) <= 0;
  3953. }
  3954. break;
  3955. // Inequality
  3956. case '<>':
  3957. if (is_numeric($operand1) && is_numeric($operand2)) {
  3958. $result = (abs($operand1 - $operand2) > 1E-14);
  3959. } else {
  3960. $result = strcmp($operand1, $operand2) != 0;
  3961. }
  3962. break;
  3963. }
  3964. // Log the result details
  3965. $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
  3966. // And push the result onto the stack
  3967. $stack->push('Value', $result);
  3968. return true;
  3969. }
  3970. /**
  3971. * Compare two strings in the same way as strcmp() except that lowercase come before uppercase letters.
  3972. *
  3973. * @param string $str1 First string value for the comparison
  3974. * @param string $str2 Second string value for the comparison
  3975. *
  3976. * @return int
  3977. */
  3978. private function strcmpLowercaseFirst($str1, $str2)
  3979. {
  3980. $inversedStr1 = Shared\StringHelper::strCaseReverse($str1);
  3981. $inversedStr2 = Shared\StringHelper::strCaseReverse($str2);
  3982. return strcmp($inversedStr1, $inversedStr2);
  3983. }
  3984. /**
  3985. * @param mixed $operand1
  3986. * @param mixed $operand2
  3987. * @param mixed $operation
  3988. * @param string $matrixFunction
  3989. * @param mixed $stack
  3990. *
  3991. * @return bool
  3992. */
  3993. private function executeNumericBinaryOperation($operand1, $operand2, $operation, $matrixFunction, &$stack)
  3994. {
  3995. // Validate the two operands
  3996. if (!$this->validateBinaryOperand($operand1, $stack)) {
  3997. return false;
  3998. }
  3999. if (!$this->validateBinaryOperand($operand2, $stack)) {
  4000. return false;
  4001. }
  4002. // If either of the operands is a matrix, we need to treat them both as matrices
  4003. // (converting the other operand to a matrix if need be); then perform the required
  4004. // matrix operation
  4005. if ((is_array($operand1)) || (is_array($operand2))) {
  4006. // Ensure that both operands are arrays/matrices of the same size
  4007. self::checkMatrixOperands($operand1, $operand2, 2);
  4008. try {
  4009. // Convert operand 1 from a PHP array to a matrix
  4010. $matrix = new Shared\JAMA\Matrix($operand1);
  4011. // Perform the required operation against the operand 1 matrix, passing in operand 2
  4012. $matrixResult = $matrix->$matrixFunction($operand2);
  4013. $result = $matrixResult->getArray();
  4014. } catch (\Exception $ex) {
  4015. $this->debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
  4016. $result = '#VALUE!';
  4017. }
  4018. } else {
  4019. if ((Functions::getCompatibilityMode() != Functions::COMPATIBILITY_OPENOFFICE) &&
  4020. ((is_string($operand1) && !is_numeric($operand1) && strlen($operand1) > 0) ||
  4021. (is_string($operand2) && !is_numeric($operand2) && strlen($operand2) > 0))) {
  4022. $result = Functions::VALUE();
  4023. } else {
  4024. // If we're dealing with non-matrix operations, execute the necessary operation
  4025. switch ($operation) {
  4026. // Addition
  4027. case '+':
  4028. $result = $operand1 + $operand2;
  4029. break;
  4030. // Subtraction
  4031. case '-':
  4032. $result = $operand1 - $operand2;
  4033. break;
  4034. // Multiplication
  4035. case '*':
  4036. $result = $operand1 * $operand2;
  4037. break;
  4038. // Division
  4039. case '/':
  4040. if ($operand2 == 0) {
  4041. // Trap for Divide by Zero error
  4042. $stack->push('Value', '#DIV/0!');
  4043. $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails('#DIV/0!'));
  4044. return false;
  4045. }
  4046. $result = $operand1 / $operand2;
  4047. break;
  4048. // Power
  4049. case '^':
  4050. $result = pow($operand1, $operand2);
  4051. break;
  4052. }
  4053. }
  4054. }
  4055. // Log the result details
  4056. $this->debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
  4057. // And push the result onto the stack
  4058. $stack->push('Value', $result);
  4059. return true;
  4060. }
  4061. // trigger an error, but nicely, if need be
  4062. protected function raiseFormulaError($errorMessage)
  4063. {
  4064. $this->formulaError = $errorMessage;
  4065. $this->cyclicReferenceStack->clear();
  4066. if (!$this->suppressFormulaErrors) {
  4067. throw new Exception($errorMessage);
  4068. }
  4069. trigger_error($errorMessage, E_USER_ERROR);
  4070. return false;
  4071. }
  4072. /**
  4073. * Extract range values.
  4074. *
  4075. * @param string &$pRange String based range representation
  4076. * @param Worksheet $pSheet Worksheet
  4077. * @param bool $resetLog Flag indicating whether calculation log should be reset or not
  4078. *
  4079. * @return mixed Array of values in range if range contains more than one element. Otherwise, a single value is returned.
  4080. */
  4081. public function extractCellRange(&$pRange = 'A1', Worksheet $pSheet = null, $resetLog = true)
  4082. {
  4083. // Return value
  4084. $returnValue = [];
  4085. if ($pSheet !== null) {
  4086. $pSheetName = $pSheet->getTitle();
  4087. if (strpos($pRange, '!') !== false) {
  4088. list($pSheetName, $pRange) = Worksheet::extractSheetTitle($pRange, true);
  4089. $pSheet = $this->spreadsheet->getSheetByName($pSheetName);
  4090. }
  4091. // Extract range
  4092. $aReferences = Coordinate::extractAllCellReferencesInRange($pRange);
  4093. $pRange = $pSheetName . '!' . $pRange;
  4094. if (!isset($aReferences[1])) {
  4095. $currentCol = '';
  4096. $currentRow = 0;
  4097. // Single cell in range
  4098. sscanf($aReferences[0], '%[A-Z]%d', $currentCol, $currentRow);
  4099. if ($pSheet->cellExists($aReferences[0])) {
  4100. $returnValue[$currentRow][$currentCol] = $pSheet->getCell($aReferences[0])->getCalculatedValue($resetLog);
  4101. } else {
  4102. $returnValue[$currentRow][$currentCol] = null;
  4103. }
  4104. } else {
  4105. // Extract cell data for all cells in the range
  4106. foreach ($aReferences as $reference) {
  4107. $currentCol = '';
  4108. $currentRow = 0;
  4109. // Extract range
  4110. sscanf($reference, '%[A-Z]%d', $currentCol, $currentRow);
  4111. if ($pSheet->cellExists($reference)) {
  4112. $returnValue[$currentRow][$currentCol] = $pSheet->getCell($reference)->getCalculatedValue($resetLog);
  4113. } else {
  4114. $returnValue[$currentRow][$currentCol] = null;
  4115. }
  4116. }
  4117. }
  4118. }
  4119. return $returnValue;
  4120. }
  4121. /**
  4122. * Extract range values.
  4123. *
  4124. * @param string &$pRange String based range representation
  4125. * @param Worksheet $pSheet Worksheet
  4126. * @param bool $resetLog Flag indicating whether calculation log should be reset or not
  4127. *
  4128. * @return mixed Array of values in range if range contains more than one element. Otherwise, a single value is returned.
  4129. */
  4130. public function extractNamedRange(&$pRange = 'A1', Worksheet $pSheet = null, $resetLog = true)
  4131. {
  4132. // Return value
  4133. $returnValue = [];
  4134. if ($pSheet !== null) {
  4135. $pSheetName = $pSheet->getTitle();
  4136. if (strpos($pRange, '!') !== false) {
  4137. list($pSheetName, $pRange) = Worksheet::extractSheetTitle($pRange, true);
  4138. $pSheet = $this->spreadsheet->getSheetByName($pSheetName);
  4139. }
  4140. // Named range?
  4141. $namedRange = NamedRange::resolveRange($pRange, $pSheet);
  4142. if ($namedRange !== null) {
  4143. $pSheet = $namedRange->getWorksheet();
  4144. $pRange = $namedRange->getRange();
  4145. $splitRange = Coordinate::splitRange($pRange);
  4146. // Convert row and column references
  4147. if (ctype_alpha($splitRange[0][0])) {
  4148. $pRange = $splitRange[0][0] . '1:' . $splitRange[0][1] . $namedRange->getWorksheet()->getHighestRow();
  4149. } elseif (ctype_digit($splitRange[0][0])) {
  4150. $pRange = 'A' . $splitRange[0][0] . ':' . $namedRange->getWorksheet()->getHighestColumn() . $splitRange[0][1];
  4151. }
  4152. } else {
  4153. return Functions::REF();
  4154. }
  4155. // Extract range
  4156. $aReferences = Coordinate::extractAllCellReferencesInRange($pRange);
  4157. if (!isset($aReferences[1])) {
  4158. // Single cell (or single column or row) in range
  4159. list($currentCol, $currentRow) = Coordinate::coordinateFromString($aReferences[0]);
  4160. if ($pSheet->cellExists($aReferences[0])) {
  4161. $returnValue[$currentRow][$currentCol] = $pSheet->getCell($aReferences[0])->getCalculatedValue($resetLog);
  4162. } else {
  4163. $returnValue[$currentRow][$currentCol] = null;
  4164. }
  4165. } else {
  4166. // Extract cell data for all cells in the range
  4167. foreach ($aReferences as $reference) {
  4168. // Extract range
  4169. list($currentCol, $currentRow) = Coordinate::coordinateFromString($reference);
  4170. if ($pSheet->cellExists($reference)) {
  4171. $returnValue[$currentRow][$currentCol] = $pSheet->getCell($reference)->getCalculatedValue($resetLog);
  4172. } else {
  4173. $returnValue[$currentRow][$currentCol] = null;
  4174. }
  4175. }
  4176. }
  4177. }
  4178. return $returnValue;
  4179. }
  4180. /**
  4181. * Is a specific function implemented?
  4182. *
  4183. * @param string $pFunction Function Name
  4184. *
  4185. * @return bool
  4186. */
  4187. public function isImplemented($pFunction)
  4188. {
  4189. $pFunction = strtoupper($pFunction);
  4190. $notImplemented = !isset(self::$phpSpreadsheetFunctions[$pFunction]) || (is_array(self::$phpSpreadsheetFunctions[$pFunction]['functionCall']) && self::$phpSpreadsheetFunctions[$pFunction]['functionCall'][1] === 'DUMMY');
  4191. return !$notImplemented;
  4192. }
  4193. /**
  4194. * Get a list of all implemented functions as an array of function objects.
  4195. *
  4196. * @return array of Category
  4197. */
  4198. public function getFunctions()
  4199. {
  4200. return self::$phpSpreadsheetFunctions;
  4201. }
  4202. /**
  4203. * Get a list of implemented Excel function names.
  4204. *
  4205. * @return array
  4206. */
  4207. public function getImplementedFunctionNames()
  4208. {
  4209. $returnValue = [];
  4210. foreach (self::$phpSpreadsheetFunctions as $functionName => $function) {
  4211. if ($this->isImplemented($functionName)) {
  4212. $returnValue[] = $functionName;
  4213. }
  4214. }
  4215. return $returnValue;
  4216. }
  4217. /**
  4218. * Add cell reference if needed while making sure that it is the last argument.
  4219. *
  4220. * @param array $args
  4221. * @param bool $passCellReference
  4222. * @param array|string $functionCall
  4223. * @param null|Cell $pCell
  4224. *
  4225. * @return array
  4226. */
  4227. private function addCellReference(array $args, $passCellReference, $functionCall, Cell $pCell = null)
  4228. {
  4229. if ($passCellReference) {
  4230. if (is_array($functionCall)) {
  4231. $className = $functionCall[0];
  4232. $methodName = $functionCall[1];
  4233. $reflectionMethod = new \ReflectionMethod($className, $methodName);
  4234. $argumentCount = count($reflectionMethod->getParameters());
  4235. while (count($args) < $argumentCount - 1) {
  4236. $args[] = null;
  4237. }
  4238. }
  4239. $args[] = $pCell;
  4240. }
  4241. return $args;
  4242. }
  4243. }