c34cd9c98ce70098c6be7bcb22f9f783ac827333.svn-base 91 KB

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