Worksheet.php 86 KB

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