Worksheet.php 92 KB

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