Worksheet.php 94 KB

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