No Description

Worksheet.php 91KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Worksheet
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Worksheet
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Worksheet
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Worksheet implements PHPExcel_IComparable
  35. {
  36. /* Break types */
  37. const BREAK_NONE = 0;
  38. const BREAK_ROW = 1;
  39. const BREAK_COLUMN = 2;
  40. /* Sheet state */
  41. const SHEETSTATE_VISIBLE = 'visible';
  42. const SHEETSTATE_HIDDEN = 'hidden';
  43. const SHEETSTATE_VERYHIDDEN = 'veryHidden';
  44. /**
  45. * Invalid characters in sheet title
  46. *
  47. * @var array
  48. */
  49. private static $_invalidCharacters = array('*', ':', '/', '\\', '?', '[', ']');
  50. /**
  51. * Parent spreadsheet
  52. *
  53. * @var PHPExcel
  54. */
  55. private $_parent;
  56. /**
  57. * Cacheable collection of cells
  58. *
  59. * @var PHPExcel_CachedObjectStorage_xxx
  60. */
  61. private $_cellCollection = null;
  62. /**
  63. * Collection of row dimensions
  64. *
  65. * @var PHPExcel_Worksheet_RowDimension[]
  66. */
  67. private $_rowDimensions = array();
  68. /**
  69. * Default row dimension
  70. *
  71. * @var PHPExcel_Worksheet_RowDimension
  72. */
  73. private $_defaultRowDimension = null;
  74. /**
  75. * Collection of column dimensions
  76. *
  77. * @var PHPExcel_Worksheet_ColumnDimension[]
  78. */
  79. private $_columnDimensions = array();
  80. /**
  81. * Default column dimension
  82. *
  83. * @var PHPExcel_Worksheet_ColumnDimension
  84. */
  85. private $_defaultColumnDimension = null;
  86. /**
  87. * Collection of drawings
  88. *
  89. * @var PHPExcel_Worksheet_BaseDrawing[]
  90. */
  91. private $_drawingCollection = null;
  92. /**
  93. * Collection of Chart objects
  94. *
  95. * @var PHPExcel_Chart[]
  96. */
  97. private $_chartCollection = array();
  98. /**
  99. * Worksheet title
  100. *
  101. * @var string
  102. */
  103. private $_title;
  104. /**
  105. * Sheet state
  106. *
  107. * @var string
  108. */
  109. private $_sheetState;
  110. /**
  111. * Page setup
  112. *
  113. * @var PHPExcel_Worksheet_PageSetup
  114. */
  115. private $_pageSetup;
  116. /**
  117. * Page margins
  118. *
  119. * @var PHPExcel_Worksheet_PageMargins
  120. */
  121. private $_pageMargins;
  122. /**
  123. * Page header/footer
  124. *
  125. * @var PHPExcel_Worksheet_HeaderFooter
  126. */
  127. private $_headerFooter;
  128. /**
  129. * Sheet view
  130. *
  131. * @var PHPExcel_Worksheet_SheetView
  132. */
  133. private $_sheetView;
  134. /**
  135. * Protection
  136. *
  137. * @var PHPExcel_Worksheet_Protection
  138. */
  139. private $_protection;
  140. /**
  141. * Collection of styles
  142. *
  143. * @var PHPExcel_Style[]
  144. */
  145. private $_styles = array();
  146. /**
  147. * Conditional styles. Indexed by cell coordinate, e.g. 'A1'
  148. *
  149. * @var array
  150. */
  151. private $_conditionalStylesCollection = array();
  152. /**
  153. * Is the current cell collection sorted already?
  154. *
  155. * @var boolean
  156. */
  157. private $_cellCollectionIsSorted = false;
  158. /**
  159. * Collection of breaks
  160. *
  161. * @var array
  162. */
  163. private $_breaks = array();
  164. /**
  165. * Collection of merged cell ranges
  166. *
  167. * @var array
  168. */
  169. private $_mergeCells = array();
  170. /**
  171. * Collection of protected cell ranges
  172. *
  173. * @var array
  174. */
  175. private $_protectedCells = array();
  176. /**
  177. * Autofilter Range and selection
  178. *
  179. * @var PHPExcel_Worksheet_AutoFilter
  180. */
  181. private $_autoFilter = NULL;
  182. /**
  183. * Freeze pane
  184. *
  185. * @var string
  186. */
  187. private $_freezePane = '';
  188. /**
  189. * Show gridlines?
  190. *
  191. * @var boolean
  192. */
  193. private $_showGridlines = true;
  194. /**
  195. * Print gridlines?
  196. *
  197. * @var boolean
  198. */
  199. private $_printGridlines = false;
  200. /**
  201. * Show row and column headers?
  202. *
  203. * @var boolean
  204. */
  205. private $_showRowColHeaders = true;
  206. /**
  207. * Show summary below? (Row/Column outline)
  208. *
  209. * @var boolean
  210. */
  211. private $_showSummaryBelow = true;
  212. /**
  213. * Show summary right? (Row/Column outline)
  214. *
  215. * @var boolean
  216. */
  217. private $_showSummaryRight = true;
  218. /**
  219. * Collection of comments
  220. *
  221. * @var PHPExcel_Comment[]
  222. */
  223. private $_comments = array();
  224. /**
  225. * Active cell. (Only one!)
  226. *
  227. * @var string
  228. */
  229. private $_activeCell = 'A1';
  230. /**
  231. * Selected cells
  232. *
  233. * @var string
  234. */
  235. private $_selectedCells = 'A1';
  236. /**
  237. * Cached highest column
  238. *
  239. * @var string
  240. */
  241. private $_cachedHighestColumn = 'A';
  242. /**
  243. * Cached highest row
  244. *
  245. * @var int
  246. */
  247. private $_cachedHighestRow = 1;
  248. /**
  249. * Right-to-left?
  250. *
  251. * @var boolean
  252. */
  253. private $_rightToLeft = false;
  254. /**
  255. * Hyperlinks. Indexed by cell coordinate, e.g. 'A1'
  256. *
  257. * @var array
  258. */
  259. private $_hyperlinkCollection = array();
  260. /**
  261. * Data validation objects. Indexed by cell coordinate, e.g. 'A1'
  262. *
  263. * @var array
  264. */
  265. private $_dataValidationCollection = array();
  266. /**
  267. * Tab color
  268. *
  269. * @var PHPExcel_Style_Color
  270. */
  271. private $_tabColor;
  272. /**
  273. * Dirty flag
  274. *
  275. * @var boolean
  276. */
  277. private $_dirty = true;
  278. /**
  279. * Hash
  280. *
  281. * @var string
  282. */
  283. private $_hash = null;
  284. /**
  285. * CodeName
  286. *
  287. * @var string
  288. */
  289. private $_codeName = null;
  290. /**
  291. * Create a new worksheet
  292. *
  293. * @param PHPExcel $pParent
  294. * @param string $pTitle
  295. */
  296. public function __construct(PHPExcel $pParent = null, $pTitle = 'Worksheet')
  297. {
  298. // Set parent and title
  299. $this->_parent = $pParent;
  300. $this->setTitle($pTitle, FALSE);
  301. // setTitle can change $pTitle
  302. $this->setCodeName($this->getTitle());
  303. $this->setSheetState(PHPExcel_Worksheet::SHEETSTATE_VISIBLE);
  304. $this->_cellCollection = PHPExcel_CachedObjectStorageFactory::getInstance($this);
  305. // Set page setup
  306. $this->_pageSetup = new PHPExcel_Worksheet_PageSetup();
  307. // Set page margins
  308. $this->_pageMargins = new PHPExcel_Worksheet_PageMargins();
  309. // Set page header/footer
  310. $this->_headerFooter = new PHPExcel_Worksheet_HeaderFooter();
  311. // Set sheet view
  312. $this->_sheetView = new PHPExcel_Worksheet_SheetView();
  313. // Drawing collection
  314. $this->_drawingCollection = new ArrayObject();
  315. // Chart collection
  316. $this->_chartCollection = new ArrayObject();
  317. // Protection
  318. $this->_protection = new PHPExcel_Worksheet_Protection();
  319. // Default row dimension
  320. $this->_defaultRowDimension = new PHPExcel_Worksheet_RowDimension(NULL);
  321. // Default column dimension
  322. $this->_defaultColumnDimension = new PHPExcel_Worksheet_ColumnDimension(NULL);
  323. $this->_autoFilter = new PHPExcel_Worksheet_AutoFilter(NULL, $this);
  324. }
  325. /**
  326. * Disconnect all cells from this PHPExcel_Worksheet object,
  327. * typically so that the worksheet object can be unset
  328. *
  329. */
  330. public function disconnectCells() {
  331. if ( $this->_cellCollection !== NULL){
  332. $this->_cellCollection->unsetWorksheetCells();
  333. $this->_cellCollection = NULL;
  334. }
  335. // detach ourself from the workbook, so that it can then delete this worksheet successfully
  336. $this->_parent = null;
  337. }
  338. /**
  339. * Code to execute when this worksheet is unset()
  340. *
  341. */
  342. function __destruct() {
  343. PHPExcel_Calculation::getInstance($this->_parent)
  344. ->clearCalculationCacheForWorksheet($this->_title);
  345. $this->disconnectCells();
  346. }
  347. /**
  348. * Return the cache controller for the cell collection
  349. *
  350. * @return PHPExcel_CachedObjectStorage_xxx
  351. */
  352. public function getCellCacheController() {
  353. return $this->_cellCollection;
  354. } // function getCellCacheController()
  355. /**
  356. * Get array of invalid characters for sheet title
  357. *
  358. * @return array
  359. */
  360. public static function getInvalidCharacters()
  361. {
  362. return self::$_invalidCharacters;
  363. }
  364. /**
  365. * Check sheet code name for valid Excel syntax
  366. *
  367. * @param string $pValue The string to check
  368. * @return string The valid string
  369. * @throws Exception
  370. */
  371. private static function _checkSheetCodeName($pValue)
  372. {
  373. $CharCount = PHPExcel_Shared_String::CountCharacters($pValue);
  374. if ($CharCount == 0) {
  375. throw new PHPExcel_Exception('Sheet code name cannot be empty.');
  376. }
  377. // Some of the printable ASCII characters are invalid: * : / \ ? [ ] and first and last characters cannot be a "'"
  378. if ((str_replace(self::$_invalidCharacters, '', $pValue) !== $pValue) ||
  379. (PHPExcel_Shared_String::Substring($pValue,-1,1)=='\'') ||
  380. (PHPExcel_Shared_String::Substring($pValue,0,1)=='\'')) {
  381. throw new PHPExcel_Exception('Invalid character found in sheet code name');
  382. }
  383. // Maximum 31 characters allowed for sheet title
  384. if ($CharCount > 31) {
  385. throw new PHPExcel_Exception('Maximum 31 characters allowed in sheet code name.');
  386. }
  387. return $pValue;
  388. }
  389. /**
  390. * Check sheet title for valid Excel syntax
  391. *
  392. * @param string $pValue The string to check
  393. * @return string The valid string
  394. * @throws PHPExcel_Exception
  395. */
  396. private static function _checkSheetTitle($pValue)
  397. {
  398. // Some of the printable ASCII characters are invalid: * : / \ ? [ ]
  399. if (str_replace(self::$_invalidCharacters, '', $pValue) !== $pValue) {
  400. throw new PHPExcel_Exception('Invalid character found in sheet title');
  401. }
  402. // Maximum 31 characters allowed for sheet title
  403. if (PHPExcel_Shared_String::CountCharacters($pValue) > 31) {
  404. throw new PHPExcel_Exception('Maximum 31 characters allowed in sheet title.');
  405. }
  406. return $pValue;
  407. }
  408. /**
  409. * Get collection of cells
  410. *
  411. * @param boolean $pSorted Also sort the cell collection?
  412. * @return PHPExcel_Cell[]
  413. */
  414. public function getCellCollection($pSorted = true)
  415. {
  416. if ($pSorted) {
  417. // Re-order cell collection
  418. return $this->sortCellCollection();
  419. }
  420. if ($this->_cellCollection !== NULL) {
  421. return $this->_cellCollection->getCellList();
  422. }
  423. return array();
  424. }
  425. /**
  426. * Sort collection of cells
  427. *
  428. * @return PHPExcel_Worksheet
  429. */
  430. public function sortCellCollection()
  431. {
  432. if ($this->_cellCollection !== NULL) {
  433. return $this->_cellCollection->getSortedCellList();
  434. }
  435. return array();
  436. }
  437. /**
  438. * Get collection of row dimensions
  439. *
  440. * @return PHPExcel_Worksheet_RowDimension[]
  441. */
  442. public function getRowDimensions()
  443. {
  444. return $this->_rowDimensions;
  445. }
  446. /**
  447. * Get default row dimension
  448. *
  449. * @return PHPExcel_Worksheet_RowDimension
  450. */
  451. public function getDefaultRowDimension()
  452. {
  453. return $this->_defaultRowDimension;
  454. }
  455. /**
  456. * Get collection of column dimensions
  457. *
  458. * @return PHPExcel_Worksheet_ColumnDimension[]
  459. */
  460. public function getColumnDimensions()
  461. {
  462. return $this->_columnDimensions;
  463. }
  464. /**
  465. * Get default column dimension
  466. *
  467. * @return PHPExcel_Worksheet_ColumnDimension
  468. */
  469. public function getDefaultColumnDimension()
  470. {
  471. return $this->_defaultColumnDimension;
  472. }
  473. /**
  474. * Get collection of drawings
  475. *
  476. * @return PHPExcel_Worksheet_BaseDrawing[]
  477. */
  478. public function getDrawingCollection()
  479. {
  480. return $this->_drawingCollection;
  481. }
  482. /**
  483. * Get collection of charts
  484. *
  485. * @return PHPExcel_Chart[]
  486. */
  487. public function getChartCollection()
  488. {
  489. return $this->_chartCollection;
  490. }
  491. /**
  492. * Add chart
  493. *
  494. * @param PHPExcel_Chart $pChart
  495. * @param int|null $iChartIndex Index where chart should go (0,1,..., or null for last)
  496. * @return PHPExcel_Chart
  497. */
  498. public function addChart(PHPExcel_Chart $pChart = null, $iChartIndex = null)
  499. {
  500. $pChart->setWorksheet($this);
  501. if (is_null($iChartIndex)) {
  502. $this->_chartCollection[] = $pChart;
  503. } else {
  504. // Insert the chart at the requested index
  505. array_splice($this->_chartCollection, $iChartIndex, 0, array($pChart));
  506. }
  507. return $pChart;
  508. }
  509. /**
  510. * Return the count of charts on this worksheet
  511. *
  512. * @return int The number of charts
  513. */
  514. public function getChartCount()
  515. {
  516. return count($this->_chartCollection);
  517. }
  518. /**
  519. * Get a chart by its index position
  520. *
  521. * @param string $index Chart index position
  522. * @return false|PHPExcel_Chart
  523. * @throws PHPExcel_Exception
  524. */
  525. public function getChartByIndex($index = null)
  526. {
  527. $chartCount = count($this->_chartCollection);
  528. if ($chartCount == 0) {
  529. return false;
  530. }
  531. if (is_null($index)) {
  532. $index = --$chartCount;
  533. }
  534. if (!isset($this->_chartCollection[$index])) {
  535. return false;
  536. }
  537. return $this->_chartCollection[$index];
  538. }
  539. /**
  540. * Return an array of the names of charts on this worksheet
  541. *
  542. * @return string[] The names of charts
  543. * @throws PHPExcel_Exception
  544. */
  545. public function getChartNames()
  546. {
  547. $chartNames = array();
  548. foreach($this->_chartCollection as $chart) {
  549. $chartNames[] = $chart->getName();
  550. }
  551. return $chartNames;
  552. }
  553. /**
  554. * Get a chart by name
  555. *
  556. * @param string $chartName Chart name
  557. * @return false|PHPExcel_Chart
  558. * @throws PHPExcel_Exception
  559. */
  560. public function getChartByName($chartName = '')
  561. {
  562. $chartCount = count($this->_chartCollection);
  563. if ($chartCount == 0) {
  564. return false;
  565. }
  566. foreach($this->_chartCollection as $index => $chart) {
  567. if ($chart->getName() == $chartName) {
  568. return $this->_chartCollection[$index];
  569. }
  570. }
  571. return false;
  572. }
  573. /**
  574. * Refresh column dimensions
  575. *
  576. * @return PHPExcel_Worksheet
  577. */
  578. public function refreshColumnDimensions()
  579. {
  580. $currentColumnDimensions = $this->getColumnDimensions();
  581. $newColumnDimensions = array();
  582. foreach ($currentColumnDimensions as $objColumnDimension) {
  583. $newColumnDimensions[$objColumnDimension->getColumnIndex()] = $objColumnDimension;
  584. }
  585. $this->_columnDimensions = $newColumnDimensions;
  586. return $this;
  587. }
  588. /**
  589. * Refresh row dimensions
  590. *
  591. * @return PHPExcel_Worksheet
  592. */
  593. public function refreshRowDimensions()
  594. {
  595. $currentRowDimensions = $this->getRowDimensions();
  596. $newRowDimensions = array();
  597. foreach ($currentRowDimensions as $objRowDimension) {
  598. $newRowDimensions[$objRowDimension->getRowIndex()] = $objRowDimension;
  599. }
  600. $this->_rowDimensions = $newRowDimensions;
  601. return $this;
  602. }
  603. /**
  604. * Calculate worksheet dimension
  605. *
  606. * @return string String containing the dimension of this worksheet
  607. */
  608. public function calculateWorksheetDimension()
  609. {
  610. // Return
  611. return 'A1' . ':' . $this->getHighestColumn() . $this->getHighestRow();
  612. }
  613. /**
  614. * Calculate worksheet data dimension
  615. *
  616. * @return string String containing the dimension of this worksheet that actually contain data
  617. */
  618. public function calculateWorksheetDataDimension()
  619. {
  620. // Return
  621. return 'A1' . ':' . $this->getHighestDataColumn() . $this->getHighestDataRow();
  622. }
  623. /**
  624. * Calculate widths for auto-size columns
  625. *
  626. * @param boolean $calculateMergeCells Calculate merge cell width
  627. * @return PHPExcel_Worksheet;
  628. */
  629. public function calculateColumnWidths($calculateMergeCells = false)
  630. {
  631. // initialize $autoSizes array
  632. $autoSizes = array();
  633. foreach ($this->getColumnDimensions() as $colDimension) {
  634. if ($colDimension->getAutoSize()) {
  635. $autoSizes[$colDimension->getColumnIndex()] = -1;
  636. }
  637. }
  638. // There is only something to do if there are some auto-size columns
  639. if (!empty($autoSizes)) {
  640. // build list of cells references that participate in a merge
  641. $isMergeCell = array();
  642. foreach ($this->getMergeCells() as $cells) {
  643. foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cells) as $cellReference) {
  644. $isMergeCell[$cellReference] = true;
  645. }
  646. }
  647. // loop through all cells in the worksheet
  648. foreach ($this->getCellCollection(false) as $cellID) {
  649. $cell = $this->getCell($cellID);
  650. if (isset($autoSizes[$this->_cellCollection->getCurrentColumn()])) {
  651. // Determine width if cell does not participate in a merge
  652. if (!isset($isMergeCell[$this->_cellCollection->getCurrentAddress()])) {
  653. // Calculated value
  654. // To formatted string
  655. $cellValue = PHPExcel_Style_NumberFormat::toFormattedString(
  656. $cell->getCalculatedValue(),
  657. $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode()
  658. );
  659. $autoSizes[$this->_cellCollection->getCurrentColumn()] = max(
  660. (float) $autoSizes[$this->_cellCollection->getCurrentColumn()],
  661. (float)PHPExcel_Shared_Font::calculateColumnWidth(
  662. $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getFont(),
  663. $cellValue,
  664. $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getAlignment()->getTextRotation(),
  665. $this->getDefaultStyle()->getFont()
  666. )
  667. );
  668. }
  669. }
  670. }
  671. // adjust column widths
  672. foreach ($autoSizes as $columnIndex => $width) {
  673. if ($width == -1) $width = $this->getDefaultColumnDimension()->getWidth();
  674. $this->getColumnDimension($columnIndex)->setWidth($width);
  675. }
  676. }
  677. return $this;
  678. }
  679. /**
  680. * Get parent
  681. *
  682. * @return PHPExcel
  683. */
  684. public function getParent() {
  685. return $this->_parent;
  686. }
  687. /**
  688. * Re-bind parent
  689. *
  690. * @param PHPExcel $parent
  691. * @return PHPExcel_Worksheet
  692. */
  693. public function rebindParent(PHPExcel $parent) {
  694. if ($this->_parent !== null) {
  695. $namedRanges = $this->_parent->getNamedRanges();
  696. foreach ($namedRanges as $namedRange) {
  697. $parent->addNamedRange($namedRange);
  698. }
  699. $this->_parent->removeSheetByIndex(
  700. $this->_parent->getIndex($this)
  701. );
  702. }
  703. $this->_parent = $parent;
  704. return $this;
  705. }
  706. /**
  707. * Get title
  708. *
  709. * @return string
  710. */
  711. public function getTitle()
  712. {
  713. return $this->_title;
  714. }
  715. /**
  716. * Set title
  717. *
  718. * @param string $pValue String containing the dimension of this worksheet
  719. * @param string $updateFormulaCellReferences boolean Flag indicating whether cell references in formulae should
  720. * be updated to reflect the new sheet name.
  721. * This should be left as the default true, unless you are
  722. * certain that no formula cells on any worksheet contain
  723. * references to this worksheet
  724. * @return PHPExcel_Worksheet
  725. */
  726. public function setTitle($pValue = 'Worksheet', $updateFormulaCellReferences = true)
  727. {
  728. // Is this a 'rename' or not?
  729. if ($this->getTitle() == $pValue) {
  730. return $this;
  731. }
  732. // Syntax check
  733. self::_checkSheetTitle($pValue);
  734. // Old title
  735. $oldTitle = $this->getTitle();
  736. if ($this->_parent) {
  737. // Is there already such sheet name?
  738. if ($this->_parent->sheetNameExists($pValue)) {
  739. // Use name, but append with lowest possible integer
  740. if (PHPExcel_Shared_String::CountCharacters($pValue) > 29) {
  741. $pValue = PHPExcel_Shared_String::Substring($pValue,0,29);
  742. }
  743. $i = 1;
  744. while ($this->_parent->sheetNameExists($pValue . ' ' . $i)) {
  745. ++$i;
  746. if ($i == 10) {
  747. if (PHPExcel_Shared_String::CountCharacters($pValue) > 28) {
  748. $pValue = PHPExcel_Shared_String::Substring($pValue,0,28);
  749. }
  750. } elseif ($i == 100) {
  751. if (PHPExcel_Shared_String::CountCharacters($pValue) > 27) {
  752. $pValue = PHPExcel_Shared_String::Substring($pValue,0,27);
  753. }
  754. }
  755. }
  756. $altTitle = $pValue . ' ' . $i;
  757. return $this->setTitle($altTitle,$updateFormulaCellReferences);
  758. }
  759. }
  760. // Set title
  761. $this->_title = $pValue;
  762. $this->_dirty = true;
  763. if ($this->_parent) {
  764. // New title
  765. $newTitle = $this->getTitle();
  766. PHPExcel_Calculation::getInstance($this->_parent)
  767. ->renameCalculationCacheForWorksheet($oldTitle, $newTitle);
  768. if ($updateFormulaCellReferences)
  769. PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->_parent, $oldTitle, $newTitle);
  770. }
  771. return $this;
  772. }
  773. /**
  774. * Get sheet state
  775. *
  776. * @return string Sheet state (visible, hidden, veryHidden)
  777. */
  778. public function getSheetState() {
  779. return $this->_sheetState;
  780. }
  781. /**
  782. * Set sheet state
  783. *
  784. * @param string $value Sheet state (visible, hidden, veryHidden)
  785. * @return PHPExcel_Worksheet
  786. */
  787. public function setSheetState($value = PHPExcel_Worksheet::SHEETSTATE_VISIBLE) {
  788. $this->_sheetState = $value;
  789. return $this;
  790. }
  791. /**
  792. * Get page setup
  793. *
  794. * @return PHPExcel_Worksheet_PageSetup
  795. */
  796. public function getPageSetup()
  797. {
  798. return $this->_pageSetup;
  799. }
  800. /**
  801. * Set page setup
  802. *
  803. * @param PHPExcel_Worksheet_PageSetup $pValue
  804. * @return PHPExcel_Worksheet
  805. */
  806. public function setPageSetup(PHPExcel_Worksheet_PageSetup $pValue)
  807. {
  808. $this->_pageSetup = $pValue;
  809. return $this;
  810. }
  811. /**
  812. * Get page margins
  813. *
  814. * @return PHPExcel_Worksheet_PageMargins
  815. */
  816. public function getPageMargins()
  817. {
  818. return $this->_pageMargins;
  819. }
  820. /**
  821. * Set page margins
  822. *
  823. * @param PHPExcel_Worksheet_PageMargins $pValue
  824. * @return PHPExcel_Worksheet
  825. */
  826. public function setPageMargins(PHPExcel_Worksheet_PageMargins $pValue)
  827. {
  828. $this->_pageMargins = $pValue;
  829. return $this;
  830. }
  831. /**
  832. * Get page header/footer
  833. *
  834. * @return PHPExcel_Worksheet_HeaderFooter
  835. */
  836. public function getHeaderFooter()
  837. {
  838. return $this->_headerFooter;
  839. }
  840. /**
  841. * Set page header/footer
  842. *
  843. * @param PHPExcel_Worksheet_HeaderFooter $pValue
  844. * @return PHPExcel_Worksheet
  845. */
  846. public function setHeaderFooter(PHPExcel_Worksheet_HeaderFooter $pValue)
  847. {
  848. $this->_headerFooter = $pValue;
  849. return $this;
  850. }
  851. /**
  852. * Get sheet view
  853. *
  854. * @return PHPExcel_Worksheet_SheetView
  855. */
  856. public function getSheetView()
  857. {
  858. return $this->_sheetView;
  859. }
  860. /**
  861. * Set sheet view
  862. *
  863. * @param PHPExcel_Worksheet_SheetView $pValue
  864. * @return PHPExcel_Worksheet
  865. */
  866. public function setSheetView(PHPExcel_Worksheet_SheetView $pValue)
  867. {
  868. $this->_sheetView = $pValue;
  869. return $this;
  870. }
  871. /**
  872. * Get Protection
  873. *
  874. * @return PHPExcel_Worksheet_Protection
  875. */
  876. public function getProtection()
  877. {
  878. return $this->_protection;
  879. }
  880. /**
  881. * Set Protection
  882. *
  883. * @param PHPExcel_Worksheet_Protection $pValue
  884. * @return PHPExcel_Worksheet
  885. */
  886. public function setProtection(PHPExcel_Worksheet_Protection $pValue)
  887. {
  888. $this->_protection = $pValue;
  889. $this->_dirty = true;
  890. return $this;
  891. }
  892. /**
  893. * Get highest worksheet column
  894. *
  895. * @param string $row Return the data highest column for the specified row,
  896. * or the highest column of any row if no row number is passed
  897. * @return string Highest column name
  898. */
  899. public function getHighestColumn($row = null)
  900. {
  901. if ($row == null) {
  902. return $this->_cachedHighestColumn;
  903. }
  904. return $this->getHighestDataColumn($row);
  905. }
  906. /**
  907. * Get highest worksheet column that contains data
  908. *
  909. * @param string $row Return the highest data column for the specified row,
  910. * or the highest data column of any row if no row number is passed
  911. * @return string Highest column name that contains data
  912. */
  913. public function getHighestDataColumn($row = null)
  914. {
  915. return $this->_cellCollection->getHighestColumn($row);
  916. }
  917. /**
  918. * Get highest worksheet row
  919. *
  920. * @param string $column Return the highest data row for the specified column,
  921. * or the highest row of any column if no column letter is passed
  922. * @return int Highest row number
  923. */
  924. public function getHighestRow($column = null)
  925. {
  926. if ($column == null) {
  927. return $this->_cachedHighestRow;
  928. }
  929. return $this->getHighestDataRow($column);
  930. }
  931. /**
  932. * Get highest worksheet row that contains data
  933. *
  934. * @param string $column Return the highest data row for the specified column,
  935. * or the highest data row of any column if no column letter is passed
  936. * @return string Highest row number that contains data
  937. */
  938. public function getHighestDataRow($column = null)
  939. {
  940. return $this->_cellCollection->getHighestRow($column);
  941. }
  942. /**
  943. * Get highest worksheet column and highest row that have cell records
  944. *
  945. * @return array Highest column name and highest row number
  946. */
  947. public function getHighestRowAndColumn()
  948. {
  949. return $this->_cellCollection->getHighestRowAndColumn();
  950. }
  951. /**
  952. * Set a cell value
  953. *
  954. * @param string $pCoordinate Coordinate of the cell
  955. * @param mixed $pValue Value of the cell
  956. * @param bool $returnCell Return the worksheet (false, default) or the cell (true)
  957. * @return PHPExcel_Worksheet|PHPExcel_Cell Depending on the last parameter being specified
  958. */
  959. public function setCellValue($pCoordinate = 'A1', $pValue = null, $returnCell = false)
  960. {
  961. $cell = $this->getCell(strtoupper($pCoordinate))->setValue($pValue);
  962. return ($returnCell) ? $cell : $this;
  963. }
  964. /**
  965. * Set a cell value by using numeric cell coordinates
  966. *
  967. * @param string $pColumn Numeric column coordinate of the cell (A = 0)
  968. * @param string $pRow Numeric row coordinate of the cell
  969. * @param mixed $pValue Value of the cell
  970. * @param bool $returnCell Return the worksheet (false, default) or the cell (true)
  971. * @return PHPExcel_Worksheet|PHPExcel_Cell Depending on the last parameter being specified
  972. */
  973. public function setCellValueByColumnAndRow($pColumn = 0, $pRow = 1, $pValue = null, $returnCell = false)
  974. {
  975. $cell = $this->getCellByColumnAndRow($pColumn, $pRow)->setValue($pValue);
  976. return ($returnCell) ? $cell : $this;
  977. }
  978. /**
  979. * Set a cell value
  980. *
  981. * @param string $pCoordinate Coordinate of the cell
  982. * @param mixed $pValue Value of the cell
  983. * @param string $pDataType Explicit data type
  984. * @param bool $returnCell Return the worksheet (false, default) or the cell (true)
  985. * @return PHPExcel_Worksheet|PHPExcel_Cell Depending on the last parameter being specified
  986. */
  987. public function setCellValueExplicit($pCoordinate = 'A1', $pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING, $returnCell = false)
  988. {
  989. // Set value
  990. $cell = $this->getCell(strtoupper($pCoordinate))->setValueExplicit($pValue, $pDataType);
  991. return ($returnCell) ? $cell : $this;
  992. }
  993. /**
  994. * Set a cell value by using numeric cell coordinates
  995. *
  996. * @param string $pColumn Numeric column coordinate of the cell
  997. * @param string $pRow Numeric row coordinate of the cell
  998. * @param mixed $pValue Value of the cell
  999. * @param string $pDataType Explicit data type
  1000. * @param bool $returnCell Return the worksheet (false, default) or the cell (true)
  1001. * @return PHPExcel_Worksheet|PHPExcel_Cell Depending on the last parameter being specified
  1002. */
  1003. public function setCellValueExplicitByColumnAndRow($pColumn = 0, $pRow = 1, $pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING, $returnCell = false)
  1004. {
  1005. $cell = $this->getCellByColumnAndRow($pColumn, $pRow)->setValueExplicit($pValue, $pDataType);
  1006. return ($returnCell) ? $cell : $this;
  1007. }
  1008. /**
  1009. * Get cell at a specific coordinate
  1010. *
  1011. * @param string $pCoordinate Coordinate of the cell
  1012. * @throws PHPExcel_Exception
  1013. * @return PHPExcel_Cell Cell that was found
  1014. */
  1015. public function getCell($pCoordinate = 'A1')
  1016. {
  1017. $pCoordinate = strtoupper($pCoordinate);
  1018. // Check cell collection
  1019. if ($this->_cellCollection->isDataSet($pCoordinate)) {
  1020. return $this->_cellCollection->getCacheData($pCoordinate);
  1021. }
  1022. // Worksheet reference?
  1023. if (strpos($pCoordinate, '!') !== false) {
  1024. $worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pCoordinate, true);
  1025. return $this->_parent->getSheetByName($worksheetReference[0])->getCell($worksheetReference[1]);
  1026. }
  1027. // Named range?
  1028. if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $pCoordinate, $matches)) &&
  1029. (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $pCoordinate, $matches))) {
  1030. $namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this);
  1031. if ($namedRange !== NULL) {
  1032. $pCoordinate = $namedRange->getRange();
  1033. return $namedRange->getWorksheet()->getCell($pCoordinate);
  1034. }
  1035. }
  1036. // Uppercase coordinate
  1037. $pCoordinate = strtoupper($pCoordinate);
  1038. if (strpos($pCoordinate, ':') !== false || strpos($pCoordinate, ',') !== false) {
  1039. throw new PHPExcel_Exception('Cell coordinate can not be a range of cells.');
  1040. } elseif (strpos($pCoordinate, '$') !== false) {
  1041. throw new PHPExcel_Exception('Cell coordinate must not be absolute.');
  1042. }
  1043. // Create new cell object
  1044. return $this->_createNewCell($pCoordinate);
  1045. }
  1046. /**
  1047. * Get cell at a specific coordinate by using numeric cell coordinates
  1048. *
  1049. * @param string $pColumn Numeric column coordinate of the cell
  1050. * @param string $pRow Numeric row coordinate of the cell
  1051. * @return PHPExcel_Cell Cell that was found
  1052. */
  1053. public function getCellByColumnAndRow($pColumn = 0, $pRow = 1)
  1054. {
  1055. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($pColumn);
  1056. $coordinate = $columnLetter . $pRow;
  1057. if ($this->_cellCollection->isDataSet($coordinate)) {
  1058. return $this->_cellCollection->getCacheData($coordinate);
  1059. }
  1060. return $this->_createNewCell($coordinate);
  1061. }
  1062. /**
  1063. * Create a new cell at the specified coordinate
  1064. *
  1065. * @param string $pCoordinate Coordinate of the cell
  1066. * @return PHPExcel_Cell Cell that was created
  1067. */
  1068. private function _createNewCell($pCoordinate)
  1069. {
  1070. $cell = $this->_cellCollection->addCacheData(
  1071. $pCoordinate,
  1072. new PHPExcel_Cell(
  1073. NULL,
  1074. PHPExcel_Cell_DataType::TYPE_NULL,
  1075. $this
  1076. )
  1077. );
  1078. $this->_cellCollectionIsSorted = false;
  1079. // Coordinates
  1080. $aCoordinates = PHPExcel_Cell::coordinateFromString($pCoordinate);
  1081. if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < PHPExcel_Cell::columnIndexFromString($aCoordinates[0]))
  1082. $this->_cachedHighestColumn = $aCoordinates[0];
  1083. $this->_cachedHighestRow = max($this->_cachedHighestRow, $aCoordinates[1]);
  1084. // Cell needs appropriate xfIndex from dimensions records
  1085. // but don't create dimension records if they don't already exist
  1086. $rowDimension = $this->getRowDimension($aCoordinates[1], FALSE);
  1087. $columnDimension = $this->getColumnDimension($aCoordinates[0], FALSE);
  1088. if ($rowDimension !== NULL && $rowDimension->getXfIndex() > 0) {
  1089. // then there is a row dimension with explicit style, assign it to the cell
  1090. $cell->setXfIndex($rowDimension->getXfIndex());
  1091. } elseif ($columnDimension !== NULL && $columnDimension->getXfIndex() > 0) {
  1092. // then there is a column dimension, assign it to the cell
  1093. $cell->setXfIndex($columnDimension->getXfIndex());
  1094. }
  1095. return $cell;
  1096. }
  1097. /**
  1098. * Does the cell at a specific coordinate exist?
  1099. *
  1100. * @param string $pCoordinate Coordinate of the cell
  1101. * @throws PHPExcel_Exception
  1102. * @return boolean
  1103. */
  1104. public function cellExists($pCoordinate = 'A1')
  1105. {
  1106. // Worksheet reference?
  1107. if (strpos($pCoordinate, '!') !== false) {
  1108. $worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pCoordinate, true);
  1109. return $this->_parent->getSheetByName($worksheetReference[0])->cellExists(strtoupper($worksheetReference[1]));
  1110. }
  1111. // Named range?
  1112. if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $pCoordinate, $matches)) &&
  1113. (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $pCoordinate, $matches))) {
  1114. $namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this);
  1115. if ($namedRange !== NULL) {
  1116. $pCoordinate = $namedRange->getRange();
  1117. if ($this->getHashCode() != $namedRange->getWorksheet()->getHashCode()) {
  1118. if (!$namedRange->getLocalOnly()) {
  1119. return $namedRange->getWorksheet()->cellExists($pCoordinate);
  1120. } else {
  1121. throw new PHPExcel_Exception('Named range ' . $namedRange->getName() . ' is not accessible from within sheet ' . $this->getTitle());
  1122. }
  1123. }
  1124. }
  1125. else { return false; }
  1126. }
  1127. // Uppercase coordinate
  1128. $pCoordinate = strtoupper($pCoordinate);
  1129. if (strpos($pCoordinate,':') !== false || strpos($pCoordinate,',') !== false) {
  1130. throw new PHPExcel_Exception('Cell coordinate can not be a range of cells.');
  1131. } elseif (strpos($pCoordinate,'$') !== false) {
  1132. throw new PHPExcel_Exception('Cell coordinate must not be absolute.');
  1133. } else {
  1134. // Coordinates
  1135. $aCoordinates = PHPExcel_Cell::coordinateFromString($pCoordinate);
  1136. // Cell exists?
  1137. return $this->_cellCollection->isDataSet($pCoordinate);
  1138. }
  1139. }
  1140. /**
  1141. * Cell at a specific coordinate by using numeric cell coordinates exists?
  1142. *
  1143. * @param string $pColumn Numeric column coordinate of the cell
  1144. * @param string $pRow Numeric row coordinate of the cell
  1145. * @return boolean
  1146. */
  1147. public function cellExistsByColumnAndRow($pColumn = 0, $pRow = 1)
  1148. {
  1149. return $this->cellExists(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  1150. }
  1151. /**
  1152. * Get row dimension at a specific row
  1153. *
  1154. * @param int $pRow Numeric index of the row
  1155. * @return PHPExcel_Worksheet_RowDimension
  1156. */
  1157. public function getRowDimension($pRow = 1, $create = TRUE)
  1158. {
  1159. // Found
  1160. $found = null;
  1161. // Get row dimension
  1162. if (!isset($this->_rowDimensions[$pRow])) {
  1163. if (!$create)
  1164. return NULL;
  1165. $this->_rowDimensions[$pRow] = new PHPExcel_Worksheet_RowDimension($pRow);
  1166. $this->_cachedHighestRow = max($this->_cachedHighestRow,$pRow);
  1167. }
  1168. return $this->_rowDimensions[$pRow];
  1169. }
  1170. /**
  1171. * Get column dimension at a specific column
  1172. *
  1173. * @param string $pColumn String index of the column
  1174. * @return PHPExcel_Worksheet_ColumnDimension
  1175. */
  1176. public function getColumnDimension($pColumn = 'A', $create = TRUE)
  1177. {
  1178. // Uppercase coordinate
  1179. $pColumn = strtoupper($pColumn);
  1180. // Fetch dimensions
  1181. if (!isset($this->_columnDimensions[$pColumn])) {
  1182. if (!$create)
  1183. return NULL;
  1184. $this->_columnDimensions[$pColumn] = new PHPExcel_Worksheet_ColumnDimension($pColumn);
  1185. if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < PHPExcel_Cell::columnIndexFromString($pColumn))
  1186. $this->_cachedHighestColumn = $pColumn;
  1187. }
  1188. return $this->_columnDimensions[$pColumn];
  1189. }
  1190. /**
  1191. * Get column dimension at a specific column by using numeric cell coordinates
  1192. *
  1193. * @param string $pColumn Numeric column coordinate of the cell
  1194. * @return PHPExcel_Worksheet_ColumnDimension
  1195. */
  1196. public function getColumnDimensionByColumn($pColumn = 0)
  1197. {
  1198. return $this->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($pColumn));
  1199. }
  1200. /**
  1201. * Get styles
  1202. *
  1203. * @return PHPExcel_Style[]
  1204. */
  1205. public function getStyles()
  1206. {
  1207. return $this->_styles;
  1208. }
  1209. /**
  1210. * Get default style of workbook.
  1211. *
  1212. * @deprecated
  1213. * @return PHPExcel_Style
  1214. * @throws PHPExcel_Exception
  1215. */
  1216. public function getDefaultStyle()
  1217. {
  1218. return $this->_parent->getDefaultStyle();
  1219. }
  1220. /**
  1221. * Set default style - should only be used by PHPExcel_IReader implementations!
  1222. *
  1223. * @deprecated
  1224. * @param PHPExcel_Style $pValue
  1225. * @throws PHPExcel_Exception
  1226. * @return PHPExcel_Worksheet
  1227. */
  1228. public function setDefaultStyle(PHPExcel_Style $pValue)
  1229. {
  1230. $this->_parent->getDefaultStyle()->applyFromArray(array(
  1231. 'font' => array(
  1232. 'name' => $pValue->getFont()->getName(),
  1233. 'size' => $pValue->getFont()->getSize(),
  1234. ),
  1235. ));
  1236. return $this;
  1237. }
  1238. /**
  1239. * Get style for cell
  1240. *
  1241. * @param string $pCellCoordinate Cell coordinate (or range) to get style for
  1242. * @return PHPExcel_Style
  1243. * @throws PHPExcel_Exception
  1244. */
  1245. public function getStyle($pCellCoordinate = 'A1')
  1246. {
  1247. // set this sheet as active
  1248. $this->_parent->setActiveSheetIndex($this->_parent->getIndex($this));
  1249. // set cell coordinate as active
  1250. $this->setSelectedCells(strtoupper($pCellCoordinate));
  1251. return $this->_parent->getCellXfSupervisor();
  1252. }
  1253. /**
  1254. * Get conditional styles for a cell
  1255. *
  1256. * @param string $pCoordinate
  1257. * @return PHPExcel_Style_Conditional[]
  1258. */
  1259. public function getConditionalStyles($pCoordinate = 'A1')
  1260. {
  1261. $pCoordinate = strtoupper($pCoordinate);
  1262. if (!isset($this->_conditionalStylesCollection[$pCoordinate])) {
  1263. $this->_conditionalStylesCollection[$pCoordinate] = array();
  1264. }
  1265. return $this->_conditionalStylesCollection[$pCoordinate];
  1266. }
  1267. /**
  1268. * Do conditional styles exist for this cell?
  1269. *
  1270. * @param string $pCoordinate
  1271. * @return boolean
  1272. */
  1273. public function conditionalStylesExists($pCoordinate = 'A1')
  1274. {
  1275. if (isset($this->_conditionalStylesCollection[strtoupper($pCoordinate)])) {
  1276. return true;
  1277. }
  1278. return false;
  1279. }
  1280. /**
  1281. * Removes conditional styles for a cell
  1282. *
  1283. * @param string $pCoordinate
  1284. * @return PHPExcel_Worksheet
  1285. */
  1286. public function removeConditionalStyles($pCoordinate = 'A1')
  1287. {
  1288. unset($this->_conditionalStylesCollection[strtoupper($pCoordinate)]);
  1289. return $this;
  1290. }
  1291. /**
  1292. * Get collection of conditional styles
  1293. *
  1294. * @return array
  1295. */
  1296. public function getConditionalStylesCollection()
  1297. {
  1298. return $this->_conditionalStylesCollection;
  1299. }
  1300. /**
  1301. * Set conditional styles
  1302. *
  1303. * @param $pCoordinate string E.g. 'A1'
  1304. * @param $pValue PHPExcel_Style_Conditional[]
  1305. * @return PHPExcel_Worksheet
  1306. */
  1307. public function setConditionalStyles($pCoordinate = 'A1', $pValue)
  1308. {
  1309. $this->_conditionalStylesCollection[strtoupper($pCoordinate)] = $pValue;
  1310. return $this;
  1311. }
  1312. /**
  1313. * Get style for cell by using numeric cell coordinates
  1314. *
  1315. * @param int $pColumn Numeric column coordinate of the cell
  1316. * @param int $pRow Numeric row coordinate of the cell
  1317. * @param int pColumn2 Numeric column coordinate of the range cell
  1318. * @param int pRow2 Numeric row coordinate of the range cell
  1319. * @return PHPExcel_Style
  1320. */
  1321. public function getStyleByColumnAndRow($pColumn = 0, $pRow = 1, $pColumn2 = null, $pRow2 = null)
  1322. {
  1323. if (!is_null($pColumn2) && !is_null($pRow2)) {
  1324. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow . ':' .
  1325. PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1326. return $this->getStyle($cellRange);
  1327. }
  1328. return $this->getStyle(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  1329. }
  1330. /**
  1331. * Set shared cell style to a range of cells
  1332. *
  1333. * Please note that this will overwrite existing cell styles for cells in range!
  1334. *
  1335. * @deprecated
  1336. * @param PHPExcel_Style $pSharedCellStyle Cell style to share
  1337. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  1338. * @throws PHPExcel_Exception
  1339. * @return PHPExcel_Worksheet
  1340. */
  1341. public function setSharedStyle(PHPExcel_Style $pSharedCellStyle = null, $pRange = '')
  1342. {
  1343. $this->duplicateStyle($pSharedCellStyle, $pRange);
  1344. return $this;
  1345. }
  1346. /**
  1347. * Duplicate cell style to a range of cells
  1348. *
  1349. * Please note that this will overwrite existing cell styles for cells in range!
  1350. *
  1351. * @param PHPExcel_Style $pCellStyle Cell style to duplicate
  1352. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  1353. * @throws PHPExcel_Exception
  1354. * @return PHPExcel_Worksheet
  1355. */
  1356. public function duplicateStyle(PHPExcel_Style $pCellStyle = null, $pRange = '')
  1357. {
  1358. // make sure we have a real style and not supervisor
  1359. $style = $pCellStyle->getIsSupervisor() ? $pCellStyle->getSharedComponent() : $pCellStyle;
  1360. // Add the style to the workbook if necessary
  1361. $workbook = $this->_parent;
  1362. if ($existingStyle = $this->_parent->getCellXfByHashCode($pCellStyle->getHashCode())) {
  1363. // there is already such cell Xf in our collection
  1364. $xfIndex = $existingStyle->getIndex();
  1365. } else {
  1366. // we don't have such a cell Xf, need to add
  1367. $workbook->addCellXf($pCellStyle);
  1368. $xfIndex = $pCellStyle->getIndex();
  1369. }
  1370. // Calculate range outer borders
  1371. list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange . ':' . $pRange);
  1372. // Make sure we can loop upwards on rows and columns
  1373. if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
  1374. $tmp = $rangeStart;
  1375. $rangeStart = $rangeEnd;
  1376. $rangeEnd = $tmp;
  1377. }
  1378. // Loop through cells and apply styles
  1379. for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
  1380. for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
  1381. $this->getCell(PHPExcel_Cell::stringFromColumnIndex($col - 1) . $row)->setXfIndex($xfIndex);
  1382. }
  1383. }
  1384. return $this;
  1385. }
  1386. /**
  1387. * Duplicate conditional style to a range of cells
  1388. *
  1389. * Please note that this will overwrite existing cell styles for cells in range!
  1390. *
  1391. * @param array of PHPExcel_Style_Conditional $pCellStyle Cell style to duplicate
  1392. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  1393. * @throws PHPExcel_Exception
  1394. * @return PHPExcel_Worksheet
  1395. */
  1396. public function duplicateConditionalStyle(array $pCellStyle = null, $pRange = '')
  1397. {
  1398. foreach($pCellStyle as $cellStyle) {
  1399. if (!($cellStyle instanceof PHPExcel_Style_Conditional)) {
  1400. throw new PHPExcel_Exception('Style is not a conditional style');
  1401. }
  1402. }
  1403. // Calculate range outer borders
  1404. list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange . ':' . $pRange);
  1405. // Make sure we can loop upwards on rows and columns
  1406. if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
  1407. $tmp = $rangeStart;
  1408. $rangeStart = $rangeEnd;
  1409. $rangeEnd = $tmp;
  1410. }
  1411. // Loop through cells and apply styles
  1412. for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
  1413. for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
  1414. $this->setConditionalStyles(PHPExcel_Cell::stringFromColumnIndex($col - 1) . $row, $pCellStyle);
  1415. }
  1416. }
  1417. return $this;
  1418. }
  1419. /**
  1420. * Duplicate cell style array to a range of cells
  1421. *
  1422. * Please note that this will overwrite existing cell styles for cells in range,
  1423. * if they are in the styles array. For example, if you decide to set a range of
  1424. * cells to font bold, only include font bold in the styles array.
  1425. *
  1426. * @deprecated
  1427. * @param array $pStyles Array containing style information
  1428. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  1429. * @param boolean $pAdvanced Advanced mode for setting borders.
  1430. * @throws PHPExcel_Exception
  1431. * @return PHPExcel_Worksheet
  1432. */
  1433. public function duplicateStyleArray($pStyles = null, $pRange = '', $pAdvanced = true)
  1434. {
  1435. $this->getStyle($pRange)->applyFromArray($pStyles, $pAdvanced);
  1436. return $this;
  1437. }
  1438. /**
  1439. * Set break on a cell
  1440. *
  1441. * @param string $pCell Cell coordinate (e.g. A1)
  1442. * @param int $pBreak Break type (type of PHPExcel_Worksheet::BREAK_*)
  1443. * @throws PHPExcel_Exception
  1444. * @return PHPExcel_Worksheet
  1445. */
  1446. public function setBreak($pCell = 'A1', $pBreak = PHPExcel_Worksheet::BREAK_NONE)
  1447. {
  1448. // Uppercase coordinate
  1449. $pCell = strtoupper($pCell);
  1450. if ($pCell != '') {
  1451. if ($pBreak == PHPExcel_Worksheet::BREAK_NONE) {
  1452. if (isset($this->_breaks[$pCell])) {
  1453. unset($this->_breaks[$pCell]);
  1454. }
  1455. } else {
  1456. $this->_breaks[$pCell] = $pBreak;
  1457. }
  1458. } else {
  1459. throw new PHPExcel_Exception('No cell coordinate specified.');
  1460. }
  1461. return $this;
  1462. }
  1463. /**
  1464. * Set break on a cell by using numeric cell coordinates
  1465. *
  1466. * @param integer $pColumn Numeric column coordinate of the cell
  1467. * @param integer $pRow Numeric row coordinate of the cell
  1468. * @param integer $pBreak Break type (type of PHPExcel_Worksheet::BREAK_*)
  1469. * @return PHPExcel_Worksheet
  1470. */
  1471. public function setBreakByColumnAndRow($pColumn = 0, $pRow = 1, $pBreak = PHPExcel_Worksheet::BREAK_NONE)
  1472. {
  1473. return $this->setBreak(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow, $pBreak);
  1474. }
  1475. /**
  1476. * Get breaks
  1477. *
  1478. * @return array[]
  1479. */
  1480. public function getBreaks()
  1481. {
  1482. return $this->_breaks;
  1483. }
  1484. /**
  1485. * Set merge on a cell range
  1486. *
  1487. * @param string $pRange Cell range (e.g. A1:E1)
  1488. * @throws PHPExcel_Exception
  1489. * @return PHPExcel_Worksheet
  1490. */
  1491. public function mergeCells($pRange = 'A1:A1')
  1492. {
  1493. // Uppercase coordinate
  1494. $pRange = strtoupper($pRange);
  1495. if (strpos($pRange,':') !== false) {
  1496. $this->_mergeCells[$pRange] = $pRange;
  1497. // make sure cells are created
  1498. // get the cells in the range
  1499. $aReferences = PHPExcel_Cell::extractAllCellReferencesInRange($pRange);
  1500. // create upper left cell if it does not already exist
  1501. $upperLeft = $aReferences[0];
  1502. if (!$this->cellExists($upperLeft)) {
  1503. $this->getCell($upperLeft)->setValueExplicit(null, PHPExcel_Cell_DataType::TYPE_NULL);
  1504. }
  1505. // create or blank out the rest of the cells in the range
  1506. $count = count($aReferences);
  1507. for ($i = 1; $i < $count; $i++) {
  1508. $this->getCell($aReferences[$i])->setValueExplicit(null, PHPExcel_Cell_DataType::TYPE_NULL);
  1509. }
  1510. } else {
  1511. throw new PHPExcel_Exception('Merge must be set on a range of cells.');
  1512. }
  1513. return $this;
  1514. }
  1515. /**
  1516. * Set merge on a cell range by using numeric cell coordinates
  1517. *
  1518. * @param int $pColumn1 Numeric column coordinate of the first cell
  1519. * @param int $pRow1 Numeric row coordinate of the first cell
  1520. * @param int $pColumn2 Numeric column coordinate of the last cell
  1521. * @param int $pRow2 Numeric row coordinate of the last cell
  1522. * @throws PHPExcel_Exception
  1523. * @return PHPExcel_Worksheet
  1524. */
  1525. public function mergeCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1)
  1526. {
  1527. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1528. return $this->mergeCells($cellRange);
  1529. }
  1530. /**
  1531. * Remove merge on a cell range
  1532. *
  1533. * @param string $pRange Cell range (e.g. A1:E1)
  1534. * @throws PHPExcel_Exception
  1535. * @return PHPExcel_Worksheet
  1536. */
  1537. public function unmergeCells($pRange = 'A1:A1')
  1538. {
  1539. // Uppercase coordinate
  1540. $pRange = strtoupper($pRange);
  1541. if (strpos($pRange,':') !== false) {
  1542. if (isset($this->_mergeCells[$pRange])) {
  1543. unset($this->_mergeCells[$pRange]);
  1544. } else {
  1545. throw new PHPExcel_Exception('Cell range ' . $pRange . ' not known as merged.');
  1546. }
  1547. } else {
  1548. throw new PHPExcel_Exception('Merge can only be removed from a range of cells.');
  1549. }
  1550. return $this;
  1551. }
  1552. /**
  1553. * Remove merge on a cell range by using numeric cell coordinates
  1554. *
  1555. * @param int $pColumn1 Numeric column coordinate of the first cell
  1556. * @param int $pRow1 Numeric row coordinate of the first cell
  1557. * @param int $pColumn2 Numeric column coordinate of the last cell
  1558. * @param int $pRow2 Numeric row coordinate of the last cell
  1559. * @throws PHPExcel_Exception
  1560. * @return PHPExcel_Worksheet
  1561. */
  1562. public function unmergeCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1)
  1563. {
  1564. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1565. return $this->unmergeCells($cellRange);
  1566. }
  1567. /**
  1568. * Get merge cells array.
  1569. *
  1570. * @return array[]
  1571. */
  1572. public function getMergeCells()
  1573. {
  1574. return $this->_mergeCells;
  1575. }
  1576. /**
  1577. * Set merge cells array for the entire sheet. Use instead mergeCells() to merge
  1578. * a single cell range.
  1579. *
  1580. * @param array
  1581. */
  1582. public function setMergeCells($pValue = array())
  1583. {
  1584. $this->_mergeCells = $pValue;
  1585. return $this;
  1586. }
  1587. /**
  1588. * Set protection on a cell range
  1589. *
  1590. * @param string $pRange Cell (e.g. A1) or cell range (e.g. A1:E1)
  1591. * @param string $pPassword Password to unlock the protection
  1592. * @param boolean $pAlreadyHashed If the password has already been hashed, set this to true
  1593. * @throws PHPExcel_Exception
  1594. * @return PHPExcel_Worksheet
  1595. */
  1596. public function protectCells($pRange = 'A1', $pPassword = '', $pAlreadyHashed = false)
  1597. {
  1598. // Uppercase coordinate
  1599. $pRange = strtoupper($pRange);
  1600. if (!$pAlreadyHashed) {
  1601. $pPassword = PHPExcel_Shared_PasswordHasher::hashPassword($pPassword);
  1602. }
  1603. $this->_protectedCells[$pRange] = $pPassword;
  1604. return $this;
  1605. }
  1606. /**
  1607. * Set protection on a cell range by using numeric cell coordinates
  1608. *
  1609. * @param int $pColumn1 Numeric column coordinate of the first cell
  1610. * @param int $pRow1 Numeric row coordinate of the first cell
  1611. * @param int $pColumn2 Numeric column coordinate of the last cell
  1612. * @param int $pRow2 Numeric row coordinate of the last cell
  1613. * @param string $pPassword Password to unlock the protection
  1614. * @param boolean $pAlreadyHashed If the password has already been hashed, set this to true
  1615. * @throws PHPExcel_Exception
  1616. * @return PHPExcel_Worksheet
  1617. */
  1618. public function protectCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1, $pPassword = '', $pAlreadyHashed = false)
  1619. {
  1620. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1621. return $this->protectCells($cellRange, $pPassword, $pAlreadyHashed);
  1622. }
  1623. /**
  1624. * Remove protection on a cell range
  1625. *
  1626. * @param string $pRange Cell (e.g. A1) or cell range (e.g. A1:E1)
  1627. * @throws PHPExcel_Exception
  1628. * @return PHPExcel_Worksheet
  1629. */
  1630. public function unprotectCells($pRange = 'A1')
  1631. {
  1632. // Uppercase coordinate
  1633. $pRange = strtoupper($pRange);
  1634. if (isset($this->_protectedCells[$pRange])) {
  1635. unset($this->_protectedCells[$pRange]);
  1636. } else {
  1637. throw new PHPExcel_Exception('Cell range ' . $pRange . ' not known as protected.');
  1638. }
  1639. return $this;
  1640. }
  1641. /**
  1642. * Remove protection on a cell range by using numeric cell coordinates
  1643. *
  1644. * @param int $pColumn1 Numeric column coordinate of the first cell
  1645. * @param int $pRow1 Numeric row coordinate of the first cell
  1646. * @param int $pColumn2 Numeric column coordinate of the last cell
  1647. * @param int $pRow2 Numeric row coordinate of the last cell
  1648. * @param string $pPassword Password to unlock the protection
  1649. * @param boolean $pAlreadyHashed If the password has already been hashed, set this to true
  1650. * @throws PHPExcel_Exception
  1651. * @return PHPExcel_Worksheet
  1652. */
  1653. public function unprotectCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1, $pPassword = '', $pAlreadyHashed = false)
  1654. {
  1655. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1656. return $this->unprotectCells($cellRange, $pPassword, $pAlreadyHashed);
  1657. }
  1658. /**
  1659. * Get protected cells
  1660. *
  1661. * @return array[]
  1662. */
  1663. public function getProtectedCells()
  1664. {
  1665. return $this->_protectedCells;
  1666. }
  1667. /**
  1668. * Get Autofilter
  1669. *
  1670. * @return PHPExcel_Worksheet_AutoFilter
  1671. */
  1672. public function getAutoFilter()
  1673. {
  1674. return $this->_autoFilter;
  1675. }
  1676. /**
  1677. * Set AutoFilter
  1678. *
  1679. * @param PHPExcel_Worksheet_AutoFilter|string $pValue
  1680. * A simple string containing a Cell range like 'A1:E10' is permitted for backward compatibility
  1681. * @throws PHPExcel_Exception
  1682. * @return PHPExcel_Worksheet
  1683. */
  1684. public function setAutoFilter($pValue)
  1685. {
  1686. $pRange = strtoupper($pValue);
  1687. if (is_string($pValue)) {
  1688. $this->_autoFilter->setRange($pValue);
  1689. } elseif(is_object($pValue) && ($pValue instanceof PHPExcel_Worksheet_AutoFilter)) {
  1690. $this->_autoFilter = $pValue;
  1691. }
  1692. return $this;
  1693. }
  1694. /**
  1695. * Set Autofilter Range by using numeric cell coordinates
  1696. *
  1697. * @param integer $pColumn1 Numeric column coordinate of the first cell
  1698. * @param integer $pRow1 Numeric row coordinate of the first cell
  1699. * @param integer $pColumn2 Numeric column coordinate of the second cell
  1700. * @param integer $pRow2 Numeric row coordinate of the second cell
  1701. * @throws PHPExcel_Exception
  1702. * @return PHPExcel_Worksheet
  1703. */
  1704. public function setAutoFilterByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1)
  1705. {
  1706. return $this->setAutoFilter(
  1707. PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1
  1708. . ':' .
  1709. PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2
  1710. );
  1711. }
  1712. /**
  1713. * Remove autofilter
  1714. *
  1715. * @return PHPExcel_Worksheet
  1716. */
  1717. public function removeAutoFilter()
  1718. {
  1719. $this->_autoFilter->setRange(NULL);
  1720. return $this;
  1721. }
  1722. /**
  1723. * Get Freeze Pane
  1724. *
  1725. * @return string
  1726. */
  1727. public function getFreezePane()
  1728. {
  1729. return $this->_freezePane;
  1730. }
  1731. /**
  1732. * Freeze Pane
  1733. *
  1734. * @param string $pCell Cell (i.e. A2)
  1735. * Examples:
  1736. * A2 will freeze the rows above cell A2 (i.e row 1)
  1737. * B1 will freeze the columns to the left of cell B1 (i.e column A)
  1738. * B2 will freeze the rows above and to the left of cell A2
  1739. * (i.e row 1 and column A)
  1740. * @throws PHPExcel_Exception
  1741. * @return PHPExcel_Worksheet
  1742. */
  1743. public function freezePane($pCell = '')
  1744. {
  1745. // Uppercase coordinate
  1746. $pCell = strtoupper($pCell);
  1747. if (strpos($pCell,':') === false && strpos($pCell,',') === false) {
  1748. $this->_freezePane = $pCell;
  1749. } else {
  1750. throw new PHPExcel_Exception('Freeze pane can not be set on a range of cells.');
  1751. }
  1752. return $this;
  1753. }
  1754. /**
  1755. * Freeze Pane by using numeric cell coordinates
  1756. *
  1757. * @param int $pColumn Numeric column coordinate of the cell
  1758. * @param int $pRow Numeric row coordinate of the cell
  1759. * @throws PHPExcel_Exception
  1760. * @return PHPExcel_Worksheet
  1761. */
  1762. public function freezePaneByColumnAndRow($pColumn = 0, $pRow = 1)
  1763. {
  1764. return $this->freezePane(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  1765. }
  1766. /**
  1767. * Unfreeze Pane
  1768. *
  1769. * @return PHPExcel_Worksheet
  1770. */
  1771. public function unfreezePane()
  1772. {
  1773. return $this->freezePane('');
  1774. }
  1775. /**
  1776. * Insert a new row, updating all possible related data
  1777. *
  1778. * @param int $pBefore Insert before this one
  1779. * @param int $pNumRows Number of rows to insert
  1780. * @throws PHPExcel_Exception
  1781. * @return PHPExcel_Worksheet
  1782. */
  1783. public function insertNewRowBefore($pBefore = 1, $pNumRows = 1) {
  1784. if ($pBefore >= 1) {
  1785. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1786. $objReferenceHelper->insertNewBefore('A' . $pBefore, 0, $pNumRows, $this);
  1787. } else {
  1788. throw new PHPExcel_Exception("Rows can only be inserted before at least row 1.");
  1789. }
  1790. return $this;
  1791. }
  1792. /**
  1793. * Insert a new column, updating all possible related data
  1794. *
  1795. * @param int $pBefore Insert before this one
  1796. * @param int $pNumCols Number of columns to insert
  1797. * @throws PHPExcel_Exception
  1798. * @return PHPExcel_Worksheet
  1799. */
  1800. public function insertNewColumnBefore($pBefore = 'A', $pNumCols = 1) {
  1801. if (!is_numeric($pBefore)) {
  1802. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1803. $objReferenceHelper->insertNewBefore($pBefore . '1', $pNumCols, 0, $this);
  1804. } else {
  1805. throw new PHPExcel_Exception("Column references should not be numeric.");
  1806. }
  1807. return $this;
  1808. }
  1809. /**
  1810. * Insert a new column, updating all possible related data
  1811. *
  1812. * @param int $pBefore Insert before this one (numeric column coordinate of the cell)
  1813. * @param int $pNumCols Number of columns to insert
  1814. * @throws PHPExcel_Exception
  1815. * @return PHPExcel_Worksheet
  1816. */
  1817. public function insertNewColumnBeforeByIndex($pBefore = 0, $pNumCols = 1) {
  1818. if ($pBefore >= 0) {
  1819. return $this->insertNewColumnBefore(PHPExcel_Cell::stringFromColumnIndex($pBefore), $pNumCols);
  1820. } else {
  1821. throw new PHPExcel_Exception("Columns can only be inserted before at least column A (0).");
  1822. }
  1823. }
  1824. /**
  1825. * Delete a row, updating all possible related data
  1826. *
  1827. * @param int $pRow Remove starting with this one
  1828. * @param int $pNumRows Number of rows to remove
  1829. * @throws PHPExcel_Exception
  1830. * @return PHPExcel_Worksheet
  1831. */
  1832. public function removeRow($pRow = 1, $pNumRows = 1) {
  1833. if ($pRow >= 1) {
  1834. $highestRow = $this->getHighestDataRow();
  1835. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1836. $objReferenceHelper->insertNewBefore('A' . ($pRow + $pNumRows), 0, -$pNumRows, $this);
  1837. for($r = 0; $r < $pNumRows; ++$r) {
  1838. $this->getCellCacheController()->removeRow($highestRow);
  1839. --$highestRow;
  1840. }
  1841. } else {
  1842. throw new PHPExcel_Exception("Rows to be deleted should at least start from row 1.");
  1843. }
  1844. return $this;
  1845. }
  1846. /**
  1847. * Remove a column, updating all possible related data
  1848. *
  1849. * @param string $pColumn Remove starting with this one
  1850. * @param int $pNumCols Number of columns to remove
  1851. * @throws PHPExcel_Exception
  1852. * @return PHPExcel_Worksheet
  1853. */
  1854. public function removeColumn($pColumn = 'A', $pNumCols = 1) {
  1855. if (!is_numeric($pColumn)) {
  1856. $highestColumn = $this->getHighestDataColumn();
  1857. $pColumn = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($pColumn) - 1 + $pNumCols);
  1858. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1859. $objReferenceHelper->insertNewBefore($pColumn . '1', -$pNumCols, 0, $this);
  1860. for($c = 0; $c < $pNumCols; ++$c) {
  1861. $this->getCellCacheController()->removeColumn($highestColumn);
  1862. $highestColumn = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($highestColumn) - 2);
  1863. }
  1864. } else {
  1865. throw new PHPExcel_Exception("Column references should not be numeric.");
  1866. }
  1867. return $this;
  1868. }
  1869. /**
  1870. * Remove a column, updating all possible related data
  1871. *
  1872. * @param int $pColumn Remove starting with this one (numeric column coordinate of the cell)
  1873. * @param int $pNumCols Number of columns to remove
  1874. * @throws PHPExcel_Exception
  1875. * @return PHPExcel_Worksheet
  1876. */
  1877. public function removeColumnByIndex($pColumn = 0, $pNumCols = 1) {
  1878. if ($pColumn >= 0) {
  1879. return $this->removeColumn(PHPExcel_Cell::stringFromColumnIndex($pColumn), $pNumCols);
  1880. } else {
  1881. throw new PHPExcel_Exception("Columns to be deleted should at least start from column 0");
  1882. }
  1883. }
  1884. /**
  1885. * Show gridlines?
  1886. *
  1887. * @return boolean
  1888. */
  1889. public function getShowGridlines() {
  1890. return $this->_showGridlines;
  1891. }
  1892. /**
  1893. * Set show gridlines
  1894. *
  1895. * @param boolean $pValue Show gridlines (true/false)
  1896. * @return PHPExcel_Worksheet
  1897. */
  1898. public function setShowGridlines($pValue = false) {
  1899. $this->_showGridlines = $pValue;
  1900. return $this;
  1901. }
  1902. /**
  1903. * Print gridlines?
  1904. *
  1905. * @return boolean
  1906. */
  1907. public function getPrintGridlines() {
  1908. return $this->_printGridlines;
  1909. }
  1910. /**
  1911. * Set print gridlines
  1912. *
  1913. * @param boolean $pValue Print gridlines (true/false)
  1914. * @return PHPExcel_Worksheet
  1915. */
  1916. public function setPrintGridlines($pValue = false) {
  1917. $this->_printGridlines = $pValue;
  1918. return $this;
  1919. }
  1920. /**
  1921. * Show row and column headers?
  1922. *
  1923. * @return boolean
  1924. */
  1925. public function getShowRowColHeaders() {
  1926. return $this->_showRowColHeaders;
  1927. }
  1928. /**
  1929. * Set show row and column headers
  1930. *
  1931. * @param boolean $pValue Show row and column headers (true/false)
  1932. * @return PHPExcel_Worksheet
  1933. */
  1934. public function setShowRowColHeaders($pValue = false) {
  1935. $this->_showRowColHeaders = $pValue;
  1936. return $this;
  1937. }
  1938. /**
  1939. * Show summary below? (Row/Column outlining)
  1940. *
  1941. * @return boolean
  1942. */
  1943. public function getShowSummaryBelow() {
  1944. return $this->_showSummaryBelow;
  1945. }
  1946. /**
  1947. * Set show summary below
  1948. *
  1949. * @param boolean $pValue Show summary below (true/false)
  1950. * @return PHPExcel_Worksheet
  1951. */
  1952. public function setShowSummaryBelow($pValue = true) {
  1953. $this->_showSummaryBelow = $pValue;
  1954. return $this;
  1955. }
  1956. /**
  1957. * Show summary right? (Row/Column outlining)
  1958. *
  1959. * @return boolean
  1960. */
  1961. public function getShowSummaryRight() {
  1962. return $this->_showSummaryRight;
  1963. }
  1964. /**
  1965. * Set show summary right
  1966. *
  1967. * @param boolean $pValue Show summary right (true/false)
  1968. * @return PHPExcel_Worksheet
  1969. */
  1970. public function setShowSummaryRight($pValue = true) {
  1971. $this->_showSummaryRight = $pValue;
  1972. return $this;
  1973. }
  1974. /**
  1975. * Get comments
  1976. *
  1977. * @return PHPExcel_Comment[]
  1978. */
  1979. public function getComments()
  1980. {
  1981. return $this->_comments;
  1982. }
  1983. /**
  1984. * Set comments array for the entire sheet.
  1985. *
  1986. * @param array of PHPExcel_Comment
  1987. * @return PHPExcel_Worksheet
  1988. */
  1989. public function setComments($pValue = array())
  1990. {
  1991. $this->_comments = $pValue;
  1992. return $this;
  1993. }
  1994. /**
  1995. * Get comment for cell
  1996. *
  1997. * @param string $pCellCoordinate Cell coordinate to get comment for
  1998. * @return PHPExcel_Comment
  1999. * @throws PHPExcel_Exception
  2000. */
  2001. public function getComment($pCellCoordinate = 'A1')
  2002. {
  2003. // Uppercase coordinate
  2004. $pCellCoordinate = strtoupper($pCellCoordinate);
  2005. if (strpos($pCellCoordinate,':') !== false || strpos($pCellCoordinate,',') !== false) {
  2006. throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells.');
  2007. } else if (strpos($pCellCoordinate,'$') !== false) {
  2008. throw new PHPExcel_Exception('Cell coordinate string must not be absolute.');
  2009. } else if ($pCellCoordinate == '') {
  2010. throw new PHPExcel_Exception('Cell coordinate can not be zero-length string.');
  2011. } else {
  2012. // Check if we already have a comment for this cell.
  2013. // If not, create a new comment.
  2014. if (isset($this->_comments[$pCellCoordinate])) {
  2015. return $this->_comments[$pCellCoordinate];
  2016. } else {
  2017. $newComment = new PHPExcel_Comment();
  2018. $this->_comments[$pCellCoordinate] = $newComment;
  2019. return $newComment;
  2020. }
  2021. }
  2022. }
  2023. /**
  2024. * Get comment for cell by using numeric cell coordinates
  2025. *
  2026. * @param int $pColumn Numeric column coordinate of the cell
  2027. * @param int $pRow Numeric row coordinate of the cell
  2028. * @return PHPExcel_Comment
  2029. */
  2030. public function getCommentByColumnAndRow($pColumn = 0, $pRow = 1)
  2031. {
  2032. return $this->getComment(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  2033. }
  2034. /**
  2035. * Get selected cell
  2036. *
  2037. * @deprecated
  2038. * @return string
  2039. */
  2040. public function getSelectedCell()
  2041. {
  2042. return $this->getSelectedCells();
  2043. }
  2044. /**
  2045. * Get active cell
  2046. *
  2047. * @return string Example: 'A1'
  2048. */
  2049. public function getActiveCell()
  2050. {
  2051. return $this->_activeCell;
  2052. }
  2053. /**
  2054. * Get selected cells
  2055. *
  2056. * @return string
  2057. */
  2058. public function getSelectedCells()
  2059. {
  2060. return $this->_selectedCells;
  2061. }
  2062. /**
  2063. * Selected cell
  2064. *
  2065. * @param string $pCoordinate Cell (i.e. A1)
  2066. * @return PHPExcel_Worksheet
  2067. */
  2068. public function setSelectedCell($pCoordinate = 'A1')
  2069. {
  2070. return $this->setSelectedCells($pCoordinate);
  2071. }
  2072. /**
  2073. * Select a range of cells.
  2074. *
  2075. * @param string $pCoordinate Cell range, examples: 'A1', 'B2:G5', 'A:C', '3:6'
  2076. * @throws PHPExcel_Exception
  2077. * @return PHPExcel_Worksheet
  2078. */
  2079. public function setSelectedCells($pCoordinate = 'A1')
  2080. {
  2081. // Uppercase coordinate
  2082. $pCoordinate = strtoupper($pCoordinate);
  2083. // Convert 'A' to 'A:A'
  2084. $pCoordinate = preg_replace('/^([A-Z]+)$/', '${1}:${1}', $pCoordinate);
  2085. // Convert '1' to '1:1'
  2086. $pCoordinate = preg_replace('/^([0-9]+)$/', '${1}:${1}', $pCoordinate);
  2087. // Convert 'A:C' to 'A1:C1048576'
  2088. $pCoordinate = preg_replace('/^([A-Z]+):([A-Z]+)$/', '${1}1:${2}1048576', $pCoordinate);
  2089. // Convert '1:3' to 'A1:XFD3'
  2090. $pCoordinate = preg_replace('/^([0-9]+):([0-9]+)$/', 'A${1}:XFD${2}', $pCoordinate);
  2091. if (strpos($pCoordinate,':') !== false || strpos($pCoordinate,',') !== false) {
  2092. list($first, ) = PHPExcel_Cell::splitRange($pCoordinate);
  2093. $this->_activeCell = $first[0];
  2094. } else {
  2095. $this->_activeCell = $pCoordinate;
  2096. }
  2097. $this->_selectedCells = $pCoordinate;
  2098. return $this;
  2099. }
  2100. /**
  2101. * Selected cell by using numeric cell coordinates
  2102. *
  2103. * @param int $pColumn Numeric column coordinate of the cell
  2104. * @param int $pRow Numeric row coordinate of the cell
  2105. * @throws PHPExcel_Exception
  2106. * @return PHPExcel_Worksheet
  2107. */
  2108. public function setSelectedCellByColumnAndRow($pColumn = 0, $pRow = 1)
  2109. {
  2110. return $this->setSelectedCells(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  2111. }
  2112. /**
  2113. * Get right-to-left
  2114. *
  2115. * @return boolean
  2116. */
  2117. public function getRightToLeft() {
  2118. return $this->_rightToLeft;
  2119. }
  2120. /**
  2121. * Set right-to-left
  2122. *
  2123. * @param boolean $value Right-to-left true/false
  2124. * @return PHPExcel_Worksheet
  2125. */
  2126. public function setRightToLeft($value = false) {
  2127. $this->_rightToLeft = $value;
  2128. return $this;
  2129. }
  2130. /**
  2131. * Fill worksheet from values in array
  2132. *
  2133. * @param array $source Source array
  2134. * @param mixed $nullValue Value in source array that stands for blank cell
  2135. * @param string $startCell Insert array starting from this cell address as the top left coordinate
  2136. * @param boolean $strictNullComparison Apply strict comparison when testing for null values in the array
  2137. * @throws PHPExcel_Exception
  2138. * @return PHPExcel_Worksheet
  2139. */
  2140. public function fromArray($source = null, $nullValue = null, $startCell = 'A1', $strictNullComparison = false) {
  2141. if (is_array($source)) {
  2142. // Convert a 1-D array to 2-D (for ease of looping)
  2143. if (!is_array(end($source))) {
  2144. $source = array($source);
  2145. }
  2146. // start coordinate
  2147. list ($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($startCell);
  2148. // Loop through $source
  2149. foreach ($source as $rowData) {
  2150. $currentColumn = $startColumn;
  2151. foreach($rowData as $cellValue) {
  2152. if ($strictNullComparison) {
  2153. if ($cellValue !== $nullValue) {
  2154. // Set cell value
  2155. $this->getCell($currentColumn . $startRow)->setValue($cellValue);
  2156. }
  2157. } else {
  2158. if ($cellValue != $nullValue) {
  2159. // Set cell value
  2160. $this->getCell($currentColumn . $startRow)->setValue($cellValue);
  2161. }
  2162. }
  2163. ++$currentColumn;
  2164. }
  2165. ++$startRow;
  2166. }
  2167. } else {
  2168. throw new PHPExcel_Exception("Parameter \$source should be an array.");
  2169. }
  2170. return $this;
  2171. }
  2172. /**
  2173. * Create array from a range of cells
  2174. *
  2175. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  2176. * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist
  2177. * @param boolean $calculateFormulas Should formulas be calculated?
  2178. * @param boolean $formatData Should formatting be applied to cell values?
  2179. * @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero
  2180. * True - Return rows and columns indexed by their actual row and column IDs
  2181. * @return array
  2182. */
  2183. public function rangeToArray($pRange = 'A1', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) {
  2184. // Returnvalue
  2185. $returnValue = array();
  2186. // Identify the range that we need to extract from the worksheet
  2187. list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange);
  2188. $minCol = PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] -1);
  2189. $minRow = $rangeStart[1];
  2190. $maxCol = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] -1);
  2191. $maxRow = $rangeEnd[1];
  2192. $maxCol++;
  2193. // Loop through rows
  2194. $r = -1;
  2195. for ($row = $minRow; $row <= $maxRow; ++$row) {
  2196. $rRef = ($returnCellRef) ? $row : ++$r;
  2197. $c = -1;
  2198. // Loop through columns in the current row
  2199. for ($col = $minCol; $col != $maxCol; ++$col) {
  2200. $cRef = ($returnCellRef) ? $col : ++$c;
  2201. // Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen
  2202. // so we test and retrieve directly against _cellCollection
  2203. if ($this->_cellCollection->isDataSet($col.$row)) {
  2204. // Cell exists
  2205. $cell = $this->_cellCollection->getCacheData($col.$row);
  2206. if ($cell->getValue() !== null) {
  2207. if ($cell->getValue() instanceof PHPExcel_RichText) {
  2208. $returnValue[$rRef][$cRef] = $cell->getValue()->getPlainText();
  2209. } else {
  2210. if ($calculateFormulas) {
  2211. $returnValue[$rRef][$cRef] = $cell->getCalculatedValue();
  2212. } else {
  2213. $returnValue[$rRef][$cRef] = $cell->getValue();
  2214. }
  2215. }
  2216. if ($formatData) {
  2217. $style = $this->_parent->getCellXfByIndex($cell->getXfIndex());
  2218. $returnValue[$rRef][$cRef] = PHPExcel_Style_NumberFormat::toFormattedString(
  2219. $returnValue[$rRef][$cRef],
  2220. ($style && $style->getNumberFormat()) ?
  2221. $style->getNumberFormat()->getFormatCode() :
  2222. PHPExcel_Style_NumberFormat::FORMAT_GENERAL
  2223. );
  2224. }
  2225. } else {
  2226. // Cell holds a NULL
  2227. $returnValue[$rRef][$cRef] = $nullValue;
  2228. }
  2229. } else {
  2230. // Cell doesn't exist
  2231. $returnValue[$rRef][$cRef] = $nullValue;
  2232. }
  2233. }
  2234. }
  2235. // Return
  2236. return $returnValue;
  2237. }
  2238. /**
  2239. * Create array from a range of cells
  2240. *
  2241. * @param string $pNamedRange Name of the Named Range
  2242. * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist
  2243. * @param boolean $calculateFormulas Should formulas be calculated?
  2244. * @param boolean $formatData Should formatting be applied to cell values?
  2245. * @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero
  2246. * True - Return rows and columns indexed by their actual row and column IDs
  2247. * @return array
  2248. * @throws PHPExcel_Exception
  2249. */
  2250. public function namedRangeToArray($pNamedRange = '', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) {
  2251. $namedRange = PHPExcel_NamedRange::resolveRange($pNamedRange, $this);
  2252. if ($namedRange !== NULL) {
  2253. $pWorkSheet = $namedRange->getWorksheet();
  2254. $pCellRange = $namedRange->getRange();
  2255. return $pWorkSheet->rangeToArray( $pCellRange,
  2256. $nullValue, $calculateFormulas, $formatData, $returnCellRef);
  2257. }
  2258. throw new PHPExcel_Exception('Named Range '.$pNamedRange.' does not exist.');
  2259. }
  2260. /**
  2261. * Create array from worksheet
  2262. *
  2263. * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist
  2264. * @param boolean $calculateFormulas Should formulas be calculated?
  2265. * @param boolean $formatData Should formatting be applied to cell values?
  2266. * @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero
  2267. * True - Return rows and columns indexed by their actual row and column IDs
  2268. * @return array
  2269. */
  2270. public function toArray($nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) {
  2271. // Garbage collect...
  2272. $this->garbageCollect();
  2273. // Identify the range that we need to extract from the worksheet
  2274. $maxCol = $this->getHighestColumn();
  2275. $maxRow = $this->getHighestRow();
  2276. // Return
  2277. return $this->rangeToArray( 'A1:'.$maxCol.$maxRow,
  2278. $nullValue, $calculateFormulas, $formatData, $returnCellRef);
  2279. }
  2280. /**
  2281. * Get row iterator
  2282. *
  2283. * @param integer $startRow The row number at which to start iterating
  2284. * @param integer $endRow The row number at which to stop iterating
  2285. *
  2286. * @return PHPExcel_Worksheet_RowIterator
  2287. */
  2288. public function getRowIterator($startRow = 1, $endRow = null) {
  2289. return new PHPExcel_Worksheet_RowIterator($this, $startRow, $endRow);
  2290. }
  2291. /**
  2292. * Get column iterator
  2293. *
  2294. * @param string $startColumn The column address at which to start iterating
  2295. * @param string $endColumn The column address at which to stop iterating
  2296. *
  2297. * @return PHPExcel_Worksheet_ColumnIterator
  2298. */
  2299. public function getColumnIterator($startColumn = 'A', $endColumn = null) {
  2300. return new PHPExcel_Worksheet_ColumnIterator($this, $startColumn, $endColumn);
  2301. }
  2302. /**
  2303. * Run PHPExcel garabage collector.
  2304. *
  2305. * @return PHPExcel_Worksheet
  2306. */
  2307. public function garbageCollect() {
  2308. // Flush cache
  2309. $this->_cellCollection->getCacheData('A1');
  2310. // Build a reference table from images
  2311. // $imageCoordinates = array();
  2312. // $iterator = $this->getDrawingCollection()->getIterator();
  2313. // while ($iterator->valid()) {
  2314. // $imageCoordinates[$iterator->current()->getCoordinates()] = true;
  2315. //
  2316. // $iterator->next();
  2317. // }
  2318. //
  2319. // Lookup highest column and highest row if cells are cleaned
  2320. $colRow = $this->_cellCollection->getHighestRowAndColumn();
  2321. $highestRow = $colRow['row'];
  2322. $highestColumn = PHPExcel_Cell::columnIndexFromString($colRow['column']);
  2323. // Loop through column dimensions
  2324. foreach ($this->_columnDimensions as $dimension) {
  2325. $highestColumn = max($highestColumn,PHPExcel_Cell::columnIndexFromString($dimension->getColumnIndex()));
  2326. }
  2327. // Loop through row dimensions
  2328. foreach ($this->_rowDimensions as $dimension) {
  2329. $highestRow = max($highestRow,$dimension->getRowIndex());
  2330. }
  2331. // Cache values
  2332. if ($highestColumn < 0) {
  2333. $this->_cachedHighestColumn = 'A';
  2334. } else {
  2335. $this->_cachedHighestColumn = PHPExcel_Cell::stringFromColumnIndex(--$highestColumn);
  2336. }
  2337. $this->_cachedHighestRow = $highestRow;
  2338. // Return
  2339. return $this;
  2340. }
  2341. /**
  2342. * Get hash code
  2343. *
  2344. * @return string Hash code
  2345. */
  2346. public function getHashCode() {
  2347. if ($this->_dirty) {
  2348. $this->_hash = md5( $this->_title .
  2349. $this->_autoFilter .
  2350. ($this->_protection->isProtectionEnabled() ? 't' : 'f') .
  2351. __CLASS__
  2352. );
  2353. $this->_dirty = false;
  2354. }
  2355. return $this->_hash;
  2356. }
  2357. /**
  2358. * Extract worksheet title from range.
  2359. *
  2360. * Example: extractSheetTitle("testSheet!A1") ==> 'A1'
  2361. * Example: extractSheetTitle("'testSheet 1'!A1", true) ==> array('testSheet 1', 'A1');
  2362. *
  2363. * @param string $pRange Range to extract title from
  2364. * @param bool $returnRange Return range? (see example)
  2365. * @return mixed
  2366. */
  2367. public static function extractSheetTitle($pRange, $returnRange = false) {
  2368. // Sheet title included?
  2369. if (($sep = strpos($pRange, '!')) === false) {
  2370. return '';
  2371. }
  2372. if ($returnRange) {
  2373. return array( trim(substr($pRange, 0, $sep),"'"),
  2374. substr($pRange, $sep + 1)
  2375. );
  2376. }
  2377. return substr($pRange, $sep + 1);
  2378. }
  2379. /**
  2380. * Get hyperlink
  2381. *
  2382. * @param string $pCellCoordinate Cell coordinate to get hyperlink for
  2383. */
  2384. public function getHyperlink($pCellCoordinate = 'A1')
  2385. {
  2386. // return hyperlink if we already have one
  2387. if (isset($this->_hyperlinkCollection[$pCellCoordinate])) {
  2388. return $this->_hyperlinkCollection[$pCellCoordinate];
  2389. }
  2390. // else create hyperlink
  2391. $this->_hyperlinkCollection[$pCellCoordinate] = new PHPExcel_Cell_Hyperlink();
  2392. return $this->_hyperlinkCollection[$pCellCoordinate];
  2393. }
  2394. /**
  2395. * Set hyperlnk
  2396. *
  2397. * @param string $pCellCoordinate Cell coordinate to insert hyperlink
  2398. * @param PHPExcel_Cell_Hyperlink $pHyperlink
  2399. * @return PHPExcel_Worksheet
  2400. */
  2401. public function setHyperlink($pCellCoordinate = 'A1', PHPExcel_Cell_Hyperlink $pHyperlink = null)
  2402. {
  2403. if ($pHyperlink === null) {
  2404. unset($this->_hyperlinkCollection[$pCellCoordinate]);
  2405. } else {
  2406. $this->_hyperlinkCollection[$pCellCoordinate] = $pHyperlink;
  2407. }
  2408. return $this;
  2409. }
  2410. /**
  2411. * Hyperlink at a specific coordinate exists?
  2412. *
  2413. * @param string $pCoordinate
  2414. * @return boolean
  2415. */
  2416. public function hyperlinkExists($pCoordinate = 'A1')
  2417. {
  2418. return isset($this->_hyperlinkCollection[$pCoordinate]);
  2419. }
  2420. /**
  2421. * Get collection of hyperlinks
  2422. *
  2423. * @return PHPExcel_Cell_Hyperlink[]
  2424. */
  2425. public function getHyperlinkCollection()
  2426. {
  2427. return $this->_hyperlinkCollection;
  2428. }
  2429. /**
  2430. * Get data validation
  2431. *
  2432. * @param string $pCellCoordinate Cell coordinate to get data validation for
  2433. */
  2434. public function getDataValidation($pCellCoordinate = 'A1')
  2435. {
  2436. // return data validation if we already have one
  2437. if (isset($this->_dataValidationCollection[$pCellCoordinate])) {
  2438. return $this->_dataValidationCollection[$pCellCoordinate];
  2439. }
  2440. // else create data validation
  2441. $this->_dataValidationCollection[$pCellCoordinate] = new PHPExcel_Cell_DataValidation();
  2442. return $this->_dataValidationCollection[$pCellCoordinate];
  2443. }
  2444. /**
  2445. * Set data validation
  2446. *
  2447. * @param string $pCellCoordinate Cell coordinate to insert data validation
  2448. * @param PHPExcel_Cell_DataValidation $pDataValidation
  2449. * @return PHPExcel_Worksheet
  2450. */
  2451. public function setDataValidation($pCellCoordinate = 'A1', PHPExcel_Cell_DataValidation $pDataValidation = null)
  2452. {
  2453. if ($pDataValidation === null) {
  2454. unset($this->_dataValidationCollection[$pCellCoordinate]);
  2455. } else {
  2456. $this->_dataValidationCollection[$pCellCoordinate] = $pDataValidation;
  2457. }
  2458. return $this;
  2459. }
  2460. /**
  2461. * Data validation at a specific coordinate exists?
  2462. *
  2463. * @param string $pCoordinate
  2464. * @return boolean
  2465. */
  2466. public function dataValidationExists($pCoordinate = 'A1')
  2467. {
  2468. return isset($this->_dataValidationCollection[$pCoordinate]);
  2469. }
  2470. /**
  2471. * Get collection of data validations
  2472. *
  2473. * @return PHPExcel_Cell_DataValidation[]
  2474. */
  2475. public function getDataValidationCollection()
  2476. {
  2477. return $this->_dataValidationCollection;
  2478. }
  2479. /**
  2480. * Accepts a range, returning it as a range that falls within the current highest row and column of the worksheet
  2481. *
  2482. * @param string $range
  2483. * @return string Adjusted range value
  2484. */
  2485. public function shrinkRangeToFit($range) {
  2486. $maxCol = $this->getHighestColumn();
  2487. $maxRow = $this->getHighestRow();
  2488. $maxCol = PHPExcel_Cell::columnIndexFromString($maxCol);
  2489. $rangeBlocks = explode(' ',$range);
  2490. foreach ($rangeBlocks as &$rangeSet) {
  2491. $rangeBoundaries = PHPExcel_Cell::getRangeBoundaries($rangeSet);
  2492. if (PHPExcel_Cell::columnIndexFromString($rangeBoundaries[0][0]) > $maxCol) { $rangeBoundaries[0][0] = PHPExcel_Cell::stringFromColumnIndex($maxCol); }
  2493. if ($rangeBoundaries[0][1] > $maxRow) { $rangeBoundaries[0][1] = $maxRow; }
  2494. if (PHPExcel_Cell::columnIndexFromString($rangeBoundaries[1][0]) > $maxCol) { $rangeBoundaries[1][0] = PHPExcel_Cell::stringFromColumnIndex($maxCol); }
  2495. if ($rangeBoundaries[1][1] > $maxRow) { $rangeBoundaries[1][1] = $maxRow; }
  2496. $rangeSet = $rangeBoundaries[0][0].$rangeBoundaries[0][1].':'.$rangeBoundaries[1][0].$rangeBoundaries[1][1];
  2497. }
  2498. unset($rangeSet);
  2499. $stRange = implode(' ',$rangeBlocks);
  2500. return $stRange;
  2501. }
  2502. /**
  2503. * Get tab color
  2504. *
  2505. * @return PHPExcel_Style_Color
  2506. */
  2507. public function getTabColor()
  2508. {
  2509. if ($this->_tabColor === NULL)
  2510. $this->_tabColor = new PHPExcel_Style_Color();
  2511. return $this->_tabColor;
  2512. }
  2513. /**
  2514. * Reset tab color
  2515. *
  2516. * @return PHPExcel_Worksheet
  2517. */
  2518. public function resetTabColor()
  2519. {
  2520. $this->_tabColor = null;
  2521. unset($this->_tabColor);
  2522. return $this;
  2523. }
  2524. /**
  2525. * Tab color set?
  2526. *
  2527. * @return boolean
  2528. */
  2529. public function isTabColorSet()
  2530. {
  2531. return ($this->_tabColor !== NULL);
  2532. }
  2533. /**
  2534. * Copy worksheet (!= clone!)
  2535. *
  2536. * @return PHPExcel_Worksheet
  2537. */
  2538. public function copy() {
  2539. $copied = clone $this;
  2540. return $copied;
  2541. }
  2542. /**
  2543. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  2544. */
  2545. public function __clone() {
  2546. foreach ($this as $key => $val) {
  2547. if ($key == '_parent') {
  2548. continue;
  2549. }
  2550. if (is_object($val) || (is_array($val))) {
  2551. if ($key == '_cellCollection') {
  2552. $newCollection = clone $this->_cellCollection;
  2553. $newCollection->copyCellCollection($this);
  2554. $this->_cellCollection = $newCollection;
  2555. } elseif ($key == '_drawingCollection') {
  2556. $newCollection = clone $this->_drawingCollection;
  2557. $this->_drawingCollection = $newCollection;
  2558. } elseif (($key == '_autoFilter') && ($this->_autoFilter instanceof PHPExcel_Worksheet_AutoFilter)) {
  2559. $newAutoFilter = clone $this->_autoFilter;
  2560. $this->_autoFilter = $newAutoFilter;
  2561. $this->_autoFilter->setParent($this);
  2562. } else {
  2563. $this->{$key} = unserialize(serialize($val));
  2564. }
  2565. }
  2566. }
  2567. }
  2568. /**
  2569. * Define the code name of the sheet
  2570. *
  2571. * @param null|string Same rule as Title minus space not allowed (but, like Excel, change silently space to underscore)
  2572. * @return objWorksheet
  2573. * @throws PHPExcel_Exception
  2574. */
  2575. public function setCodeName($pValue=null){
  2576. // Is this a 'rename' or not?
  2577. if ($this->getCodeName() == $pValue) {
  2578. return $this;
  2579. }
  2580. $pValue = str_replace(' ', '_', $pValue);//Excel does this automatically without flinching, we are doing the same
  2581. // Syntax check
  2582. // throw an exception if not valid
  2583. self::_checkSheetCodeName($pValue);
  2584. // We use the same code that setTitle to find a valid codeName else not using a space (Excel don't like) but a '_'
  2585. if ($this->getParent()) {
  2586. // Is there already such sheet name?
  2587. if ($this->getParent()->sheetCodeNameExists($pValue)) {
  2588. // Use name, but append with lowest possible integer
  2589. if (PHPExcel_Shared_String::CountCharacters($pValue) > 29) {
  2590. $pValue = PHPExcel_Shared_String::Substring($pValue,0,29);
  2591. }
  2592. $i = 1;
  2593. while ($this->getParent()->sheetCodeNameExists($pValue . '_' . $i)) {
  2594. ++$i;
  2595. if ($i == 10) {
  2596. if (PHPExcel_Shared_String::CountCharacters($pValue) > 28) {
  2597. $pValue = PHPExcel_Shared_String::Substring($pValue,0,28);
  2598. }
  2599. } elseif ($i == 100) {
  2600. if (PHPExcel_Shared_String::CountCharacters($pValue) > 27) {
  2601. $pValue = PHPExcel_Shared_String::Substring($pValue,0,27);
  2602. }
  2603. }
  2604. }
  2605. $pValue = $pValue . '_' . $i;// ok, we have a valid name
  2606. //codeName is'nt used in formula : no need to call for an update
  2607. //return $this->setTitle($altTitle,$updateFormulaCellReferences);
  2608. }
  2609. }
  2610. $this->_codeName=$pValue;
  2611. return $this;
  2612. }
  2613. /**
  2614. * Return the code name of the sheet
  2615. *
  2616. * @return null|string
  2617. */
  2618. public function getCodeName(){
  2619. return $this->_codeName;
  2620. }
  2621. /**
  2622. * Sheet has a code name ?
  2623. * @return boolean
  2624. */
  2625. public function hasCodeName(){
  2626. return !(is_null($this->_codeName));
  2627. }
  2628. }