Worksheet.php 89 KB

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