PageRenderTime 98ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 2ms

/common/libraries/plugin/phpexcel/PHPExcel/Reader/Excel5.php

https://bitbucket.org/ywarnier/chamilo-dev
PHP | 8247 lines | 5256 code | 1111 blank | 1880 comment | 454 complexity | d4a0c02ad14d08ad33754fe3624e1cd6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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_Reader_Excel5
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. // Original file header of ParseXL (used as the base for this class):
  28. // --------------------------------------------------------------------------------
  29. // Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
  30. // trex005, and mmp11 (SourceForge.net)
  31. // http://sourceforge.net/projects/phpexcelreader/
  32. // Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
  33. // Modelled moreso after Perl Excel Parse/Write modules
  34. // Added Parse_Excel_Spreadsheet object
  35. // Reads a whole worksheet or tab as row,column array or as
  36. // associated hash of indexed rows and named column fields
  37. // Added variables for worksheet (tab) indexes and names
  38. // Added an object call for loading individual woorksheets
  39. // Changed default indexing defaults to 0 based arrays
  40. // Fixed date/time and percent formats
  41. // Includes patches found at SourceForge...
  42. // unicode patch by nobody
  43. // unpack("d") machine depedency patch by matchy
  44. // boundsheet utf16 patch by bjaenichen
  45. // Renamed functions for shorter names
  46. // General code cleanup and rigor, including <80 column width
  47. // Included a testcase Excel file and PHP example calls
  48. // Code works for PHP 5.x
  49. // Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
  50. // http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
  51. // Decoding of formula conditions, results, and tokens.
  52. // Support for user-defined named cells added as an array "namedcells"
  53. // Patch code for user-defined named cells supports single cells only.
  54. // NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
  55. // external sheet reference structure
  56. /** PHPExcel root directory */
  57. if (! defined('PHPEXCEL_ROOT'))
  58. {
  59. /**
  60. * @ignore
  61. */
  62. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  63. require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  64. }
  65. /**
  66. * PHPExcel_Reader_Excel5
  67. *
  68. * This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
  69. *
  70. * @category PHPExcel
  71. * @package PHPExcel_Reader_Excel5
  72. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  73. */
  74. class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
  75. {
  76. // ParseXL definitions
  77. const XLS_BIFF8 = 0x0600;
  78. const XLS_BIFF7 = 0x0500;
  79. const XLS_WorkbookGlobals = 0x0005;
  80. const XLS_Worksheet = 0x0010;
  81. // record identifiers
  82. const XLS_Type_FORMULA = 0x0006;
  83. const XLS_Type_EOF = 0x000a;
  84. const XLS_Type_PROTECT = 0x0012;
  85. const XLS_Type_OBJECTPROTECT = 0x0063;
  86. const XLS_Type_SCENPROTECT = 0x00dd;
  87. const XLS_Type_PASSWORD = 0x0013;
  88. const XLS_Type_HEADER = 0x0014;
  89. const XLS_Type_FOOTER = 0x0015;
  90. const XLS_Type_EXTERNSHEET = 0x0017;
  91. const XLS_Type_DEFINEDNAME = 0x0018;
  92. const XLS_Type_VERTICALPAGEBREAKS = 0x001a;
  93. const XLS_Type_HORIZONTALPAGEBREAKS = 0x001b;
  94. const XLS_Type_NOTE = 0x001c;
  95. const XLS_Type_SELECTION = 0x001d;
  96. const XLS_Type_DATEMODE = 0x0022;
  97. const XLS_Type_EXTERNNAME = 0x0023;
  98. const XLS_Type_LEFTMARGIN = 0x0026;
  99. const XLS_Type_RIGHTMARGIN = 0x0027;
  100. const XLS_Type_TOPMARGIN = 0x0028;
  101. const XLS_Type_BOTTOMMARGIN = 0x0029;
  102. const XLS_Type_PRINTGRIDLINES = 0x002b;
  103. const XLS_Type_FILEPASS = 0x002f;
  104. const XLS_Type_FONT = 0x0031;
  105. const XLS_Type_CONTINUE = 0x003c;
  106. const XLS_Type_PANE = 0x0041;
  107. const XLS_Type_CODEPAGE = 0x0042;
  108. const XLS_Type_DEFCOLWIDTH = 0x0055;
  109. const XLS_Type_OBJ = 0x005d;
  110. const XLS_Type_COLINFO = 0x007d;
  111. const XLS_Type_IMDATA = 0x007f;
  112. const XLS_Type_SHEETPR = 0x0081;
  113. const XLS_Type_HCENTER = 0x0083;
  114. const XLS_Type_VCENTER = 0x0084;
  115. const XLS_Type_SHEET = 0x0085;
  116. const XLS_Type_PALETTE = 0x0092;
  117. const XLS_Type_SCL = 0x00a0;
  118. const XLS_Type_PAGESETUP = 0x00a1;
  119. const XLS_Type_MULRK = 0x00bd;
  120. const XLS_Type_MULBLANK = 0x00be;
  121. const XLS_Type_DBCELL = 0x00d7;
  122. const XLS_Type_XF = 0x00e0;
  123. const XLS_Type_MERGEDCELLS = 0x00e5;
  124. const XLS_Type_MSODRAWINGGROUP = 0x00eb;
  125. const XLS_Type_MSODRAWING = 0x00ec;
  126. const XLS_Type_SST = 0x00fc;
  127. const XLS_Type_LABELSST = 0x00fd;
  128. const XLS_Type_EXTSST = 0x00ff;
  129. const XLS_Type_EXTERNALBOOK = 0x01ae;
  130. const XLS_Type_DATAVALIDATIONS = 0x01b2;
  131. const XLS_Type_TXO = 0x01b6;
  132. const XLS_Type_HYPERLINK = 0x01b8;
  133. const XLS_Type_DATAVALIDATION = 0x01be;
  134. const XLS_Type_DIMENSION = 0x0200;
  135. const XLS_Type_BLANK = 0x0201;
  136. const XLS_Type_NUMBER = 0x0203;
  137. const XLS_Type_LABEL = 0x0204;
  138. const XLS_Type_BOOLERR = 0x0205;
  139. const XLS_Type_STRING = 0x0207;
  140. const XLS_Type_ROW = 0x0208;
  141. const XLS_Type_INDEX = 0x020b;
  142. const XLS_Type_ARRAY = 0x0221;
  143. const XLS_Type_DEFAULTROWHEIGHT = 0x0225;
  144. const XLS_Type_WINDOW2 = 0x023e;
  145. const XLS_Type_RK = 0x027e;
  146. const XLS_Type_STYLE = 0x0293;
  147. const XLS_Type_FORMAT = 0x041e;
  148. const XLS_Type_SHAREDFMLA = 0x04bc;
  149. const XLS_Type_BOF = 0x0809;
  150. const XLS_Type_SHEETPROTECTION = 0x0867;
  151. const XLS_Type_RANGEPROTECTION = 0x0868;
  152. const XLS_Type_SHEETLAYOUT = 0x0862;
  153. const XLS_Type_XFEXT = 0x087d;
  154. const XLS_Type_UNKNOWN = 0xffff;
  155. /**
  156. * Read data only?
  157. * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  158. * or whether it should read both data and formatting
  159. *
  160. * @var boolean
  161. */
  162. private $_readDataOnly = false;
  163. /**
  164. * Restrict which sheets should be loaded?
  165. * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  166. *
  167. * @var array of string
  168. */
  169. private $_loadSheetsOnly = null;
  170. /**
  171. * PHPExcel_Reader_IReadFilter instance
  172. *
  173. * @var PHPExcel_Reader_IReadFilter
  174. */
  175. private $_readFilter = null;
  176. /**
  177. * Summary Information stream data.
  178. *
  179. * @var string
  180. */
  181. private $_summaryInformation;
  182. /**
  183. * Extended Summary Information stream data.
  184. *
  185. * @var string
  186. */
  187. private $_documentSummaryInformation;
  188. /**
  189. * User-Defined Properties stream data.
  190. *
  191. * @var string
  192. */
  193. private $_userDefinedProperties;
  194. /**
  195. * Workbook stream data. (Includes workbook globals substream as well as sheet substreams)
  196. *
  197. * @var string
  198. */
  199. private $_data;
  200. /**
  201. * Size in bytes of $this->_data
  202. *
  203. * @var int
  204. */
  205. private $_dataSize;
  206. /**
  207. * Current position in stream
  208. *
  209. * @var integer
  210. */
  211. private $_pos;
  212. /**
  213. * Workbook to be returned by the reader.
  214. *
  215. * @var PHPExcel
  216. */
  217. private $_phpExcel;
  218. /**
  219. * Worksheet that is currently being built by the reader.
  220. *
  221. * @var PHPExcel_Worksheet
  222. */
  223. private $_phpSheet;
  224. /**
  225. * BIFF version
  226. *
  227. * @var int
  228. */
  229. private $_version;
  230. /**
  231. * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
  232. * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'
  233. *
  234. * @var string
  235. */
  236. private $_codepage;
  237. /**
  238. * Shared formats
  239. *
  240. * @var array
  241. */
  242. private $_formats;
  243. /**
  244. * Shared fonts
  245. *
  246. * @var array
  247. */
  248. private $_objFonts;
  249. /**
  250. * Color palette
  251. *
  252. * @var array
  253. */
  254. private $_palette;
  255. /**
  256. * Worksheets
  257. *
  258. * @var array
  259. */
  260. private $_sheets;
  261. /**
  262. * External books
  263. *
  264. * @var array
  265. */
  266. private $_externalBooks;
  267. /**
  268. * REF structures. Only applies to BIFF8.
  269. *
  270. * @var array
  271. */
  272. private $_ref;
  273. /**
  274. * External names
  275. *
  276. * @var array
  277. */
  278. private $_externalNames;
  279. /**
  280. * Defined names
  281. *
  282. * @var array
  283. */
  284. private $_definedname;
  285. /**
  286. * Shared strings. Only applies to BIFF8.
  287. *
  288. * @var array
  289. */
  290. private $_sst;
  291. /**
  292. * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
  293. *
  294. * @var boolean
  295. */
  296. private $_frozen;
  297. /**
  298. * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
  299. *
  300. * @var boolean
  301. */
  302. private $_isFitToPages;
  303. /**
  304. * Objects. One OBJ record contributes with one entry.
  305. *
  306. * @var array
  307. */
  308. private $_objs;
  309. /**
  310. * Text Objects. One TXO record corresponds with one entry.
  311. *
  312. * @var array
  313. */
  314. private $_textObjects;
  315. /**
  316. * Cell Annotations (BIFF8)
  317. *
  318. * @var array
  319. */
  320. private $_cellNotes;
  321. /**
  322. * The combined MSODRAWINGGROUP data
  323. *
  324. * @var string
  325. */
  326. private $_drawingGroupData;
  327. /**
  328. * The combined MSODRAWING data (per sheet)
  329. *
  330. * @var string
  331. */
  332. private $_drawingData;
  333. /**
  334. * Keep track of XF index
  335. *
  336. * @var int
  337. */
  338. private $_xfIndex;
  339. /**
  340. * Mapping of XF index (that is a cell XF) to final index in cellXf collection
  341. *
  342. * @var array
  343. */
  344. private $_mapCellXfIndex;
  345. /**
  346. * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection
  347. *
  348. * @var array
  349. */
  350. private $_mapCellStyleXfIndex;
  351. /**
  352. * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
  353. *
  354. * @var array
  355. */
  356. private $_sharedFormulas;
  357. /**
  358. * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
  359. * refers to a shared formula.
  360. *
  361. * @var array
  362. */
  363. private $_sharedFormulaParts;
  364. /**
  365. * Read data only?
  366. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
  367. * If false (the default) it will read data and formatting.
  368. *
  369. * @return boolean
  370. */
  371. public function getReadDataOnly()
  372. {
  373. return $this->_readDataOnly;
  374. }
  375. /**
  376. * Set read data only
  377. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
  378. * Set to false (the default) to advise the Reader to read both data and formatting for cells.
  379. *
  380. * @param boolean $pValue
  381. *
  382. * @return PHPExcel_Reader_Excel5
  383. */
  384. public function setReadDataOnly($pValue = false)
  385. {
  386. $this->_readDataOnly = $pValue;
  387. return $this;
  388. }
  389. /**
  390. * Get which sheets to load
  391. * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
  392. * indicating that all worksheets in the workbook should be loaded.
  393. *
  394. * @return mixed
  395. */
  396. public function getLoadSheetsOnly()
  397. {
  398. return $this->_loadSheetsOnly;
  399. }
  400. /**
  401. * Set which sheets to load
  402. *
  403. * @param mixed $value
  404. * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
  405. * If NULL, then it tells the Reader to read all worksheets in the workbook
  406. *
  407. * @return PHPExcel_Reader_Excel5
  408. */
  409. public function setLoadSheetsOnly($value = null)
  410. {
  411. $this->_loadSheetsOnly = is_array($value) ? $value : array($value);
  412. return $this;
  413. }
  414. /**
  415. * Set all sheets to load
  416. * Tells the Reader to load all worksheets from the workbook.
  417. *
  418. * @return PHPExcel_Reader_Excel5
  419. */
  420. public function setLoadAllSheets()
  421. {
  422. $this->_loadSheetsOnly = null;
  423. return $this;
  424. }
  425. /**
  426. * Read filter
  427. *
  428. * @return PHPExcel_Reader_IReadFilter
  429. */
  430. public function getReadFilter()
  431. {
  432. return $this->_readFilter;
  433. }
  434. /**
  435. * Set read filter
  436. *
  437. * @param PHPExcel_Reader_IReadFilter $pValue
  438. * @return PHPExcel_Reader_Excel5
  439. */
  440. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue)
  441. {
  442. $this->_readFilter = $pValue;
  443. return $this;
  444. }
  445. /**
  446. * Create a new PHPExcel_Reader_Excel5 instance
  447. */
  448. public function __construct()
  449. {
  450. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  451. }
  452. /**
  453. * Can the current PHPExcel_Reader_IReader read the file?
  454. *
  455. * @param string $pFileName
  456. * @return boolean
  457. */
  458. public function canRead($pFilename)
  459. {
  460. // Check if file exists
  461. if (! file_exists($pFilename))
  462. {
  463. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  464. }
  465. try
  466. {
  467. // Use ParseXL for the hard work.
  468. $ole = new PHPExcel_Shared_OLERead();
  469. // get excel data
  470. $res = $ole->read($pFilename);
  471. return true;
  472. }
  473. catch (Exception $e)
  474. {
  475. return false;
  476. }
  477. }
  478. /**
  479. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  480. *
  481. * @param string $pFilename
  482. * @throws Exception
  483. */
  484. public function listWorksheetNames($pFilename)
  485. {
  486. // Check if file exists
  487. if (! file_exists($pFilename))
  488. {
  489. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  490. }
  491. $worksheetNames = array();
  492. // Read the OLE file
  493. $this->_loadOLE($pFilename);
  494. // total byte size of Excel data (workbook global substream + sheet substreams)
  495. $this->_dataSize = strlen($this->_data);
  496. $this->_pos = 0;
  497. $this->_sheets = array();
  498. // Parse Workbook Global Substream
  499. while ($this->_pos < $this->_dataSize)
  500. {
  501. $code = self :: _GetInt2d($this->_data, $this->_pos);
  502. switch ($code)
  503. {
  504. case self :: XLS_Type_BOF :
  505. $this->_readBof();
  506. break;
  507. case self :: XLS_Type_SHEET :
  508. $this->_readSheet();
  509. break;
  510. case self :: XLS_Type_EOF :
  511. $this->_readDefault();
  512. break 2;
  513. default :
  514. $this->_readDefault();
  515. break;
  516. }
  517. }
  518. foreach ($this->_sheets as $sheet)
  519. {
  520. if ($sheet['sheetType'] != 0x00)
  521. {
  522. // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
  523. continue;
  524. }
  525. $worksheetNames[] = $sheet['name'];
  526. }
  527. return $worksheetNames;
  528. }
  529. /**
  530. * Loads PHPExcel from file
  531. *
  532. * @param string $pFilename
  533. * @return PHPExcel
  534. * @throws Exception
  535. */
  536. public function load($pFilename)
  537. {
  538. // Read the OLE file
  539. $this->_loadOLE($pFilename);
  540. // Initialisations
  541. $this->_phpExcel = new PHPExcel();
  542. $this->_phpExcel->removeSheetByIndex(0); // remove 1st sheet
  543. if (! $this->_readDataOnly)
  544. {
  545. $this->_phpExcel->removeCellStyleXfByIndex(0); // remove the default style
  546. $this->_phpExcel->removeCellXfByIndex(0); // remove the default style
  547. }
  548. // Read the summary information stream (containing meta data)
  549. $this->_readSummaryInformation();
  550. // Read the Additional document summary information stream (containing application-specific meta data)
  551. $this->_readDocumentSummaryInformation();
  552. // total byte size of Excel data (workbook global substream + sheet substreams)
  553. $this->_dataSize = strlen($this->_data);
  554. // initialize
  555. $this->_pos = 0;
  556. $this->_codepage = 'CP1252';
  557. $this->_formats = array();
  558. $this->_objFonts = array();
  559. $this->_palette = array();
  560. $this->_sheets = array();
  561. $this->_externalBooks = array();
  562. $this->_ref = array();
  563. $this->_definedname = array();
  564. $this->_sst = array();
  565. $this->_drawingGroupData = '';
  566. $this->_xfIndex = '';
  567. $this->_mapCellXfIndex = array();
  568. $this->_mapCellStyleXfIndex = array();
  569. // Parse Workbook Global Substream
  570. while ($this->_pos < $this->_dataSize)
  571. {
  572. $code = self :: _GetInt2d($this->_data, $this->_pos);
  573. switch ($code)
  574. {
  575. case self :: XLS_Type_BOF :
  576. $this->_readBof();
  577. break;
  578. case self :: XLS_Type_FILEPASS :
  579. $this->_readFilepass();
  580. break;
  581. case self :: XLS_Type_CODEPAGE :
  582. $this->_readCodepage();
  583. break;
  584. case self :: XLS_Type_DATEMODE :
  585. $this->_readDateMode();
  586. break;
  587. case self :: XLS_Type_FONT :
  588. $this->_readFont();
  589. break;
  590. case self :: XLS_Type_FORMAT :
  591. $this->_readFormat();
  592. break;
  593. case self :: XLS_Type_XF :
  594. $this->_readXf();
  595. break;
  596. case self :: XLS_Type_XFEXT :
  597. $this->_readXfExt();
  598. break;
  599. case self :: XLS_Type_STYLE :
  600. $this->_readStyle();
  601. break;
  602. case self :: XLS_Type_PALETTE :
  603. $this->_readPalette();
  604. break;
  605. case self :: XLS_Type_SHEET :
  606. $this->_readSheet();
  607. break;
  608. case self :: XLS_Type_EXTERNALBOOK :
  609. $this->_readExternalBook();
  610. break;
  611. case self :: XLS_Type_EXTERNNAME :
  612. $this->_readExternName();
  613. break;
  614. case self :: XLS_Type_EXTERNSHEET :
  615. $this->_readExternSheet();
  616. break;
  617. case self :: XLS_Type_DEFINEDNAME :
  618. $this->_readDefinedName();
  619. break;
  620. case self :: XLS_Type_MSODRAWINGGROUP :
  621. $this->_readMsoDrawingGroup();
  622. break;
  623. case self :: XLS_Type_SST :
  624. $this->_readSst();
  625. break;
  626. case self :: XLS_Type_EOF :
  627. $this->_readDefault();
  628. break 2;
  629. default :
  630. $this->_readDefault();
  631. break;
  632. }
  633. }
  634. // Resolve indexed colors for font, fill, and border colors
  635. // Cannot be resolved already in XF record, because PALETTE record comes afterwards
  636. if (! $this->_readDataOnly)
  637. {
  638. foreach ($this->_objFonts as $objFont)
  639. {
  640. if (isset($objFont->colorIndex))
  641. {
  642. $color = self :: _readColor($objFont->colorIndex, $this->_palette, $this->_version);
  643. $objFont->getColor()->setRGB($color['rgb']);
  644. }
  645. }
  646. foreach ($this->_phpExcel->getCellXfCollection() as $objStyle)
  647. {
  648. // fill start and end color
  649. $fill = $objStyle->getFill();
  650. if (isset($fill->startcolorIndex))
  651. {
  652. $startColor = self :: _readColor($fill->startcolorIndex, $this->_palette, $this->_version);
  653. $fill->getStartColor()->setRGB($startColor['rgb']);
  654. }
  655. if (isset($fill->endcolorIndex))
  656. {
  657. $endColor = self :: _readColor($fill->endcolorIndex, $this->_palette, $this->_version);
  658. $fill->getEndColor()->setRGB($endColor['rgb']);
  659. }
  660. // border colors
  661. $top = $objStyle->getBorders()->getTop();
  662. $right = $objStyle->getBorders()->getRight();
  663. $bottom = $objStyle->getBorders()->getBottom();
  664. $left = $objStyle->getBorders()->getLeft();
  665. $diagonal = $objStyle->getBorders()->getDiagonal();
  666. if (isset($top->colorIndex))
  667. {
  668. $borderTopColor = self :: _readColor($top->colorIndex, $this->_palette, $this->_version);
  669. $top->getColor()->setRGB($borderTopColor['rgb']);
  670. }
  671. if (isset($right->colorIndex))
  672. {
  673. $borderRightColor = self :: _readColor($right->colorIndex, $this->_palette, $this->_version);
  674. $right->getColor()->setRGB($borderRightColor['rgb']);
  675. }
  676. if (isset($bottom->colorIndex))
  677. {
  678. $borderBottomColor = self :: _readColor($bottom->colorIndex, $this->_palette, $this->_version);
  679. $bottom->getColor()->setRGB($borderBottomColor['rgb']);
  680. }
  681. if (isset($left->colorIndex))
  682. {
  683. $borderLeftColor = self :: _readColor($left->colorIndex, $this->_palette, $this->_version);
  684. $left->getColor()->setRGB($borderLeftColor['rgb']);
  685. }
  686. if (isset($diagonal->colorIndex))
  687. {
  688. $borderDiagonalColor = self :: _readColor($diagonal->colorIndex, $this->_palette, $this->_version);
  689. $diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
  690. }
  691. }
  692. }
  693. // treat MSODRAWINGGROUP records, workbook-level Escher
  694. if (! $this->_readDataOnly && $this->_drawingGroupData)
  695. {
  696. $escherWorkbook = new PHPExcel_Shared_Escher();
  697. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorkbook);
  698. $escherWorkbook = $reader->load($this->_drawingGroupData);
  699. // debug Escher stream
  700. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  701. //$debug->load($this->_drawingGroupData);
  702. }
  703. // Parse the individual sheets
  704. foreach ($this->_sheets as $sheet)
  705. {
  706. if ($sheet['sheetType'] != 0x00)
  707. {
  708. // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
  709. continue;
  710. }
  711. // check if sheet should be skipped
  712. if (isset($this->_loadSheetsOnly) && ! in_array($sheet['name'], $this->_loadSheetsOnly))
  713. {
  714. continue;
  715. }
  716. // add sheet to PHPExcel object
  717. $this->_phpSheet = $this->_phpExcel->createSheet();
  718. $this->_phpSheet->setTitle($sheet['name']);
  719. $this->_phpSheet->setSheetState($sheet['sheetState']);
  720. $this->_pos = $sheet['offset'];
  721. // Initialize isFitToPages. May change after reading SHEETPR record.
  722. $this->_isFitToPages = false;
  723. // Initialize drawingData
  724. $this->_drawingData = '';
  725. // Initialize objs
  726. $this->_objs = array();
  727. // Initialize shared formula parts
  728. $this->_sharedFormulaParts = array();
  729. // Initialize shared formulas
  730. $this->_sharedFormulas = array();
  731. // Initialize text objs
  732. $this->_textObjects = array();
  733. // Initialize cell annotations
  734. $this->_cellNotes = array();
  735. $this->textObjRef = - 1;
  736. while ($this->_pos <= $this->_dataSize - 4)
  737. {
  738. $code = self :: _GetInt2d($this->_data, $this->_pos);
  739. switch ($code)
  740. {
  741. case self :: XLS_Type_BOF :
  742. $this->_readBof();
  743. break;
  744. case self :: XLS_Type_PRINTGRIDLINES :
  745. $this->_readPrintGridlines();
  746. break;
  747. case self :: XLS_Type_DEFAULTROWHEIGHT :
  748. $this->_readDefaultRowHeight();
  749. break;
  750. case self :: XLS_Type_SHEETPR :
  751. $this->_readSheetPr();
  752. break;
  753. case self :: XLS_Type_HORIZONTALPAGEBREAKS :
  754. $this->_readHorizontalPageBreaks();
  755. break;
  756. case self :: XLS_Type_VERTICALPAGEBREAKS :
  757. $this->_readVerticalPageBreaks();
  758. break;
  759. case self :: XLS_Type_HEADER :
  760. $this->_readHeader();
  761. break;
  762. case self :: XLS_Type_FOOTER :
  763. $this->_readFooter();
  764. break;
  765. case self :: XLS_Type_HCENTER :
  766. $this->_readHcenter();
  767. break;
  768. case self :: XLS_Type_VCENTER :
  769. $this->_readVcenter();
  770. break;
  771. case self :: XLS_Type_LEFTMARGIN :
  772. $this->_readLeftMargin();
  773. break;
  774. case self :: XLS_Type_RIGHTMARGIN :
  775. $this->_readRightMargin();
  776. break;
  777. case self :: XLS_Type_TOPMARGIN :
  778. $this->_readTopMargin();
  779. break;
  780. case self :: XLS_Type_BOTTOMMARGIN :
  781. $this->_readBottomMargin();
  782. break;
  783. case self :: XLS_Type_PAGESETUP :
  784. $this->_readPageSetup();
  785. break;
  786. case self :: XLS_Type_PROTECT :
  787. $this->_readProtect();
  788. break;
  789. case self :: XLS_Type_SCENPROTECT :
  790. $this->_readScenProtect();
  791. break;
  792. case self :: XLS_Type_OBJECTPROTECT :
  793. $this->_readObjectProtect();
  794. break;
  795. case self :: XLS_Type_PASSWORD :
  796. $this->_readPassword();
  797. break;
  798. case self :: XLS_Type_DEFCOLWIDTH :
  799. $this->_readDefColWidth();
  800. break;
  801. case self :: XLS_Type_COLINFO :
  802. $this->_readColInfo();
  803. break;
  804. case self :: XLS_Type_DIMENSION :
  805. $this->_readDefault();
  806. break;
  807. case self :: XLS_Type_ROW :
  808. $this->_readRow();
  809. break;
  810. case self :: XLS_Type_DBCELL :
  811. $this->_readDefault();
  812. break;
  813. case self :: XLS_Type_RK :
  814. $this->_readRk();
  815. break;
  816. case self :: XLS_Type_LABELSST :
  817. $this->_readLabelSst();
  818. break;
  819. case self :: XLS_Type_MULRK :
  820. $this->_readMulRk();
  821. break;
  822. case self :: XLS_Type_NUMBER :
  823. $this->_readNumber();
  824. break;
  825. case self :: XLS_Type_FORMULA :
  826. $this->_readFormula();
  827. break;
  828. case self :: XLS_Type_SHAREDFMLA :
  829. $this->_readSharedFmla();
  830. break;
  831. case self :: XLS_Type_BOOLERR :
  832. $this->_readBoolErr();
  833. break;
  834. case self :: XLS_Type_MULBLANK :
  835. $this->_readMulBlank();
  836. break;
  837. case self :: XLS_Type_LABEL :
  838. $this->_readLabel();
  839. break;
  840. case self :: XLS_Type_BLANK :
  841. $this->_readBlank();
  842. break;
  843. case self :: XLS_Type_MSODRAWING :
  844. $this->_readMsoDrawing();
  845. break;
  846. case self :: XLS_Type_OBJ :
  847. $this->_readObj();
  848. break;
  849. case self :: XLS_Type_WINDOW2 :
  850. $this->_readWindow2();
  851. break;
  852. case self :: XLS_Type_SCL :
  853. $this->_readScl();
  854. break;
  855. case self :: XLS_Type_PANE :
  856. $this->_readPane();
  857. break;
  858. case self :: XLS_Type_SELECTION :
  859. $this->_readSelection();
  860. break;
  861. case self :: XLS_Type_MERGEDCELLS :
  862. $this->_readMergedCells();
  863. break;
  864. case self :: XLS_Type_HYPERLINK :
  865. $this->_readHyperLink();
  866. break;
  867. case self :: XLS_Type_DATAVALIDATIONS :
  868. $this->_readDataValidations();
  869. break;
  870. case self :: XLS_Type_DATAVALIDATION :
  871. $this->_readDataValidation();
  872. break;
  873. case self :: XLS_Type_SHEETLAYOUT :
  874. $this->_readSheetLayout();
  875. break;
  876. case self :: XLS_Type_SHEETPROTECTION :
  877. $this->_readSheetProtection();
  878. break;
  879. case self :: XLS_Type_RANGEPROTECTION :
  880. $this->_readRangeProtection();
  881. break;
  882. case self :: XLS_Type_NOTE :
  883. $this->_readNote();
  884. break;
  885. //case self::XLS_Type_IMDATA: $this->_readImData(); break;
  886. case self :: XLS_Type_TXO :
  887. $this->_readTextObject();
  888. break;
  889. case self :: XLS_Type_CONTINUE :
  890. $this->_readContinue();
  891. break;
  892. case self :: XLS_Type_EOF :
  893. $this->_readDefault();
  894. break 2;
  895. default :
  896. $this->_readDefault();
  897. break;
  898. }
  899. }
  900. // treat MSODRAWING records, sheet-level Escher
  901. if (! $this->_readDataOnly && $this->_drawingData)
  902. {
  903. $escherWorksheet = new PHPExcel_Shared_Escher();
  904. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorksheet);
  905. $escherWorksheet = $reader->load($this->_drawingData);
  906. // debug Escher stream
  907. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  908. //$debug->load($this->_drawingData);
  909. // get all spContainers in one long array, so they can be mapped to OBJ records
  910. $allSpContainers = $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
  911. }
  912. // treat OBJ records
  913. foreach ($this->_objs as $n => $obj)
  914. {
  915. // echo '<hr /><b>Object</b> reference is ',$n,'<br />';
  916. // var_dump($obj);
  917. // echo '<br />';
  918. // the first shape container never has a corresponding OBJ record, hence $n + 1
  919. $spContainer = $allSpContainers[$n + 1];
  920. // we skip all spContainers that are a part of a group shape since we cannot yet handle those
  921. if ($spContainer->getNestingLevel() > 1)
  922. {
  923. continue;
  924. }
  925. // calculate the width and height of the shape
  926. list($startColumn, $startRow) = PHPExcel_Cell :: coordinateFromString($spContainer->getStartCoordinates());
  927. list($endColumn, $endRow) = PHPExcel_Cell :: coordinateFromString($spContainer->getEndCoordinates());
  928. $startOffsetX = $spContainer->getStartOffsetX();
  929. $startOffsetY = $spContainer->getStartOffsetY();
  930. $endOffsetX = $spContainer->getEndOffsetX();
  931. $endOffsetY = $spContainer->getEndOffsetY();
  932. $width = PHPExcel_Shared_Excel5 :: getDistanceX($this->_phpSheet, $startColumn, $startOffsetX, $endColumn, $endOffsetX);
  933. $height = PHPExcel_Shared_Excel5 :: getDistanceY($this->_phpSheet, $startRow, $startOffsetY, $endRow, $endOffsetY);
  934. // calculate offsetX and offsetY of the shape
  935. $offsetX = $startOffsetX * PHPExcel_Shared_Excel5 :: sizeCol($this->_phpSheet, $startColumn) / 1024;
  936. $offsetY = $startOffsetY * PHPExcel_Shared_Excel5 :: sizeRow($this->_phpSheet, $startRow) / 256;
  937. switch ($obj['otObjType'])
  938. {
  939. case 0x19 :
  940. // Note
  941. // echo 'Cell Annotation Object<br />';
  942. // echo 'Object ID is ',$obj['idObjID'],'<br />';
  943. //
  944. if (isset($this->_cellNotes[$obj['idObjID']]))
  945. {
  946. $cellNote = $this->_cellNotes[$obj['idObjID']];
  947. // echo '_cellNotes[',$obj['idObjID'],']: ';
  948. // var_dump($cellNote);
  949. // echo '<br />';
  950. //
  951. if (isset($this->_textObjects[$obj['idObjID']]))
  952. {
  953. $textObject = $this->_textObjects[$obj['idObjID']];
  954. // echo '_textObject: ';
  955. // var_dump($textObject);
  956. // echo '<br />';
  957. //
  958. $this->_cellNotes[$obj['idObjID']]['objTextData'] = $textObject;
  959. $text = $textObject['text'];
  960. }
  961. // echo $text,'<br />';
  962. }
  963. break;
  964. case 0x08 :
  965. // echo 'Picture Object<br />';
  966. // picture
  967. // get index to BSE entry (1-based)
  968. $BSEindex = $spContainer->getOPT(0x0104);
  969. $BSECollection = $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection();
  970. $BSE = $BSECollection[$BSEindex - 1];
  971. $blipType = $BSE->getBlipType();
  972. // need check because some blip types are not supported by Escher reader such as EMF
  973. if ($blip = $BSE->getBlip())
  974. {
  975. $ih = imagecreatefromstring($blip->getData());
  976. $drawing = new PHPExcel_Worksheet_MemoryDrawing();
  977. $drawing->setImageResource($ih);
  978. // width, height, offsetX, offsetY
  979. $drawing->setResizeProportional(false);
  980. $drawing->setWidth($width);
  981. $drawing->setHeight($height);
  982. $drawing->setOffsetX($offsetX);
  983. $drawing->setOffsetY($offsetY);
  984. switch ($blipType)
  985. {
  986. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE :: BLIPTYPE_JPEG :
  987. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing :: RENDERING_JPEG);
  988. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing :: MIMETYPE_JPEG);
  989. break;
  990. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE :: BLIPTYPE_PNG :
  991. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing :: RENDERING_PNG);
  992. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing :: MIMETYPE_PNG);
  993. break;
  994. }
  995. $drawing->setWorksheet($this->_phpSheet);
  996. $drawing->setCoordinates($spContainer->getStartCoordinates());
  997. }
  998. break;
  999. default :
  1000. // other object type
  1001. break;
  1002. }
  1003. }
  1004. // treat SHAREDFMLA records
  1005. if ($this->_version == self :: XLS_BIFF8)
  1006. {
  1007. foreach ($this->_sharedFormulaParts as $cell => $baseCell)
  1008. {
  1009. list($column, $row) = PHPExcel_Cell :: coordinateFromString($cell);
  1010. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle()))
  1011. {
  1012. $formula = $this->_getFormulaFromStructure($this->_sharedFormulas[$baseCell], $cell);
  1013. $this->_phpSheet->getCell($cell)->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType :: TYPE_FORMULA);
  1014. }
  1015. }
  1016. }
  1017. if (count($this->_cellNotes) > 0)
  1018. {
  1019. foreach ($this->_cellNotes as $note => $noteDetails)
  1020. {
  1021. // echo '<b>Cell annotation ',$note,'</b><br />';
  1022. // var_dump($noteDetails);
  1023. // echo '<br />';
  1024. $cellAddress = str_replace('$', '', $noteDetails['cellRef']);
  1025. $this->_phpSheet->getComment($cellAddress)->setAuthor($noteDetails['author'])->setText($this->_parseRichText($noteDetails['objTextData']['text']));
  1026. }
  1027. }
  1028. }
  1029. // add the named ranges (defined names)
  1030. foreach ($this->_definedname as $definedName)
  1031. {
  1032. if ($definedName['isBuiltInName'])
  1033. {
  1034. switch ($definedName['name'])
  1035. {
  1036. case pack('C', 0x06) :
  1037. // print area
  1038. // in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2
  1039. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  1040. $extractedRanges = array();
  1041. foreach ($ranges as $range)
  1042. {
  1043. // $range should look like one of these
  1044. // Foo!$C$7:$J$66
  1045. // Bar!$A$1:$IV$2
  1046. $explodes = explode('!', $range); // FIXME: what if sheetname contains exclamation mark?
  1047. $sheetName = $explodes[0];
  1048. if (count($explodes) == 2)
  1049. {
  1050. $extractedRanges[] = str_replace('$', '', $explodes[1]); // C7:J66
  1051. }
  1052. }
  1053. if ($docSheet = $this->_phpExcel->getSheetByName($sheetName))
  1054. {
  1055. $docSheet->getPageSetup()->setPrintArea(implode(',', $extractedRanges)); // C7:J66,A1:IV2
  1056. }
  1057. break;
  1058. case pack('C', 0x07) :
  1059. // print titles (repeating rows)
  1060. // Assuming BIFF8, there are 3 cases
  1061. // 1. repeating rows
  1062. // formula looks like this: Sheet!$A$1:$IV$2
  1063. // rows 1-2 repeat
  1064. // 2. repeating columns
  1065. // formula looks like this: Sheet!$A$1:$B$65536
  1066. // columns A-B repeat
  1067. // 3. both repeating rows and repeating columns
  1068. // formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2
  1069. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  1070. foreach ($ranges as $range)
  1071. {
  1072. // $range should look like this one of these
  1073. // Sheet!$A$1:$B$65536
  1074. // Sheet!$A$1:$IV$2
  1075. $explodes = explode('!', $range);
  1076. if (count($explodes) == 2)
  1077. {
  1078. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0]))
  1079. {
  1080. $extractedRange = $explodes[1];
  1081. $extractedRange = str_replace('$', '', $extractedRange);
  1082. $coordinateStrings = explode(':', $extractedRange);
  1083. if (count($coordinateStrings) == 2)
  1084. {
  1085. list($firstColumn, $firstRow) = PHPExcel_Cell :: coordinateFromString($coordinateStrings[0]);
  1086. list($lastColumn, $lastRow) = PHPExcel_Cell :: coordinateFromString($coordinateStrings[1]);
  1087. if ($firstColumn == 'A' and $lastColumn == 'IV')
  1088. {
  1089. // then we have repeating rows
  1090. $docSheet->getPageSetup()->setRowsToRepeatAtTop(array(
  1091. $firstRow, $lastRow));
  1092. }
  1093. elseif ($firstRow == 1 and $lastRow == 65536)
  1094. {
  1095. // then we have repeating columns
  1096. $docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array(
  1097. $firstColumn, $lastColumn));
  1098. }
  1099. }
  1100. }
  1101. }
  1102. }
  1103. break;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. // Extract range
  1109. $explodes = explode('!', $definedName['formula']);
  1110. if (count($explodes) == 2)
  1111. {
  1112. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0]))
  1113. {
  1114. $extractedRange = $explodes[1];
  1115. $extractedRange = str_replace('$', '', $extractedRange);
  1116. $localOnly = ($definedName['scope'] == 0) ? false : true;
  1117. $scope = ($definedName['scope'] == 0) ? null : $this->_phpExcel->getSheetByName($this->_sheets[$definedName['scope'] - 1]['name']);
  1118. $this->_phpExcel->addNamedRange(new PHPExcel_NamedRange((string) $definedName['name'], $docSheet, $extractedRange, $localOnly, $scope));
  1119. }
  1120. }
  1121. }
  1122. }
  1123. return $this->_phpExcel;
  1124. }
  1125. /**
  1126. * Use OLE reader to extract the relevant data streams from the OLE file
  1127. *
  1128. * @param string $pFilename
  1129. */
  1130. private function _loadOLE($pFilename)
  1131. {
  1132. // OLE reader
  1133. $ole = new PHPExcel_Shared_OLERead();
  1134. // get excel data,
  1135. $res = $ole->read($pFilename);
  1136. // Get workbook data: workbook stream + sheet streams
  1137. $this->_data = $ole->getStream($ole->wrkbook);
  1138. // Get summary information data
  1139. $this->_summaryInformation = $ole->getStream($ole->summaryInformation);
  1140. // Get additional document summary information data
  1141. $this->_documentSummaryInformation = $ole->getStream($ole->documentSummaryInformation);
  1142. // Get user-defined property data
  1143. // $this->_userDefinedProperties = $ole->getUserDefinedProperties();
  1144. }
  1145. /**
  1146. * Read summary information
  1147. */
  1148. private function _readSummaryInformation()
  1149. {
  1150. if (! isset($this->_summaryInformation))
  1151. {
  1152. return;
  1153. }
  1154. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  1155. // offset: 2; size: 2;
  1156. // offset: 4; size: 2; OS version
  1157. // offset: 6; size: 2; OS indicator
  1158. // offset: 8; size: 16
  1159. // offset: 24; size: 4; section count
  1160. $secCount = self :: _GetInt4d($this->_summaryInformation, 24);
  1161. // offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
  1162. // offset: 44; size: 4
  1163. $secOffset = self :: _GetInt4d($this->_summaryInformation, 44);
  1164. // section header
  1165. // offset: $secOffset; size: 4; section length
  1166. $secLength = self :: _GetInt4d($this->_summaryInformation, $secOffset);
  1167. // offset: $secOffset+4; size: 4; property count
  1168. $countProperties = self :: _GetInt4d($this->_summaryInformation, $secOffset + 4);
  1169. // initialize code page (used to resolve string values)
  1170. $codePage = 'CP1252';
  1171. // offset: ($secOffset+8); size: var
  1172. // loop through property decarations and properties
  1173. for($i = 0; $i < $countProperties; ++ $i)
  1174. {
  1175. // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
  1176. $id = self :: _GetInt4d($this->_summaryInformation, ($secOffset + 8) + (8 * $i));
  1177. // Use value of property id as appropriate
  1178. // offset: ($secOffset+12) + (8 * $i); size: 4; offset from beginning of section (48)
  1179. $offset = self :: _GetInt4d($this->_summaryInformation, ($secOffset + 12) + (8 * $i));
  1180. $type = self :: _GetInt4d($this->_summaryInformation, $secOffset + $offset);
  1181. // initialize property value
  1182. $value = null;
  1183. // extract property value based on property type
  1184. switch ($type)
  1185. {
  1186. case 0x02 : // 2 byte signed integer
  1187. $value = self :: _GetInt2d($this->_summaryInformation, $secOffset + 4 + $offset);
  1188. break;
  1189. case 0x03 : // 4 byte signed integer
  1190. $value = self :: _GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
  1191. break;
  1192. case 0x13 : // 4 byte unsigned integer
  1193. // not needed yet, fix later if necessary
  1194. break;
  1195. case 0x1E : // null-terminated string prepended by dword string length
  1196. $byteLength = self :: _GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
  1197. $value = substr($this->_summaryInformation, $secOffset + 8 + $offset, $byteLength);
  1198. $value = PHPExcel_Shared_String :: ConvertEncoding($value, 'UTF-8', $codePage);
  1199. $value = rtrim($value);
  1200. break;
  1201. case 0x40 : // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  1202. // PHP-time
  1203. $value = PHPExcel_Shared_OLE :: OLE2LocalDate(substr($this->_summaryInformation, $secOffset + 4 + $offset, 8));
  1204. break;
  1205. case 0x47 : // Clipboard format
  1206. // not needed yet, fix later if necessary
  1207. break;
  1208. }
  1209. switch ($id)
  1210. {
  1211. case 0x01 : // Code Page
  1212. $codePage = PHPExcel_Shared_CodePage :: NumberToName($value);
  1213. break;
  1214. case 0x02 : // Title
  1215. $this->_phpExcel->getProperties()->setTitle($value);
  1216. break;
  1217. case 0x03 : // Subject
  1218. $this->_phpExcel->getProperties()->setSubject($value);
  1219. break;
  1220. case 0x04 : // Author (Creator)
  1221. $this->_phpExcel->getProperties()->setCreator($value);
  1222. break;
  1223. case 0x05 : // Keywords
  1224. $this->_phpExcel->getProperties()->setKeywords($value);
  1225. break;
  1226. case 0x06 : // Comments (Description)
  1227. $this->_phpExcel->getProperties()->setDescription($value);
  1228. break;
  1229. case 0x07 : // Template
  1230. // Not supported by PHPExcel
  1231. break;
  1232. case 0x08 : // Last Saved By (LastModifiedBy)
  1233. $this->_phpExcel->getProperties()->setLastModifiedBy($value);
  1234. break;
  1235. case 0x09 : // Revision
  1236. // Not supported by PHPExcel
  1237. break;
  1238. case 0x0A : // Total Editing Time
  1239. // Not supported by PHPExcel
  1240. break;
  1241. case 0x0B : // Last Printed
  1242. // Not supported by PHPExcel
  1243. break;
  1244. case 0x0C : // Created Date/Time
  1245. $this->_phpExcel->getProperties()->setCreated($value);
  1246. break;
  1247. case 0x0D : // Modified Date/Time
  1248. $this->_phpExcel->getProperties()->setModified($value);
  1249. break;
  1250. case 0x0E : // Number of Pages
  1251. // Not supported by PHPExcel
  1252. break;
  1253. case 0x0F : // Number of Words
  1254. // Not supported by PHPExcel
  1255. break;
  1256. case 0x10 : // Number of Characters
  1257. // Not supported by PHPExcel
  1258. break;
  1259. case 0x11 : // Thumbnail
  1260. // Not supported by PHPExcel
  1261. break;
  1262. case 0x12 : // Name of creating application
  1263. // Not supported by PHPExcel
  1264. break;
  1265. case 0x13 : // Security
  1266. // Not supported by PHPExcel
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. /**
  1272. * Read additional document summary information
  1273. */
  1274. private function _readDocumentSummaryInformation()
  1275. {
  1276. if (! isset($this->_documentSummaryInformation))
  1277. {
  1278. return;
  1279. }
  1280. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  1281. // offset: 2; size: 2;
  1282. // offset: 4; size: 2; OS version
  1283. // offset: 6; size: 2; OS indicator
  1284. // offset: 8; size: 16
  1285. // offset: 24; size: 4; section count
  1286. $secCount = self :: _GetInt4d($this->_documentSummaryInformation, 24);
  1287. // echo '$secCount = ',$secCount,'<br />';
  1288. // offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
  1289. // offset: 44; size: 4; first section offset
  1290. $secOffset = self :: _GetInt4d($this->_documentSummaryInformation, 44);
  1291. // echo '$secOffset = ',$secOffset,'<br />';
  1292. // section header
  1293. // offset: $secOffset; size: 4; section length
  1294. $secLength = self :: _GetInt4d($this->_documentSummaryInformation, $secOffset);
  1295. // echo '$secLength = ',$secLength,'<br />';
  1296. // offset: $secOffset+4; size: 4; property count
  1297. $countProperties = self :: _GetInt4d($this->_documentSummaryInformation, $secOffset + 4);
  1298. // echo '$countProperties = ',$countProperties,'<br />';
  1299. // initialize code page (used to resolve string values)
  1300. $codePage = 'CP1252';
  1301. // offset: ($secOffset+8); size: var
  1302. // loop through property decarations and properties
  1303. for($i = 0; $i < $countProperties; ++ $i)
  1304. {
  1305. // echo 'Property ',$i,'<br />';
  1306. // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
  1307. $id = self :: _GetInt4d($this->_documentSummaryInformation, ($secOffset + 8) + (8 * $i));
  1308. // echo 'ID is ',$id,'<br />';
  1309. // Use value of property id as appropriate
  1310. // offset: 60 + 8 * $i; size: 4; offset from beginning of section (48)
  1311. $offset = self :: _GetInt4d($this->_documentSummaryInformation, ($secOffset + 12) + (8 * $i));
  1312. $type = self :: _GetInt4d($this->_documentSummaryInformation, $secOffset + $offset);
  1313. // echo 'Type is ',$type,', ';
  1314. // initialize property value
  1315. $value = null;
  1316. // extract property value based on property type
  1317. switch ($type)
  1318. {
  1319. case 0x02 : // 2 byte signed integer
  1320. $value = self :: _GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1321. break;
  1322. case 0x03 : // 4 byte signed integer
  1323. $value = self :: _GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1324. break;
  1325. case 0x13 : // 4 byte unsigned integer
  1326. // not needed yet, fix later if necessary
  1327. break;
  1328. case 0x1E : // null-terminated string prepended by dword string length
  1329. $byteLength = self :: _GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1330. $value = substr($this->_documentSummaryInformation, $secOffset + 8 + $offset, $byteLength);
  1331. $value = PHPExcel_Shared_String :: ConvertEncoding($value, 'UTF-8', $codePage);
  1332. $value = rtrim($value);
  1333. break;
  1334. case 0x40 : // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  1335. // PHP-Time
  1336. $value = PHPExcel_Shared_OLE :: OLE2LocalDate(substr($this->_documentSummaryInformation, $secOffset + 4 + $offset, 8));
  1337. break;
  1338. case 0x47 : // Clipboard format
  1339. // not needed yet, fix later if necessary
  1340. break;
  1341. }
  1342. switch ($id)
  1343. {
  1344. case 0x01 : // Code Page
  1345. $codePage = PHPExcel_Shared_CodePage :: NumberToName($value);
  1346. break;
  1347. case 0x02 : // Category
  1348. $this->_phpExcel->getProperties()->setCategory($value);
  1349. break;
  1350. case 0x03 : // Presentation Target
  1351. // Not supported by PHPExcel
  1352. break;
  1353. case 0x04 : // Bytes
  1354. // Not supported by PHPExcel
  1355. break;
  1356. case 0x05 : // Lines
  1357. // Not supported by PHPExcel
  1358. break;
  1359. case 0x06 : // Paragraphs
  1360. // Not supported by PHPExcel
  1361. break;
  1362. case 0x07 : // Slides
  1363. // Not supported by PHPExcel
  1364. break;
  1365. case 0x08 : // Notes
  1366. // Not supported by PHPExcel
  1367. break;
  1368. case 0x09 : // Hidden Slides
  1369. // Not supported by PHPExcel
  1370. break;
  1371. case 0x0A : // MM Clips
  1372. // Not supported by PHPExcel
  1373. break;
  1374. case 0x0B : // Scale Crop
  1375. // Not supported by PHPExcel
  1376. break;
  1377. case 0x0C : // Heading Pairs
  1378. // Not supported by PHPExcel
  1379. break;
  1380. case 0x0D : // Titles of Parts
  1381. // Not supported by PHPExcel
  1382. break;
  1383. case 0x0E : // Manager
  1384. $this->_phpExcel->getProperties()->setManager($value);
  1385. break;
  1386. case 0x0F : // Company
  1387. $this->_phpExcel->getProperties()->setCompany($value);
  1388. break;
  1389. case 0x10 : // Links up-to-date
  1390. // Not supported by PHPExcel
  1391. break;
  1392. }
  1393. }
  1394. }
  1395. /**
  1396. * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
  1397. */
  1398. private function _readDefault()
  1399. {
  1400. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1401. // $recordData = substr($this->_data, $this->_pos + 4, $length);
  1402. // move stream pointer to next record
  1403. $this->_pos += 4 + $length;
  1404. }
  1405. /**
  1406. * The NOTE record specifies a comment associated with a particular cell. In Excel 95 (BIFF7) and earlier versions,
  1407. * this record stores a note (cell note). This feature was significantly enhanced in Excel 97.
  1408. */
  1409. private function _readNote()
  1410. {
  1411. // echo '<b>Read Cell Annotation</b><br />';
  1412. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1413. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1414. // move stream pointer to next record
  1415. $this->_pos += 4 + $length;
  1416. if ($this->_readDataOnly)
  1417. {
  1418. return;
  1419. }
  1420. $cellAddress = $this->_readBIFF8CellAddress(substr($recordData, 0, 4));
  1421. if ($this->_version == self :: XLS_BIFF8)
  1422. {
  1423. $noteObjID = self :: _GetInt2d($recordData, 6);
  1424. $noteAuthor = self :: _readUnicodeStringLong(substr($recordData, 8));
  1425. $noteAuthor = $noteAuthor['value'];
  1426. // echo 'Note Address=',$cellAddress,'<br />';
  1427. // echo 'Note Object ID=',$noteObjID,'<br />';
  1428. // echo 'Note Author=',$noteAuthor,'<hr />';
  1429. //
  1430. $this->_cellNotes[$noteObjID] = array(
  1431. 'cellRef' => $cellAddress, 'objectID' => $noteObjID, 'author' => $noteAuthor);
  1432. }
  1433. else
  1434. {
  1435. $extension = false;
  1436. if ($cellAddress == '$B$65536')
  1437. {
  1438. // If the address row is -1 and the column is 0, (which translates as $B$65536) then this is a continuation
  1439. // note from the previous cell annotation. We're not yet handling this, so annotations longer than the
  1440. // max 2048 bytes will probably throw a wobbly.
  1441. $row = self :: _GetInt2d($recordData, 0);
  1442. $extension = true;
  1443. $cellAddress = array_pop(array_keys($this->_phpSheet->getComments()));
  1444. }
  1445. // echo 'Note Address=',$cellAddress,'<br />';
  1446. $cellAddress = str_replace('$', '', $cellAddress);
  1447. $noteLength = self :: _GetInt2d($recordData, 4);
  1448. $noteText = trim(substr($recordData, 6));
  1449. // echo 'Note Length=',$noteLength,'<br />';
  1450. // echo 'Note Text=',$noteText,'<br />';
  1451. if ($extension)
  1452. {
  1453. // Concatenate this extension with the currently set comment for the cell
  1454. $comment = $this->_phpSheet->getComment($cellAddress);
  1455. $commentText = $comment->getText()->getPlainText();
  1456. $comment->setText($this->_parseRichText($commentText . $noteText));
  1457. }
  1458. else
  1459. {
  1460. // Set comment for the cell
  1461. $this->_phpSheet->getComment($cellAddress)->// ->setAuthor( $author )
  1462. setText($this->_parseRichText($noteText));
  1463. }
  1464. }
  1465. }
  1466. /**
  1467. * The TEXT Object record contains the text associated with a cell annotation.
  1468. */
  1469. private function _readTextObject()
  1470. {
  1471. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1472. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1473. // move stream pointer to next record
  1474. $this->_pos += 4 + $length;
  1475. if ($this->_readDataOnly)
  1476. {
  1477. return;
  1478. }
  1479. // recordData consists of an array of subrecords looking like this:
  1480. // grbit: 2 bytes; Option Flags
  1481. // rot: 2 bytes; rotation
  1482. // cchText: 2 bytes; length of the text (in the first continue record)
  1483. // cbRuns: 2 bytes; length of the formatting (in the second continue record)
  1484. // followed by the continuation records containing the actual text and formatting
  1485. $grbitOpts = self :: _GetInt2d($recordData, 0);
  1486. $rot = self :: _GetInt2d($recordData, 2);
  1487. $cchText = self :: _GetInt2d($recordData, 10);
  1488. $cbRuns = self :: _GetInt2d($recordData, 12);
  1489. $text = $this->_getSplicedRecordData();
  1490. $this->_textObjects[$this->textObjRef] = array(
  1491. 'text' => substr($text["recordData"], $text["spliceOffsets"][0] + 1, $cchText),
  1492. 'format' => substr($text["recordData"], $text["spliceOffsets"][1], $cbRuns), 'alignment' => $grbitOpts,
  1493. 'rotation' => $rot);
  1494. // echo '<b>_readTextObject()</b><br />';
  1495. // var_dump($this->_textObjects[$this->textObjRef]);
  1496. // echo '<br />';
  1497. }
  1498. /**
  1499. * Read BOF
  1500. */
  1501. private function _readBof()
  1502. {
  1503. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1504. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1505. // move stream pointer to next record
  1506. $this->_pos += 4 + $length;
  1507. // offset: 2; size: 2; type of the following data
  1508. $substreamType = self :: _GetInt2d($recordData, 2);
  1509. switch ($substreamType)
  1510. {
  1511. case self :: XLS_WorkbookGlobals :
  1512. $version = self :: _GetInt2d($recordData, 0);
  1513. if (($version != self :: XLS_BIFF8) && ($version != self :: XLS_BIFF7))
  1514. {
  1515. throw new Exception('Cannot read this Excel file. Version is too old.');
  1516. }
  1517. $this->_version = $version;
  1518. break;
  1519. case self :: XLS_Worksheet :
  1520. // do not use this version information for anything
  1521. // it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
  1522. break;
  1523. default :
  1524. // substream, e.g. chart
  1525. // just skip the entire substream
  1526. do
  1527. {
  1528. $code = self :: _GetInt2d($this->_data, $this->_pos);
  1529. $this->_readDefault();
  1530. }
  1531. while ($code != self :: XLS_Type_EOF && $this->_pos < $this->_dataSize);
  1532. break;
  1533. }
  1534. }
  1535. /**
  1536. * FILEPASS
  1537. *
  1538. * This record is part of the File Protection Block. It
  1539. * contains information about the read/write password of the
  1540. * file. All record contents following this record will be
  1541. * encrypted.
  1542. *
  1543. * -- "OpenOffice.org's Documentation of the Microsoft
  1544. * Excel File Format"
  1545. */
  1546. private function _readFilepass()
  1547. {
  1548. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1549. // $recordData = substr($this->_data, $this->_pos + 4, $length);
  1550. // move stream pointer to next record
  1551. $this->_pos += 4 + $length;
  1552. throw new Exception('Cannot read encrypted file');
  1553. }
  1554. /**
  1555. * CODEPAGE
  1556. *
  1557. * This record stores the text encoding used to write byte
  1558. * strings, stored as MS Windows code page identifier.
  1559. *
  1560. * -- "OpenOffice.org's Documentation of the Microsoft
  1561. * Excel File Format"
  1562. */
  1563. private function _readCodepage()
  1564. {
  1565. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1566. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1567. // move stream pointer to next record
  1568. $this->_pos += 4 + $length;
  1569. // offset: 0; size: 2; code page identifier
  1570. $codepage = self :: _GetInt2d($recordData, 0);
  1571. $this->_codepage = PHPExcel_Shared_CodePage :: NumberToName($codepage);
  1572. }
  1573. /**
  1574. * DATEMODE
  1575. *
  1576. * This record specifies the base date for displaying date
  1577. * values. All dates are stored as count of days past this
  1578. * base date. In BIFF2-BIFF4 this record is part of the
  1579. * Calculation Settings Block. In BIFF5-BIFF8 it is
  1580. * stored in the Workbook Globals Substream.
  1581. *
  1582. * -- "OpenOffice.org's Documentation of the Microsoft
  1583. * Excel File Format"
  1584. */
  1585. private function _readDateMode()
  1586. {
  1587. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1588. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1589. // move stream pointer to next record
  1590. $this->_pos += 4 + $length;
  1591. // offset: 0; size: 2; 0 = base 1900, 1 = base 1904
  1592. PHPExcel_Shared_Date :: setExcelCalendar(PHPExcel_Shared_Date :: CALENDAR_WINDOWS_1900);
  1593. if (ord($recordData{0}) == 1)
  1594. {
  1595. PHPExcel_Shared_Date :: setExcelCalendar(PHPExcel_Shared_Date :: CALENDAR_MAC_1904);
  1596. }
  1597. }
  1598. /**
  1599. * Read a FONT record
  1600. */
  1601. private function _readFont()
  1602. {
  1603. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1604. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1605. // move stream pointer to next record
  1606. $this->_pos += 4 + $length;
  1607. if (! $this->_readDataOnly)
  1608. {
  1609. $objFont = new PHPExcel_Style_Font();
  1610. // offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
  1611. $size = self :: _GetInt2d($recordData, 0);
  1612. $objFont->setSize($size / 20);
  1613. // offset: 2; size: 2; option flags
  1614. // bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
  1615. // bit: 1; mask 0x0002; italic
  1616. $isItalic = (0x0002 & self :: _GetInt2d($recordData, 2)) >> 1;
  1617. if ($isItalic)
  1618. $objFont->setItalic(true);
  1619. // bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
  1620. // bit: 3; mask 0x0008; strike
  1621. $isStrike = (0x0008 & self :: _GetInt2d($recordData, 2)) >> 3;
  1622. if ($isStrike)
  1623. $objFont->setStrikethrough(true);
  1624. // offset: 4; size: 2; colour index
  1625. $colorIndex = self :: _GetInt2d($recordData, 4);
  1626. $objFont->colorIndex = $colorIndex;
  1627. // offset: 6; size: 2; font weight
  1628. $weight = self :: _GetInt2d($recordData, 6);
  1629. switch ($weight)
  1630. {
  1631. case 0x02BC :
  1632. $objFont->setBold(true);
  1633. break;
  1634. }
  1635. // offset: 8; size: 2; escapement type
  1636. $escapement = self :: _GetInt2d($recordData, 8);
  1637. switch ($escapement)
  1638. {
  1639. case 0x0001 :
  1640. $objFont->setSuperScript(true);
  1641. break;
  1642. case 0x0002 :
  1643. $objFont->setSubScript(true);
  1644. break;
  1645. }
  1646. // offset: 10; size: 1; underline type
  1647. $underlineType = ord($recordData{10});
  1648. switch ($underlineType)
  1649. {
  1650. case 0x00 :
  1651. break; // no underline
  1652. case 0x01 :
  1653. $objFont->setUnderline(PHPExcel_Style_Font :: UNDERLINE_SINGLE);
  1654. break;
  1655. case 0x02 :
  1656. $objFont->setUnderline(PHPExcel_Style_Font :: UNDERLINE_DOUBLE);
  1657. break;
  1658. case 0x21 :
  1659. $objFont->setUnderline(PHPExcel_Style_Font :: UNDERLINE_SINGLEACCOUNTING);
  1660. break;
  1661. case 0x22 :
  1662. $objFont->setUnderline(PHPExcel_Style_Font :: UNDERLINE_DOUBLEACCOUNTING);
  1663. break;
  1664. }
  1665. // offset: 11; size: 1; font family
  1666. // offset: 12; size: 1; character set
  1667. // offset: 13; size: 1; not used
  1668. // offset: 14; size: var; font name
  1669. if ($this->_version == self :: XLS_BIFF8)
  1670. {
  1671. $string = self :: _readUnicodeStringShort(substr($recordData, 14));
  1672. }
  1673. else
  1674. {
  1675. $string = $this->_readByteStringShort(substr($recordData, 14));
  1676. }
  1677. $objFont->setName($string['value']);
  1678. $this->_objFonts[] = $objFont;
  1679. }
  1680. }
  1681. /**
  1682. * FORMAT
  1683. *
  1684. * This record contains information about a number format.
  1685. * All FORMAT records occur together in a sequential list.
  1686. *
  1687. * In BIFF2-BIFF4 other records referencing a FORMAT record
  1688. * contain a zero-based index into this list. From BIFF5 on
  1689. * the FORMAT record contains the index itself that will be
  1690. * used by other records.
  1691. *
  1692. * -- "OpenOffice.org's Documentation of the Microsoft
  1693. * Excel File Format"
  1694. */
  1695. private function _readFormat()
  1696. {
  1697. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1698. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1699. // move stream pointer to next record
  1700. $this->_pos += 4 + $length;
  1701. if (! $this->_readDataOnly)
  1702. {
  1703. $indexCode = self :: _GetInt2d($recordData, 0);
  1704. if ($this->_version == self :: XLS_BIFF8)
  1705. {
  1706. $string = self :: _readUnicodeStringLong(substr($recordData, 2));
  1707. }
  1708. else
  1709. {
  1710. // BIFF7
  1711. $string = $this->_readByteStringShort(substr($recordData, 2));
  1712. }
  1713. $formatString = $string['value'];
  1714. $this->_formats[$indexCode] = $formatString;
  1715. }
  1716. }
  1717. /**
  1718. * XF - Extended Format
  1719. *
  1720. * This record contains formatting information for cells, rows, columns or styles.
  1721. * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF
  1722. * and 1 cell XF.
  1723. * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
  1724. * and XF record 15 is a cell XF
  1725. * We only read the first cell style XF and skip the remaining cell style XF records
  1726. * We read all cell XF records.
  1727. *
  1728. * -- "OpenOffice.org's Documentation of the Microsoft
  1729. * Excel File Format"
  1730. */
  1731. private function _readXf()
  1732. {
  1733. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  1734. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1735. // move stream pointer to next record
  1736. $this->_pos += 4 + $length;
  1737. $objStyle = new PHPExcel_Style();
  1738. if (! $this->_readDataOnly)
  1739. {
  1740. // offset: 0; size: 2; Index to FONT record
  1741. if (self :: _GetInt2d($recordData, 0) < 4)
  1742. {
  1743. $fontIndex = self :: _GetInt2d($recordData, 0);
  1744. }
  1745. else
  1746. {
  1747. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  1748. // check the OpenOffice documentation of the FONT record
  1749. $fontIndex = self :: _GetInt2d($recordData, 0) - 1;
  1750. }
  1751. $objStyle->setFont($this->_objFonts[$fontIndex]);
  1752. // offset: 2; size: 2; Index to FORMAT record
  1753. $numberFormatIndex = self :: _GetInt2d($recordData, 2);
  1754. if (isset($this->_formats[$numberFormatIndex]))
  1755. {
  1756. // then we have user-defined format code
  1757. $numberformat = array('code' => $this->_formats[$numberFormatIndex]);
  1758. }
  1759. elseif (($code = PHPExcel_Style_NumberFormat :: builtInFormatCode($numberFormatIndex)) !== '')
  1760. {
  1761. // then we have built-in format code
  1762. $numberformat = array('code' => $code);
  1763. }
  1764. else
  1765. {
  1766. // we set the general format code
  1767. $numberformat = array('code' => 'General');
  1768. }
  1769. $objStyle->getNumberFormat()->setFormatCode($numberformat['code']);
  1770. // offset: 4; size: 2; XF type, cell protection, and parent style XF
  1771. // bit 2-0; mask 0x0007; XF_TYPE_PROT
  1772. $xfTypeProt = self :: _GetInt2d($recordData, 4);
  1773. // bit 0; mask 0x01; 1 = cell is locked
  1774. $isLocked = (0x01 & $xfTypeProt) >> 0;
  1775. $objStyle->getProtection()->setLocked($isLocked ? PHPExcel_Style_Protection :: PROTECTION_INHERIT : PHPExcel_Style_Protection :: PROTECTION_UNPROTECTED);
  1776. // bit 1; mask 0x02; 1 = Formula is hidden
  1777. $isHidden = (0x02 & $xfTypeProt) >> 1;
  1778. $objStyle->getProtection()->setHidden($isHidden ? PHPExcel_Style_Protection :: PROTECTION_PROTECTED : PHPExcel_Style_Protection :: PROTECTION_UNPROTECTED);
  1779. // bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
  1780. $isCellStyleXf = (0x04 & $xfTypeProt) >> 2;
  1781. // offset: 6; size: 1; Alignment and text break
  1782. // bit 2-0, mask 0x07; horizontal alignment
  1783. $horAlign = (0x07 & ord($recordData{6})) >> 0;
  1784. switch ($horAlign)
  1785. {
  1786. case 0 :
  1787. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_GENERAL);
  1788. break;
  1789. case 1 :
  1790. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  1791. break;
  1792. case 2 :
  1793. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
  1794. break;
  1795. case 3 :
  1796. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT);
  1797. break;
  1798. case 5 :
  1799. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_JUSTIFY);
  1800. break;
  1801. case 6 :
  1802. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER_CONTINUOUS);
  1803. break;
  1804. }
  1805. // bit 3, mask 0x08; wrap text
  1806. $wrapText = (0x08 & ord($recordData{6})) >> 3;
  1807. switch ($wrapText)
  1808. {
  1809. case 0 :
  1810. $objStyle->getAlignment()->setWrapText(false);
  1811. break;
  1812. case 1 :
  1813. $objStyle->getAlignment()->setWrapText(true);
  1814. break;
  1815. }
  1816. // bit 6-4, mask 0x70; vertical alignment
  1817. $vertAlign = (0x70 & ord($recordData{6})) >> 4;
  1818. switch ($vertAlign)
  1819. {
  1820. case 0 :
  1821. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment :: VERTICAL_TOP);
  1822. break;
  1823. case 1 :
  1824. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment :: VERTICAL_CENTER);
  1825. break;
  1826. case 2 :
  1827. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment :: VERTICAL_BOTTOM);
  1828. break;
  1829. case 3 :
  1830. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment :: VERTICAL_JUSTIFY);
  1831. break;
  1832. }
  1833. if ($this->_version == self :: XLS_BIFF8)
  1834. {
  1835. // offset: 7; size: 1; XF_ROTATION: Text rotation angle
  1836. $angle = ord($recordData{7});
  1837. $rotation = 0;
  1838. if ($angle <= 90)
  1839. {
  1840. $rotation = $angle;
  1841. }
  1842. else
  1843. if ($angle <= 180)
  1844. {
  1845. $rotation = 90 - $angle;
  1846. }
  1847. else
  1848. if ($angle == 255)
  1849. {
  1850. $rotation = - 165;
  1851. }
  1852. $objStyle->getAlignment()->setTextRotation($rotation);
  1853. // offset: 8; size: 1; Indentation, shrink to cell size, and text direction
  1854. // bit: 3-0; mask: 0x0F; indent level
  1855. $indent = (0x0F & ord($recordData{8})) >> 0;
  1856. $objStyle->getAlignment()->setIndent($indent);
  1857. // bit: 4; mask: 0x10; 1 = shrink content to fit into cell
  1858. $shrinkToFit = (0x10 & ord($recordData{8})) >> 4;
  1859. switch ($shrinkToFit)
  1860. {
  1861. case 0 :
  1862. $objStyle->getAlignment()->setShrinkToFit(false);
  1863. break;
  1864. case 1 :
  1865. $objStyle->getAlignment()->setShrinkToFit(true);
  1866. break;
  1867. }
  1868. // offset: 9; size: 1; Flags used for attribute groups
  1869. // offset: 10; size: 4; Cell border lines and background area
  1870. // bit: 3-0; mask: 0x0000000F; left style
  1871. if ($bordersLeftStyle = self :: _mapBorderStyle((0x0000000F & self :: _GetInt4d($recordData, 10)) >> 0))
  1872. {
  1873. $objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
  1874. }
  1875. // bit: 7-4; mask: 0x000000F0; right style
  1876. if ($bordersRightStyle = self :: _mapBorderStyle((0x000000F0 & self :: _GetInt4d($recordData, 10)) >> 4))
  1877. {
  1878. $objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
  1879. }
  1880. // bit: 11-8; mask: 0x00000F00; top style
  1881. if ($bordersTopStyle = self :: _mapBorderStyle((0x00000F00 & self :: _GetInt4d($recordData, 10)) >> 8))
  1882. {
  1883. $objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
  1884. }
  1885. // bit: 15-12; mask: 0x0000F000; bottom style
  1886. if ($bordersBottomStyle = self :: _mapBorderStyle((0x0000F000 & self :: _GetInt4d($recordData, 10)) >> 12))
  1887. {
  1888. $objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
  1889. }
  1890. // bit: 22-16; mask: 0x007F0000; left color
  1891. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & self :: _GetInt4d($recordData, 10)) >> 16;
  1892. // bit: 29-23; mask: 0x3F800000; right color
  1893. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & self :: _GetInt4d($recordData, 10)) >> 23;
  1894. // bit: 30; mask: 0x40000000; 1 = diagonal line from top left to right bottom
  1895. $diagonalDown = (0x40000000 & self :: _GetInt4d($recordData, 10)) >> 30 ? true : false;
  1896. // bit: 31; mask: 0x80000000; 1 = diagonal line from bottom left to top right
  1897. $diagonalUp = (0x80000000 & self :: _GetInt4d($recordData, 10)) >> 31 ? true : false;
  1898. if ($diagonalUp == false && $diagonalDown == false)
  1899. {
  1900. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders :: DIAGONAL_NONE);
  1901. }
  1902. elseif ($diagonalUp == true && $diagonalDown == false)
  1903. {
  1904. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders :: DIAGONAL_UP);
  1905. }
  1906. elseif ($diagonalUp == false && $diagonalDown == true)
  1907. {
  1908. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders :: DIAGONAL_DOWN);
  1909. }
  1910. elseif ($diagonalUp == true && $diagonalDown == true)
  1911. {
  1912. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders :: DIAGONAL_BOTH);
  1913. }
  1914. // offset: 14; size: 4;
  1915. // bit: 6-0; mask: 0x0000007F; top color
  1916. $objStyle->getBorders()->getTop()->colorIndex = (0x0000007F & self :: _GetInt4d($recordData, 14)) >> 0;
  1917. // bit: 13-7; mask: 0x00003F80; bottom color
  1918. $objStyle->getBorders()->getBottom()->colorIndex = (0x00003F80 & self :: _GetInt4d($recordData, 14)) >> 7;
  1919. // bit: 20-14; mask: 0x001FC000; diagonal color
  1920. $objStyle->getBorders()->getDiagonal()->colorIndex = (0x001FC000 & self :: _GetInt4d($recordData, 14)) >> 14;
  1921. // bit: 24-21; mask: 0x01E00000; diagonal style
  1922. if ($bordersDiagonalStyle = self :: _mapBorderStyle((0x01E00000 & self :: _GetInt4d($recordData, 14)) >> 21))
  1923. {
  1924. $objStyle->getBorders()->getDiagonal()->setBorderStyle($bordersDiagonalStyle);
  1925. }
  1926. // bit: 31-26; mask: 0xFC000000 fill pattern
  1927. if ($fillType = self :: _mapFillPattern((0xFC000000 & self :: _GetInt4d($recordData, 14)) >> 26))
  1928. {
  1929. $objStyle->getFill()->setFillType($fillType);
  1930. }
  1931. // offset: 18; size: 2; pattern and background colour
  1932. // bit: 6-0; mask: 0x007F; color index for pattern color
  1933. $objStyle->getFill()->startcolorIndex = (0x007F & self :: _GetInt2d($recordData, 18)) >> 0;
  1934. // bit: 13-7; mask: 0x3F80; color index for pattern background
  1935. $objStyle->getFill()->endcolorIndex = (0x3F80 & self :: _GetInt2d($recordData, 18)) >> 7;
  1936. }
  1937. else
  1938. {
  1939. // BIFF5
  1940. // offset: 7; size: 1; Text orientation and flags
  1941. $orientationAndFlags = ord($recordData{7});
  1942. // bit: 1-0; mask: 0x03; XF_ORIENTATION: Text orientation
  1943. $xfOrientation = (0x03 & $orientationAndFlags) >> 0;
  1944. switch ($xfOrientation)
  1945. {
  1946. case 0 :
  1947. $objStyle->getAlignment()->setTextRotation(0);
  1948. break;
  1949. case 1 :
  1950. $objStyle->getAlignment()->setTextRotation(- 165);
  1951. break;
  1952. case 2 :
  1953. $objStyle->getAlignment()->setTextRotation(90);
  1954. break;
  1955. case 3 :
  1956. $objStyle->getAlignment()->setTextRotation(- 90);
  1957. break;
  1958. }
  1959. // offset: 8; size: 4; cell border lines and background area
  1960. $borderAndBackground = self :: _GetInt4d($recordData, 8);
  1961. // bit: 6-0; mask: 0x0000007F; color index for pattern color
  1962. $objStyle->getFill()->startcolorIndex = (0x0000007F & $borderAndBackground) >> 0;
  1963. // bit: 13-7; mask: 0x00003F80; color index for pattern background
  1964. $objStyle->getFill()->endcolorIndex = (0x00003F80 & $borderAndBackground) >> 7;
  1965. // bit: 21-16; mask: 0x003F0000; fill pattern
  1966. $objStyle->getFill()->setFillType(self :: _mapFillPattern((0x003F0000 & $borderAndBackground) >> 16));
  1967. // bit: 24-22; mask: 0x01C00000; bottom line style
  1968. $objStyle->getBorders()->getBottom()->setBorderStyle(self :: _mapBorderStyle((0x01C00000 & $borderAndBackground) >> 22));
  1969. // bit: 31-25; mask: 0xFE000000; bottom line color
  1970. $objStyle->getBorders()->getBottom()->colorIndex = (0xFE000000 & $borderAndBackground) >> 25;
  1971. // offset: 12; size: 4; cell border lines
  1972. $borderLines = self :: _GetInt4d($recordData, 12);
  1973. // bit: 2-0; mask: 0x00000007; top line style
  1974. $objStyle->getBorders()->getTop()->setBorderStyle(self :: _mapBorderStyle((0x00000007 & $borderLines) >> 0));
  1975. // bit: 5-3; mask: 0x00000038; left line style
  1976. $objStyle->getBorders()->getLeft()->setBorderStyle(self :: _mapBorderStyle((0x00000038 & $borderLines) >> 3));
  1977. // bit: 8-6; mask: 0x000001C0; right line style
  1978. $objStyle->getBorders()->getRight()->setBorderStyle(self :: _mapBorderStyle((0x000001C0 & $borderLines) >> 6));
  1979. // bit: 15-9; mask: 0x0000FE00; top line color index
  1980. $objStyle->getBorders()->getTop()->colorIndex = (0x0000FE00 & $borderLines) >> 9;
  1981. // bit: 22-16; mask: 0x007F0000; left line color index
  1982. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & $borderLines) >> 16;
  1983. // bit: 29-23; mask: 0x3F800000; right line color index
  1984. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & $borderLines) >> 23;
  1985. }
  1986. // add cellStyleXf or cellXf and update mapping
  1987. if ($isCellStyleXf)
  1988. {
  1989. // we only read one style XF record which is always the first
  1990. if ($this->_xfIndex == 0)
  1991. {
  1992. $this->_phpExcel->addCellStyleXf($objStyle);
  1993. $this->_mapCellStyleXfIndex[$this->_xfIndex] = 0;
  1994. }
  1995. }
  1996. else
  1997. {
  1998. // we read all cell XF records
  1999. $this->_phpExcel->addCellXf($objStyle);
  2000. $this->_mapCellXfIndex[$this->_xfIndex] = count($this->_phpExcel->getCellXfCollection()) - 1;
  2001. }
  2002. // update XF index for when we read next record
  2003. ++ $this->_xfIndex;
  2004. }
  2005. }
  2006. /**
  2007. *
  2008. */
  2009. private function _readXfExt()
  2010. {
  2011. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2012. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2013. // move stream pointer to next record
  2014. $this->_pos += 4 + $length;
  2015. if (! $this->_readDataOnly)
  2016. {
  2017. // offset: 0; size: 2; 0x087D = repeated header
  2018. // offset: 2; size: 2
  2019. // offset: 4; size: 8; not used
  2020. // offset: 12; size: 2; record version
  2021. // offset: 14; size: 2; index to XF record which this record modifies
  2022. $ixfe = self :: _GetInt2d($recordData, 14);
  2023. // offset: 16; size: 2; not used
  2024. // offset: 18; size: 2; number of extension properties that follow
  2025. $cexts = self :: _GetInt2d($recordData, 18);
  2026. // start reading the actual extension data
  2027. $offset = 20;
  2028. while ($offset < $length)
  2029. {
  2030. // extension type
  2031. $extType = self :: _GetInt2d($recordData, $offset);
  2032. // extension length
  2033. $cb = self :: _GetInt2d($recordData, $offset + 2);
  2034. // extension data
  2035. $extData = substr($recordData, $offset + 4, $cb);
  2036. switch ($extType)
  2037. {
  2038. case 4 : // fill start color
  2039. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2040. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2041. if ($xclfType == 2)
  2042. {
  2043. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2044. // modify the relevant style property
  2045. if (isset($this->_mapCellXfIndex[$ixfe]))
  2046. {
  2047. $fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  2048. $fill->getStartColor()->setRGB($rgb);
  2049. unset($fill->startcolorIndex); // normal color index does not apply, discard
  2050. }
  2051. }
  2052. break;
  2053. case 5 : // fill end color
  2054. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2055. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2056. if ($xclfType == 2)
  2057. {
  2058. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2059. // modify the relevant style property
  2060. if (isset($this->_mapCellXfIndex[$ixfe]))
  2061. {
  2062. $fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  2063. $fill->getEndColor()->setRGB($rgb);
  2064. unset($fill->endcolorIndex); // normal color index does not apply, discard
  2065. }
  2066. }
  2067. break;
  2068. case 7 : // border color top
  2069. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2070. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2071. if ($xclfType == 2)
  2072. {
  2073. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2074. // modify the relevant style property
  2075. if (isset($this->_mapCellXfIndex[$ixfe]))
  2076. {
  2077. $top = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getTop();
  2078. $top->getColor()->setRGB($rgb);
  2079. unset($top->colorIndex); // normal color index does not apply, discard
  2080. }
  2081. }
  2082. break;
  2083. case 8 : // border color bottom
  2084. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2085. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2086. if ($xclfType == 2)
  2087. {
  2088. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2089. // modify the relevant style property
  2090. if (isset($this->_mapCellXfIndex[$ixfe]))
  2091. {
  2092. $bottom = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getBottom();
  2093. $bottom->getColor()->setRGB($rgb);
  2094. unset($bottom->colorIndex); // normal color index does not apply, discard
  2095. }
  2096. }
  2097. break;
  2098. case 9 : // border color left
  2099. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2100. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2101. if ($xclfType == 2)
  2102. {
  2103. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2104. // modify the relevant style property
  2105. if (isset($this->_mapCellXfIndex[$ixfe]))
  2106. {
  2107. $left = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getLeft();
  2108. $left->getColor()->setRGB($rgb);
  2109. unset($left->colorIndex); // normal color index does not apply, discard
  2110. }
  2111. }
  2112. break;
  2113. case 10 : // border color right
  2114. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2115. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2116. if ($xclfType == 2)
  2117. {
  2118. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2119. // modify the relevant style property
  2120. if (isset($this->_mapCellXfIndex[$ixfe]))
  2121. {
  2122. $right = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getRight();
  2123. $right->getColor()->setRGB($rgb);
  2124. unset($right->colorIndex); // normal color index does not apply, discard
  2125. }
  2126. }
  2127. break;
  2128. case 11 : // border color diagonal
  2129. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2130. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2131. if ($xclfType == 2)
  2132. {
  2133. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2134. // modify the relevant style property
  2135. if (isset($this->_mapCellXfIndex[$ixfe]))
  2136. {
  2137. $diagonal = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getDiagonal();
  2138. $diagonal->getColor()->setRGB($rgb);
  2139. unset($diagonal->colorIndex); // normal color index does not apply, discard
  2140. }
  2141. }
  2142. break;
  2143. case 13 : // font color
  2144. $xclfType = self :: _GetInt2d($extData, 0); // color type
  2145. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2146. if ($xclfType == 2)
  2147. {
  2148. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2149. // modify the relevant style property
  2150. if (isset($this->_mapCellXfIndex[$ixfe]))
  2151. {
  2152. $font = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFont();
  2153. $font->getColor()->setRGB($rgb);
  2154. unset($font->colorIndex); // normal color index does not apply, discard
  2155. }
  2156. }
  2157. break;
  2158. }
  2159. $offset += $cb;
  2160. }
  2161. }
  2162. }
  2163. /**
  2164. * Read STYLE record
  2165. */
  2166. private function _readStyle()
  2167. {
  2168. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2169. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2170. // move stream pointer to next record
  2171. $this->_pos += 4 + $length;
  2172. if (! $this->_readDataOnly)
  2173. {
  2174. // offset: 0; size: 2; index to XF record and flag for built-in style
  2175. $ixfe = self :: _GetInt2d($recordData, 0);
  2176. // bit: 11-0; mask 0x0FFF; index to XF record
  2177. $xfIndex = (0x0FFF & $ixfe) >> 0;
  2178. // bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style
  2179. $isBuiltIn = (bool) ((0x8000 & $ixfe) >> 15);
  2180. if ($isBuiltIn)
  2181. {
  2182. // offset: 2; size: 1; identifier for built-in style
  2183. $builtInId = ord($recordData{2});
  2184. switch ($builtInId)
  2185. {
  2186. case 0x00 :
  2187. // currently, we are not using this for anything
  2188. break;
  2189. default :
  2190. break;
  2191. }
  2192. }
  2193. else
  2194. {
  2195. // user-defined; not supported by PHPExcel
  2196. }
  2197. }
  2198. }
  2199. /**
  2200. * Read PALETTE record
  2201. */
  2202. private function _readPalette()
  2203. {
  2204. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2205. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2206. // move stream pointer to next record
  2207. $this->_pos += 4 + $length;
  2208. if (! $this->_readDataOnly)
  2209. {
  2210. // offset: 0; size: 2; number of following colors
  2211. $nm = self :: _GetInt2d($recordData, 0);
  2212. // list of RGB colors
  2213. for($i = 0; $i < $nm; ++ $i)
  2214. {
  2215. $rgb = substr($recordData, 2 + 4 * $i, 4);
  2216. $this->_palette[] = self :: _readRGB($rgb);
  2217. }
  2218. }
  2219. }
  2220. /**
  2221. * SHEET
  2222. *
  2223. * This record is located in the Workbook Globals
  2224. * Substream and represents a sheet inside the workbook.
  2225. * One SHEET record is written for each sheet. It stores the
  2226. * sheet name and a stream offset to the BOF record of the
  2227. * respective Sheet Substream within the Workbook Stream.
  2228. *
  2229. * -- "OpenOffice.org's Documentation of the Microsoft
  2230. * Excel File Format"
  2231. */
  2232. private function _readSheet()
  2233. {
  2234. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2235. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2236. // move stream pointer to next record
  2237. $this->_pos += 4 + $length;
  2238. // offset: 0; size: 4; absolute stream position of the BOF record of the sheet
  2239. $rec_offset = self :: _GetInt4d($recordData, 0);
  2240. // offset: 4; size: 1; sheet state
  2241. switch (ord($recordData{4}))
  2242. {
  2243. case 0x00 :
  2244. $sheetState = PHPExcel_Worksheet :: SHEETSTATE_VISIBLE;
  2245. break;
  2246. case 0x01 :
  2247. $sheetState = PHPExcel_Worksheet :: SHEETSTATE_HIDDEN;
  2248. break;
  2249. case 0x02 :
  2250. $sheetState = PHPExcel_Worksheet :: SHEETSTATE_VERYHIDDEN;
  2251. break;
  2252. default :
  2253. $sheetState = PHPExcel_Worksheet :: SHEETSTATE_VISIBLE;
  2254. break;
  2255. }
  2256. // offset: 5; size: 1; sheet type
  2257. $sheetType = ord($recordData{5});
  2258. // offset: 6; size: var; sheet name
  2259. if ($this->_version == self :: XLS_BIFF8)
  2260. {
  2261. $string = self :: _readUnicodeStringShort(substr($recordData, 6));
  2262. $rec_name = $string['value'];
  2263. }
  2264. elseif ($this->_version == self :: XLS_BIFF7)
  2265. {
  2266. $string = $this->_readByteStringShort(substr($recordData, 6));
  2267. $rec_name = $string['value'];
  2268. }
  2269. $this->_sheets[] = array('name' => $rec_name, 'offset' => $rec_offset, 'sheetState' => $sheetState,
  2270. 'sheetType' => $sheetType);
  2271. }
  2272. /**
  2273. * Read EXTERNALBOOK record
  2274. */
  2275. private function _readExternalBook()
  2276. {
  2277. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2278. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2279. // move stream pointer to next record
  2280. $this->_pos += 4 + $length;
  2281. // offset within record data
  2282. $offset = 0;
  2283. // there are 4 types of records
  2284. if (strlen($recordData) > 4)
  2285. {
  2286. // external reference
  2287. // offset: 0; size: 2; number of sheet names ($nm)
  2288. $nm = self :: _GetInt2d($recordData, 0);
  2289. $offset += 2;
  2290. // offset: 2; size: var; encoded URL without sheet name (Unicode string, 16-bit length)
  2291. $encodedUrlString = self :: _readUnicodeStringLong(substr($recordData, 2));
  2292. $offset += $encodedUrlString['size'];
  2293. // offset: var; size: var; list of $nm sheet names (Unicode strings, 16-bit length)
  2294. $externalSheetNames = array();
  2295. for($i = 0; $i < $nm; ++ $i)
  2296. {
  2297. $externalSheetNameString = self :: _readUnicodeStringLong(substr($recordData, $offset));
  2298. $externalSheetNames[] = $externalSheetNameString['value'];
  2299. $offset += $externalSheetNameString['size'];
  2300. }
  2301. // store the record data
  2302. $this->_externalBooks[] = array('type' => 'external',
  2303. 'encodedUrl' => $encodedUrlString['value'], 'externalSheetNames' => $externalSheetNames);
  2304. }
  2305. elseif (substr($recordData, 2, 2) == pack('CC', 0x01, 0x04))
  2306. {
  2307. // internal reference
  2308. // offset: 0; size: 2; number of sheet in this document
  2309. // offset: 2; size: 2; 0x01 0x04
  2310. $this->_externalBooks[] = array('type' => 'internal');
  2311. }
  2312. elseif (substr($recordData, 0, 4) == pack('vCC', 0x0001, 0x01, 0x3A))
  2313. {
  2314. // add-in function
  2315. // offset: 0; size: 2; 0x0001
  2316. $this->_externalBooks[] = array('type' => 'addInFunction');
  2317. }
  2318. elseif (substr($recordData, 0, 2) == pack('v', 0x0000))
  2319. {
  2320. // DDE links, OLE links
  2321. // offset: 0; size: 2; 0x0000
  2322. // offset: 2; size: var; encoded source document name
  2323. $this->_externalBooks[] = array('type' => 'DDEorOLE');
  2324. }
  2325. }
  2326. /**
  2327. * Read EXTERNNAME record.
  2328. */
  2329. private function _readExternName()
  2330. {
  2331. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2332. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2333. // move stream pointer to next record
  2334. $this->_pos += 4 + $length;
  2335. // external sheet references provided for named cells
  2336. if ($this->_version == self :: XLS_BIFF8)
  2337. {
  2338. // offset: 0; size: 2; options
  2339. $options = self :: _GetInt2d($recordData, 0);
  2340. // offset: 2; size: 2;
  2341. // offset: 4; size: 2; not used
  2342. // offset: 6; size: var
  2343. $nameString = self :: _readUnicodeStringShort(substr($recordData, 6));
  2344. // offset: var; size: var; formula data
  2345. $offset = 6 + $nameString['size'];
  2346. $formula = $this->_getFormulaFromStructure(substr($recordData, $offset));
  2347. $this->_externalNames[] = array('name' => $nameString['value'], 'formula' => $formula);
  2348. }
  2349. }
  2350. /**
  2351. * Read EXTERNSHEET record
  2352. */
  2353. private function _readExternSheet()
  2354. {
  2355. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2356. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2357. // move stream pointer to next record
  2358. $this->_pos += 4 + $length;
  2359. // external sheet references provided for named cells
  2360. if ($this->_version == self :: XLS_BIFF8)
  2361. {
  2362. // offset: 0; size: 2; number of following ref structures
  2363. $nm = self :: _GetInt2d($recordData, 0);
  2364. for($i = 0; $i < $nm; ++ $i)
  2365. {
  2366. $this->_ref[] = array(// offset: 2 + 6 * $i; index to EXTERNALBOOK record
  2367. 'externalBookIndex' => self :: _GetInt2d($recordData, 2 + 6 * $i),
  2368. // offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
  2369. 'firstSheetIndex' => self :: _GetInt2d($recordData, 4 + 6 * $i),
  2370. // offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
  2371. 'lastSheetIndex' => self :: _GetInt2d($recordData, 6 + 6 * $i));
  2372. }
  2373. }
  2374. }
  2375. /**
  2376. * DEFINEDNAME
  2377. *
  2378. * This record is part of a Link Table. It contains the name
  2379. * and the token array of an internal defined name. Token
  2380. * arrays of defined names contain tokens with aberrant
  2381. * token classes.
  2382. *
  2383. * -- "OpenOffice.org's Documentation of the Microsoft
  2384. * Excel File Format"
  2385. */
  2386. private function _readDefinedName()
  2387. {
  2388. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2389. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2390. // move stream pointer to next record
  2391. $this->_pos += 4 + $length;
  2392. if ($this->_version == self :: XLS_BIFF8)
  2393. {
  2394. // retrieves named cells
  2395. // offset: 0; size: 2; option flags
  2396. $opts = self :: _GetInt2d($recordData, 0);
  2397. // bit: 5; mask: 0x0020; 0 = user-defined name, 1 = built-in-name
  2398. $isBuiltInName = (0x0020 & $opts) >> 5;
  2399. // offset: 2; size: 1; keyboard shortcut
  2400. // offset: 3; size: 1; length of the name (character count)
  2401. $nlen = ord($recordData{3});
  2402. // offset: 4; size: 2; size of the formula data (it can happen that this is zero)
  2403. // note: there can also be additional data, this is not included in $flen
  2404. $flen = self :: _GetInt2d($recordData, 4);
  2405. // offset: 8; size: 2; 0=Global name, otherwise index to sheet (1-based)
  2406. $scope = self :: _GetInt2d($recordData, 8);
  2407. // offset: 14; size: var; Name (Unicode string without length field)
  2408. $string = self :: _readUnicodeString(substr($recordData, 14), $nlen);
  2409. // offset: var; size: $flen; formula data
  2410. $offset = 14 + $string['size'];
  2411. $formulaStructure = pack('v', $flen) . substr($recordData, $offset);
  2412. try
  2413. {
  2414. $formula = $this->_getFormulaFromStructure($formulaStructure);
  2415. }
  2416. catch (Exception $e)
  2417. {
  2418. $formula = '';
  2419. }
  2420. $this->_definedname[] = array('isBuiltInName' => $isBuiltInName, 'name' => $string['value'],
  2421. 'formula' => $formula, 'scope' => $scope);
  2422. }
  2423. }
  2424. /**
  2425. * Read MSODRAWINGGROUP record
  2426. */
  2427. private function _readMsoDrawingGroup()
  2428. {
  2429. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2430. // get spliced record data
  2431. $splicedRecordData = $this->_getSplicedRecordData();
  2432. $recordData = $splicedRecordData['recordData'];
  2433. $this->_drawingGroupData .= $recordData;
  2434. }
  2435. /**
  2436. * SST - Shared String Table
  2437. *
  2438. * This record contains a list of all strings used anywhere
  2439. * in the workbook. Each string occurs only once. The
  2440. * workbook uses indexes into the list to reference the
  2441. * strings.
  2442. *
  2443. * -- "OpenOffice.org's Documentation of the Microsoft
  2444. * Excel File Format"
  2445. **/
  2446. private function _readSst()
  2447. {
  2448. // offset within (spliced) record data
  2449. $pos = 0;
  2450. // get spliced record data
  2451. $splicedRecordData = $this->_getSplicedRecordData();
  2452. $recordData = $splicedRecordData['recordData'];
  2453. $spliceOffsets = $splicedRecordData['spliceOffsets'];
  2454. // offset: 0; size: 4; total number of strings in the workbook
  2455. $pos += 4;
  2456. // offset: 4; size: 4; number of following strings ($nm)
  2457. $nm = self :: _GetInt4d($recordData, 4);
  2458. $pos += 4;
  2459. // loop through the Unicode strings (16-bit length)
  2460. for($i = 0; $i < $nm; ++ $i)
  2461. {
  2462. // number of characters in the Unicode string
  2463. $numChars = self :: _GetInt2d($recordData, $pos);
  2464. $pos += 2;
  2465. // option flags
  2466. $optionFlags = ord($recordData{$pos});
  2467. ++ $pos;
  2468. // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
  2469. $isCompressed = (($optionFlags & 0x01) == 0);
  2470. // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
  2471. $hasAsian = (($optionFlags & 0x04) != 0);
  2472. // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
  2473. $hasRichText = (($optionFlags & 0x08) != 0);
  2474. if ($hasRichText)
  2475. {
  2476. // number of Rich-Text formatting runs
  2477. $formattingRuns = self :: _GetInt2d($recordData, $pos);
  2478. $pos += 2;
  2479. }
  2480. if ($hasAsian)
  2481. {
  2482. // size of Asian phonetic setting
  2483. $extendedRunLength = self :: _GetInt4d($recordData, $pos);
  2484. $pos += 4;
  2485. }
  2486. // expected byte length of character array if not split
  2487. $len = ($isCompressed) ? $numChars : $numChars * 2;
  2488. // look up limit position
  2489. foreach ($spliceOffsets as $spliceOffset)
  2490. {
  2491. // it can happen that the string is empty, therefore we need
  2492. // <= and not just <
  2493. if ($pos <= $spliceOffset)
  2494. {
  2495. $limitpos = $spliceOffset;
  2496. break;
  2497. }
  2498. }
  2499. if ($pos + $len <= $limitpos)
  2500. {
  2501. // character array is not split between records
  2502. $retstr = substr($recordData, $pos, $len);
  2503. $pos += $len;
  2504. }
  2505. else
  2506. {
  2507. // character array is split between records
  2508. // first part of character array
  2509. $retstr = substr($recordData, $pos, $limitpos - $pos);
  2510. $bytesRead = $limitpos - $pos;
  2511. // remaining characters in Unicode string
  2512. $charsLeft = $numChars - (($isCompressed) ? $bytesRead : ($bytesRead / 2));
  2513. $pos = $limitpos;
  2514. // keep reading the characters
  2515. while ($charsLeft > 0)
  2516. {
  2517. // look up next limit position, in case the string span more than one continue record
  2518. foreach ($spliceOffsets as $spliceOffset)
  2519. {
  2520. if ($pos < $spliceOffset)
  2521. {
  2522. $limitpos = $spliceOffset;
  2523. break;
  2524. }
  2525. }
  2526. // repeated option flags
  2527. // OpenOffice.org documentation 5.21
  2528. $option = ord($recordData{$pos});
  2529. ++ $pos;
  2530. if ($isCompressed && ($option == 0))
  2531. {
  2532. // 1st fragment compressed
  2533. // this fragment compressed
  2534. $len = min($charsLeft, $limitpos - $pos);
  2535. $retstr .= substr($recordData, $pos, $len);
  2536. $charsLeft -= $len;
  2537. $isCompressed = true;
  2538. }
  2539. elseif (! $isCompressed && ($option != 0))
  2540. {
  2541. // 1st fragment uncompressed
  2542. // this fragment uncompressed
  2543. $len = min($charsLeft * 2, $limitpos - $pos);
  2544. $retstr .= substr($recordData, $pos, $len);
  2545. $charsLeft -= $len / 2;
  2546. $isCompressed = false;
  2547. }
  2548. elseif (! $isCompressed && ($option == 0))
  2549. {
  2550. // 1st fragment uncompressed
  2551. // this fragment compressed
  2552. $len = min($charsLeft, $limitpos - $pos);
  2553. for($j = 0; $j < $len; ++ $j)
  2554. {
  2555. $retstr .= $recordData{$pos + $j} . chr(0);
  2556. }
  2557. $charsLeft -= $len;
  2558. $isCompressed = false;
  2559. }
  2560. else
  2561. {
  2562. // 1st fragment compressed
  2563. // this fragment uncompressed
  2564. $newstr = '';
  2565. for($j = 0; $j < strlen($retstr); ++ $j)
  2566. {
  2567. $newstr .= $retstr[$j] . chr(0);
  2568. }
  2569. $retstr = $newstr;
  2570. $len = min($charsLeft * 2, $limitpos - $pos);
  2571. $retstr .= substr($recordData, $pos, $len);
  2572. $charsLeft -= $len / 2;
  2573. $isCompressed = false;
  2574. }
  2575. $pos += $len;
  2576. }
  2577. }
  2578. // convert to UTF-8
  2579. $retstr = self :: _encodeUTF16($retstr, $isCompressed);
  2580. // read additional Rich-Text information, if any
  2581. $fmtRuns = array();
  2582. if ($hasRichText)
  2583. {
  2584. // list of formatting runs
  2585. for($j = 0; $j < $formattingRuns; ++ $j)
  2586. {
  2587. // first formatted character; zero-based
  2588. $charPos = self :: _GetInt2d($recordData, $pos + $j * 4);
  2589. // index to font record
  2590. $fontIndex = self :: _GetInt2d($recordData, $pos + 2 + $j * 4);
  2591. $fmtRuns[] = array('charPos' => $charPos, 'fontIndex' => $fontIndex);
  2592. }
  2593. $pos += 4 * $formattingRuns;
  2594. }
  2595. // read additional Asian phonetics information, if any
  2596. if ($hasAsian)
  2597. {
  2598. // For Asian phonetic settings, we skip the extended string data
  2599. $pos += $extendedRunLength;
  2600. }
  2601. // store the shared sting
  2602. $this->_sst[] = array('value' => $retstr, 'fmtRuns' => $fmtRuns);
  2603. }
  2604. // _getSplicedRecordData() takes care of moving current position in data stream
  2605. }
  2606. /**
  2607. * Read PRINTGRIDLINES record
  2608. */
  2609. private function _readPrintGridlines()
  2610. {
  2611. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2612. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2613. // move stream pointer to next record
  2614. $this->_pos += 4 + $length;
  2615. if ($this->_version == self :: XLS_BIFF8 && ! $this->_readDataOnly)
  2616. {
  2617. // offset: 0; size: 2; 0 = do not print sheet grid lines; 1 = print sheet gridlines
  2618. $printGridlines = (bool) self :: _GetInt2d($recordData, 0);
  2619. $this->_phpSheet->setPrintGridlines($printGridlines);
  2620. }
  2621. }
  2622. /**
  2623. * Read DEFAULTROWHEIGHT record
  2624. */
  2625. private function _readDefaultRowHeight()
  2626. {
  2627. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2628. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2629. // move stream pointer to next record
  2630. $this->_pos += 4 + $length;
  2631. // offset: 0; size: 2; option flags
  2632. // offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
  2633. $height = self :: _GetInt2d($recordData, 2);
  2634. $this->_phpSheet->getDefaultRowDimension()->setRowHeight($height / 20);
  2635. }
  2636. /**
  2637. * Read SHEETPR record
  2638. */
  2639. private function _readSheetPr()
  2640. {
  2641. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2642. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2643. // move stream pointer to next record
  2644. $this->_pos += 4 + $length;
  2645. // offset: 0; size: 2
  2646. // bit: 6; mask: 0x0040; 0 = outline buttons above outline group
  2647. $isSummaryBelow = (0x0040 & self :: _GetInt2d($recordData, 0)) >> 6;
  2648. $this->_phpSheet->setShowSummaryBelow($isSummaryBelow);
  2649. // bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
  2650. $isSummaryRight = (0x0080 & self :: _GetInt2d($recordData, 0)) >> 7;
  2651. $this->_phpSheet->setShowSummaryRight($isSummaryRight);
  2652. // bit: 8; mask: 0x100; 0 = scale printout in percent, 1 = fit printout to number of pages
  2653. // this corresponds to radio button setting in page setup dialog in Excel
  2654. $this->_isFitToPages = (bool) ((0x0100 & self :: _GetInt2d($recordData, 0)) >> 8);
  2655. }
  2656. /**
  2657. * Read HORIZONTALPAGEBREAKS record
  2658. */
  2659. private function _readHorizontalPageBreaks()
  2660. {
  2661. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2662. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2663. // move stream pointer to next record
  2664. $this->_pos += 4 + $length;
  2665. if ($this->_version == self :: XLS_BIFF8 && ! $this->_readDataOnly)
  2666. {
  2667. // offset: 0; size: 2; number of the following row index structures
  2668. $nm = self :: _GetInt2d($recordData, 0);
  2669. // offset: 2; size: 6 * $nm; list of $nm row index structures
  2670. for($i = 0; $i < $nm; ++ $i)
  2671. {
  2672. $r = self :: _GetInt2d($recordData, 2 + 6 * $i);
  2673. $cf = self :: _GetInt2d($recordData, 2 + 6 * $i + 2);
  2674. $cl = self :: _GetInt2d($recordData, 2 + 6 * $i + 4);
  2675. // not sure why two column indexes are necessary?
  2676. $this->_phpSheet->setBreakByColumnAndRow($cf, $r, PHPExcel_Worksheet :: BREAK_ROW);
  2677. }
  2678. }
  2679. }
  2680. /**
  2681. * Read VERTICALPAGEBREAKS record
  2682. */
  2683. private function _readVerticalPageBreaks()
  2684. {
  2685. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2686. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2687. // move stream pointer to next record
  2688. $this->_pos += 4 + $length;
  2689. if ($this->_version == self :: XLS_BIFF8 && ! $this->_readDataOnly)
  2690. {
  2691. // offset: 0; size: 2; number of the following column index structures
  2692. $nm = self :: _GetInt2d($recordData, 0);
  2693. // offset: 2; size: 6 * $nm; list of $nm row index structures
  2694. for($i = 0; $i < $nm; ++ $i)
  2695. {
  2696. $c = self :: _GetInt2d($recordData, 2 + 6 * $i);
  2697. $rf = self :: _GetInt2d($recordData, 2 + 6 * $i + 2);
  2698. $rl = self :: _GetInt2d($recordData, 2 + 6 * $i + 4);
  2699. // not sure why two row indexes are necessary?
  2700. $this->_phpSheet->setBreakByColumnAndRow($c, $rf, PHPExcel_Worksheet :: BREAK_COLUMN);
  2701. }
  2702. }
  2703. }
  2704. /**
  2705. * Read HEADER record
  2706. */
  2707. private function _readHeader()
  2708. {
  2709. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2710. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2711. // move stream pointer to next record
  2712. $this->_pos += 4 + $length;
  2713. if (! $this->_readDataOnly)
  2714. {
  2715. // offset: 0; size: var
  2716. // realized that $recordData can be empty even when record exists
  2717. if ($recordData)
  2718. {
  2719. if ($this->_version == self :: XLS_BIFF8)
  2720. {
  2721. $string = self :: _readUnicodeStringLong($recordData);
  2722. }
  2723. else
  2724. {
  2725. $string = $this->_readByteStringShort($recordData);
  2726. }
  2727. $this->_phpSheet->getHeaderFooter()->setOddHeader($string['value']);
  2728. $this->_phpSheet->getHeaderFooter()->setEvenHeader($string['value']);
  2729. }
  2730. }
  2731. }
  2732. /**
  2733. * Read FOOTER record
  2734. */
  2735. private function _readFooter()
  2736. {
  2737. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2738. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2739. // move stream pointer to next record
  2740. $this->_pos += 4 + $length;
  2741. if (! $this->_readDataOnly)
  2742. {
  2743. // offset: 0; size: var
  2744. // realized that $recordData can be empty even when record exists
  2745. if ($recordData)
  2746. {
  2747. if ($this->_version == self :: XLS_BIFF8)
  2748. {
  2749. $string = self :: _readUnicodeStringLong($recordData);
  2750. }
  2751. else
  2752. {
  2753. $string = $this->_readByteStringShort($recordData);
  2754. }
  2755. $this->_phpSheet->getHeaderFooter()->setOddFooter($string['value']);
  2756. $this->_phpSheet->getHeaderFooter()->setEvenFooter($string['value']);
  2757. }
  2758. }
  2759. }
  2760. /**
  2761. * Read HCENTER record
  2762. */
  2763. private function _readHcenter()
  2764. {
  2765. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2766. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2767. // move stream pointer to next record
  2768. $this->_pos += 4 + $length;
  2769. if (! $this->_readDataOnly)
  2770. {
  2771. // offset: 0; size: 2; 0 = print sheet left aligned, 1 = print sheet centered horizontally
  2772. $isHorizontalCentered = (bool) self :: _GetInt2d($recordData, 0);
  2773. $this->_phpSheet->getPageSetup()->setHorizontalCentered($isHorizontalCentered);
  2774. }
  2775. }
  2776. /**
  2777. * Read VCENTER record
  2778. */
  2779. private function _readVcenter()
  2780. {
  2781. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2782. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2783. // move stream pointer to next record
  2784. $this->_pos += 4 + $length;
  2785. if (! $this->_readDataOnly)
  2786. {
  2787. // offset: 0; size: 2; 0 = print sheet aligned at top page border, 1 = print sheet vertically centered
  2788. $isVerticalCentered = (bool) self :: _GetInt2d($recordData, 0);
  2789. $this->_phpSheet->getPageSetup()->setVerticalCentered($isVerticalCentered);
  2790. }
  2791. }
  2792. /**
  2793. * Read LEFTMARGIN record
  2794. */
  2795. private function _readLeftMargin()
  2796. {
  2797. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2798. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2799. // move stream pointer to next record
  2800. $this->_pos += 4 + $length;
  2801. if (! $this->_readDataOnly)
  2802. {
  2803. // offset: 0; size: 8
  2804. $this->_phpSheet->getPageMargins()->setLeft(self :: _extractNumber($recordData));
  2805. }
  2806. }
  2807. /**
  2808. * Read RIGHTMARGIN record
  2809. */
  2810. private function _readRightMargin()
  2811. {
  2812. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2813. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2814. // move stream pointer to next record
  2815. $this->_pos += 4 + $length;
  2816. if (! $this->_readDataOnly)
  2817. {
  2818. // offset: 0; size: 8
  2819. $this->_phpSheet->getPageMargins()->setRight(self :: _extractNumber($recordData));
  2820. }
  2821. }
  2822. /**
  2823. * Read TOPMARGIN record
  2824. */
  2825. private function _readTopMargin()
  2826. {
  2827. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2828. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2829. // move stream pointer to next record
  2830. $this->_pos += 4 + $length;
  2831. if (! $this->_readDataOnly)
  2832. {
  2833. // offset: 0; size: 8
  2834. $this->_phpSheet->getPageMargins()->setTop(self :: _extractNumber($recordData));
  2835. }
  2836. }
  2837. /**
  2838. * Read BOTTOMMARGIN record
  2839. */
  2840. private function _readBottomMargin()
  2841. {
  2842. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2843. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2844. // move stream pointer to next record
  2845. $this->_pos += 4 + $length;
  2846. if (! $this->_readDataOnly)
  2847. {
  2848. // offset: 0; size: 8
  2849. $this->_phpSheet->getPageMargins()->setBottom(self :: _extractNumber($recordData));
  2850. }
  2851. }
  2852. /**
  2853. * Read PAGESETUP record
  2854. */
  2855. private function _readPageSetup()
  2856. {
  2857. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2858. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2859. // move stream pointer to next record
  2860. $this->_pos += 4 + $length;
  2861. if (! $this->_readDataOnly)
  2862. {
  2863. // offset: 0; size: 2; paper size
  2864. $paperSize = self :: _GetInt2d($recordData, 0);
  2865. // offset: 2; size: 2; scaling factor
  2866. $scale = self :: _GetInt2d($recordData, 2);
  2867. // offset: 6; size: 2; fit worksheet width to this number of pages, 0 = use as many as needed
  2868. $fitToWidth = self :: _GetInt2d($recordData, 6);
  2869. // offset: 8; size: 2; fit worksheet height to this number of pages, 0 = use as many as needed
  2870. $fitToHeight = self :: _GetInt2d($recordData, 8);
  2871. // offset: 10; size: 2; option flags
  2872. // bit: 1; mask: 0x0002; 0=landscape, 1=portrait
  2873. $isPortrait = (0x0002 & self :: _GetInt2d($recordData, 10)) >> 1;
  2874. // bit: 2; mask: 0x0004; 1= paper size, scaling factor, paper orient. not init
  2875. // when this bit is set, do not use flags for those properties
  2876. $isNotInit = (0x0004 & self :: _GetInt2d($recordData, 10)) >> 2;
  2877. if (! $isNotInit)
  2878. {
  2879. $this->_phpSheet->getPageSetup()->setPaperSize($paperSize);
  2880. switch ($isPortrait)
  2881. {
  2882. case 0 :
  2883. $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup :: ORIENTATION_LANDSCAPE);
  2884. break;
  2885. case 1 :
  2886. $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup :: ORIENTATION_PORTRAIT);
  2887. break;
  2888. }
  2889. $this->_phpSheet->getPageSetup()->setScale($scale, false);
  2890. $this->_phpSheet->getPageSetup()->setFitToPage((bool) $this->_isFitToPages);
  2891. $this->_phpSheet->getPageSetup()->setFitToWidth($fitToWidth, false);
  2892. $this->_phpSheet->getPageSetup()->setFitToHeight($fitToHeight, false);
  2893. }
  2894. // offset: 16; size: 8; header margin (IEEE 754 floating-point value)
  2895. $marginHeader = self :: _extractNumber(substr($recordData, 16, 8));
  2896. $this->_phpSheet->getPageMargins()->setHeader($marginHeader);
  2897. // offset: 24; size: 8; footer margin (IEEE 754 floating-point value)
  2898. $marginFooter = self :: _extractNumber(substr($recordData, 24, 8));
  2899. $this->_phpSheet->getPageMargins()->setFooter($marginFooter);
  2900. }
  2901. }
  2902. /**
  2903. * PROTECT - Sheet protection (BIFF2 through BIFF8)
  2904. * if this record is omitted, then it also means no sheet protection
  2905. */
  2906. private function _readProtect()
  2907. {
  2908. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2909. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2910. // move stream pointer to next record
  2911. $this->_pos += 4 + $length;
  2912. if ($this->_readDataOnly)
  2913. {
  2914. return;
  2915. }
  2916. // offset: 0; size: 2;
  2917. // bit 0, mask 0x01; 1 = sheet is protected
  2918. $bool = (0x01 & self :: _GetInt2d($recordData, 0)) >> 0;
  2919. $this->_phpSheet->getProtection()->setSheet((bool) $bool);
  2920. }
  2921. /**
  2922. * SCENPROTECT
  2923. */
  2924. private function _readScenProtect()
  2925. {
  2926. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2927. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2928. // move stream pointer to next record
  2929. $this->_pos += 4 + $length;
  2930. if ($this->_readDataOnly)
  2931. {
  2932. return;
  2933. }
  2934. // offset: 0; size: 2;
  2935. // bit: 0, mask 0x01; 1 = scenarios are protected
  2936. $bool = (0x01 & self :: _GetInt2d($recordData, 0)) >> 0;
  2937. $this->_phpSheet->getProtection()->setScenarios((bool) $bool);
  2938. }
  2939. /**
  2940. * OBJECTPROTECT
  2941. */
  2942. private function _readObjectProtect()
  2943. {
  2944. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2945. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2946. // move stream pointer to next record
  2947. $this->_pos += 4 + $length;
  2948. if ($this->_readDataOnly)
  2949. {
  2950. return;
  2951. }
  2952. // offset: 0; size: 2;
  2953. // bit: 0, mask 0x01; 1 = objects are protected
  2954. $bool = (0x01 & self :: _GetInt2d($recordData, 0)) >> 0;
  2955. $this->_phpSheet->getProtection()->setObjects((bool) $bool);
  2956. }
  2957. /**
  2958. * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
  2959. */
  2960. private function _readPassword()
  2961. {
  2962. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2963. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2964. // move stream pointer to next record
  2965. $this->_pos += 4 + $length;
  2966. if (! $this->_readDataOnly)
  2967. {
  2968. // offset: 0; size: 2; 16-bit hash value of password
  2969. $password = strtoupper(dechex(self :: _GetInt2d($recordData, 0))); // the hashed password
  2970. $this->_phpSheet->getProtection()->setPassword($password, true);
  2971. }
  2972. }
  2973. /**
  2974. * Read DEFCOLWIDTH record
  2975. */
  2976. private function _readDefColWidth()
  2977. {
  2978. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2979. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2980. // move stream pointer to next record
  2981. $this->_pos += 4 + $length;
  2982. // offset: 0; size: 2; default column width
  2983. $width = self :: _GetInt2d($recordData, 0);
  2984. if ($width != 8)
  2985. {
  2986. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width);
  2987. }
  2988. }
  2989. /**
  2990. * Read COLINFO record
  2991. */
  2992. private function _readColInfo()
  2993. {
  2994. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  2995. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2996. // move stream pointer to next record
  2997. $this->_pos += 4 + $length;
  2998. if (! $this->_readDataOnly)
  2999. {
  3000. // offset: 0; size: 2; index to first column in range
  3001. $fc = self :: _GetInt2d($recordData, 0); // first column index
  3002. // offset: 2; size: 2; index to last column in range
  3003. $lc = self :: _GetInt2d($recordData, 2); // first column index
  3004. // offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
  3005. $width = self :: _GetInt2d($recordData, 4);
  3006. // offset: 6; size: 2; index to XF record for default column formatting
  3007. $xfIndex = self :: _GetInt2d($recordData, 6);
  3008. // offset: 8; size: 2; option flags
  3009. // bit: 0; mask: 0x0001; 1= columns are hidden
  3010. $isHidden = (0x0001 & self :: _GetInt2d($recordData, 8)) >> 0;
  3011. // bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
  3012. $level = (0x0700 & self :: _GetInt2d($recordData, 8)) >> 8;
  3013. // bit: 12; mask: 0x1000; 1 = collapsed
  3014. $isCollapsed = (0x1000 & self :: _GetInt2d($recordData, 8)) >> 12;
  3015. // offset: 10; size: 2; not used
  3016. for($i = $fc; $i <= $lc; ++ $i)
  3017. {
  3018. if ($lc == 255 || $lc == 256)
  3019. {
  3020. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width / 256);
  3021. break;
  3022. }
  3023. $this->_phpSheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
  3024. $this->_phpSheet->getColumnDimensionByColumn($i)->setVisible(! $isHidden);
  3025. $this->_phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  3026. $this->_phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
  3027. $this->_phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3028. }
  3029. }
  3030. }
  3031. /**
  3032. * ROW
  3033. *
  3034. * This record contains the properties of a single row in a
  3035. * sheet. Rows and cells in a sheet are divided into blocks
  3036. * of 32 rows.
  3037. *
  3038. * -- "OpenOffice.org's Documentation of the Microsoft
  3039. * Excel File Format"
  3040. */
  3041. private function _readRow()
  3042. {
  3043. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3044. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3045. // move stream pointer to next record
  3046. $this->_pos += 4 + $length;
  3047. if (! $this->_readDataOnly)
  3048. {
  3049. // offset: 0; size: 2; index of this row
  3050. $r = self :: _GetInt2d($recordData, 0);
  3051. // offset: 2; size: 2; index to column of the first cell which is described by a cell record
  3052. // offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
  3053. // offset: 6; size: 2;
  3054. // bit: 14-0; mask: 0x7FFF; height of the row, in twips = 1/20 of a point
  3055. $height = (0x7FFF & self :: _GetInt2d($recordData, 6)) >> 0;
  3056. // bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
  3057. $useDefaultHeight = (0x8000 & self :: _GetInt2d($recordData, 6)) >> 15;
  3058. if (! $useDefaultHeight)
  3059. {
  3060. $this->_phpSheet->getRowDimension($r + 1)->setRowHeight($height / 20);
  3061. }
  3062. // offset: 8; size: 2; not used
  3063. // offset: 10; size: 2; not used in BIFF5-BIFF8
  3064. // offset: 12; size: 4; option flags and default row formatting
  3065. // bit: 2-0: mask: 0x00000007; outline level of the row
  3066. $level = (0x00000007 & self :: _GetInt4d($recordData, 12)) >> 0;
  3067. $this->_phpSheet->getRowDimension($r + 1)->setOutlineLevel($level);
  3068. // bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
  3069. $isCollapsed = (0x00000010 & self :: _GetInt4d($recordData, 12)) >> 4;
  3070. $this->_phpSheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
  3071. // bit: 5; mask: 0x00000020; 1 = row is hidden
  3072. $isHidden = (0x00000020 & self :: _GetInt4d($recordData, 12)) >> 5;
  3073. $this->_phpSheet->getRowDimension($r + 1)->setVisible(! $isHidden);
  3074. // bit: 7; mask: 0x00000080; 1 = row has explicit format
  3075. $hasExplicitFormat = (0x00000080 & self :: _GetInt4d($recordData, 12)) >> 7;
  3076. // bit: 27-16; mask: 0x0FFF0000; only applies when hasExplicitFormat = 1; index to XF record
  3077. $xfIndex = (0x0FFF0000 & self :: _GetInt4d($recordData, 12)) >> 16;
  3078. if ($hasExplicitFormat)
  3079. {
  3080. $this->_phpSheet->getRowDimension($r + 1)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3081. }
  3082. }
  3083. }
  3084. /**
  3085. * Read RK record
  3086. * This record represents a cell that contains an RK value
  3087. * (encoded integer or floating-point value). If a
  3088. * floating-point value cannot be encoded to an RK value,
  3089. * a NUMBER record will be written. This record replaces the
  3090. * record INTEGER written in BIFF2.
  3091. *
  3092. * -- "OpenOffice.org's Documentation of the Microsoft
  3093. * Excel File Format"
  3094. */
  3095. private function _readRk()
  3096. {
  3097. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3098. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3099. // move stream pointer to next record
  3100. $this->_pos += 4 + $length;
  3101. // offset: 0; size: 2; index to row
  3102. $row = self :: _GetInt2d($recordData, 0);
  3103. // offset: 2; size: 2; index to column
  3104. $column = self :: _GetInt2d($recordData, 2);
  3105. $columnString = PHPExcel_Cell :: stringFromColumnIndex($column);
  3106. // Read cell?
  3107. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3108. {
  3109. // offset: 4; size: 2; index to XF record
  3110. $xfIndex = self :: _GetInt2d($recordData, 4);
  3111. // offset: 6; size: 4; RK value
  3112. $rknum = self :: _GetInt4d($recordData, 6);
  3113. $numValue = self :: _GetIEEE754($rknum);
  3114. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3115. if (! $this->_readDataOnly)
  3116. {
  3117. // add style information
  3118. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3119. }
  3120. // add cell
  3121. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType :: TYPE_NUMERIC);
  3122. }
  3123. }
  3124. /**
  3125. * Read LABELSST record
  3126. * This record represents a cell that contains a string. It
  3127. * replaces the LABEL record and RSTRING record used in
  3128. * BIFF2-BIFF5.
  3129. *
  3130. * -- "OpenOffice.org's Documentation of the Microsoft
  3131. * Excel File Format"
  3132. */
  3133. private function _readLabelSst()
  3134. {
  3135. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3136. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3137. // move stream pointer to next record
  3138. $this->_pos += 4 + $length;
  3139. // offset: 0; size: 2; index to row
  3140. $row = self :: _GetInt2d($recordData, 0);
  3141. // offset: 2; size: 2; index to column
  3142. $column = self :: _GetInt2d($recordData, 2);
  3143. $columnString = PHPExcel_Cell :: stringFromColumnIndex($column);
  3144. // Read cell?
  3145. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3146. {
  3147. // offset: 4; size: 2; index to XF record
  3148. $xfIndex = self :: _GetInt2d($recordData, 4);
  3149. // offset: 6; size: 4; index to SST record
  3150. $index = self :: _GetInt4d($recordData, 6);
  3151. // add cell
  3152. if (($fmtRuns = $this->_sst[$index]['fmtRuns']) && ! $this->_readDataOnly)
  3153. {
  3154. // then we should treat as rich text
  3155. $richText = new PHPExcel_RichText();
  3156. $charPos = 0;
  3157. $sstCount = count($this->_sst[$index]['fmtRuns']);
  3158. for($i = 0; $i <= $sstCount; ++ $i)
  3159. {
  3160. if (isset($fmtRuns[$i]))
  3161. {
  3162. $text = PHPExcel_Shared_String :: Substring($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos);
  3163. $charPos = $fmtRuns[$i]['charPos'];
  3164. }
  3165. else
  3166. {
  3167. $text = PHPExcel_Shared_String :: Substring($this->_sst[$index]['value'], $charPos, PHPExcel_Shared_String :: CountCharacters($this->_sst[$index]['value']));
  3168. }
  3169. if (PHPExcel_Shared_String :: CountCharacters($text) > 0)
  3170. {
  3171. if ($i == 0)
  3172. { // first text run, no style
  3173. $richText->createText($text);
  3174. }
  3175. else
  3176. {
  3177. $textRun = $richText->createTextRun($text);
  3178. if (isset($fmtRuns[$i - 1]))
  3179. {
  3180. if ($fmtRuns[$i - 1]['fontIndex'] < 4)
  3181. {
  3182. $fontIndex = $fmtRuns[$i - 1]['fontIndex'];
  3183. }
  3184. else
  3185. {
  3186. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  3187. // check the OpenOffice documentation of the FONT record
  3188. $fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
  3189. }
  3190. $textRun->setFont(clone $this->_objFonts[$fontIndex]);
  3191. }
  3192. }
  3193. }
  3194. }
  3195. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3196. $cell->setValueExplicit($richText, PHPExcel_Cell_DataType :: TYPE_STRING);
  3197. }
  3198. else
  3199. {
  3200. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3201. $cell->setValueExplicit($this->_sst[$index]['value'], PHPExcel_Cell_DataType :: TYPE_STRING);
  3202. }
  3203. if (! $this->_readDataOnly)
  3204. {
  3205. // add style information
  3206. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3207. }
  3208. }
  3209. }
  3210. /**
  3211. * Read MULRK record
  3212. * This record represents a cell range containing RK value
  3213. * cells. All cells are located in the same row.
  3214. *
  3215. * -- "OpenOffice.org's Documentation of the Microsoft
  3216. * Excel File Format"
  3217. */
  3218. private function _readMulRk()
  3219. {
  3220. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3221. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3222. // move stream pointer to next record
  3223. $this->_pos += 4 + $length;
  3224. // offset: 0; size: 2; index to row
  3225. $row = self :: _GetInt2d($recordData, 0);
  3226. // offset: 2; size: 2; index to first column
  3227. $colFirst = self :: _GetInt2d($recordData, 2);
  3228. // offset: var; size: 2; index to last column
  3229. $colLast = self :: _GetInt2d($recordData, $length - 2);
  3230. $columns = $colLast - $colFirst + 1;
  3231. // offset within record data
  3232. $offset = 4;
  3233. for($i = 0; $i < $columns; ++ $i)
  3234. {
  3235. $columnString = PHPExcel_Cell :: stringFromColumnIndex($colFirst + $i);
  3236. // Read cell?
  3237. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3238. {
  3239. // offset: var; size: 2; index to XF record
  3240. $xfIndex = self :: _GetInt2d($recordData, $offset);
  3241. // offset: var; size: 4; RK value
  3242. $numValue = self :: _GetIEEE754(self :: _GetInt4d($recordData, $offset + 2));
  3243. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3244. if (! $this->_readDataOnly)
  3245. {
  3246. // add style
  3247. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3248. }
  3249. // add cell value
  3250. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType :: TYPE_NUMERIC);
  3251. }
  3252. $offset += 6;
  3253. }
  3254. }
  3255. /**
  3256. * Read NUMBER record
  3257. * This record represents a cell that contains a
  3258. * floating-point value.
  3259. *
  3260. * -- "OpenOffice.org's Documentation of the Microsoft
  3261. * Excel File Format"
  3262. */
  3263. private function _readNumber()
  3264. {
  3265. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3266. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3267. // move stream pointer to next record
  3268. $this->_pos += 4 + $length;
  3269. // offset: 0; size: 2; index to row
  3270. $row = self :: _GetInt2d($recordData, 0);
  3271. // offset: 2; size 2; index to column
  3272. $column = self :: _GetInt2d($recordData, 2);
  3273. $columnString = PHPExcel_Cell :: stringFromColumnIndex($column);
  3274. // Read cell?
  3275. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3276. {
  3277. // offset 4; size: 2; index to XF record
  3278. $xfIndex = self :: _GetInt2d($recordData, 4);
  3279. $numValue = self :: _extractNumber(substr($recordData, 6, 8));
  3280. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3281. if (! $this->_readDataOnly)
  3282. {
  3283. // add cell style
  3284. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3285. }
  3286. // add cell value
  3287. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType :: TYPE_NUMERIC);
  3288. }
  3289. }
  3290. /**
  3291. * Read FORMULA record + perhaps a following STRING record if formula result is a string
  3292. * This record contains the token array and the result of a
  3293. * formula cell.
  3294. *
  3295. * -- "OpenOffice.org's Documentation of the Microsoft
  3296. * Excel File Format"
  3297. */
  3298. private function _readFormula()
  3299. {
  3300. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3301. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3302. // move stream pointer to next record
  3303. $this->_pos += 4 + $length;
  3304. // offset: 0; size: 2; row index
  3305. $row = self :: _GetInt2d($recordData, 0);
  3306. // offset: 2; size: 2; col index
  3307. $column = self :: _GetInt2d($recordData, 2);
  3308. $columnString = PHPExcel_Cell :: stringFromColumnIndex($column);
  3309. // offset: 20: size: variable; formula structure
  3310. $formulaStructure = substr($recordData, 20);
  3311. // offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
  3312. $options = self :: _GetInt2d($recordData, 14);
  3313. // bit: 0; mask: 0x0001; 1 = recalculate always
  3314. // bit: 1; mask: 0x0002; 1 = calculate on open
  3315. // bit: 2; mask: 0x0008; 1 = part of a shared formula
  3316. $isPartOfSharedFormula = (bool) (0x0008 & $options);
  3317. // WARNING:
  3318. // We can apparently not rely on $isPartOfSharedFormula. Even when $isPartOfSharedFormula = true
  3319. // the formula data may be ordinary formula data, therefore we need to check
  3320. // explicitly for the tExp token (0x01)
  3321. $isPartOfSharedFormula = $isPartOfSharedFormula && ord($formulaStructure{2}) == 0x01;
  3322. if ($isPartOfSharedFormula)
  3323. {
  3324. // part of shared formula which means there will be a formula with a tExp token and nothing else
  3325. // get the base cell, grab tExp token
  3326. $baseRow = self :: _GetInt2d($formulaStructure, 3);
  3327. $baseCol = self :: _GetInt2d($formulaStructure, 5);
  3328. $this->_baseCell = PHPExcel_Cell :: stringFromColumnIndex($baseCol) . ($baseRow + 1);
  3329. }
  3330. // Read cell?
  3331. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3332. {
  3333. if ($isPartOfSharedFormula)
  3334. {
  3335. // formula is added to this cell after the sheet has been read
  3336. $this->_sharedFormulaParts[$columnString . ($row + 1)] = $this->_baseCell;
  3337. }
  3338. // offset: 16: size: 4; not used
  3339. // offset: 4; size: 2; XF index
  3340. $xfIndex = self :: _GetInt2d($recordData, 4);
  3341. // offset: 6; size: 8; result of the formula
  3342. if ((ord($recordData{6}) == 0) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255))
  3343. {
  3344. // String formula. Result follows in appended STRING record
  3345. $dataType = PHPExcel_Cell_DataType :: TYPE_STRING;
  3346. // read possible SHAREDFMLA record
  3347. $code = self :: _GetInt2d($this->_data, $this->_pos);
  3348. if ($code == self :: XLS_Type_SHAREDFMLA)
  3349. {
  3350. $this->_readSharedFmla();
  3351. }
  3352. // read STRING record
  3353. $value = $this->_readString();
  3354. }
  3355. elseif ((ord($recordData{6}) == 1) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255))
  3356. {
  3357. // Boolean formula. Result is in +2; 0=false, 1=true
  3358. $dataType = PHPExcel_Cell_DataType :: TYPE_BOOL;
  3359. $value = (bool) ord($recordData{8});
  3360. }
  3361. elseif ((ord($recordData{6}) == 2) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255))
  3362. {
  3363. // Error formula. Error code is in +2
  3364. $dataType = PHPExcel_Cell_DataType :: TYPE_ERROR;
  3365. $value = self :: _mapErrorCode(ord($recordData{8}));
  3366. }
  3367. elseif ((ord($recordData{6}) == 3) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255))
  3368. {
  3369. // Formula result is a null string
  3370. $dataType = PHPExcel_Cell_DataType :: TYPE_NULL;
  3371. $value = '';
  3372. }
  3373. else
  3374. {
  3375. // forumla result is a number, first 14 bytes like _NUMBER record
  3376. $dataType = PHPExcel_Cell_DataType :: TYPE_NUMERIC;
  3377. $value = self :: _extractNumber(substr($recordData, 6, 8));
  3378. }
  3379. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3380. if (! $this->_readDataOnly)
  3381. {
  3382. // add cell style
  3383. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3384. }
  3385. // store the formula
  3386. if (! $isPartOfSharedFormula)
  3387. {
  3388. // not part of shared formula
  3389. // add cell value. If we can read formula, populate with formula, otherwise just used cached value
  3390. try
  3391. {
  3392. if ($this->_version != self :: XLS_BIFF8)
  3393. {
  3394. throw new Exception('Not BIFF8. Can only read BIFF8 formulas');
  3395. }
  3396. $formula = $this->_getFormulaFromStructure($formulaStructure); // get formula in human language
  3397. $cell->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType :: TYPE_FORMULA);
  3398. }
  3399. catch (Exception $e)
  3400. {
  3401. $cell->setValueExplicit($value, $dataType);
  3402. }
  3403. }
  3404. else
  3405. {
  3406. if ($this->_version == self :: XLS_BIFF8)
  3407. {
  3408. // do nothing at this point, formula id added later in the code
  3409. }
  3410. else
  3411. {
  3412. $cell->setValueExplicit($value, $dataType);
  3413. }
  3414. }
  3415. // store the cached calculated value
  3416. $cell->setCalculatedValue($value);
  3417. }
  3418. }
  3419. /**
  3420. * Read a SHAREDFMLA record. This function just stores the binary shared formula in the reader,
  3421. * which usually contains relative references.
  3422. * These will be used to construct the formula in each shared formula part after the sheet is read.
  3423. */
  3424. private function _readSharedFmla()
  3425. {
  3426. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3427. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3428. // move stream pointer to next record
  3429. $this->_pos += 4 + $length;
  3430. // offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything
  3431. $cellRange = substr($recordData, 0, 6);
  3432. $cellRange = $this->_readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax
  3433. // offset: 6, size: 1; not used
  3434. // offset: 7, size: 1; number of existing FORMULA records for this shared formula
  3435. $no = ord($recordData{7});
  3436. // offset: 8, size: var; Binary token array of the shared formula
  3437. $formula = substr($recordData, 8);
  3438. // at this point we only store the shared formula for later use
  3439. $this->_sharedFormulas[$this->_baseCell] = $formula;
  3440. }
  3441. /**
  3442. * Read a STRING record from current stream position and advance the stream pointer to next record
  3443. * This record is used for storing result from FORMULA record when it is a string, and
  3444. * it occurs directly after the FORMULA record
  3445. *
  3446. * @return string The string contents as UTF-8
  3447. */
  3448. private function _readString()
  3449. {
  3450. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3451. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3452. // move stream pointer to next record
  3453. $this->_pos += 4 + $length;
  3454. if ($this->_version == self :: XLS_BIFF8)
  3455. {
  3456. $string = self :: _readUnicodeStringLong($recordData);
  3457. $value = $string['value'];
  3458. }
  3459. else
  3460. {
  3461. $string = $this->_readByteStringLong($recordData);
  3462. $value = $string['value'];
  3463. }
  3464. return $value;
  3465. }
  3466. /**
  3467. * Read BOOLERR record
  3468. * This record represents a Boolean value or error value
  3469. * cell.
  3470. *
  3471. * -- "OpenOffice.org's Documentation of the Microsoft
  3472. * Excel File Format"
  3473. */
  3474. private function _readBoolErr()
  3475. {
  3476. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3477. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3478. // move stream pointer to next record
  3479. $this->_pos += 4 + $length;
  3480. // offset: 0; size: 2; row index
  3481. $row = self :: _GetInt2d($recordData, 0);
  3482. // offset: 2; size: 2; column index
  3483. $column = self :: _GetInt2d($recordData, 2);
  3484. $columnString = PHPExcel_Cell :: stringFromColumnIndex($column);
  3485. // Read cell?
  3486. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3487. {
  3488. // offset: 4; size: 2; index to XF record
  3489. $xfIndex = self :: _GetInt2d($recordData, 4);
  3490. // offset: 6; size: 1; the boolean value or error value
  3491. $boolErr = ord($recordData{6});
  3492. // offset: 7; size: 1; 0=boolean; 1=error
  3493. $isError = ord($recordData{7});
  3494. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3495. switch ($isError)
  3496. {
  3497. case 0 : // boolean
  3498. $value = (bool) $boolErr;
  3499. // add cell value
  3500. $cell->setValueExplicit($value, PHPExcel_Cell_DataType :: TYPE_BOOL);
  3501. break;
  3502. case 1 : // error type
  3503. $value = self :: _mapErrorCode($boolErr);
  3504. // add cell value
  3505. $cell->setValueExplicit($value, PHPExcel_Cell_DataType :: TYPE_ERROR);
  3506. break;
  3507. }
  3508. if (! $this->_readDataOnly)
  3509. {
  3510. // add cell style
  3511. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3512. }
  3513. }
  3514. }
  3515. /**
  3516. * Read MULBLANK record
  3517. * This record represents a cell range of empty cells. All
  3518. * cells are located in the same row
  3519. *
  3520. * -- "OpenOffice.org's Documentation of the Microsoft
  3521. * Excel File Format"
  3522. */
  3523. private function _readMulBlank()
  3524. {
  3525. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3526. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3527. // move stream pointer to next record
  3528. $this->_pos += 4 + $length;
  3529. // offset: 0; size: 2; index to row
  3530. $row = self :: _GetInt2d($recordData, 0);
  3531. // offset: 2; size: 2; index to first column
  3532. $fc = self :: _GetInt2d($recordData, 2);
  3533. // offset: 4; size: 2 x nc; list of indexes to XF records
  3534. // add style information
  3535. if (! $this->_readDataOnly)
  3536. {
  3537. for($i = 0; $i < $length / 2 - 3; ++ $i)
  3538. {
  3539. $columnString = PHPExcel_Cell :: stringFromColumnIndex($fc + $i);
  3540. // Read cell?
  3541. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3542. {
  3543. $xfIndex = self :: _GetInt2d($recordData, 4 + 2 * $i);
  3544. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3545. }
  3546. }
  3547. }
  3548. // offset: 6; size 2; index to last column (not needed)
  3549. }
  3550. /**
  3551. * Read LABEL record
  3552. * This record represents a cell that contains a string. In
  3553. * BIFF8 it is usually replaced by the LABELSST record.
  3554. * Excel still uses this record, if it copies unformatted
  3555. * text cells to the clipboard.
  3556. *
  3557. * -- "OpenOffice.org's Documentation of the Microsoft
  3558. * Excel File Format"
  3559. */
  3560. private function _readLabel()
  3561. {
  3562. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3563. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3564. // move stream pointer to next record
  3565. $this->_pos += 4 + $length;
  3566. // offset: 0; size: 2; index to row
  3567. $row = self :: _GetInt2d($recordData, 0);
  3568. // offset: 2; size: 2; index to column
  3569. $column = self :: _GetInt2d($recordData, 2);
  3570. $columnString = PHPExcel_Cell :: stringFromColumnIndex($column);
  3571. // Read cell?
  3572. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3573. {
  3574. // offset: 4; size: 2; XF index
  3575. $xfIndex = self :: _GetInt2d($recordData, 4);
  3576. // add cell value
  3577. // todo: what if string is very long? continue record
  3578. if ($this->_version == self :: XLS_BIFF8)
  3579. {
  3580. $string = self :: _readUnicodeStringLong(substr($recordData, 6));
  3581. $value = $string['value'];
  3582. }
  3583. else
  3584. {
  3585. $string = $this->_readByteStringLong(substr($recordData, 6));
  3586. $value = $string['value'];
  3587. }
  3588. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3589. $cell->setValueExplicit($value, PHPExcel_Cell_DataType :: TYPE_STRING);
  3590. if (! $this->_readDataOnly)
  3591. {
  3592. // add cell style
  3593. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3594. }
  3595. }
  3596. }
  3597. /**
  3598. * Read BLANK record
  3599. */
  3600. private function _readBlank()
  3601. {
  3602. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3603. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3604. // move stream pointer to next record
  3605. $this->_pos += 4 + $length;
  3606. // offset: 0; size: 2; row index
  3607. $row = self :: _GetInt2d($recordData, 0);
  3608. // offset: 2; size: 2; col index
  3609. $col = self :: _GetInt2d($recordData, 2);
  3610. $columnString = PHPExcel_Cell :: stringFromColumnIndex($col);
  3611. // Read cell?
  3612. if (! is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()))
  3613. {
  3614. // offset: 4; size: 2; XF index
  3615. $xfIndex = self :: _GetInt2d($recordData, 4);
  3616. // add style information
  3617. if (! $this->_readDataOnly)
  3618. {
  3619. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3620. }
  3621. }
  3622. }
  3623. /**
  3624. * Read MSODRAWING record
  3625. */
  3626. private function _readMsoDrawing()
  3627. {
  3628. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3629. // get spliced record data
  3630. $splicedRecordData = $this->_getSplicedRecordData();
  3631. $recordData = $splicedRecordData['recordData'];
  3632. $this->_drawingData .= $recordData;
  3633. }
  3634. /**
  3635. * Read OBJ record
  3636. */
  3637. private function _readObj()
  3638. {
  3639. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3640. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3641. // move stream pointer to next record
  3642. $this->_pos += 4 + $length;
  3643. if ($this->_readDataOnly || $this->_version != self :: XLS_BIFF8)
  3644. {
  3645. return;
  3646. }
  3647. // recordData consists of an array of subrecords looking like this:
  3648. // ft: 2 bytes; ftCmo type (0x15)
  3649. // cb: 2 bytes; size in bytes of ftCmo data
  3650. // ot: 2 bytes; Object Type
  3651. // id: 2 bytes; Object id number
  3652. // grbit: 2 bytes; Option Flags
  3653. // data: var; subrecord data
  3654. // for now, we are just interested in the second subrecord containing the object type
  3655. $ftCmoType = self :: _GetInt2d($recordData, 0);
  3656. $cbCmoSize = self :: _GetInt2d($recordData, 2);
  3657. $otObjType = self :: _GetInt2d($recordData, 4);
  3658. $idObjID = self :: _GetInt2d($recordData, 6);
  3659. $grbitOpts = self :: _GetInt2d($recordData, 6);
  3660. $this->_objs[] = array('ftCmoType' => $ftCmoType, 'cbCmoSize' => $cbCmoSize, 'otObjType' => $otObjType,
  3661. 'idObjID' => $idObjID, 'grbitOpts' => $grbitOpts);
  3662. $this->textObjRef = $idObjID;
  3663. // echo '<b>_readObj()</b><br />';
  3664. // var_dump(end($this->_objs));
  3665. // echo '<br />';
  3666. }
  3667. /**
  3668. * Read WINDOW2 record
  3669. */
  3670. private function _readWindow2()
  3671. {
  3672. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3673. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3674. // move stream pointer to next record
  3675. $this->_pos += 4 + $length;
  3676. // offset: 0; size: 2; option flags
  3677. $options = self :: _GetInt2d($recordData, 0);
  3678. // bit: 1; mask: 0x0002; 0 = do not show gridlines, 1 = show gridlines
  3679. $showGridlines = (bool) ((0x0002 & $options) >> 1);
  3680. $this->_phpSheet->setShowGridlines($showGridlines);
  3681. // bit: 2; mask: 0x0004; 0 = do not show headers, 1 = show headers
  3682. $showRowColHeaders = (bool) ((0x0004 & $options) >> 2);
  3683. $this->_phpSheet->setShowRowColHeaders($showRowColHeaders);
  3684. // bit: 3; mask: 0x0008; 0 = panes are not frozen, 1 = panes are frozen
  3685. $this->_frozen = (bool) ((0x0008 & $options) >> 3);
  3686. // bit: 6; mask: 0x0040; 0 = columns from left to right, 1 = columns from right to left
  3687. $this->_phpSheet->setRightToLeft((bool) ((0x0040 & $options) >> 6));
  3688. // bit: 10; mask: 0x0400; 0 = sheet not active, 1 = sheet active
  3689. $isActive = (bool) ((0x0400 & $options) >> 10);
  3690. if ($isActive)
  3691. {
  3692. $this->_phpExcel->setActiveSheetIndex($this->_phpExcel->getIndex($this->_phpSheet));
  3693. }
  3694. }
  3695. /**
  3696. * Read SCL record
  3697. */
  3698. private function _readScl()
  3699. {
  3700. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3701. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3702. // move stream pointer to next record
  3703. $this->_pos += 4 + $length;
  3704. // offset: 0; size: 2; numerator of the view magnification
  3705. $numerator = self :: _GetInt2d($recordData, 0);
  3706. // offset: 2; size: 2; numerator of the view magnification
  3707. $denumerator = self :: _GetInt2d($recordData, 2);
  3708. // set the zoom scale (in percent)
  3709. $this->_phpSheet->getSheetView()->setZoomScale($numerator * 100 / $denumerator);
  3710. }
  3711. /**
  3712. * Read PANE record
  3713. */
  3714. private function _readPane()
  3715. {
  3716. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3717. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3718. // move stream pointer to next record
  3719. $this->_pos += 4 + $length;
  3720. if (! $this->_readDataOnly)
  3721. {
  3722. // offset: 0; size: 2; position of vertical split
  3723. $px = self :: _GetInt2d($recordData, 0);
  3724. // offset: 2; size: 2; position of horizontal split
  3725. $py = self :: _GetInt2d($recordData, 2);
  3726. if ($this->_frozen)
  3727. {
  3728. // frozen panes
  3729. $this->_phpSheet->freezePane(PHPExcel_Cell :: stringFromColumnIndex($px) . ($py + 1));
  3730. }
  3731. else
  3732. {
  3733. // unfrozen panes; split windows; not supported by PHPExcel core
  3734. }
  3735. }
  3736. }
  3737. /**
  3738. * Read SELECTION record. There is one such record for each pane in the sheet.
  3739. */
  3740. private function _readSelection()
  3741. {
  3742. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3743. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3744. // move stream pointer to next record
  3745. $this->_pos += 4 + $length;
  3746. if (! $this->_readDataOnly)
  3747. {
  3748. // offset: 0; size: 1; pane identifier
  3749. $paneId = ord($recordData{0});
  3750. // offset: 1; size: 2; index to row of the active cell
  3751. $r = self :: _GetInt2d($recordData, 1);
  3752. // offset: 3; size: 2; index to column of the active cell
  3753. $c = self :: _GetInt2d($recordData, 3);
  3754. // offset: 5; size: 2; index into the following cell range list to the
  3755. // entry that contains the active cell
  3756. $index = self :: _GetInt2d($recordData, 5);
  3757. // offset: 7; size: var; cell range address list containing all selected cell ranges
  3758. $data = substr($recordData, 7);
  3759. $cellRangeAddressList = $this->_readBIFF5CellRangeAddressList($data); // note: also BIFF8 uses BIFF5 syntax
  3760. $selectedCells = $cellRangeAddressList['cellRangeAddresses'][0];
  3761. // first row '1' + last row '16384' indicates that full column is selected (apparently also in BIFF8!)
  3762. if (preg_match('/^([A-Z]+1\:[A-Z]+)16384$/', $selectedCells))
  3763. {
  3764. $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)16384$/', '${1}1048576', $selectedCells);
  3765. }
  3766. // first row '1' + last row '65536' indicates that full column is selected
  3767. if (preg_match('/^([A-Z]+1\:[A-Z]+)65536$/', $selectedCells))
  3768. {
  3769. $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)65536$/', '${1}1048576', $selectedCells);
  3770. }
  3771. // first column 'A' + last column 'IV' indicates that full row is selected
  3772. if (preg_match('/^(A[0-9]+\:)IV([0-9]+)$/', $selectedCells))
  3773. {
  3774. $selectedCells = preg_replace('/^(A[0-9]+\:)IV([0-9]+)$/', '${1}XFD${2}', $selectedCells);
  3775. }
  3776. $this->_phpSheet->setSelectedCells($selectedCells);
  3777. }
  3778. }
  3779. private function _includeCellRangeFiltered($cellRangeAddress)
  3780. {
  3781. $includeCellRange = true;
  3782. if (! is_null($this->getReadFilter()))
  3783. {
  3784. $includeCellRange = false;
  3785. $rangeBoundaries = PHPExcel_Cell :: getRangeBoundaries($cellRangeAddress);
  3786. $rangeBoundaries[1][0] ++;
  3787. for($row = $rangeBoundaries[0][1]; $row <= $rangeBoundaries[1][1]; $row ++)
  3788. {
  3789. for($column = $rangeBoundaries[0][0]; $column != $rangeBoundaries[1][0]; $column ++)
  3790. {
  3791. if ($this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle()))
  3792. {
  3793. $includeCellRange = true;
  3794. break 2;
  3795. }
  3796. }
  3797. }
  3798. }
  3799. return $includeCellRange;
  3800. }
  3801. /**
  3802. * MERGEDCELLS
  3803. *
  3804. * This record contains the addresses of merged cell ranges
  3805. * in the current sheet.
  3806. *
  3807. * -- "OpenOffice.org's Documentation of the Microsoft
  3808. * Excel File Format"
  3809. */
  3810. private function _readMergedCells()
  3811. {
  3812. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3813. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3814. // move stream pointer to next record
  3815. $this->_pos += 4 + $length;
  3816. if ($this->_version == self :: XLS_BIFF8 && ! $this->_readDataOnly)
  3817. {
  3818. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($recordData);
  3819. foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress)
  3820. {
  3821. if ($this->_includeCellRangeFiltered($cellRangeAddress))
  3822. {
  3823. $this->_phpSheet->mergeCells($cellRangeAddress);
  3824. }
  3825. }
  3826. }
  3827. }
  3828. /**
  3829. * Read HYPERLINK record
  3830. */
  3831. private function _readHyperLink()
  3832. {
  3833. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3834. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3835. // move stream pointer forward to next record
  3836. $this->_pos += 4 + $length;
  3837. if (! $this->_readDataOnly)
  3838. {
  3839. // offset: 0; size: 8; cell range address of all cells containing this hyperlink
  3840. try
  3841. {
  3842. $cellRange = $this->_readBIFF8CellRangeAddressFixed($recordData, 0, 8);
  3843. }
  3844. catch (Exception $e)
  3845. {
  3846. return;
  3847. }
  3848. // offset: 8, size: 16; GUID of StdLink
  3849. // offset: 24, size: 4; unknown value
  3850. // offset: 28, size: 4; option flags
  3851. // bit: 0; mask: 0x00000001; 0 = no link or extant, 1 = file link or URL
  3852. $isFileLinkOrUrl = (0x00000001 & self :: _GetInt2d($recordData, 28)) >> 0;
  3853. // bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL
  3854. $isAbsPathOrUrl = (0x00000001 & self :: _GetInt2d($recordData, 28)) >> 1;
  3855. // bit: 2 (and 4); mask: 0x00000014; 0 = no description
  3856. $hasDesc = (0x00000014 & self :: _GetInt2d($recordData, 28)) >> 2;
  3857. // bit: 3; mask: 0x00000008; 0 = no text, 1 = has text
  3858. $hasText = (0x00000008 & self :: _GetInt2d($recordData, 28)) >> 3;
  3859. // bit: 7; mask: 0x00000080; 0 = no target frame, 1 = has target frame
  3860. $hasFrame = (0x00000080 & self :: _GetInt2d($recordData, 28)) >> 7;
  3861. // bit: 8; mask: 0x00000100; 0 = file link or URL, 1 = UNC path (inc. server name)
  3862. $isUNC = (0x00000100 & self :: _GetInt2d($recordData, 28)) >> 8;
  3863. // offset within record data
  3864. $offset = 32;
  3865. if ($hasDesc)
  3866. {
  3867. // offset: 32; size: var; character count of description text
  3868. $dl = self :: _GetInt4d($recordData, 32);
  3869. // offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated
  3870. $desc = self :: _encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false);
  3871. $offset += 4 + 2 * $dl;
  3872. }
  3873. if ($hasFrame)
  3874. {
  3875. $fl = self :: _GetInt4d($recordData, $offset);
  3876. $offset += 4 + 2 * $fl;
  3877. }
  3878. // detect type of hyperlink (there are 4 types)
  3879. $hyperlinkType = null;
  3880. if ($isUNC)
  3881. {
  3882. $hyperlinkType = 'UNC';
  3883. }
  3884. else
  3885. if (! $isFileLinkOrUrl)
  3886. {
  3887. $hyperlinkType = 'workbook';
  3888. }
  3889. else
  3890. if (ord($recordData{$offset}) == 0x03)
  3891. {
  3892. $hyperlinkType = 'local';
  3893. }
  3894. else
  3895. if (ord($recordData{$offset}) == 0xE0)
  3896. {
  3897. $hyperlinkType = 'URL';
  3898. }
  3899. switch ($hyperlinkType)
  3900. {
  3901. case 'URL' :
  3902. // section 5.58.2: Hyperlink containing a URL
  3903. // e.g. http://example.org/index.php
  3904. // offset: var; size: 16; GUID of URL Moniker
  3905. $offset += 16;
  3906. // offset: var; size: 4; size (in bytes) of character array of the URL including trailing zero word
  3907. $us = self :: _GetInt4d($recordData, $offset);
  3908. $offset += 4;
  3909. // offset: var; size: $us; character array of the URL, no Unicode string header, always 16-bit characters, zero-terminated
  3910. $url = self :: _encodeUTF16(substr($recordData, $offset, $us - 2), false);
  3911. $url .= $hasText ? '#' : '';
  3912. $offset += $us;
  3913. break;
  3914. case 'local' :
  3915. // section 5.58.3: Hyperlink to local file
  3916. // examples:
  3917. // mydoc.txt
  3918. // ../../somedoc.xls#Sheet!A1
  3919. // offset: var; size: 16; GUI of File Moniker
  3920. $offset += 16;
  3921. // offset: var; size: 2; directory up-level count.
  3922. $upLevelCount = self :: _GetInt2d($recordData, $offset);
  3923. $offset += 2;
  3924. // offset: var; size: 4; character count of the shortened file path and name, including trailing zero word
  3925. $sl = self :: _GetInt4d($recordData, $offset);
  3926. $offset += 4;
  3927. // offset: var; size: sl; character array of the shortened file path and name in 8.3-DOS-format (compressed Unicode string)
  3928. $shortenedFilePath = substr($recordData, $offset, $sl);
  3929. $shortenedFilePath = self :: _encodeUTF16($shortenedFilePath, true);
  3930. $shortenedFilePath = substr($shortenedFilePath, 0, - 1); // remove trailing zero
  3931. $offset += $sl;
  3932. // offset: var; size: 24; unknown sequence
  3933. $offset += 24;
  3934. // extended file path
  3935. // offset: var; size: 4; size of the following file link field including string lenth mark
  3936. $sz = self :: _GetInt4d($recordData, $offset);
  3937. $offset += 4;
  3938. // only present if $sz > 0
  3939. if ($sz > 0)
  3940. {
  3941. // offset: var; size: 4; size of the character array of the extended file path and name
  3942. $xl = self :: _GetInt4d($recordData, $offset);
  3943. $offset += 4;
  3944. // offset: var; size 2; unknown
  3945. $offset += 2;
  3946. // offset: var; size $xl; character array of the extended file path and name.
  3947. $extendedFilePath = substr($recordData, $offset, $xl);
  3948. $extendedFilePath = self :: _encodeUTF16($extendedFilePath, false);
  3949. $offset += $xl;
  3950. }
  3951. // construct the path
  3952. $url = str_repeat('..\\', $upLevelCount);
  3953. $url .= ($sz > 0) ? $extendedFilePath : $shortenedFilePath; // use extended path if available
  3954. $url .= $hasText ? '#' : '';
  3955. break;
  3956. case 'UNC' :
  3957. // section 5.58.4: Hyperlink to a File with UNC (Universal Naming Convention) Path
  3958. // todo: implement
  3959. return;
  3960. case 'workbook' :
  3961. // section 5.58.5: Hyperlink to the Current Workbook
  3962. // e.g. Sheet2!B1:C2, stored in text mark field
  3963. $url = 'sheet://';
  3964. break;
  3965. default :
  3966. return;
  3967. }
  3968. if ($hasText)
  3969. {
  3970. // offset: var; size: 4; character count of text mark including trailing zero word
  3971. $tl = self :: _GetInt4d($recordData, $offset);
  3972. $offset += 4;
  3973. // offset: var; size: var; character array of the text mark without the # sign, no Unicode header, always 16-bit characters, zero-terminated
  3974. $text = self :: _encodeUTF16(substr($recordData, $offset, 2 * ($tl - 1)), false);
  3975. $url .= $text;
  3976. }
  3977. // apply the hyperlink to all the relevant cells
  3978. foreach (PHPExcel_Cell :: extractAllCellReferencesInRange($cellRange) as $coordinate)
  3979. {
  3980. $this->_phpSheet->getCell($coordinate)->getHyperLink()->setUrl($url);
  3981. }
  3982. }
  3983. }
  3984. /**
  3985. * Read DATAVALIDATIONS record
  3986. */
  3987. private function _readDataValidations()
  3988. {
  3989. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  3990. $recordData = substr($this->_data, $this->_pos + 4, $length);
  3991. // move stream pointer forward to next record
  3992. $this->_pos += 4 + $length;
  3993. }
  3994. /**
  3995. * Read DATAVALIDATION record
  3996. */
  3997. private function _readDataValidation()
  3998. {
  3999. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  4000. $recordData = substr($this->_data, $this->_pos + 4, $length);
  4001. // move stream pointer forward to next record
  4002. $this->_pos += 4 + $length;
  4003. if ($this->_readDataOnly)
  4004. {
  4005. return;
  4006. }
  4007. // offset: 0; size: 4; Options
  4008. $options = self :: _GetInt4d($recordData, 0);
  4009. // bit: 0-3; mask: 0x0000000F; type
  4010. $type = (0x0000000F & $options) >> 0;
  4011. switch ($type)
  4012. {
  4013. case 0x00 :
  4014. $type = PHPExcel_Cell_DataValidation :: TYPE_NONE;
  4015. break;
  4016. case 0x01 :
  4017. $type = PHPExcel_Cell_DataValidation :: TYPE_WHOLE;
  4018. break;
  4019. case 0x02 :
  4020. $type = PHPExcel_Cell_DataValidation :: TYPE_DECIMAL;
  4021. break;
  4022. case 0x03 :
  4023. $type = PHPExcel_Cell_DataValidation :: TYPE_LIST;
  4024. break;
  4025. case 0x04 :
  4026. $type = PHPExcel_Cell_DataValidation :: TYPE_DATE;
  4027. break;
  4028. case 0x05 :
  4029. $type = PHPExcel_Cell_DataValidation :: TYPE_TIME;
  4030. break;
  4031. case 0x06 :
  4032. $type = PHPExcel_Cell_DataValidation :: TYPE_TEXTLENGTH;
  4033. break;
  4034. case 0x07 :
  4035. $type = PHPExcel_Cell_DataValidation :: TYPE_CUSTOM;
  4036. break;
  4037. }
  4038. // bit: 4-6; mask: 0x00000070; error type
  4039. $errorStyle = (0x00000070 & $options) >> 4;
  4040. switch ($errorStyle)
  4041. {
  4042. case 0x00 :
  4043. $errorStyle = PHPExcel_Cell_DataValidation :: STYLE_STOP;
  4044. break;
  4045. case 0x01 :
  4046. $errorStyle = PHPExcel_Cell_DataValidation :: STYLE_WARNING;
  4047. break;
  4048. case 0x02 :
  4049. $errorStyle = PHPExcel_Cell_DataValidation :: STYLE_INFORMATION;
  4050. break;
  4051. }
  4052. // bit: 7; mask: 0x00000080; 1= formula is explicit (only applies to list)
  4053. // I have only seen cases where this is 1
  4054. $explicitFormula = (0x00000080 & $options) >> 7;
  4055. // bit: 8; mask: 0x00000100; 1= empty cells allowed
  4056. $allowBlank = (0x00000100 & $options) >> 8;
  4057. // bit: 9; mask: 0x00000200; 1= suppress drop down arrow in list type validity
  4058. $suppressDropDown = (0x00000200 & $options) >> 9;
  4059. // bit: 18; mask: 0x00040000; 1= show prompt box if cell selected
  4060. $showInputMessage = (0x00040000 & $options) >> 18;
  4061. // bit: 19; mask: 0x00080000; 1= show error box if invalid values entered
  4062. $showErrorMessage = (0x00080000 & $options) >> 19;
  4063. // bit: 20-23; mask: 0x00F00000; condition operator
  4064. $operator = (0x00F00000 & $options) >> 20;
  4065. switch ($operator)
  4066. {
  4067. case 0x00 :
  4068. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_BETWEEN;
  4069. break;
  4070. case 0x01 :
  4071. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_NOTBETWEEN;
  4072. break;
  4073. case 0x02 :
  4074. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_EQUAL;
  4075. break;
  4076. case 0x03 :
  4077. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_NOTEQUAL;
  4078. break;
  4079. case 0x04 :
  4080. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_GREATERTHAN;
  4081. break;
  4082. case 0x05 :
  4083. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_LESSTHAN;
  4084. break;
  4085. case 0x06 :
  4086. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_GREATERTHANOREQUAL;
  4087. break;
  4088. case 0x07 :
  4089. $operator = PHPExcel_Cell_DataValidation :: OPERATOR_LESSTHANOREQUAL;
  4090. break;
  4091. }
  4092. // offset: 4; size: var; title of the prompt box
  4093. $offset = 4;
  4094. $string = self :: _readUnicodeStringLong(substr($recordData, $offset));
  4095. $promptTitle = $string['value'] !== chr(0) ? $string['value'] : '';
  4096. $offset += $string['size'];
  4097. // offset: var; size: var; title of the error box
  4098. $string = self :: _readUnicodeStringLong(substr($recordData, $offset));
  4099. $errorTitle = $string['value'] !== chr(0) ? $string['value'] : '';
  4100. $offset += $string['size'];
  4101. // offset: var; size: var; text of the prompt box
  4102. $string = self :: _readUnicodeStringLong(substr($recordData, $offset));
  4103. $prompt = $string['value'] !== chr(0) ? $string['value'] : '';
  4104. $offset += $string['size'];
  4105. // offset: var; size: var; text of the error box
  4106. $string = self :: _readUnicodeStringLong(substr($recordData, $offset));
  4107. $error = $string['value'] !== chr(0) ? $string['value'] : '';
  4108. $offset += $string['size'];
  4109. // offset: var; size: 2; size of the formula data for the first condition
  4110. $sz1 = self :: _GetInt2d($recordData, $offset);
  4111. $offset += 2;
  4112. // offset: var; size: 2; not used
  4113. $offset += 2;
  4114. // offset: var; size: $sz1; formula data for first condition (without size field)
  4115. $formula1 = substr($recordData, $offset, $sz1);
  4116. $formula1 = pack('v', $sz1) . $formula1; // prepend the length
  4117. try
  4118. {
  4119. $formula1 = $this->_getFormulaFromStructure($formula1);
  4120. // in list type validity, null characters are used as item separators
  4121. if ($type == PHPExcel_Cell_DataValidation :: TYPE_LIST)
  4122. {
  4123. $formula1 = str_replace(chr(0), ',', $formula1);
  4124. }
  4125. }
  4126. catch (Exception $e)
  4127. {
  4128. return;
  4129. }
  4130. $offset += $sz1;
  4131. // offset: var; size: 2; size of the formula data for the first condition
  4132. $sz2 = self :: _GetInt2d($recordData, $offset);
  4133. $offset += 2;
  4134. // offset: var; size: 2; not used
  4135. $offset += 2;
  4136. // offset: var; size: $sz2; formula data for second condition (without size field)
  4137. $formula2 = substr($recordData, $offset, $sz2);
  4138. $formula2 = pack('v', $sz2) . $formula2; // prepend the length
  4139. try
  4140. {
  4141. $formula2 = $this->_getFormulaFromStructure($formula2);
  4142. }
  4143. catch (Exception $e)
  4144. {
  4145. return;
  4146. }
  4147. $offset += $sz2;
  4148. // offset: var; size: var; cell range address list with
  4149. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList(substr($recordData, $offset));
  4150. $cellRangeAddresses = $cellRangeAddressList['cellRangeAddresses'];
  4151. foreach ($cellRangeAddresses as $cellRange)
  4152. {
  4153. $stRange = $this->_phpSheet->shrinkRangeToFit($cellRange);
  4154. $stRange = PHPExcel_Cell :: extractAllCellReferencesInRange($stRange);
  4155. foreach ($stRange as $coordinate)
  4156. {
  4157. $objValidation = $this->_phpSheet->getCell($coordinate)->getDataValidation();
  4158. $objValidation->setType($type);
  4159. $objValidation->setErrorStyle($errorStyle);
  4160. $objValidation->setAllowBlank((bool) $allowBlank);
  4161. $objValidation->setShowInputMessage((bool) $showInputMessage);
  4162. $objValidation->setShowErrorMessage((bool) $showErrorMessage);
  4163. $objValidation->setShowDropDown(! $suppressDropDown);
  4164. $objValidation->setOperator($operator);
  4165. $objValidation->setErrorTitle($errorTitle);
  4166. $objValidation->setError($error);
  4167. $objValidation->setPromptTitle($promptTitle);
  4168. $objValidation->setPrompt($prompt);
  4169. $objValidation->setFormula1($formula1);
  4170. $objValidation->setFormula2($formula2);
  4171. }
  4172. }
  4173. }
  4174. /**
  4175. * Read SHEETLAYOUT record. Stores sheet tab color information.
  4176. */
  4177. private function _readSheetLayout()
  4178. {
  4179. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  4180. $recordData = substr($this->_data, $this->_pos + 4, $length);
  4181. // move stream pointer to next record
  4182. $this->_pos += 4 + $length;
  4183. // local pointer in record data
  4184. $offset = 0;
  4185. if (! $this->_readDataOnly)
  4186. {
  4187. // offset: 0; size: 2; repeated record identifier 0x0862
  4188. // offset: 2; size: 10; not used
  4189. // offset: 12; size: 4; size of record data
  4190. // Excel 2003 uses size of 0x14 (documented), Excel 2007 uses size of 0x28 (not documented?)
  4191. $sz = self :: _GetInt4d($recordData, 12);
  4192. switch ($sz)
  4193. {
  4194. case 0x14 :
  4195. // offset: 16; size: 2; color index for sheet tab
  4196. $colorIndex = self :: _GetInt2d($recordData, 16);
  4197. $color = self :: _readColor($colorIndex, $this->_palette, $this->_version);
  4198. $this->_phpSheet->getTabColor()->setRGB($color['rgb']);
  4199. break;
  4200. case 0x28 :
  4201. // TODO: Investigate structure for .xls SHEETLAYOUT record as saved by MS Office Excel 2007
  4202. return;
  4203. break;
  4204. }
  4205. }
  4206. }
  4207. /**
  4208. * Read SHEETPROTECTION record (FEATHEADR)
  4209. */
  4210. private function _readSheetProtection()
  4211. {
  4212. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  4213. $recordData = substr($this->_data, $this->_pos + 4, $length);
  4214. // move stream pointer to next record
  4215. $this->_pos += 4 + $length;
  4216. if ($this->_readDataOnly)
  4217. {
  4218. return;
  4219. }
  4220. // offset: 0; size: 2; repeated record header
  4221. // offset: 2; size: 2; FRT cell reference flag (=0 currently)
  4222. // offset: 4; size: 8; Currently not used and set to 0
  4223. // offset: 12; size: 2; Shared feature type index (2=Enhanced Protetion, 4=SmartTag)
  4224. $isf = self :: _GetInt2d($recordData, 12);
  4225. if ($isf != 2)
  4226. {
  4227. return;
  4228. }
  4229. // offset: 14; size: 1; =1 since this is a feat header
  4230. // offset: 15; size: 4; size of rgbHdrSData
  4231. // rgbHdrSData, assume "Enhanced Protection"
  4232. // offset: 19; size: 2; option flags
  4233. $options = self :: _GetInt2d($recordData, 19);
  4234. // bit: 0; mask 0x0001; 1 = user may edit objects, 0 = users must not edit objects
  4235. $bool = (0x0001 & $options) >> 0;
  4236. $this->_phpSheet->getProtection()->setObjects(! $bool);
  4237. // bit: 1; mask 0x0002; edit scenarios
  4238. $bool = (0x0002 & $options) >> 1;
  4239. $this->_phpSheet->getProtection()->setScenarios(! $bool);
  4240. // bit: 2; mask 0x0004; format cells
  4241. $bool = (0x0004 & $options) >> 2;
  4242. $this->_phpSheet->getProtection()->setFormatCells(! $bool);
  4243. // bit: 3; mask 0x0008; format columns
  4244. $bool = (0x0008 & $options) >> 3;
  4245. $this->_phpSheet->getProtection()->setFormatColumns(! $bool);
  4246. // bit: 4; mask 0x0010; format rows
  4247. $bool = (0x0010 & $options) >> 4;
  4248. $this->_phpSheet->getProtection()->setFormatRows(! $bool);
  4249. // bit: 5; mask 0x0020; insert columns
  4250. $bool = (0x0020 & $options) >> 5;
  4251. $this->_phpSheet->getProtection()->setInsertColumns(! $bool);
  4252. // bit: 6; mask 0x0040; insert rows
  4253. $bool = (0x0040 & $options) >> 6;
  4254. $this->_phpSheet->getProtection()->setInsertRows(! $bool);
  4255. // bit: 7; mask 0x0080; insert hyperlinks
  4256. $bool = (0x0080 & $options) >> 7;
  4257. $this->_phpSheet->getProtection()->setInsertHyperlinks(! $bool);
  4258. // bit: 8; mask 0x0100; delete columns
  4259. $bool = (0x0100 & $options) >> 8;
  4260. $this->_phpSheet->getProtection()->setDeleteColumns(! $bool);
  4261. // bit: 9; mask 0x0200; delete rows
  4262. $bool = (0x0200 & $options) >> 9;
  4263. $this->_phpSheet->getProtection()->setDeleteRows(! $bool);
  4264. // bit: 10; mask 0x0400; select locked cells
  4265. $bool = (0x0400 & $options) >> 10;
  4266. $this->_phpSheet->getProtection()->setSelectLockedCells(! $bool);
  4267. // bit: 11; mask 0x0800; sort cell range
  4268. $bool = (0x0800 & $options) >> 11;
  4269. $this->_phpSheet->getProtection()->setSort(! $bool);
  4270. // bit: 12; mask 0x1000; auto filter
  4271. $bool = (0x1000 & $options) >> 12;
  4272. $this->_phpSheet->getProtection()->setAutoFilter(! $bool);
  4273. // bit: 13; mask 0x2000; pivot tables
  4274. $bool = (0x2000 & $options) >> 13;
  4275. $this->_phpSheet->getProtection()->setPivotTables(! $bool);
  4276. // bit: 14; mask 0x4000; select unlocked cells
  4277. $bool = (0x4000 & $options) >> 14;
  4278. $this->_phpSheet->getProtection()->setSelectUnlockedCells(! $bool);
  4279. // offset: 21; size: 2; not used
  4280. }
  4281. /**
  4282. * Read RANGEPROTECTION record
  4283. * Reading of this record is based on Microsoft Office Excel 97-2000 Binary File Format Specification,
  4284. * where it is referred to as FEAT record
  4285. */
  4286. private function _readRangeProtection()
  4287. {
  4288. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  4289. $recordData = substr($this->_data, $this->_pos + 4, $length);
  4290. // move stream pointer to next record
  4291. $this->_pos += 4 + $length;
  4292. // local pointer in record data
  4293. $offset = 0;
  4294. if (! $this->_readDataOnly)
  4295. {
  4296. $offset += 12;
  4297. // offset: 12; size: 2; shared feature type, 2 = enhanced protection, 4 = smart tag
  4298. $isf = self :: _GetInt2d($recordData, 12);
  4299. if ($isf != 2)
  4300. {
  4301. // we only read FEAT records of type 2
  4302. return;
  4303. }
  4304. $offset += 2;
  4305. $offset += 5;
  4306. // offset: 19; size: 2; count of ref ranges this feature is on
  4307. $cref = self :: _GetInt2d($recordData, 19);
  4308. $offset += 2;
  4309. $offset += 6;
  4310. // offset: 27; size: 8 * $cref; list of cell ranges (like in hyperlink record)
  4311. $cellRanges = array();
  4312. for($i = 0; $i < $cref; ++ $i)
  4313. {
  4314. try
  4315. {
  4316. $cellRange = $this->_readBIFF8CellRangeAddressFixed(substr($recordData, 27 + 8 * $i, 8));
  4317. }
  4318. catch (Exception $e)
  4319. {
  4320. return;
  4321. }
  4322. $cellRanges[] = $cellRange;
  4323. $offset += 8;
  4324. }
  4325. // offset: var; size: var; variable length of feature specific data
  4326. $rgbFeat = substr($recordData, $offset);
  4327. $offset += 4;
  4328. // offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit)
  4329. $wPassword = self :: _GetInt4d($recordData, $offset);
  4330. $offset += 4;
  4331. // Apply range protection to sheet
  4332. if ($cellRanges)
  4333. {
  4334. $this->_phpSheet->protectCells(implode(' ', $cellRanges), strtoupper(dechex($wPassword)), true);
  4335. }
  4336. }
  4337. }
  4338. /**
  4339. * Read IMDATA record
  4340. */
  4341. private function _readImData()
  4342. {
  4343. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  4344. // get spliced record data
  4345. $splicedRecordData = $this->_getSplicedRecordData();
  4346. $recordData = $splicedRecordData['recordData'];
  4347. // UNDER CONSTRUCTION
  4348. // offset: 0; size: 2; image format
  4349. $cf = self :: _GetInt2d($recordData, 0);
  4350. // offset: 2; size: 2; environment from which the file was written
  4351. $env = self :: _GetInt2d($recordData, 2);
  4352. // offset: 4; size: 4; length of the image data
  4353. $lcb = self :: _GetInt4d($recordData, 4);
  4354. // offset: 8; size: var; image data
  4355. $iData = substr($recordData, 8);
  4356. switch ($cf)
  4357. {
  4358. case 0x09 : // Windows bitmap format
  4359. // BITMAPCOREINFO
  4360. // 1. BITMAPCOREHEADER
  4361. // offset: 0; size: 4; bcSize, Specifies the number of bytes required by the structure
  4362. $bcSize = self :: _GetInt4d($iData, 0);
  4363. // var_dump($bcSize);
  4364. // offset: 4; size: 2; bcWidth, specifies the width of the bitmap, in pixels
  4365. $bcWidth = self :: _GetInt2d($iData, 4);
  4366. // var_dump($bcWidth);
  4367. // offset: 6; size: 2; bcHeight, specifies the height of the bitmap, in pixels.
  4368. $bcHeight = self :: _GetInt2d($iData, 6);
  4369. // var_dump($bcHeight);
  4370. $ih = imagecreatetruecolor($bcWidth, $bcHeight);
  4371. // offset: 8; size: 2; bcPlanes, specifies the number of planes for the target device. This value must be 1
  4372. // offset: 10; size: 2; bcBitCount specifies the number of bits-per-pixel. This value must be 1, 4, 8, or 24
  4373. $bcBitCount = self :: _GetInt2d($iData, 10);
  4374. // var_dump($bcBitCount);
  4375. $rgbString = substr($iData, 12);
  4376. $rgbTriples = array();
  4377. while (strlen($rgbString) > 0)
  4378. {
  4379. $rgbTriples[] = unpack('Cb/Cg/Cr', $rgbString);
  4380. $rgbString = substr($rgbString, 3);
  4381. }
  4382. $x = 0;
  4383. $y = 0;
  4384. foreach ($rgbTriples as $i => $rgbTriple)
  4385. {
  4386. $color = imagecolorallocate($ih, $rgbTriple['r'], $rgbTriple['g'], $rgbTriple['b']);
  4387. imagesetpixel($ih, $x, $bcHeight - 1 - $y, $color);
  4388. $x = ($x + 1) % $bcWidth;
  4389. $y = $y + floor(($x + 1) / $bcWidth);
  4390. }
  4391. //imagepng($ih, 'image.png');
  4392. $drawing = new PHPExcel_Worksheet_Drawing();
  4393. $drawing->setPath($filename);
  4394. $drawing->setWorksheet($this->_phpSheet);
  4395. break;
  4396. case 0x02 : // Windows metafile or Macintosh PICT format
  4397. case 0x0e : // native format
  4398. default :
  4399. break;
  4400. }
  4401. // _getSplicedRecordData() takes care of moving current position in data stream
  4402. }
  4403. /**
  4404. * Read a free CONTINUE record. Free CONTINUE record may be a camouflaged MSODRAWING record
  4405. * When MSODRAWING data on a sheet exceeds 8224 bytes, CONTINUE records are used instead. Undocumented.
  4406. * In this case, we must treat the CONTINUE record as a MSODRAWING record
  4407. */
  4408. private function _readContinue()
  4409. {
  4410. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  4411. $recordData = substr($this->_data, $this->_pos + 4, $length);
  4412. // check if we are reading drawing data
  4413. // this is in case a free CONTINUE record occurs in other circumstances we are unaware of
  4414. if ($this->_drawingData == '')
  4415. {
  4416. // move stream pointer to next record
  4417. $this->_pos += 4 + $length;
  4418. return;
  4419. }
  4420. // check if record data is at least 4 bytes long, otherwise there is no chance this is MSODRAWING data
  4421. if ($length < 4)
  4422. {
  4423. // move stream pointer to next record
  4424. $this->_pos += 4 + $length;
  4425. return;
  4426. }
  4427. // dirty check to see if CONTINUE record could be a camouflaged MSODRAWING record
  4428. // look inside CONTINUE record to see if it looks like a part of an Escher stream
  4429. // we know that Escher stream may be split at least at
  4430. // 0xF003 MsofbtSpgrContainer
  4431. // 0xF004 MsofbtSpContainer
  4432. // 0xF00D MsofbtClientTextbox
  4433. $validSplitPoints = array(0xF003, 0xF004, 0xF00D); // add identifiers if we find more
  4434. $splitPoint = self :: _GetInt2d($recordData, 2);
  4435. if (in_array($splitPoint, $validSplitPoints))
  4436. {
  4437. // get spliced record data (and move pointer to next record)
  4438. $splicedRecordData = $this->_getSplicedRecordData();
  4439. $this->_drawingData .= $splicedRecordData['recordData'];
  4440. return;
  4441. }
  4442. // move stream pointer to next record
  4443. $this->_pos += 4 + $length;
  4444. }
  4445. /**
  4446. * Reads a record from current position in data stream and continues reading data as long as CONTINUE
  4447. * records are found. Splices the record data pieces and returns the combined string as if record data
  4448. * is in one piece.
  4449. * Moves to next current position in data stream to start of next record different from a CONtINUE record
  4450. *
  4451. * @return array
  4452. */
  4453. private function _getSplicedRecordData()
  4454. {
  4455. $data = '';
  4456. $spliceOffsets = array();
  4457. $i = 0;
  4458. $spliceOffsets[0] = 0;
  4459. do
  4460. {
  4461. ++ $i;
  4462. // offset: 0; size: 2; identifier
  4463. $identifier = self :: _GetInt2d($this->_data, $this->_pos);
  4464. // offset: 2; size: 2; length
  4465. $length = self :: _GetInt2d($this->_data, $this->_pos + 2);
  4466. $data .= substr($this->_data, $this->_pos + 4, $length);
  4467. $spliceOffsets[$i] = $spliceOffsets[$i - 1] + $length;
  4468. $this->_pos += 4 + $length;
  4469. $nextIdentifier = self :: _GetInt2d($this->_data, $this->_pos);
  4470. }
  4471. while ($nextIdentifier == self :: XLS_Type_CONTINUE);
  4472. $splicedData = array('recordData' => $data, 'spliceOffsets' => $spliceOffsets);
  4473. return $splicedData;
  4474. }
  4475. /**
  4476. * Convert formula structure into human readable Excel formula like 'A3+A5*5'
  4477. *
  4478. * @param string $formulaStructure The complete binary data for the formula
  4479. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4480. * @return string Human readable formula
  4481. */
  4482. private function _getFormulaFromStructure($formulaStructure, $baseCell = 'A1')
  4483. {
  4484. // offset: 0; size: 2; size of the following formula data
  4485. $sz = self :: _GetInt2d($formulaStructure, 0);
  4486. // offset: 2; size: sz
  4487. $formulaData = substr($formulaStructure, 2, $sz);
  4488. // for debug: dump the formula data
  4489. //echo '<xmp>';
  4490. //echo 'size: ' . $sz . "\n";
  4491. //echo 'the entire formula data: ';
  4492. //Debug::dump($formulaData);
  4493. //echo "\n----\n";
  4494. // offset: 2 + sz; size: variable (optional)
  4495. if (strlen($formulaStructure) > 2 + $sz)
  4496. {
  4497. $additionalData = substr($formulaStructure, 2 + $sz);
  4498. // for debug: dump the additional data
  4499. //echo 'the entire additional data: ';
  4500. //Debug::dump($additionalData);
  4501. //echo "\n----\n";
  4502. }
  4503. else
  4504. {
  4505. $additionalData = '';
  4506. }
  4507. return $this->_getFormulaFromData($formulaData, $additionalData, $baseCell);
  4508. }
  4509. /**
  4510. * Take formula data and additional data for formula and return human readable formula
  4511. *
  4512. * @param string $formulaData The binary data for the formula itself
  4513. * @param string $additionalData Additional binary data going with the formula
  4514. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4515. * @return string Human readable formula
  4516. */
  4517. private function _getFormulaFromData($formulaData, $additionalData = '', $baseCell = 'A1')
  4518. {
  4519. // start parsing the formula data
  4520. $tokens = array();
  4521. while (strlen($formulaData) > 0 and $token = $this->_getNextToken($formulaData, $baseCell))
  4522. {
  4523. $tokens[] = $token;
  4524. $formulaData = substr($formulaData, $token['size']);
  4525. // for debug: dump the token
  4526. //var_dump($token);
  4527. }
  4528. $formulaString = $this->_createFormulaFromTokens($tokens, $additionalData);
  4529. return $formulaString;
  4530. }
  4531. /**
  4532. * Take array of tokens together with additional data for formula and return human readable formula
  4533. *
  4534. * @param array $tokens
  4535. * @param array $additionalData Additional binary data going with the formula
  4536. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4537. * @return string Human readable formula
  4538. */
  4539. private function _createFormulaFromTokens($tokens, $additionalData)
  4540. {
  4541. // empty formula?
  4542. if (count($tokens) == 0)
  4543. {
  4544. return '';
  4545. }
  4546. $formulaStrings = array();
  4547. foreach ($tokens as $token)
  4548. {
  4549. // initialize spaces
  4550. $space0 = isset($space0) ? $space0 : ''; // spaces before next token, not tParen
  4551. $space1 = isset($space1) ? $space1 : ''; // carriage returns before next token, not tParen
  4552. $space2 = isset($space2) ? $space2 : ''; // spaces before opening parenthesis
  4553. $space3 = isset($space3) ? $space3 : ''; // carriage returns before opening parenthesis
  4554. $space4 = isset($space4) ? $space4 : ''; // spaces before closing parenthesis
  4555. $space5 = isset($space5) ? $space5 : ''; // carriage returns before closing parenthesis
  4556. switch ($token['name'])
  4557. {
  4558. case 'tAdd' : // addition
  4559. case 'tConcat' : // addition
  4560. case 'tDiv' : // division
  4561. case 'tEQ' : // equality
  4562. case 'tGE' : // greater than or equal
  4563. case 'tGT' : // greater than
  4564. case 'tIsect' : // intersection
  4565. case 'tLE' : // less than or equal
  4566. case 'tList' : // less than or equal
  4567. case 'tLT' : // less than
  4568. case 'tMul' : // multiplication
  4569. case 'tNE' : // multiplication
  4570. case 'tPower' : // power
  4571. case 'tRange' : // range
  4572. case 'tSub' : // subtraction
  4573. $op2 = array_pop($formulaStrings);
  4574. $op1 = array_pop($formulaStrings);
  4575. $formulaStrings[] = "$op1$space1$space0{$token['data']}$op2";
  4576. unset($space0, $space1);
  4577. break;
  4578. case 'tUplus' : // unary plus
  4579. case 'tUminus' : // unary minus
  4580. $op = array_pop($formulaStrings);
  4581. $formulaStrings[] = "$space1$space0{$token['data']}$op";
  4582. unset($space0, $space1);
  4583. break;
  4584. case 'tPercent' : // percent sign
  4585. $op = array_pop($formulaStrings);
  4586. $formulaStrings[] = "$op$space1$space0{$token['data']}";
  4587. unset($space0, $space1);
  4588. break;
  4589. case 'tAttrVolatile' : // indicates volatile function
  4590. case 'tAttrIf' :
  4591. case 'tAttrSkip' :
  4592. case 'tAttrChoose' :
  4593. // token is only important for Excel formula evaluator
  4594. // do nothing
  4595. break;
  4596. case 'tAttrSpace' : // space / carriage return
  4597. // space will be used when next token arrives, do not alter formulaString stack
  4598. switch ($token['data']['spacetype'])
  4599. {
  4600. case 'type0' :
  4601. $space0 = str_repeat(' ', $token['data']['spacecount']);
  4602. break;
  4603. case 'type1' :
  4604. $space1 = str_repeat("\n", $token['data']['spacecount']);
  4605. break;
  4606. case 'type2' :
  4607. $space2 = str_repeat(' ', $token['data']['spacecount']);
  4608. break;
  4609. case 'type3' :
  4610. $space3 = str_repeat("\n", $token['data']['spacecount']);
  4611. break;
  4612. case 'type4' :
  4613. $space4 = str_repeat(' ', $token['data']['spacecount']);
  4614. break;
  4615. case 'type5' :
  4616. $space5 = str_repeat("\n", $token['data']['spacecount']);
  4617. break;
  4618. }
  4619. break;
  4620. case 'tAttrSum' : // SUM function with one parameter
  4621. $op = array_pop($formulaStrings);
  4622. $formulaStrings[] = "{$space1}{$space0}SUM($op)";
  4623. unset($space0, $space1);
  4624. break;
  4625. case 'tFunc' : // function with fixed number of arguments
  4626. case 'tFuncV' : // function with variable number of arguments
  4627. if ($token['data']['function'] != '')
  4628. {
  4629. // normal function
  4630. $ops = array(); // array of operators
  4631. for($i = 0; $i < $token['data']['args']; ++ $i)
  4632. {
  4633. $ops[] = array_pop($formulaStrings);
  4634. }
  4635. $ops = array_reverse($ops);
  4636. $formulaStrings[] = "$space1$space0{$token['data']['function']}(" . implode(',', $ops) . ")";
  4637. unset($space0, $space1);
  4638. }
  4639. else
  4640. {
  4641. // add-in function
  4642. $ops = array(); // array of operators
  4643. for($i = 0; $i < $token['data']['args'] - 1; ++ $i)
  4644. {
  4645. $ops[] = array_pop($formulaStrings);
  4646. }
  4647. $ops = array_reverse($ops);
  4648. $function = array_pop($formulaStrings);
  4649. $formulaStrings[] = "$space1$space0$function(" . implode(',', $ops) . ")";
  4650. unset($space0, $space1);
  4651. }
  4652. break;
  4653. case 'tParen' : // parenthesis
  4654. $expression = array_pop($formulaStrings);
  4655. $formulaStrings[] = "$space3$space2($expression$space5$space4)";
  4656. unset($space2, $space3, $space4, $space5);
  4657. break;
  4658. case 'tArray' : // array constant
  4659. $constantArray = self :: _readBIFF8ConstantArray($additionalData);
  4660. $formulaStrings[] = $space1 . $space0 . $constantArray['value'];
  4661. $additionalData = substr($additionalData, $constantArray['size']); // bite of chunk of additional data
  4662. unset($space0, $space1);
  4663. break;
  4664. case 'tMemArea' :
  4665. // bite off chunk of additional data
  4666. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($additionalData);
  4667. $additionalData = substr($additionalData, $cellRangeAddressList['size']);
  4668. $formulaStrings[] = "$space1$space0{$token['data']}";
  4669. unset($space0, $space1);
  4670. break;
  4671. case 'tArea' : // cell range address
  4672. case 'tBool' : // boolean
  4673. case 'tErr' : // error code
  4674. case 'tInt' : // integer
  4675. case 'tMemErr' :
  4676. case 'tMemFunc' :
  4677. case 'tMissArg' :
  4678. case 'tName' :
  4679. case 'tNameX' :
  4680. case 'tNum' : // number
  4681. case 'tRef' : // single cell reference
  4682. case 'tRef3d' : // 3d cell reference
  4683. case 'tArea3d' : // 3d cell range reference
  4684. case 'tRefN' :
  4685. case 'tAreaN' :
  4686. case 'tStr' : // string
  4687. $formulaStrings[] = "$space1$space0{$token['data']}";
  4688. unset($space0, $space1);
  4689. break;
  4690. }
  4691. }
  4692. $formulaString = $formulaStrings[0];
  4693. // for debug: dump the human readable formula
  4694. //echo '----' . "\n";
  4695. //echo 'Formula: ' . $formulaString;
  4696. return $formulaString;
  4697. }
  4698. /**
  4699. * Fetch next token from binary formula data
  4700. *
  4701. * @param string Formula data
  4702. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4703. * @return array
  4704. * @throws Exception
  4705. */
  4706. private function _getNextToken($formulaData, $baseCell = 'A1')
  4707. {
  4708. // offset: 0; size: 1; token id
  4709. $id = ord($formulaData[0]); // token id
  4710. $name = false; // initialize token name
  4711. switch ($id)
  4712. {
  4713. case 0x03 :
  4714. $name = 'tAdd';
  4715. $size = 1;
  4716. $data = '+';
  4717. break;
  4718. case 0x04 :
  4719. $name = 'tSub';
  4720. $size = 1;
  4721. $data = '-';
  4722. break;
  4723. case 0x05 :
  4724. $name = 'tMul';
  4725. $size = 1;
  4726. $data = '*';
  4727. break;
  4728. case 0x06 :
  4729. $name = 'tDiv';
  4730. $size = 1;
  4731. $data = '/';
  4732. break;
  4733. case 0x07 :
  4734. $name = 'tPower';
  4735. $size = 1;
  4736. $data = '^';
  4737. break;
  4738. case 0x08 :
  4739. $name = 'tConcat';
  4740. $size = 1;
  4741. $data = '&';
  4742. break;
  4743. case 0x09 :
  4744. $name = 'tLT';
  4745. $size = 1;
  4746. $data = '<';
  4747. break;
  4748. case 0x0A :
  4749. $name = 'tLE';
  4750. $size = 1;
  4751. $data = '<=';
  4752. break;
  4753. case 0x0B :
  4754. $name = 'tEQ';
  4755. $size = 1;
  4756. $data = '=';
  4757. break;
  4758. case 0x0C :
  4759. $name = 'tGE';
  4760. $size = 1;
  4761. $data = '>=';
  4762. break;
  4763. case 0x0D :
  4764. $name = 'tGT';
  4765. $size = 1;
  4766. $data = '>';
  4767. break;
  4768. case 0x0E :
  4769. $name = 'tNE';
  4770. $size = 1;
  4771. $data = '<>';
  4772. break;
  4773. case 0x0F :
  4774. $name = 'tIsect';
  4775. $size = 1;
  4776. $data = ' ';
  4777. break;
  4778. case 0x10 :
  4779. $name = 'tList';
  4780. $size = 1;
  4781. $data = ',';
  4782. break;
  4783. case 0x11 :
  4784. $name = 'tRange';
  4785. $size = 1;
  4786. $data = ':';
  4787. break;
  4788. case 0x12 :
  4789. $name = 'tUplus';
  4790. $size = 1;
  4791. $data = '+';
  4792. break;
  4793. case 0x13 :
  4794. $name = 'tUminus';
  4795. $size = 1;
  4796. $data = '-';
  4797. break;
  4798. case 0x14 :
  4799. $name = 'tPercent';
  4800. $size = 1;
  4801. $data = '%';
  4802. break;
  4803. case 0x15 : // parenthesis
  4804. $name = 'tParen';
  4805. $size = 1;
  4806. $data = null;
  4807. break;
  4808. case 0x16 : // missing argument
  4809. $name = 'tMissArg';
  4810. $size = 1;
  4811. $data = '';
  4812. break;
  4813. case 0x17 : // string
  4814. $name = 'tStr';
  4815. // offset: 1; size: var; Unicode string, 8-bit string length
  4816. $string = self :: _readUnicodeStringShort(substr($formulaData, 1));
  4817. $size = 1 + $string['size'];
  4818. $data = self :: _UTF8toExcelDoubleQuoted($string['value']);
  4819. break;
  4820. case 0x19 : // Special attribute
  4821. // offset: 1; size: 1; attribute type flags:
  4822. switch (ord($formulaData[1]))
  4823. {
  4824. case 0x01 :
  4825. $name = 'tAttrVolatile';
  4826. $size = 4;
  4827. $data = null;
  4828. break;
  4829. case 0x02 :
  4830. $name = 'tAttrIf';
  4831. $size = 4;
  4832. $data = null;
  4833. break;
  4834. case 0x04 :
  4835. $name = 'tAttrChoose';
  4836. // offset: 2; size: 2; number of choices in the CHOOSE function ($nc, number of parameters decreased by 1)
  4837. $nc = self :: _GetInt2d($formulaData, 2);
  4838. // offset: 4; size: 2 * $nc
  4839. // offset: 4 + 2 * $nc; size: 2
  4840. $size = 2 * $nc + 6;
  4841. $data = null;
  4842. break;
  4843. case 0x08 :
  4844. $name = 'tAttrSkip';
  4845. $size = 4;
  4846. $data = null;
  4847. break;
  4848. case 0x10 :
  4849. $name = 'tAttrSum';
  4850. $size = 4;
  4851. $data = null;
  4852. break;
  4853. case 0x40 :
  4854. case 0x41 :
  4855. $name = 'tAttrSpace';
  4856. $size = 4;
  4857. // offset: 2; size: 2; space type and position
  4858. switch (ord($formulaData[2]))
  4859. {
  4860. case 0x00 :
  4861. $spacetype = 'type0';
  4862. break;
  4863. case 0x01 :
  4864. $spacetype = 'type1';
  4865. break;
  4866. case 0x02 :
  4867. $spacetype = 'type2';
  4868. break;
  4869. case 0x03 :
  4870. $spacetype = 'type3';
  4871. break;
  4872. case 0x04 :
  4873. $spacetype = 'type4';
  4874. break;
  4875. case 0x05 :
  4876. $spacetype = 'type5';
  4877. break;
  4878. default :
  4879. throw new Exception('Unrecognized space type in tAttrSpace token');
  4880. break;
  4881. }
  4882. // offset: 3; size: 1; number of inserted spaces/carriage returns
  4883. $spacecount = ord($formulaData[3]);
  4884. $data = array('spacetype' => $spacetype, 'spacecount' => $spacecount);
  4885. break;
  4886. default :
  4887. throw new Exception('Unrecognized attribute flag in tAttr token');
  4888. break;
  4889. }
  4890. break;
  4891. case 0x1C : // error code
  4892. // offset: 1; size: 1; error code
  4893. $name = 'tErr';
  4894. $size = 2;
  4895. $data = self :: _mapErrorCode(ord($formulaData[1]));
  4896. break;
  4897. case 0x1D : // boolean
  4898. // offset: 1; size: 1; 0 = false, 1 = true;
  4899. $name = 'tBool';
  4900. $size = 2;
  4901. $data = ord($formulaData[1]) ? 'TRUE' : 'FALSE';
  4902. break;
  4903. case 0x1E : // integer
  4904. // offset: 1; size: 2; unsigned 16-bit integer
  4905. $name = 'tInt';
  4906. $size = 3;
  4907. $data = self :: _GetInt2d($formulaData, 1);
  4908. break;
  4909. case 0x1F : // number
  4910. // offset: 1; size: 8;
  4911. $name = 'tNum';
  4912. $size = 9;
  4913. $data = self :: _extractNumber(substr($formulaData, 1));
  4914. $data = str_replace(',', '.', (string) $data); // in case non-English locale
  4915. break;
  4916. case 0x20 : // array constant
  4917. case 0x40 :
  4918. case 0x60 :
  4919. // offset: 1; size: 7; not used
  4920. $name = 'tArray';
  4921. $size = 8;
  4922. $data = null;
  4923. break;
  4924. case 0x21 : // function with fixed number of arguments
  4925. case 0x41 :
  4926. case 0x61 :
  4927. $name = 'tFunc';
  4928. $size = 3;
  4929. // offset: 1; size: 2; index to built-in sheet function
  4930. switch (self :: _GetInt2d($formulaData, 1))
  4931. {
  4932. case 2 :
  4933. $function = 'ISNA';
  4934. $args = 1;
  4935. break;
  4936. case 3 :
  4937. $function = 'ISERROR';
  4938. $args = 1;
  4939. break;
  4940. case 10 :
  4941. $function = 'NA';
  4942. $args = 0;
  4943. break;
  4944. case 15 :
  4945. $function = 'SIN';
  4946. $args = 1;
  4947. break;
  4948. case 16 :
  4949. $function = 'COS';
  4950. $args = 1;
  4951. break;
  4952. case 17 :
  4953. $function = 'TAN';
  4954. $args = 1;
  4955. break;
  4956. case 18 :
  4957. $function = 'ATAN';
  4958. $args = 1;
  4959. break;
  4960. case 19 :
  4961. $function = 'PI';
  4962. $args = 0;
  4963. break;
  4964. case 20 :
  4965. $function = 'SQRT';
  4966. $args = 1;
  4967. break;
  4968. case 21 :
  4969. $function = 'EXP';
  4970. $args = 1;
  4971. break;
  4972. case 22 :
  4973. $function = 'LN';
  4974. $args = 1;
  4975. break;
  4976. case 23 :
  4977. $function = 'LOG10';
  4978. $args = 1;
  4979. break;
  4980. case 24 :
  4981. $function = 'ABS';
  4982. $args = 1;
  4983. break;
  4984. case 25 :
  4985. $function = 'INT';
  4986. $args = 1;
  4987. break;
  4988. case 26 :
  4989. $function = 'SIGN';
  4990. $args = 1;
  4991. break;
  4992. case 27 :
  4993. $function = 'ROUND';
  4994. $args = 2;
  4995. break;
  4996. case 30 :
  4997. $function = 'REPT';
  4998. $args = 2;
  4999. break;
  5000. case 31 :
  5001. $function = 'MID';
  5002. $args = 3;
  5003. break;
  5004. case 32 :
  5005. $function = 'LEN';
  5006. $args = 1;
  5007. break;
  5008. case 33 :
  5009. $function = 'VALUE';
  5010. $args = 1;
  5011. break;
  5012. case 34 :
  5013. $function = 'TRUE';
  5014. $args = 0;
  5015. break;
  5016. case 35 :
  5017. $function = 'FALSE';
  5018. $args = 0;
  5019. break;
  5020. case 38 :
  5021. $function = 'NOT';
  5022. $args = 1;
  5023. break;
  5024. case 39 :
  5025. $function = 'MOD';
  5026. $args = 2;
  5027. break;
  5028. case 40 :
  5029. $function = 'DCOUNT';
  5030. $args = 3;
  5031. break;
  5032. case 41 :
  5033. $function = 'DSUM';
  5034. $args = 3;
  5035. break;
  5036. case 42 :
  5037. $function = 'DAVERAGE';
  5038. $args = 3;
  5039. break;
  5040. case 43 :
  5041. $function = 'DMIN';
  5042. $args = 3;
  5043. break;
  5044. case 44 :
  5045. $function = 'DMAX';
  5046. $args = 3;
  5047. break;
  5048. case 45 :
  5049. $function = 'DSTDEV';
  5050. $args = 3;
  5051. break;
  5052. case 48 :
  5053. $function = 'TEXT';
  5054. $args = 2;
  5055. break;
  5056. case 61 :
  5057. $function = 'MIRR';
  5058. $args = 3;
  5059. break;
  5060. case 63 :
  5061. $function = 'RAND';
  5062. $args = 0;
  5063. break;
  5064. case 65 :
  5065. $function = 'DATE';
  5066. $args = 3;
  5067. break;
  5068. case 66 :
  5069. $function = 'TIME';
  5070. $args = 3;
  5071. break;
  5072. case 67 :
  5073. $function = 'DAY';
  5074. $args = 1;
  5075. break;
  5076. case 68 :
  5077. $function = 'MONTH';
  5078. $args = 1;
  5079. break;
  5080. case 69 :
  5081. $function = 'YEAR';
  5082. $args = 1;
  5083. break;
  5084. case 71 :
  5085. $function = 'HOUR';
  5086. $args = 1;
  5087. break;
  5088. case 72 :
  5089. $function = 'MINUTE';
  5090. $args = 1;
  5091. break;
  5092. case 73 :
  5093. $function = 'SECOND';
  5094. $args = 1;
  5095. break;
  5096. case 74 :
  5097. $function = 'NOW';
  5098. $args = 0;
  5099. break;
  5100. case 75 :
  5101. $function = 'AREAS';
  5102. $args = 1;
  5103. break;
  5104. case 76 :
  5105. $function = 'ROWS';
  5106. $args = 1;
  5107. break;
  5108. case 77 :
  5109. $function = 'COLUMNS';
  5110. $args = 1;
  5111. break;
  5112. case 83 :
  5113. $function = 'TRANSPOSE';
  5114. $args = 1;
  5115. break;
  5116. case 86 :
  5117. $function = 'TYPE';
  5118. $args = 1;
  5119. break;
  5120. case 97 :
  5121. $function = 'ATAN2';
  5122. $args = 2;
  5123. break;
  5124. case 98 :
  5125. $function = 'ASIN';
  5126. $args = 1;
  5127. break;
  5128. case 99 :
  5129. $function = 'ACOS';
  5130. $args = 1;
  5131. break;
  5132. case 105 :
  5133. $function = 'ISREF';
  5134. $args = 1;
  5135. break;
  5136. case 111 :
  5137. $function = 'CHAR';
  5138. $args = 1;
  5139. break;
  5140. case 112 :
  5141. $function = 'LOWER';
  5142. $args = 1;
  5143. break;
  5144. case 113 :
  5145. $function = 'UPPER';
  5146. $args = 1;
  5147. break;
  5148. case 114 :
  5149. $function = 'PROPER';
  5150. $args = 1;
  5151. break;
  5152. case 117 :
  5153. $function = 'EXACT';
  5154. $args = 2;
  5155. break;
  5156. case 118 :
  5157. $function = 'TRIM';
  5158. $args = 1;
  5159. break;
  5160. case 119 :
  5161. $function = 'REPLACE';
  5162. $args = 4;
  5163. break;
  5164. case 121 :
  5165. $function = 'CODE';
  5166. $args = 1;
  5167. break;
  5168. case 126 :
  5169. $function = 'ISERR';
  5170. $args = 1;
  5171. break;
  5172. case 127 :
  5173. $function = 'ISTEXT';
  5174. $args = 1;
  5175. break;
  5176. case 128 :
  5177. $function = 'ISNUMBER';
  5178. $args = 1;
  5179. break;
  5180. case 129 :
  5181. $function = 'ISBLANK';
  5182. $args = 1;
  5183. break;
  5184. case 130 :
  5185. $function = 'T';
  5186. $args = 1;
  5187. break;
  5188. case 131 :
  5189. $function = 'N';
  5190. $args = 1;
  5191. break;
  5192. case 140 :
  5193. $function = 'DATEVALUE';
  5194. $args = 1;
  5195. break;
  5196. case 141 :
  5197. $function = 'TIMEVALUE';
  5198. $args = 1;
  5199. break;
  5200. case 142 :
  5201. $function = 'SLN';
  5202. $args = 3;
  5203. break;
  5204. case 143 :
  5205. $function = 'SYD';
  5206. $args = 4;
  5207. break;
  5208. case 162 :
  5209. $function = 'CLEAN';
  5210. $args = 1;
  5211. break;
  5212. case 163 :
  5213. $function = 'MDETERM';
  5214. $args = 1;
  5215. break;
  5216. case 164 :
  5217. $function = 'MINVERSE';
  5218. $args = 1;
  5219. break;
  5220. case 165 :
  5221. $function = 'MMULT';
  5222. $args = 2;
  5223. break;
  5224. case 184 :
  5225. $function = 'FACT';
  5226. $args = 1;
  5227. break;
  5228. case 189 :
  5229. $function = 'DPRODUCT';
  5230. $args = 3;
  5231. break;
  5232. case 190 :
  5233. $function = 'ISNONTEXT';
  5234. $args = 1;
  5235. break;
  5236. case 195 :
  5237. $function = 'DSTDEVP';
  5238. $args = 3;
  5239. break;
  5240. case 196 :
  5241. $function = 'DVARP';
  5242. $args = 3;
  5243. break;
  5244. case 198 :
  5245. $function = 'ISLOGICAL';
  5246. $args = 1;
  5247. break;
  5248. case 199 :
  5249. $function = 'DCOUNTA';
  5250. $args = 3;
  5251. break;
  5252. case 207 :
  5253. $function = 'REPLACEB';
  5254. $args = 4;
  5255. break;
  5256. case 210 :
  5257. $function = 'MIDB';
  5258. $args = 3;
  5259. break;
  5260. case 211 :
  5261. $function = 'LENB';
  5262. $args = 1;
  5263. break;
  5264. case 212 :
  5265. $function = 'ROUNDUP';
  5266. $args = 2;
  5267. break;
  5268. case 213 :
  5269. $function = 'ROUNDDOWN';
  5270. $args = 2;
  5271. break;
  5272. case 214 :
  5273. $function = 'ASC';
  5274. $args = 1;
  5275. break;
  5276. case 215 :
  5277. $function = 'DBCS';
  5278. $args = 1;
  5279. break;
  5280. case 221 :
  5281. $function = 'TODAY';
  5282. $args = 0;
  5283. break;
  5284. case 229 :
  5285. $function = 'SINH';
  5286. $args = 1;
  5287. break;
  5288. case 230 :
  5289. $function = 'COSH';
  5290. $args = 1;
  5291. break;
  5292. case 231 :
  5293. $function = 'TANH';
  5294. $args = 1;
  5295. break;
  5296. case 232 :
  5297. $function = 'ASINH';
  5298. $args = 1;
  5299. break;
  5300. case 233 :
  5301. $function = 'ACOSH';
  5302. $args = 1;
  5303. break;
  5304. case 234 :
  5305. $function = 'ATANH';
  5306. $args = 1;
  5307. break;
  5308. case 235 :
  5309. $function = 'DGET';
  5310. $args = 3;
  5311. break;
  5312. case 244 :
  5313. $function = 'INFO';
  5314. $args = 1;
  5315. break;
  5316. case 252 :
  5317. $function = 'FREQUENCY';
  5318. $args = 2;
  5319. break;
  5320. case 261 :
  5321. $function = 'ERROR.TYPE';
  5322. $args = 1;
  5323. break;
  5324. case 271 :
  5325. $function = 'GAMMALN';
  5326. $args = 1;
  5327. break;
  5328. case 273 :
  5329. $function = 'BINOMDIST';
  5330. $args = 4;
  5331. break;
  5332. case 274 :
  5333. $function = 'CHIDIST';
  5334. $args = 2;
  5335. break;
  5336. case 275 :
  5337. $function = 'CHIINV';
  5338. $args = 2;
  5339. break;
  5340. case 276 :
  5341. $function = 'COMBIN';
  5342. $args = 2;
  5343. break;
  5344. case 277 :
  5345. $function = 'CONFIDENCE';
  5346. $args = 3;
  5347. break;
  5348. case 278 :
  5349. $function = 'CRITBINOM';
  5350. $args = 3;
  5351. break;
  5352. case 279 :
  5353. $function = 'EVEN';
  5354. $args = 1;
  5355. break;
  5356. case 280 :
  5357. $function = 'EXPONDIST';
  5358. $args = 3;
  5359. break;
  5360. case 281 :
  5361. $function = 'FDIST';
  5362. $args = 3;
  5363. break;
  5364. case 282 :
  5365. $function = 'FINV';
  5366. $args = 3;
  5367. break;
  5368. case 283 :
  5369. $function = 'FISHER';
  5370. $args = 1;
  5371. break;
  5372. case 284 :
  5373. $function = 'FISHERINV';
  5374. $args = 1;
  5375. break;
  5376. case 285 :
  5377. $function = 'FLOOR';
  5378. $args = 2;
  5379. break;
  5380. case 286 :
  5381. $function = 'GAMMADIST';
  5382. $args = 4;
  5383. break;
  5384. case 287 :
  5385. $function = 'GAMMAINV';
  5386. $args = 3;
  5387. break;
  5388. case 288 :
  5389. $function = 'CEILING';
  5390. $args = 2;
  5391. break;
  5392. case 289 :
  5393. $function = 'HYPGEOMDIST';
  5394. $args = 4;
  5395. break;
  5396. case 290 :
  5397. $function = 'LOGNORMDIST';
  5398. $args = 3;
  5399. break;
  5400. case 291 :
  5401. $function = 'LOGINV';
  5402. $args = 3;
  5403. break;
  5404. case 292 :
  5405. $function = 'NEGBINOMDIST';
  5406. $args = 3;
  5407. break;
  5408. case 293 :
  5409. $function = 'NORMDIST';
  5410. $args = 4;
  5411. break;
  5412. case 294 :
  5413. $function = 'NORMSDIST';
  5414. $args = 1;
  5415. break;
  5416. case 295 :
  5417. $function = 'NORMINV';
  5418. $args = 3;
  5419. break;
  5420. case 296 :
  5421. $function = 'NORMSINV';
  5422. $args = 1;
  5423. break;
  5424. case 297 :
  5425. $function = 'STANDARDIZE';
  5426. $args = 3;
  5427. break;
  5428. case 298 :
  5429. $function = 'ODD';
  5430. $args = 1;
  5431. break;
  5432. case 299 :
  5433. $function = 'PERMUT';
  5434. $args = 2;
  5435. break;
  5436. case 300 :
  5437. $function = 'POISSON';
  5438. $args = 3;
  5439. break;
  5440. case 301 :
  5441. $function = 'TDIST';
  5442. $args = 3;
  5443. break;
  5444. case 302 :
  5445. $function = 'WEIBULL';
  5446. $args = 4;
  5447. break;
  5448. case 303 :
  5449. $function = 'SUMXMY2';
  5450. $args = 2;
  5451. break;
  5452. case 304 :
  5453. $function = 'SUMX2MY2';
  5454. $args = 2;
  5455. break;
  5456. case 305 :
  5457. $function = 'SUMX2PY2';
  5458. $args = 2;
  5459. break;
  5460. case 306 :
  5461. $function = 'CHITEST';
  5462. $args = 2;
  5463. break;
  5464. case 307 :
  5465. $function = 'CORREL';
  5466. $args = 2;
  5467. break;
  5468. case 308 :
  5469. $function = 'COVAR';
  5470. $args = 2;
  5471. break;
  5472. case 309 :
  5473. $function = 'FORECAST';
  5474. $args = 3;
  5475. break;
  5476. case 310 :
  5477. $function = 'FTEST';
  5478. $args = 2;
  5479. break;
  5480. case 311 :
  5481. $function = 'INTERCEPT';
  5482. $args = 2;
  5483. break;
  5484. case 312 :
  5485. $function = 'PEARSON';
  5486. $args = 2;
  5487. break;
  5488. case 313 :
  5489. $function = 'RSQ';
  5490. $args = 2;
  5491. break;
  5492. case 314 :
  5493. $function = 'STEYX';
  5494. $args = 2;
  5495. break;
  5496. case 315 :
  5497. $function = 'SLOPE';
  5498. $args = 2;
  5499. break;
  5500. case 316 :
  5501. $function = 'TTEST';
  5502. $args = 4;
  5503. break;
  5504. case 325 :
  5505. $function = 'LARGE';
  5506. $args = 2;
  5507. break;
  5508. case 326 :
  5509. $function = 'SMALL';
  5510. $args = 2;
  5511. break;
  5512. case 327 :
  5513. $function = 'QUARTILE';
  5514. $args = 2;
  5515. break;
  5516. case 328 :
  5517. $function = 'PERCENTILE';
  5518. $args = 2;
  5519. break;
  5520. case 331 :
  5521. $function = 'TRIMMEAN';
  5522. $args = 2;
  5523. break;
  5524. case 332 :
  5525. $function = 'TINV';
  5526. $args = 2;
  5527. break;
  5528. case 337 :
  5529. $function = 'POWER';
  5530. $args = 2;
  5531. break;
  5532. case 342 :
  5533. $function = 'RADIANS';
  5534. $args = 1;
  5535. break;
  5536. case 343 :
  5537. $function = 'DEGREES';
  5538. $args = 1;
  5539. break;
  5540. case 346 :
  5541. $function = 'COUNTIF';
  5542. $args = 2;
  5543. break;
  5544. case 347 :
  5545. $function = 'COUNTBLANK';
  5546. $args = 1;
  5547. break;
  5548. case 350 :
  5549. $function = 'ISPMT';
  5550. $args = 4;
  5551. break;
  5552. case 351 :
  5553. $function = 'DATEDIF';
  5554. $args = 3;
  5555. break;
  5556. case 352 :
  5557. $function = 'DATESTRING';
  5558. $args = 1;
  5559. break;
  5560. case 353 :
  5561. $function = 'NUMBERSTRING';
  5562. $args = 2;
  5563. break;
  5564. case 360 :
  5565. $function = 'PHONETIC';
  5566. $args = 1;
  5567. break;
  5568. case 368 :
  5569. $function = 'BAHTTEXT';
  5570. $args = 1;
  5571. break;
  5572. default :
  5573. throw new Exception('Unrecognized function in formula');
  5574. break;
  5575. }
  5576. $data = array('function' => $function, 'args' => $args);
  5577. break;
  5578. case 0x22 : // function with variable number of arguments
  5579. case 0x42 :
  5580. case 0x62 :
  5581. $name = 'tFuncV';
  5582. $size = 4;
  5583. // offset: 1; size: 1; number of arguments
  5584. $args = ord($formulaData[1]);
  5585. // offset: 2: size: 2; index to built-in sheet function
  5586. $index = self :: _GetInt2d($formulaData, 2);
  5587. switch ($index)
  5588. {
  5589. case 0 :
  5590. $function = 'COUNT';
  5591. break;
  5592. case 1 :
  5593. $function = 'IF';
  5594. break;
  5595. case 4 :
  5596. $function = 'SUM';
  5597. break;
  5598. case 5 :
  5599. $function = 'AVERAGE';
  5600. break;
  5601. case 6 :
  5602. $function = 'MIN';
  5603. break;
  5604. case 7 :
  5605. $function = 'MAX';
  5606. break;
  5607. case 8 :
  5608. $function = 'ROW';
  5609. break;
  5610. case 9 :
  5611. $function = 'COLUMN';
  5612. break;
  5613. case 11 :
  5614. $function = 'NPV';
  5615. break;
  5616. case 12 :
  5617. $function = 'STDEV';
  5618. break;
  5619. case 13 :
  5620. $function = 'DOLLAR';
  5621. break;
  5622. case 14 :
  5623. $function = 'FIXED';
  5624. break;
  5625. case 28 :
  5626. $function = 'LOOKUP';
  5627. break;
  5628. case 29 :
  5629. $function = 'INDEX';
  5630. break;
  5631. case 36 :
  5632. $function = 'AND';
  5633. break;
  5634. case 37 :
  5635. $function = 'OR';
  5636. break;
  5637. case 46 :
  5638. $function = 'VAR';
  5639. break;
  5640. case 49 :
  5641. $function = 'LINEST';
  5642. break;
  5643. case 50 :
  5644. $function = 'TREND';
  5645. break;
  5646. case 51 :
  5647. $function = 'LOGEST';
  5648. break;
  5649. case 52 :
  5650. $function = 'GROWTH';
  5651. break;
  5652. case 56 :
  5653. $function = 'PV';
  5654. break;
  5655. case 57 :
  5656. $function = 'FV';
  5657. break;
  5658. case 58 :
  5659. $function = 'NPER';
  5660. break;
  5661. case 59 :
  5662. $function = 'PMT';
  5663. break;
  5664. case 60 :
  5665. $function = 'RATE';
  5666. break;
  5667. case 62 :
  5668. $function = 'IRR';
  5669. break;
  5670. case 64 :
  5671. $function = 'MATCH';
  5672. break;
  5673. case 70 :
  5674. $function = 'WEEKDAY';
  5675. break;
  5676. case 78 :
  5677. $function = 'OFFSET';
  5678. break;
  5679. case 82 :
  5680. $function = 'SEARCH';
  5681. break;
  5682. case 100 :
  5683. $function = 'CHOOSE';
  5684. break;
  5685. case 101 :
  5686. $function = 'HLOOKUP';
  5687. break;
  5688. case 102 :
  5689. $function = 'VLOOKUP';
  5690. break;
  5691. case 109 :
  5692. $function = 'LOG';
  5693. break;
  5694. case 115 :
  5695. $function = 'LEFT';
  5696. break;
  5697. case 116 :
  5698. $function = 'RIGHT';
  5699. break;
  5700. case 120 :
  5701. $function = 'SUBSTITUTE';
  5702. break;
  5703. case 124 :
  5704. $function = 'FIND';
  5705. break;
  5706. case 125 :
  5707. $function = 'CELL';
  5708. break;
  5709. case 144 :
  5710. $function = 'DDB';
  5711. break;
  5712. case 148 :
  5713. $function = 'INDIRECT';
  5714. break;
  5715. case 167 :
  5716. $function = 'IPMT';
  5717. break;
  5718. case 168 :
  5719. $function = 'PPMT';
  5720. break;
  5721. case 169 :
  5722. $function = 'COUNTA';
  5723. break;
  5724. case 183 :
  5725. $function = 'PRODUCT';
  5726. break;
  5727. case 193 :
  5728. $function = 'STDEVP';
  5729. break;
  5730. case 194 :
  5731. $function = 'VARP';
  5732. break;
  5733. case 197 :
  5734. $function = 'TRUNC';
  5735. break;
  5736. case 204 :
  5737. $function = 'USDOLLAR';
  5738. break;
  5739. case 205 :
  5740. $function = 'FINDB';
  5741. break;
  5742. case 206 :
  5743. $function = 'SEARCHB';
  5744. break;
  5745. case 208 :
  5746. $function = 'LEFTB';
  5747. break;
  5748. case 209 :
  5749. $function = 'RIGHTB';
  5750. break;
  5751. case 216 :
  5752. $function = 'RANK';
  5753. break;
  5754. case 219 :
  5755. $function = 'ADDRESS';
  5756. break;
  5757. case 220 :
  5758. $function = 'DAYS360';
  5759. break;
  5760. case 222 :
  5761. $function = 'VDB';
  5762. break;
  5763. case 227 :
  5764. $function = 'MEDIAN';
  5765. break;
  5766. case 228 :
  5767. $function = 'SUMPRODUCT';
  5768. break;
  5769. case 247 :
  5770. $function = 'DB';
  5771. break;
  5772. case 255 :
  5773. $function = '';
  5774. break;
  5775. case 269 :
  5776. $function = 'AVEDEV';
  5777. break;
  5778. case 270 :
  5779. $function = 'BETADIST';
  5780. break;
  5781. case 272 :
  5782. $function = 'BETAINV';
  5783. break;
  5784. case 317 :
  5785. $function = 'PROB';
  5786. break;
  5787. case 318 :
  5788. $function = 'DEVSQ';
  5789. break;
  5790. case 319 :
  5791. $function = 'GEOMEAN';
  5792. break;
  5793. case 320 :
  5794. $function = 'HARMEAN';
  5795. break;
  5796. case 321 :
  5797. $function = 'SUMSQ';
  5798. break;
  5799. case 322 :
  5800. $function = 'KURT';
  5801. break;
  5802. case 323 :
  5803. $function = 'SKEW';
  5804. break;
  5805. case 324 :
  5806. $function = 'ZTEST';
  5807. break;
  5808. case 329 :
  5809. $function = 'PERCENTRANK';
  5810. break;
  5811. case 330 :
  5812. $function = 'MODE';
  5813. break;
  5814. case 336 :
  5815. $function = 'CONCATENATE';
  5816. break;
  5817. case 344 :
  5818. $function = 'SUBTOTAL';
  5819. break;
  5820. case 345 :
  5821. $function = 'SUMIF';
  5822. break;
  5823. case 354 :
  5824. $function = 'ROMAN';
  5825. break;
  5826. case 358 :
  5827. $function = 'GETPIVOTDATA';
  5828. break;
  5829. case 359 :
  5830. $function = 'HYPERLINK';
  5831. break;
  5832. case 361 :
  5833. $function = 'AVERAGEA';
  5834. break;
  5835. case 362 :
  5836. $function = 'MAXA';
  5837. break;
  5838. case 363 :
  5839. $function = 'MINA';
  5840. break;
  5841. case 364 :
  5842. $function = 'STDEVPA';
  5843. break;
  5844. case 365 :
  5845. $function = 'VARPA';
  5846. break;
  5847. case 366 :
  5848. $function = 'STDEVA';
  5849. break;
  5850. case 367 :
  5851. $function = 'VARA';
  5852. break;
  5853. default :
  5854. throw new Exception('Unrecognized function in formula');
  5855. break;
  5856. }
  5857. $data = array('function' => $function, 'args' => $args);
  5858. break;
  5859. case 0x23 : // index to defined name
  5860. case 0x43 :
  5861. case 0x63 :
  5862. $name = 'tName';
  5863. $size = 5;
  5864. // offset: 1; size: 2; one-based index to definedname record
  5865. $definedNameIndex = self :: _GetInt2d($formulaData, 1) - 1;
  5866. // offset: 2; size: 2; not used
  5867. $data = $this->_definedname[$definedNameIndex]['name'];
  5868. break;
  5869. case 0x24 : // single cell reference e.g. A5
  5870. case 0x44 :
  5871. case 0x64 :
  5872. $name = 'tRef';
  5873. $size = 5;
  5874. $data = $this->_readBIFF8CellAddress(substr($formulaData, 1, 4));
  5875. break;
  5876. case 0x25 : // cell range reference to cells in the same sheet (2d)
  5877. case 0x45 :
  5878. case 0x65 :
  5879. $name = 'tArea';
  5880. $size = 9;
  5881. $data = $this->_readBIFF8CellRangeAddress(substr($formulaData, 1, 8));
  5882. break;
  5883. case 0x26 : // Constant reference sub-expression
  5884. case 0x46 :
  5885. case 0x66 :
  5886. $name = 'tMemArea';
  5887. // offset: 1; size: 4; not used
  5888. // offset: 5; size: 2; size of the following subexpression
  5889. $subSize = self :: _GetInt2d($formulaData, 5);
  5890. $size = 7 + $subSize;
  5891. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  5892. break;
  5893. case 0x27 : // Deleted constant reference sub-expression
  5894. case 0x47 :
  5895. case 0x67 :
  5896. $name = 'tMemErr';
  5897. // offset: 1; size: 4; not used
  5898. // offset: 5; size: 2; size of the following subexpression
  5899. $subSize = self :: _GetInt2d($formulaData, 5);
  5900. $size = 7 + $subSize;
  5901. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  5902. break;
  5903. case 0x29 : // Variable reference sub-expression
  5904. case 0x49 :
  5905. case 0x69 :
  5906. $name = 'tMemFunc';
  5907. // offset: 1; size: 2; size of the following sub-expression
  5908. $subSize = self :: _GetInt2d($formulaData, 1);
  5909. $size = 3 + $subSize;
  5910. $data = $this->_getFormulaFromData(substr($formulaData, 3, $subSize));
  5911. break;
  5912. case 0x2C : // Relative 2d cell reference reference, used in shared formulas and some other places
  5913. case 0x4C :
  5914. case 0x6C :
  5915. $name = 'tRefN';
  5916. $size = 5;
  5917. $data = $this->_readBIFF8CellAddressB(substr($formulaData, 1, 4), $baseCell);
  5918. break;
  5919. case 0x2D : // Relative 2d range reference
  5920. case 0x4D :
  5921. case 0x6D :
  5922. $name = 'tAreaN';
  5923. $size = 9;
  5924. $data = $this->_readBIFF8CellRangeAddressB(substr($formulaData, 1, 8), $baseCell);
  5925. break;
  5926. case 0x39 : // External name
  5927. case 0x59 :
  5928. case 0x79 :
  5929. $name = 'tNameX';
  5930. $size = 7;
  5931. // offset: 1; size: 2; index to REF entry in EXTERNSHEET record
  5932. // offset: 3; size: 2; one-based index to DEFINEDNAME or EXTERNNAME record
  5933. $index = self :: _GetInt2d($formulaData, 3);
  5934. // assume index is to EXTERNNAME record
  5935. $data = $this->_externalNames[$index - 1]['name'];
  5936. // offset: 5; size: 2; not used
  5937. break;
  5938. case 0x3A : // 3d reference to cell
  5939. case 0x5A :
  5940. case 0x7A :
  5941. $name = 'tRef3d';
  5942. $size = 7;
  5943. try
  5944. {
  5945. // offset: 1; size: 2; index to REF entry
  5946. $sheetRange = $this->_readSheetRangeByRefIndex(self :: _GetInt2d($formulaData, 1));
  5947. // offset: 3; size: 4; cell address
  5948. $cellAddress = $this->_readBIFF8CellAddress(substr($formulaData, 3, 4));
  5949. $data = "$sheetRange!$cellAddress";
  5950. }
  5951. catch (Exception $e)
  5952. {
  5953. // deleted sheet reference
  5954. $data = '#REF!';
  5955. }
  5956. break;
  5957. case 0x3B : // 3d reference to cell range
  5958. case 0x5B :
  5959. case 0x7B :
  5960. $name = 'tArea3d';
  5961. $size = 11;
  5962. try
  5963. {
  5964. // offset: 1; size: 2; index to REF entry
  5965. $sheetRange = $this->_readSheetRangeByRefIndex(self :: _GetInt2d($formulaData, 1));
  5966. // offset: 3; size: 8; cell address
  5967. $cellRangeAddress = $this->_readBIFF8CellRangeAddress(substr($formulaData, 3, 8));
  5968. $data = "$sheetRange!$cellRangeAddress";
  5969. }
  5970. catch (Exception $e)
  5971. {
  5972. // deleted sheet reference
  5973. $data = '#REF!';
  5974. }
  5975. break;
  5976. // Unknown cases // don't know how to deal with
  5977. default :
  5978. throw new Exception('Unrecognized token ' . sprintf('%02X', $id) . ' in formula');
  5979. break;
  5980. }
  5981. return array('id' => $id, 'name' => $name, 'size' => $size, 'data' => $data);
  5982. }
  5983. /**
  5984. * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
  5985. * section 3.3.4
  5986. *
  5987. * @param string $cellAddressStructure
  5988. * @return string
  5989. */
  5990. private function _readBIFF8CellAddress($cellAddressStructure)
  5991. {
  5992. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  5993. $row = self :: _GetInt2d($cellAddressStructure, 0) + 1;
  5994. // offset: 2; size: 2; index to column or column offset + relative flags
  5995. // bit: 7-0; mask 0x00FF; column index
  5996. $column = PHPExcel_Cell :: stringFromColumnIndex(0x00FF & self :: _GetInt2d($cellAddressStructure, 2));
  5997. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5998. if (! (0x4000 & self :: _GetInt2d($cellAddressStructure, 2)))
  5999. {
  6000. $column = '$' . $column;
  6001. }
  6002. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  6003. if (! (0x8000 & self :: _GetInt2d($cellAddressStructure, 2)))
  6004. {
  6005. $row = '$' . $row;
  6006. }
  6007. return $column . $row;
  6008. }
  6009. /**
  6010. * Reads a cell address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  6011. * to indicate offsets from a base cell
  6012. * section 3.3.4
  6013. *
  6014. * @param string $cellAddressStructure
  6015. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  6016. * @return string
  6017. */
  6018. private function _readBIFF8CellAddressB($cellAddressStructure, $baseCell = 'A1')
  6019. {
  6020. list($baseCol, $baseRow) = PHPExcel_Cell :: coordinateFromString($baseCell);
  6021. $baseCol = PHPExcel_Cell :: columnIndexFromString($baseCol) - 1;
  6022. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  6023. $rowIndex = self :: _GetInt2d($cellAddressStructure, 0);
  6024. $row = self :: _GetInt2d($cellAddressStructure, 0) + 1;
  6025. // offset: 2; size: 2; index to column or column offset + relative flags
  6026. // bit: 7-0; mask 0x00FF; column index
  6027. $colIndex = 0x00FF & self :: _GetInt2d($cellAddressStructure, 2);
  6028. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  6029. if (! (0x4000 & self :: _GetInt2d($cellAddressStructure, 2)))
  6030. {
  6031. $column = PHPExcel_Cell :: stringFromColumnIndex($colIndex);
  6032. $column = '$' . $column;
  6033. }
  6034. else
  6035. {
  6036. $colIndex = ($colIndex <= 127) ? $colIndex : $colIndex - 256;
  6037. $column = PHPExcel_Cell :: stringFromColumnIndex($baseCol + $colIndex);
  6038. }
  6039. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  6040. if (! (0x8000 & self :: _GetInt2d($cellAddressStructure, 2)))
  6041. {
  6042. $row = '$' . $row;
  6043. }
  6044. else
  6045. {
  6046. $rowIndex = ($rowIndex <= 32767) ? $rowIndex : $rowIndex - 65536;
  6047. $row = $baseRow + $rowIndex;
  6048. }
  6049. return $column . $row;
  6050. }
  6051. /**
  6052. * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
  6053. * always fixed range
  6054. * section 2.5.14
  6055. *
  6056. * @param string $subData
  6057. * @return string
  6058. * @throws Exception
  6059. */
  6060. private function _readBIFF5CellRangeAddressFixed($subData)
  6061. {
  6062. // offset: 0; size: 2; index to first row
  6063. $fr = self :: _GetInt2d($subData, 0) + 1;
  6064. // offset: 2; size: 2; index to last row
  6065. $lr = self :: _GetInt2d($subData, 2) + 1;
  6066. // offset: 4; size: 1; index to first column
  6067. $fc = ord($subData{4});
  6068. // offset: 5; size: 1; index to last column
  6069. $lc = ord($subData{5});
  6070. // check values
  6071. if ($fr > $lr || $fc > $lc)
  6072. {
  6073. throw new Exception('Not a cell range address');
  6074. }
  6075. // column index to letter
  6076. $fc = PHPExcel_Cell :: stringFromColumnIndex($fc);
  6077. $lc = PHPExcel_Cell :: stringFromColumnIndex($lc);
  6078. if ($fr == $lr and $fc == $lc)
  6079. {
  6080. return "$fc$fr";
  6081. }
  6082. return "$fc$fr:$lc$lr";
  6083. }
  6084. /**
  6085. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or 'A1'
  6086. * always fixed range
  6087. * section 2.5.14
  6088. *
  6089. * @param string $subData
  6090. * @return string
  6091. * @throws Exception
  6092. */
  6093. private function _readBIFF8CellRangeAddressFixed($subData)
  6094. {
  6095. // offset: 0; size: 2; index to first row
  6096. $fr = self :: _GetInt2d($subData, 0) + 1;
  6097. // offset: 2; size: 2; index to last row
  6098. $lr = self :: _GetInt2d($subData, 2) + 1;
  6099. // offset: 4; size: 2; index to first column
  6100. $fc = self :: _GetInt2d($subData, 4);
  6101. // offset: 6; size: 2; index to last column
  6102. $lc = self :: _GetInt2d($subData, 6);
  6103. // check values
  6104. if ($fr > $lr || $fc > $lc)
  6105. {
  6106. throw new Exception('Not a cell range address');
  6107. }
  6108. // column index to letter
  6109. $fc = PHPExcel_Cell :: stringFromColumnIndex($fc);
  6110. $lc = PHPExcel_Cell :: stringFromColumnIndex($lc);
  6111. if ($fr == $lr and $fc == $lc)
  6112. {
  6113. return "$fc$fr";
  6114. }
  6115. return "$fc$fr:$lc$lr";
  6116. }
  6117. /**
  6118. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
  6119. * there are flags indicating whether column/row index is relative
  6120. * section 3.3.4
  6121. *
  6122. * @param string $subData
  6123. * @return string
  6124. */
  6125. private function _readBIFF8CellRangeAddress($subData)
  6126. {
  6127. // todo: if cell range is just a single cell, should this funciton
  6128. // not just return e.g. 'A1' and not 'A1:A1' ?
  6129. // offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
  6130. $fr = self :: _GetInt2d($subData, 0) + 1;
  6131. // offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
  6132. $lr = self :: _GetInt2d($subData, 2) + 1;
  6133. // offset: 4; size: 2; index to first column or column offset + relative flags
  6134. // bit: 7-0; mask 0x00FF; column index
  6135. $fc = PHPExcel_Cell :: stringFromColumnIndex(0x00FF & self :: _GetInt2d($subData, 4));
  6136. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  6137. if (! (0x4000 & self :: _GetInt2d($subData, 4)))
  6138. {
  6139. $fc = '$' . $fc;
  6140. }
  6141. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  6142. if (! (0x8000 & self :: _GetInt2d($subData, 4)))
  6143. {
  6144. $fr = '$' . $fr;
  6145. }
  6146. // offset: 6; size: 2; index to last column or column offset + relative flags
  6147. // bit: 7-0; mask 0x00FF; column index
  6148. $lc = PHPExcel_Cell :: stringFromColumnIndex(0x00FF & self :: _GetInt2d($subData, 6));
  6149. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  6150. if (! (0x4000 & self :: _GetInt2d($subData, 6)))
  6151. {
  6152. $lc = '$' . $lc;
  6153. }
  6154. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  6155. if (! (0x8000 & self :: _GetInt2d($subData, 6)))
  6156. {
  6157. $lr = '$' . $lr;
  6158. }
  6159. return "$fc$fr:$lc$lr";
  6160. }
  6161. /**
  6162. * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  6163. * to indicate offsets from a base cell
  6164. * section 3.3.4
  6165. *
  6166. * @param string $subData
  6167. * @param string $baseCell Base cell
  6168. * @return string Cell range address
  6169. */
  6170. private function _readBIFF8CellRangeAddressB($subData, $baseCell = 'A1')
  6171. {
  6172. list($baseCol, $baseRow) = PHPExcel_Cell :: coordinateFromString($baseCell);
  6173. $baseCol = PHPExcel_Cell :: columnIndexFromString($baseCol) - 1;
  6174. // TODO: if cell range is just a single cell, should this funciton
  6175. // not just return e.g. 'A1' and not 'A1:A1' ?
  6176. // offset: 0; size: 2; first row
  6177. $frIndex = self :: _GetInt2d($subData, 0); // adjust below
  6178. // offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
  6179. $lrIndex = self :: _GetInt2d($subData, 2); // adjust below
  6180. // offset: 4; size: 2; first column with relative/absolute flags
  6181. // bit: 7-0; mask 0x00FF; column index
  6182. $fcIndex = 0x00FF & self :: _GetInt2d($subData, 4);
  6183. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  6184. if (! (0x4000 & self :: _GetInt2d($subData, 4)))
  6185. {
  6186. // absolute column index
  6187. $fc = PHPExcel_Cell :: stringFromColumnIndex($fcIndex);
  6188. $fc = '$' . $fc;
  6189. }
  6190. else
  6191. {
  6192. // column offset
  6193. $fcIndex = ($fcIndex <= 127) ? $fcIndex : $fcIndex - 256;
  6194. $fc = PHPExcel_Cell :: stringFromColumnIndex($baseCol + $fcIndex);
  6195. }
  6196. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  6197. if (! (0x8000 & self :: _GetInt2d($subData, 4)))
  6198. {
  6199. // absolute row index
  6200. $fr = $frIndex + 1;
  6201. $fr = '$' . $fr;
  6202. }
  6203. else
  6204. {
  6205. // row offset
  6206. $frIndex = ($frIndex <= 32767) ? $frIndex : $frIndex - 65536;
  6207. $fr = $baseRow + $frIndex;
  6208. }
  6209. // offset: 6; size: 2; last column with relative/absolute flags
  6210. // bit: 7-0; mask 0x00FF; column index
  6211. $lcIndex = 0x00FF & self :: _GetInt2d($subData, 6);
  6212. $lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
  6213. $lc = PHPExcel_Cell :: stringFromColumnIndex($baseCol + $lcIndex);
  6214. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  6215. if (! (0x4000 & self :: _GetInt2d($subData, 6)))
  6216. {
  6217. // absolute column index
  6218. $lc = PHPExcel_Cell :: stringFromColumnIndex($lcIndex);
  6219. $lc = '$' . $lc;
  6220. }
  6221. else
  6222. {
  6223. // column offset
  6224. $lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
  6225. $lc = PHPExcel_Cell :: stringFromColumnIndex($baseCol + $lcIndex);
  6226. }
  6227. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  6228. if (! (0x8000 & self :: _GetInt2d($subData, 6)))
  6229. {
  6230. // absolute row index
  6231. $lr = $lrIndex + 1;
  6232. $lr = '$' . $lr;
  6233. }
  6234. else
  6235. {
  6236. // row offset
  6237. $lrIndex = ($lrIndex <= 32767) ? $lrIndex : $lrIndex - 65536;
  6238. $lr = $baseRow + $lrIndex;
  6239. }
  6240. return "$fc$fr:$lc$lr";
  6241. }
  6242. /**
  6243. * Read BIFF8 cell range address list
  6244. * section 2.5.15
  6245. *
  6246. * @param string $subData
  6247. * @return array
  6248. */
  6249. private function _readBIFF8CellRangeAddressList($subData)
  6250. {
  6251. $cellRangeAddresses = array();
  6252. // offset: 0; size: 2; number of the following cell range addresses
  6253. $nm = self :: _GetInt2d($subData, 0);
  6254. $offset = 2;
  6255. // offset: 2; size: 8 * $nm; list of $nm (fixed) cell range addresses
  6256. for($i = 0; $i < $nm; ++ $i)
  6257. {
  6258. $cellRangeAddresses[] = $this->_readBIFF8CellRangeAddressFixed(substr($subData, $offset, 8));
  6259. $offset += 8;
  6260. }
  6261. return array('size' => 2 + 8 * $nm, 'cellRangeAddresses' => $cellRangeAddresses);
  6262. }
  6263. /**
  6264. * Read BIFF5 cell range address list
  6265. * section 2.5.15
  6266. *
  6267. * @param string $subData
  6268. * @return array
  6269. */
  6270. private function _readBIFF5CellRangeAddressList($subData)
  6271. {
  6272. $cellRangeAddresses = array();
  6273. // offset: 0; size: 2; number of the following cell range addresses
  6274. $nm = self :: _GetInt2d($subData, 0);
  6275. $offset = 2;
  6276. // offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
  6277. for($i = 0; $i < $nm; ++ $i)
  6278. {
  6279. $cellRangeAddresses[] = $this->_readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
  6280. $offset += 6;
  6281. }
  6282. return array('size' => 2 + 6 * $nm, 'cellRangeAddresses' => $cellRangeAddresses);
  6283. }
  6284. /**
  6285. * Get a sheet range like Sheet1:Sheet3 from REF index
  6286. * Note: If there is only one sheet in the range, one gets e.g Sheet1
  6287. * It can also happen that the REF structure uses the -1 (FFFF) code to indicate deleted sheets,
  6288. * in which case an exception is thrown
  6289. *
  6290. * @param int $index
  6291. * @return string|false
  6292. * @throws Exception
  6293. */
  6294. private function _readSheetRangeByRefIndex($index)
  6295. {
  6296. if (isset($this->_ref[$index]))
  6297. {
  6298. $type = $this->_externalBooks[$this->_ref[$index]['externalBookIndex']]['type'];
  6299. switch ($type)
  6300. {
  6301. case 'internal' :
  6302. // check if we have a deleted 3d reference
  6303. if ($this->_ref[$index]['firstSheetIndex'] == 0xFFFF or $this->_ref[$index]['lastSheetIndex'] == 0xFFFF)
  6304. {
  6305. throw new Exception('Deleted sheet reference');
  6306. }
  6307. // we have normal sheet range (collapsed or uncollapsed)
  6308. $firstSheetName = $this->_sheets[$this->_ref[$index]['firstSheetIndex']]['name'];
  6309. $lastSheetName = $this->_sheets[$this->_ref[$index]['lastSheetIndex']]['name'];
  6310. if ($firstSheetName == $lastSheetName)
  6311. {
  6312. // collapsed sheet range
  6313. $sheetRange = $firstSheetName;
  6314. }
  6315. else
  6316. {
  6317. $sheetRange = "$firstSheetName:$lastSheetName";
  6318. }
  6319. // escape the single-quotes
  6320. $sheetRange = str_replace("'", "''", $sheetRange);
  6321. // if there are special characters, we need to enclose the range in single-quotes
  6322. // todo: check if we have identified the whole set of special characters
  6323. // it seems that the following characters are not accepted for sheet names
  6324. // and we may assume that they are not present: []*/:\?
  6325. if (preg_match("/[ !\"@#ÂŁ$%&{()}<>=+'|^,;-]/", $sheetRange))
  6326. {
  6327. $sheetRange = "'$sheetRange'";
  6328. }
  6329. return $sheetRange;
  6330. break;
  6331. default :
  6332. // TODO: external sheet support
  6333. throw new Exception('Excel5 reader only supports internal sheets in fomulas');
  6334. break;
  6335. }
  6336. }
  6337. return false;
  6338. }
  6339. /**
  6340. * read BIFF8 constant value array from array data
  6341. * returns e.g. array('value' => '{1,2;3,4}', 'size' => 40}
  6342. * section 2.5.8
  6343. *
  6344. * @param string $arrayData
  6345. * @return array
  6346. */
  6347. private static function _readBIFF8ConstantArray($arrayData)
  6348. {
  6349. // offset: 0; size: 1; number of columns decreased by 1
  6350. $nc = ord($arrayData[0]);
  6351. // offset: 1; size: 2; number of rows decreased by 1
  6352. $nr = self :: _GetInt2d($arrayData, 1);
  6353. $size = 3; // initialize
  6354. $arrayData = substr($arrayData, 3);
  6355. // offset: 3; size: var; list of ($nc + 1) * ($nr + 1) constant values
  6356. $matrixChunks = array();
  6357. for($r = 1; $r <= $nr + 1; ++ $r)
  6358. {
  6359. $items = array();
  6360. for($c = 1; $c <= $nc + 1; ++ $c)
  6361. {
  6362. $constant = self :: _readBIFF8Constant($arrayData);
  6363. $items[] = $constant['value'];
  6364. $arrayData = substr($arrayData, $constant['size']);
  6365. $size += $constant['size'];
  6366. }
  6367. $matrixChunks[] = implode(',', $items); // looks like e.g. '1,"hello"'
  6368. }
  6369. $matrix = '{' . implode(';', $matrixChunks) . '}';
  6370. return array('value' => $matrix, 'size' => $size);
  6371. }
  6372. /**
  6373. * read BIFF8 constant value which may be 'Empty Value', 'Number', 'String Value', 'Boolean Value', 'Error Value'
  6374. * section 2.5.7
  6375. * returns e.g. array('value' => '5', 'size' => 9)
  6376. *
  6377. * @param string $valueData
  6378. * @return array
  6379. */
  6380. private static function _readBIFF8Constant($valueData)
  6381. {
  6382. // offset: 0; size: 1; identifier for type of constant
  6383. $identifier = ord($valueData[0]);
  6384. switch ($identifier)
  6385. {
  6386. case 0x00 : // empty constant (what is this?)
  6387. $value = '';
  6388. $size = 9;
  6389. break;
  6390. case 0x01 : // number
  6391. // offset: 1; size: 8; IEEE 754 floating-point value
  6392. $value = self :: _extractNumber(substr($valueData, 1, 8));
  6393. $size = 9;
  6394. break;
  6395. case 0x02 : // string value
  6396. // offset: 1; size: var; Unicode string, 16-bit string length
  6397. $string = self :: _readUnicodeStringLong(substr($valueData, 1));
  6398. $value = '"' . $string['value'] . '"';
  6399. $size = 1 + $string['size'];
  6400. break;
  6401. case 0x04 : // boolean
  6402. // offset: 1; size: 1; 0 = FALSE, 1 = TRUE
  6403. if (ord($valueData[1]))
  6404. {
  6405. $value = 'TRUE';
  6406. }
  6407. else
  6408. {
  6409. $value = 'FALSE';
  6410. }
  6411. $size = 9;
  6412. break;
  6413. case 0x10 : // error code
  6414. // offset: 1; size: 1; error code
  6415. $value = self :: _mapErrorCode(ord($valueData[1]));
  6416. $size = 9;
  6417. break;
  6418. }
  6419. return array('value' => $value, 'size' => $size);
  6420. }
  6421. /**
  6422. * Extract RGB color
  6423. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.4
  6424. *
  6425. * @param string $rgb Encoded RGB value (4 bytes)
  6426. * @return array
  6427. */
  6428. private static function _readRGB($rgb)
  6429. {
  6430. // offset: 0; size 1; Red component
  6431. $r = ord($rgb{0});
  6432. // offset: 1; size: 1; Green component
  6433. $g = ord($rgb{1});
  6434. // offset: 2; size: 1; Blue component
  6435. $b = ord($rgb{2});
  6436. // HEX notation, e.g. 'FF00FC'
  6437. $rgb = sprintf('%02X%02X%02X', $r, $g, $b);
  6438. return array('rgb' => $rgb);
  6439. }
  6440. /**
  6441. * Read byte string (8-bit string length)
  6442. * OpenOffice documentation: 2.5.2
  6443. *
  6444. * @param string $subData
  6445. * @return array
  6446. */
  6447. private function _readByteStringShort($subData)
  6448. {
  6449. // offset: 0; size: 1; length of the string (character count)
  6450. $ln = ord($subData[0]);
  6451. // offset: 1: size: var; character array (8-bit characters)
  6452. $value = $this->_decodeCodepage(substr($subData, 1, $ln));
  6453. return array('value' => $value, 'size' => 1 + $ln)// size in bytes of data structure
  6454. ;
  6455. }
  6456. /**
  6457. * Read byte string (16-bit string length)
  6458. * OpenOffice documentation: 2.5.2
  6459. *
  6460. * @param string $subData
  6461. * @return array
  6462. */
  6463. private function _readByteStringLong($subData)
  6464. {
  6465. // offset: 0; size: 2; length of the string (character count)
  6466. $ln = self :: _GetInt2d($subData, 0);
  6467. // offset: 2: size: var; character array (8-bit characters)
  6468. $value = $this->_decodeCodepage(substr($subData, 2));
  6469. //return $string;
  6470. return array('value' => $value, 'size' => 2 + $ln)// size in bytes of data structure
  6471. ;
  6472. }
  6473. /**
  6474. * Extracts an Excel Unicode short string (8-bit string length)
  6475. * OpenOffice documentation: 2.5.3
  6476. * function will automatically find out where the Unicode string ends.
  6477. *
  6478. * @param string $subData
  6479. * @return array
  6480. */
  6481. private static function _readUnicodeStringShort($subData)
  6482. {
  6483. $value = '';
  6484. // offset: 0: size: 1; length of the string (character count)
  6485. $characterCount = ord($subData[0]);
  6486. $string = self :: _readUnicodeString(substr($subData, 1), $characterCount);
  6487. // add 1 for the string length
  6488. $string['size'] += 1;
  6489. return $string;
  6490. }
  6491. /**
  6492. * Extracts an Excel Unicode long string (16-bit string length)
  6493. * OpenOffice documentation: 2.5.3
  6494. * this function is under construction, needs to support rich text, and Asian phonetic settings
  6495. *
  6496. * @param string $subData
  6497. * @return array
  6498. */
  6499. private static function _readUnicodeStringLong($subData)
  6500. {
  6501. $value = '';
  6502. // offset: 0: size: 2; length of the string (character count)
  6503. $characterCount = self :: _GetInt2d($subData, 0);
  6504. $string = self :: _readUnicodeString(substr($subData, 2), $characterCount);
  6505. // add 2 for the string length
  6506. $string['size'] += 2;
  6507. return $string;
  6508. }
  6509. /**
  6510. * Read Unicode string with no string length field, but with known character count
  6511. * this function is under construction, needs to support rich text, and Asian phonetic settings
  6512. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3
  6513. *
  6514. * @param string $subData
  6515. * @param int $characterCount
  6516. * @return array
  6517. */
  6518. private static function _readUnicodeString($subData, $characterCount)
  6519. {
  6520. $value = '';
  6521. // offset: 0: size: 1; option flags
  6522. // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  6523. $isCompressed = ! ((0x01 & ord($subData[0])) >> 0);
  6524. // bit: 2; mask: 0x04; Asian phonetic settings
  6525. $hasAsian = (0x04) & ord($subData[0]) >> 2;
  6526. // bit: 3; mask: 0x08; Rich-Text settings
  6527. $hasRichText = (0x08) & ord($subData[0]) >> 3;
  6528. // offset: 1: size: var; character array
  6529. // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  6530. // needs to be fixed
  6531. $value = self :: _encodeUTF16(substr($subData, 1, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
  6532. return array('value' => $value, 'size' => $isCompressed ? 1 + $characterCount : 1 + 2 * $characterCount)// the size in bytes including the option flags
  6533. ;
  6534. }
  6535. /**
  6536. * Convert UTF-8 string to string surounded by double quotes. Used for explicit string tokens in formulas.
  6537. * Example: hello"world --> "hello""world"
  6538. *
  6539. * @param string $value UTF-8 encoded string
  6540. * @return string
  6541. */
  6542. private static function _UTF8toExcelDoubleQuoted($value)
  6543. {
  6544. return '"' . str_replace('"', '""', $value) . '"';
  6545. }
  6546. /**
  6547. * Reads first 8 bytes of a string and return IEEE 754 float
  6548. *
  6549. * @param string $data Binary string that is at least 8 bytes long
  6550. * @return float
  6551. */
  6552. private static function _extractNumber($data)
  6553. {
  6554. $rknumhigh = self :: _GetInt4d($data, 4);
  6555. $rknumlow = self :: _GetInt4d($data, 0);
  6556. $sign = ($rknumhigh & 0x80000000) >> 31;
  6557. $exp = (($rknumhigh & 0x7ff00000) >> 20) - 1023;
  6558. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  6559. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  6560. $mantissalow2 = ($rknumlow & 0x7fffffff);
  6561. $value = $mantissa / pow(2, (20 - $exp));
  6562. if ($mantissalow1 != 0)
  6563. {
  6564. $value += 1 / pow(2, (21 - $exp));
  6565. }
  6566. $value += $mantissalow2 / pow(2, (52 - $exp));
  6567. if ($sign)
  6568. {
  6569. $value = - 1 * $value;
  6570. }
  6571. return $value;
  6572. }
  6573. private static function _GetIEEE754($rknum)
  6574. {
  6575. if (($rknum & 0x02) != 0)
  6576. {
  6577. $value = $rknum >> 2;
  6578. }
  6579. else
  6580. {
  6581. // changes by mmp, info on IEEE754 encoding from
  6582. // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  6583. // The RK format calls for using only the most significant 30 bits
  6584. // of the 64 bit floating point value. The other 34 bits are assumed
  6585. // to be 0 so we use the upper 30 bits of $rknum as follows...
  6586. $sign = ($rknum & 0x80000000) >> 31;
  6587. $exp = ($rknum & 0x7ff00000) >> 20;
  6588. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  6589. $value = $mantissa / pow(2, (20 - ($exp - 1023)));
  6590. if ($sign)
  6591. {
  6592. $value = - 1 * $value;
  6593. }
  6594. //end of changes by mmp
  6595. }
  6596. if (($rknum & 0x01) != 0)
  6597. {
  6598. $value /= 100;
  6599. }
  6600. return $value;
  6601. }
  6602. /**
  6603. * Get UTF-8 string from (compressed or uncompressed) UTF-16 string
  6604. *
  6605. * @param string $string
  6606. * @param bool $compressed
  6607. * @return string
  6608. */
  6609. private static function _encodeUTF16($string, $compressed = '')
  6610. {
  6611. if ($compressed)
  6612. {
  6613. $string = self :: _uncompressByteString($string);
  6614. }
  6615. return PHPExcel_Shared_String :: ConvertEncoding($string, 'UTF-8', 'UTF-16LE');
  6616. }
  6617. /**
  6618. * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
  6619. *
  6620. * @param string $string
  6621. * @return string
  6622. */
  6623. private static function _uncompressByteString($string)
  6624. {
  6625. $uncompressedString = '';
  6626. $strLen = strlen($string);
  6627. for($i = 0; $i < $strLen; ++ $i)
  6628. {
  6629. $uncompressedString .= $string[$i] . "\0";
  6630. }
  6631. return $uncompressedString;
  6632. }
  6633. /**
  6634. * Convert string to UTF-8. Only used for BIFF5.
  6635. *
  6636. * @param string $string
  6637. * @return string
  6638. */
  6639. private function _decodeCodepage($string)
  6640. {
  6641. return PHPExcel_Shared_String :: ConvertEncoding($string, 'UTF-8', $this->_codepage);
  6642. }
  6643. /**
  6644. * Read 16-bit unsigned integer
  6645. *
  6646. * @param string $data
  6647. * @param int $pos
  6648. * @return int
  6649. */
  6650. public static function _GetInt2d($data, $pos)
  6651. {
  6652. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8);
  6653. }
  6654. /**
  6655. * Read 32-bit signed integer
  6656. *
  6657. * @param string $data
  6658. * @param int $pos
  6659. * @return int
  6660. */
  6661. public static function _GetInt4d($data, $pos)
  6662. {
  6663. // FIX: represent numbers correctly on 64-bit system
  6664. // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  6665. // Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
  6666. $_or_24 = ord($data[$pos + 3]);
  6667. if ($_or_24 >= 128)
  6668. {
  6669. // negative number
  6670. $_ord_24 = - abs((256 - $_or_24) << 24);
  6671. }
  6672. else
  6673. {
  6674. $_ord_24 = ($_or_24 & 127) << 24;
  6675. }
  6676. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24;
  6677. }
  6678. /**
  6679. * Read color
  6680. *
  6681. * @param int $color Indexed color
  6682. * @param array $palette Color palette
  6683. * @return array RGB color value, example: array('rgb' => 'FF0000')
  6684. */
  6685. private static function _readColor($color, $palette, $version)
  6686. {
  6687. if ($color <= 0x07 || $color >= 0x40)
  6688. {
  6689. // special built-in color
  6690. return self :: _mapBuiltInColor($color);
  6691. }
  6692. elseif (isset($palette) && isset($palette[$color - 8]))
  6693. {
  6694. // palette color, color index 0x08 maps to pallete index 0
  6695. return $palette[$color - 8];
  6696. }
  6697. else
  6698. {
  6699. // default color table
  6700. if ($version == self :: XLS_BIFF8)
  6701. {
  6702. return self :: _mapColor($color);
  6703. }
  6704. else
  6705. {
  6706. // BIFF5
  6707. return self :: _mapColorBIFF5($color);
  6708. }
  6709. }
  6710. return $color;
  6711. }
  6712. /**
  6713. * Map border style
  6714. * OpenOffice documentation: 2.5.11
  6715. *
  6716. * @param int $index
  6717. * @return string
  6718. */
  6719. private static function _mapBorderStyle($index)
  6720. {
  6721. switch ($index)
  6722. {
  6723. case 0x00 :
  6724. return PHPExcel_Style_Border :: BORDER_NONE;
  6725. case 0x01 :
  6726. return PHPExcel_Style_Border :: BORDER_THIN;
  6727. case 0x02 :
  6728. return PHPExcel_Style_Border :: BORDER_MEDIUM;
  6729. case 0x03 :
  6730. return PHPExcel_Style_Border :: BORDER_DASHED;
  6731. case 0x04 :
  6732. return PHPExcel_Style_Border :: BORDER_DOTTED;
  6733. case 0x05 :
  6734. return PHPExcel_Style_Border :: BORDER_THICK;
  6735. case 0x06 :
  6736. return PHPExcel_Style_Border :: BORDER_DOUBLE;
  6737. case 0x07 :
  6738. return PHPExcel_Style_Border :: BORDER_HAIR;
  6739. case 0x08 :
  6740. return PHPExcel_Style_Border :: BORDER_MEDIUMDASHED;
  6741. case 0x09 :
  6742. return PHPExcel_Style_Border :: BORDER_DASHDOT;
  6743. case 0x0A :
  6744. return PHPExcel_Style_Border :: BORDER_MEDIUMDASHDOT;
  6745. case 0x0B :
  6746. return PHPExcel_Style_Border :: BORDER_DASHDOTDOT;
  6747. case 0x0C :
  6748. return PHPExcel_Style_Border :: BORDER_MEDIUMDASHDOTDOT;
  6749. case 0x0D :
  6750. return PHPExcel_Style_Border :: BORDER_SLANTDASHDOT;
  6751. default :
  6752. return PHPExcel_Style_Border :: BORDER_NONE;
  6753. }
  6754. }
  6755. /**
  6756. * Get fill pattern from index
  6757. * OpenOffice documentation: 2.5.12
  6758. *
  6759. * @param int $index
  6760. * @return string
  6761. */
  6762. private static function _mapFillPattern($index)
  6763. {
  6764. switch ($index)
  6765. {
  6766. case 0x00 :
  6767. return PHPExcel_Style_Fill :: FILL_NONE;
  6768. case 0x01 :
  6769. return PHPExcel_Style_Fill :: FILL_SOLID;
  6770. case 0x02 :
  6771. return PHPExcel_Style_Fill :: FILL_PATTERN_MEDIUMGRAY;
  6772. case 0x03 :
  6773. return PHPExcel_Style_Fill :: FILL_PATTERN_DARKGRAY;
  6774. case 0x04 :
  6775. return PHPExcel_Style_Fill :: FILL_PATTERN_LIGHTGRAY;
  6776. case 0x05 :
  6777. return PHPExcel_Style_Fill :: FILL_PATTERN_DARKHORIZONTAL;
  6778. case 0x06 :
  6779. return PHPExcel_Style_Fill :: FILL_PATTERN_DARKVERTICAL;
  6780. case 0x07 :
  6781. return PHPExcel_Style_Fill :: FILL_PATTERN_DARKDOWN;
  6782. case 0x08 :
  6783. return PHPExcel_Style_Fill :: FILL_PATTERN_DARKUP;
  6784. case 0x09 :
  6785. return PHPExcel_Style_Fill :: FILL_PATTERN_DARKGRID;
  6786. case 0x0A :
  6787. return PHPExcel_Style_Fill :: FILL_PATTERN_DARKTRELLIS;
  6788. case 0x0B :
  6789. return PHPExcel_Style_Fill :: FILL_PATTERN_LIGHTHORIZONTAL;
  6790. case 0x0C :
  6791. return PHPExcel_Style_Fill :: FILL_PATTERN_LIGHTVERTICAL;
  6792. case 0x0D :
  6793. return PHPExcel_Style_Fill :: FILL_PATTERN_LIGHTDOWN;
  6794. case 0x0E :
  6795. return PHPExcel_Style_Fill :: FILL_PATTERN_LIGHTUP;
  6796. case 0x0F :
  6797. return PHPExcel_Style_Fill :: FILL_PATTERN_LIGHTGRID;
  6798. case 0x10 :
  6799. return PHPExcel_Style_Fill :: FILL_PATTERN_LIGHTTRELLIS;
  6800. case 0x11 :
  6801. return PHPExcel_Style_Fill :: FILL_PATTERN_GRAY125;
  6802. case 0x12 :
  6803. return PHPExcel_Style_Fill :: FILL_PATTERN_GRAY0625;
  6804. default :
  6805. return PHPExcel_Style_Fill :: FILL_NONE;
  6806. }
  6807. }
  6808. /**
  6809. * Map error code, e.g. '#N/A'
  6810. *
  6811. * @param int $subData
  6812. * @return string
  6813. */
  6814. private static function _mapErrorCode($subData)
  6815. {
  6816. switch ($subData)
  6817. {
  6818. case 0x00 :
  6819. return '#NULL!';
  6820. break;
  6821. case 0x07 :
  6822. return '#DIV/0!';
  6823. break;
  6824. case 0x0F :
  6825. return '#VALUE!';
  6826. break;
  6827. case 0x17 :
  6828. return '#REF!';
  6829. break;
  6830. case 0x1D :
  6831. return '#NAME?';
  6832. break;
  6833. case 0x24 :
  6834. return '#NUM!';
  6835. break;
  6836. case 0x2A :
  6837. return '#N/A';
  6838. break;
  6839. default :
  6840. return false;
  6841. }
  6842. }
  6843. /**
  6844. * Map built-in color to RGB value
  6845. *
  6846. * @param int $color Indexed color
  6847. * @return array
  6848. */
  6849. private static function _mapBuiltInColor($color)
  6850. {
  6851. switch ($color)
  6852. {
  6853. case 0x00 :
  6854. return array('rgb' => '000000');
  6855. case 0x01 :
  6856. return array('rgb' => 'FFFFFF');
  6857. case 0x02 :
  6858. return array('rgb' => 'FF0000');
  6859. case 0x03 :
  6860. return array('rgb' => '00FF00');
  6861. case 0x04 :
  6862. return array('rgb' => '0000FF');
  6863. case 0x05 :
  6864. return array('rgb' => 'FFFF00');
  6865. case 0x06 :
  6866. return array('rgb' => 'FF00FF');
  6867. case 0x07 :
  6868. return array('rgb' => '00FFFF');
  6869. case 0x40 :
  6870. return array('rgb' => '000000'); // system window text color
  6871. case 0x41 :
  6872. return array('rgb' => 'FFFFFF'); // system window background color
  6873. default :
  6874. return array('rgb' => '000000');
  6875. }
  6876. }
  6877. /**
  6878. * Map color array from BIFF5 built-in color index
  6879. *
  6880. * @param int $subData
  6881. * @return array
  6882. */
  6883. private static function _mapColorBIFF5($subData)
  6884. {
  6885. switch ($subData)
  6886. {
  6887. case 0x08 :
  6888. return array('rgb' => '000000');
  6889. case 0x09 :
  6890. return array('rgb' => 'FFFFFF');
  6891. case 0x0A :
  6892. return array('rgb' => 'FF0000');
  6893. case 0x0B :
  6894. return array('rgb' => '00FF00');
  6895. case 0x0C :
  6896. return array('rgb' => '0000FF');
  6897. case 0x0D :
  6898. return array('rgb' => 'FFFF00');
  6899. case 0x0E :
  6900. return array('rgb' => 'FF00FF');
  6901. case 0x0F :
  6902. return array('rgb' => '00FFFF');
  6903. case 0x10 :
  6904. return array('rgb' => '800000');
  6905. case 0x11 :
  6906. return array('rgb' => '008000');
  6907. case 0x12 :
  6908. return array('rgb' => '000080');
  6909. case 0x13 :
  6910. return array('rgb' => '808000');
  6911. case 0x14 :
  6912. return array('rgb' => '800080');
  6913. case 0x15 :
  6914. return array('rgb' => '008080');
  6915. case 0x16 :
  6916. return array('rgb' => 'C0C0C0');
  6917. case 0x17 :
  6918. return array('rgb' => '808080');
  6919. case 0x18 :
  6920. return array('rgb' => '8080FF');
  6921. case 0x19 :
  6922. return array('rgb' => '802060');
  6923. case 0x1A :
  6924. return array('rgb' => 'FFFFC0');
  6925. case 0x1B :
  6926. return array('rgb' => 'A0E0F0');
  6927. case 0x1C :
  6928. return array('rgb' => '600080');
  6929. case 0x1D :
  6930. return array('rgb' => 'FF8080');
  6931. case 0x1E :
  6932. return array('rgb' => '0080C0');
  6933. case 0x1F :
  6934. return array('rgb' => 'C0C0FF');
  6935. case 0x20 :
  6936. return array('rgb' => '000080');
  6937. case 0x21 :
  6938. return array('rgb' => 'FF00FF');
  6939. case 0x22 :
  6940. return array('rgb' => 'FFFF00');
  6941. case 0x23 :
  6942. return array('rgb' => '00FFFF');
  6943. case 0x24 :
  6944. return array('rgb' => '800080');
  6945. case 0x25 :
  6946. return array('rgb' => '800000');
  6947. case 0x26 :
  6948. return array('rgb' => '008080');
  6949. case 0x27 :
  6950. return array('rgb' => '0000FF');
  6951. case 0x28 :
  6952. return array('rgb' => '00CFFF');
  6953. case 0x29 :
  6954. return array('rgb' => '69FFFF');
  6955. case 0x2A :
  6956. return array('rgb' => 'E0FFE0');
  6957. case 0x2B :
  6958. return array('rgb' => 'FFFF80');
  6959. case 0x2C :
  6960. return array('rgb' => 'A6CAF0');
  6961. case 0x2D :
  6962. return array('rgb' => 'DD9CB3');
  6963. case 0x2E :
  6964. return array('rgb' => 'B38FEE');
  6965. case 0x2F :
  6966. return array('rgb' => 'E3E3E3');
  6967. case 0x30 :
  6968. return array('rgb' => '2A6FF9');
  6969. case 0x31 :
  6970. return array('rgb' => '3FB8CD');
  6971. case 0x32 :
  6972. return array('rgb' => '488436');
  6973. case 0x33 :
  6974. return array('rgb' => '958C41');
  6975. case 0x34 :
  6976. return array('rgb' => '8E5E42');
  6977. case 0x35 :
  6978. return array('rgb' => 'A0627A');
  6979. case 0x36 :
  6980. return array('rgb' => '624FAC');
  6981. case 0x37 :
  6982. return array('rgb' => '969696');
  6983. case 0x38 :
  6984. return array('rgb' => '1D2FBE');
  6985. case 0x39 :
  6986. return array('rgb' => '286676');
  6987. case 0x3A :
  6988. return array('rgb' => '004500');
  6989. case 0x3B :
  6990. return array('rgb' => '453E01');
  6991. case 0x3C :
  6992. return array('rgb' => '6A2813');
  6993. case 0x3D :
  6994. return array('rgb' => '85396A');
  6995. case 0x3E :
  6996. return array('rgb' => '4A3285');
  6997. case 0x3F :
  6998. return array('rgb' => '424242');
  6999. default :
  7000. return array('rgb' => '000000');
  7001. }
  7002. }
  7003. /**
  7004. * Map color array from BIFF8 built-in color index
  7005. *
  7006. * @param int $subData
  7007. * @return array
  7008. */
  7009. private static function _mapColor($subData)
  7010. {
  7011. switch ($subData)
  7012. {
  7013. case 0x08 :
  7014. return array('rgb' => '000000');
  7015. case 0x09 :
  7016. return array('rgb' => 'FFFFFF');
  7017. case 0x0A :
  7018. return array('rgb' => 'FF0000');
  7019. case 0x0B :
  7020. return array('rgb' => '00FF00');
  7021. case 0x0C :
  7022. return array('rgb' => '0000FF');
  7023. case 0x0D :
  7024. return array('rgb' => 'FFFF00');
  7025. case 0x0E :
  7026. return array('rgb' => 'FF00FF');
  7027. case 0x0F :
  7028. return array('rgb' => '00FFFF');
  7029. case 0x10 :
  7030. return array('rgb' => '800000');
  7031. case 0x11 :
  7032. return array('rgb' => '008000');
  7033. case 0x12 :
  7034. return array('rgb' => '000080');
  7035. case 0x13 :
  7036. return array('rgb' => '808000');
  7037. case 0x14 :
  7038. return array('rgb' => '800080');
  7039. case 0x15 :
  7040. return array('rgb' => '008080');
  7041. case 0x16 :
  7042. return array('rgb' => 'C0C0C0');
  7043. case 0x17 :
  7044. return array('rgb' => '808080');
  7045. case 0x18 :
  7046. return array('rgb' => '9999FF');
  7047. case 0x19 :
  7048. return array('rgb' => '993366');
  7049. case 0x1A :
  7050. return array('rgb' => 'FFFFCC');
  7051. case 0x1B :
  7052. return array('rgb' => 'CCFFFF');
  7053. case 0x1C :
  7054. return array('rgb' => '660066');
  7055. case 0x1D :
  7056. return array('rgb' => 'FF8080');
  7057. case 0x1E :
  7058. return array('rgb' => '0066CC');
  7059. case 0x1F :
  7060. return array('rgb' => 'CCCCFF');
  7061. case 0x20 :
  7062. return array('rgb' => '000080');
  7063. case 0x21 :
  7064. return array('rgb' => 'FF00FF');
  7065. case 0x22 :
  7066. return array('rgb' => 'FFFF00');
  7067. case 0x23 :
  7068. return array('rgb' => '00FFFF');
  7069. case 0x24 :
  7070. return array('rgb' => '800080');
  7071. case 0x25 :
  7072. return array('rgb' => '800000');
  7073. case 0x26 :
  7074. return array('rgb' => '008080');
  7075. case 0x27 :
  7076. return array('rgb' => '0000FF');
  7077. case 0x28 :
  7078. return array('rgb' => '00CCFF');
  7079. case 0x29 :
  7080. return array('rgb' => 'CCFFFF');
  7081. case 0x2A :
  7082. return array('rgb' => 'CCFFCC');
  7083. case 0x2B :
  7084. return array('rgb' => 'FFFF99');
  7085. case 0x2C :
  7086. return array('rgb' => '99CCFF');
  7087. case 0x2D :
  7088. return array('rgb' => 'FF99CC');
  7089. case 0x2E :
  7090. return array('rgb' => 'CC99FF');
  7091. case 0x2F :
  7092. return array('rgb' => 'FFCC99');
  7093. case 0x30 :
  7094. return array('rgb' => '3366FF');
  7095. case 0x31 :
  7096. return array('rgb' => '33CCCC');
  7097. case 0x32 :
  7098. return array('rgb' => '99CC00');
  7099. case 0x33 :
  7100. return array('rgb' => 'FFCC00');
  7101. case 0x34 :
  7102. return array('rgb' => 'FF9900');
  7103. case 0x35 :
  7104. return array('rgb' => 'FF6600');
  7105. case 0x36 :
  7106. return array('rgb' => '666699');
  7107. case 0x37 :
  7108. return array('rgb' => '969696');
  7109. case 0x38 :
  7110. return array('rgb' => '003366');
  7111. case 0x39 :
  7112. return array('rgb' => '339966');
  7113. case 0x3A :
  7114. return array('rgb' => '003300');
  7115. case 0x3B :
  7116. return array('rgb' => '333300');
  7117. case 0x3C :
  7118. return array('rgb' => '993300');
  7119. case 0x3D :
  7120. return array('rgb' => '993366');
  7121. case 0x3E :
  7122. return array('rgb' => '333399');
  7123. case 0x3F :
  7124. return array('rgb' => '333333');
  7125. default :
  7126. return array('rgb' => '000000');
  7127. }
  7128. }
  7129. private function _parseRichText($is = '')
  7130. {
  7131. $value = new PHPExcel_RichText();
  7132. $value->createText($is);
  7133. return $value;
  7134. }
  7135. }