PageRenderTime 78ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/add-ons/PHPExcel/PHPExcel/Reader/Excel5.php

https://github.com/jcplat/console-seolan
PHP | 5416 lines | 3082 code | 806 blank | 1528 comment | 378 complexity | b5e81007ba75bc114280ff950053af5d MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.1, 2009-11-02
  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. * @ignore
  60. */
  61. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  62. }
  63. /** PHPExcel */
  64. require_once PHPEXCEL_ROOT . 'PHPExcel.php';
  65. /** PHPExcel_Reader_IReader */
  66. require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/IReader.php';
  67. /** PHPExcel_Reader_Excel5_Escher */
  68. require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/Excel5/Escher.php';
  69. /** PHPExcel_Shared_Date */
  70. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Date.php';
  71. /** PHPExcel_Shared_Excel5 */
  72. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Excel5.php';
  73. /** PHPExcel_Shared_Escher */
  74. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Escher.php';
  75. /** PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE */
  76. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Escher/DggContainer/BstoreContainer/BSE.php';
  77. /** PHPExcel_Shared_OLERead */
  78. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/OLERead.php';
  79. /** PHPExcel_Shared_String */
  80. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
  81. /** PHPExcel_Cell */
  82. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
  83. /** PHPExcel_NamedRange */
  84. require_once PHPEXCEL_ROOT . 'PHPExcel/NamedRange.php';
  85. /** PHPExcel_Reader_IReadFilter */
  86. require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/IReadFilter.php';
  87. /** PHPExcel_Reader_DefaultReadFilter */
  88. require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/DefaultReadFilter.php';
  89. /** PHPExcel_Worksheet_MemoryDrawing */
  90. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/MemoryDrawing.php';
  91. /**
  92. * PHPExcel_Reader_Excel5
  93. *
  94. * This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
  95. *
  96. * @category PHPExcel
  97. * @package PHPExcel_Reader_Excel5
  98. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  99. */
  100. class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
  101. {
  102. // ParseXL definitions
  103. const XLS_BIFF8 = 0x0600;
  104. const XLS_BIFF7 = 0x0500;
  105. const XLS_WorkbookGlobals = 0x0005;
  106. const XLS_Worksheet = 0x0010;
  107. // record identifiers
  108. const XLS_Type_FORMULA = 0x0006;
  109. const XLS_Type_EOF = 0x000a;
  110. const XLS_Type_PROTECT = 0x0012;
  111. const XLS_Type_PASSWORD = 0x0013;
  112. const XLS_Type_HEADER = 0x0014;
  113. const XLS_Type_FOOTER = 0x0015;
  114. const XLS_Type_EXTERNSHEET = 0x0017;
  115. const XLS_Type_DEFINEDNAME = 0x0018;
  116. const XLS_Type_VERTICALPAGEBREAKS = 0x001a;
  117. const XLS_Type_HORIZONTALPAGEBREAKS = 0x001b;
  118. const XLS_Type_NOTE = 0x001c;
  119. const XLS_Type_DATEMODE = 0x0022;
  120. const XLS_Type_LEFTMARGIN = 0x0026;
  121. const XLS_Type_RIGHTMARGIN = 0x0027;
  122. const XLS_Type_TOPMARGIN = 0x0028;
  123. const XLS_Type_BOTTOMMARGIN = 0x0029;
  124. const XLS_Type_PRINTGRIDLINES = 0x002b;
  125. const XLS_Type_FILEPASS = 0x002f;
  126. const XLS_Type_FONT = 0x0031;
  127. const XLS_Type_CONTINUE = 0x003c;
  128. const XLS_Type_PANE = 0x0041;
  129. const XLS_Type_CODEPAGE = 0x0042;
  130. const XLS_Type_DEFCOLWIDTH = 0x0055;
  131. const XLS_Type_OBJ = 0x005d;
  132. const XLS_Type_COLINFO = 0x007d;
  133. const XLS_Type_IMDATA = 0x007f;
  134. const XLS_Type_SHEETPR = 0x0081;
  135. const XLS_Type_HCENTER = 0x0083;
  136. const XLS_Type_VCENTER = 0x0084;
  137. const XLS_Type_SHEET = 0x0085;
  138. const XLS_Type_PALETTE = 0x0092;
  139. const XLS_Type_SCL = 0x00a0;
  140. const XLS_Type_PAGESETUP = 0x00a1;
  141. const XLS_Type_MULRK = 0x00bd;
  142. const XLS_Type_MULBLANK = 0x00be;
  143. const XLS_Type_DBCELL = 0x00d7;
  144. const XLS_Type_XF = 0x00e0;
  145. const XLS_Type_MERGEDCELLS = 0x00e5;
  146. const XLS_Type_MSODRAWINGGROUP = 0x00eb;
  147. const XLS_Type_MSODRAWING = 0x00ec;
  148. const XLS_Type_SST = 0x00fc;
  149. const XLS_Type_LABELSST = 0x00fd;
  150. const XLS_Type_EXTSST = 0x00ff;
  151. const XLS_Type_EXTERNALBOOK = 0x01ae;
  152. const XLS_Type_TXO = 0x01b6;
  153. const XLS_Type_HYPERLINK = 0x01b8;
  154. const XLS_Type_DIMENSION = 0x0200;
  155. const XLS_Type_BLANK = 0x0201;
  156. const XLS_Type_NUMBER = 0x0203;
  157. const XLS_Type_LABEL = 0x0204;
  158. const XLS_Type_BOOLERR = 0x0205;
  159. const XLS_Type_STRING = 0x0207;
  160. const XLS_Type_ROW = 0x0208;
  161. const XLS_Type_INDEX = 0x020b;
  162. const XLS_Type_ARRAY = 0x0221;
  163. const XLS_Type_DEFAULTROWHEIGHT = 0x0225;
  164. const XLS_Type_WINDOW2 = 0x023e;
  165. const XLS_Type_RK = 0x027e;
  166. const XLS_Type_STYLE = 0x0293;
  167. const XLS_Type_FORMAT = 0x041e;
  168. const XLS_Type_SHAREDFMLA = 0x04bc;
  169. const XLS_Type_BOF = 0x0809;
  170. const XLS_Type_RANGEPROTECTION = 0x0868;
  171. const XLS_Type_SHEETLAYOUT = 0x0862;
  172. const XLS_Type_UNKNOWN = 0xffff;
  173. /**
  174. * Read data only?
  175. *
  176. * @var boolean
  177. */
  178. private $_readDataOnly = false;
  179. /**
  180. * Restict which sheets should be loaded?
  181. *
  182. * @var array
  183. */
  184. private $_loadSheetsOnly = null;
  185. /**
  186. * PHPExcel_Reader_IReadFilter instance
  187. *
  188. * @var PHPExcel_Reader_IReadFilter
  189. */
  190. private $_readFilter = null;
  191. /**
  192. * OLE reader
  193. *
  194. * @var PHPExcel_Shared_OLERead
  195. */
  196. private $_ole;
  197. /**
  198. * Stream data that is read. Includes workbook globals substream as well as sheet substreams
  199. *
  200. * @var string
  201. */
  202. private $_data;
  203. /**
  204. * Size in bytes of $this->_data
  205. *
  206. * @var int
  207. */
  208. private $_dataSize;
  209. /**
  210. * Current position in stream
  211. *
  212. * @var integer
  213. */
  214. private $_pos;
  215. /**
  216. * Workbook to be returned by the reader.
  217. *
  218. * @var PHPExcel
  219. */
  220. private $_phpExcel;
  221. /**
  222. * Worksheet that is currently being built by the reader.
  223. *
  224. * @var PHPExcel_Worksheet
  225. */
  226. private $_phpSheet;
  227. /**
  228. * BIFF version
  229. *
  230. * @var int
  231. */
  232. private $_version;
  233. /**
  234. * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
  235. * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'
  236. *
  237. * @var string
  238. */
  239. private $_codepage;
  240. /**
  241. * Shared formats
  242. *
  243. * @var array
  244. */
  245. private $_formats;
  246. /**
  247. * Shared fonts
  248. *
  249. * @var array
  250. */
  251. private $_objFonts;
  252. /**
  253. * Color palette
  254. *
  255. * @var array
  256. */
  257. private $_palette;
  258. /**
  259. * Worksheets
  260. *
  261. * @var array
  262. */
  263. private $_sheets;
  264. /**
  265. * External books
  266. *
  267. * @var array
  268. */
  269. private $_externalBooks;
  270. /**
  271. * REF structures. Only applies to BIFF8.
  272. *
  273. * @var array
  274. */
  275. private $_ref;
  276. /**
  277. * Defined names
  278. *
  279. * @var array
  280. */
  281. private $_definedname;
  282. /**
  283. * Shared strings. Only applies to BIFF8.
  284. *
  285. * @var array
  286. */
  287. private $_sst;
  288. /**
  289. * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
  290. *
  291. * @var boolean
  292. */
  293. private $_frozen;
  294. /**
  295. * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
  296. *
  297. * @var boolean
  298. */
  299. private $_isFitToPages;
  300. /**
  301. * Objects. One OBJ record contributes with one entry.
  302. *
  303. * @var array
  304. */
  305. private $_objs;
  306. /**
  307. * The combined MSODRAWINGGROUP data
  308. *
  309. * @var string
  310. */
  311. private $_drawingGroupData;
  312. /**
  313. * The combined MSODRAWING data (per sheet)
  314. *
  315. * @var string
  316. */
  317. private $_drawingData;
  318. /**
  319. * Keep track of XF index
  320. *
  321. * @var int
  322. */
  323. private $_xfIndex;
  324. /**
  325. * Mapping of XF index (that is a cell XF) to final index in cellXf collection
  326. *
  327. * @var array
  328. */
  329. private $_mapCellXfIndex;
  330. /**
  331. * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection
  332. *
  333. * @var array
  334. */
  335. private $_mapCellStyleXfIndex;
  336. /**
  337. * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
  338. *
  339. * @var array
  340. */
  341. private $_sharedFormulas;
  342. /**
  343. * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
  344. * refers to a shared formula.
  345. *
  346. * @var array
  347. */
  348. private $_sharedFormulaParts;
  349. /**
  350. * Read data only?
  351. *
  352. * @return boolean
  353. */
  354. public function getReadDataOnly()
  355. {
  356. return $this->_readDataOnly;
  357. }
  358. /**
  359. * Set read data only
  360. *
  361. * @param boolean $pValue
  362. * @return PHPExcel_Reader_Excel5
  363. */
  364. public function setReadDataOnly($pValue = false)
  365. {
  366. $this->_readDataOnly = $pValue;
  367. return $this;
  368. }
  369. /**
  370. * Get which sheets to load
  371. *
  372. * @return mixed
  373. */
  374. public function getLoadSheetsOnly()
  375. {
  376. return $this->_loadSheetsOnly;
  377. }
  378. /**
  379. * Set which sheets to load
  380. *
  381. * @param mixed $value
  382. * @return PHPExcel_Reader_Excel5
  383. */
  384. public function setLoadSheetsOnly($value = null)
  385. {
  386. $this->_loadSheetsOnly = is_array($value) ?
  387. $value : array($value);
  388. return $this;
  389. }
  390. /**
  391. * Set all sheets to load
  392. *
  393. * @return PHPExcel_Reader_Excel5
  394. */
  395. public function setLoadAllSheets()
  396. {
  397. $this->_loadSheetsOnly = null;
  398. return $this;
  399. }
  400. /**
  401. * Read filter
  402. *
  403. * @return PHPExcel_Reader_IReadFilter
  404. */
  405. public function getReadFilter() {
  406. return $this->_readFilter;
  407. }
  408. /**
  409. * Set read filter
  410. *
  411. * @param PHPExcel_Reader_IReadFilter $pValue
  412. * @return PHPExcel_Reader_Excel5
  413. */
  414. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  415. $this->_readFilter = $pValue;
  416. return $this;
  417. }
  418. /**
  419. * Create a new PHPExcel_Reader_Excel5 instance
  420. */
  421. public function __construct() {
  422. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  423. }
  424. /**
  425. * Can the current PHPExcel_Reader_IReader read the file?
  426. *
  427. * @param string $pFileName
  428. * @return boolean
  429. */
  430. public function canRead($pFilename)
  431. {
  432. // Check if file exists
  433. if (!file_exists($pFilename)) {
  434. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  435. }
  436. try {
  437. // Use ParseXL for the hard work.
  438. $this->_ole = new PHPExcel_Shared_OLERead();
  439. // get excel data
  440. $res = $this->_ole->read($pFilename);
  441. return true;
  442. } catch (Exception $e) {
  443. return false;
  444. }
  445. }
  446. /**
  447. * Loads PHPExcel from file
  448. *
  449. * @param string $pFilename
  450. * @throws Exception
  451. */
  452. public function load($pFilename)
  453. {
  454. // Initialisations
  455. $this->_phpExcel = new PHPExcel;
  456. $this->_phpExcel->removeSheetByIndex(0); // remove 1st sheet
  457. if (!$this->_readDataOnly) {
  458. $this->_phpExcel->removeCellStyleXfByIndex(0); // remove the default style
  459. $this->_phpExcel->removeCellXfByIndex(0); // remove the default style
  460. }
  461. // Use ParseXL for the hard work.
  462. $this->_ole = new PHPExcel_Shared_OLERead();
  463. // get excel data
  464. $res = $this->_ole->read($pFilename);
  465. $this->_data = $this->_ole->getWorkBook();
  466. // total byte size of Excel data (workbook global substream + sheet substreams)
  467. $this->_dataSize = strlen($this->_data);
  468. // initialize
  469. $this->_pos = 0;
  470. $this->_codepage = 'CP1252';
  471. $this->_formats = array();
  472. $this->_objFonts = array();
  473. $this->_palette = array();
  474. $this->_sheets = array();
  475. $this->_externalBooks = array();
  476. $this->_ref = array();
  477. $this->_definedname = array();
  478. $this->_sst = array();
  479. $this->_drawingGroupData = '';
  480. $this->_xfIndex = '';
  481. $this->_mapCellXfIndex = array();
  482. $this->_mapCellStyleXfIndex = array();
  483. // Parse Workbook Global Substream
  484. while ($this->_pos < $this->_dataSize) {
  485. $code = $this->_GetInt2d($this->_data, $this->_pos);
  486. switch ($code) {
  487. case self::XLS_Type_BOF:
  488. $pos = $this->_pos;
  489. $length = $this->_GetInt2d($this->_data, $pos + 2);
  490. $recordData = substr($this->_data, $pos + 4, $length);
  491. // offset: 0; size: 2; BIFF version
  492. $this->_version = $this->_GetInt2d($this->_data, $pos + 4);
  493. if (($this->_version != self::XLS_BIFF8) && ($this->_version != self::XLS_BIFF7)) {
  494. return false;
  495. }
  496. // offset: 2; size: 2; type of stream
  497. $substreamType = $this->_GetInt2d($this->_data, $pos + 6);
  498. if ($substreamType != self::XLS_WorkbookGlobals) {
  499. return false;
  500. }
  501. $this->_pos += 4 + $length;
  502. break;
  503. case self::XLS_Type_FILEPASS: $this->_readFilepass(); break;
  504. case self::XLS_Type_CODEPAGE: $this->_readCodepage(); break;
  505. case self::XLS_Type_DATEMODE: $this->_readDateMode(); break;
  506. case self::XLS_Type_FONT: $this->_readFont(); break;
  507. case self::XLS_Type_FORMAT: $this->_readFormat(); break;
  508. case self::XLS_Type_XF: $this->_readXf(); break;
  509. case self::XLS_Type_STYLE: $this->_readStyle(); break;
  510. case self::XLS_Type_PALETTE: $this->_readPalette(); break;
  511. case self::XLS_Type_SHEET: $this->_readSheet(); break;
  512. case self::XLS_Type_EXTERNALBOOK: $this->_readExternalBook(); break;
  513. case self::XLS_Type_EXTERNSHEET: $this->_readExternSheet(); break;
  514. case self::XLS_Type_DEFINEDNAME: $this->_readDefinedName(); break;
  515. case self::XLS_Type_MSODRAWINGGROUP: $this->_readMsoDrawingGroup(); break;
  516. case self::XLS_Type_SST: $this->_readSst(); break;
  517. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  518. default: $this->_readDefault(); break;
  519. }
  520. }
  521. // Resolve indexed colors for font, fill, and border colors
  522. // Cannot be resolved already in XF record, because PALETTE record comes afterwards
  523. if (!$this->_readDataOnly) {
  524. foreach ($this->_objFonts as $objFont) {
  525. $color = $this->_readColor($objFont->colorIndex);
  526. $objFont->getColor()->setRGB($color['rgb']);
  527. }
  528. foreach ($this->_phpExcel->getCellXfCollection() as $objStyle) {
  529. // fill start and end color
  530. $startColor = $this->_readColor($objStyle->getFill()->startcolorIndex);
  531. $objStyle->getFill()->getStartColor()->setRGB($startColor['rgb']);
  532. $endColor = $this->_readColor($objStyle->getFill()->endcolorIndex);
  533. $objStyle->getFill()->getEndColor()->setRGB($endColor['rgb']);
  534. // border colors
  535. $borderTopColor = $this->_readColor($objStyle->getBorders()->getTop()->colorIndex);
  536. $objStyle->getBorders()->getTop()->getColor()->setRGB($borderTopColor['rgb']);
  537. $borderRightColor = $this->_readColor($objStyle->getBorders()->getRight()->colorIndex);
  538. $objStyle->getBorders()->getRight()->getColor()->setRGB($borderRightColor['rgb']);
  539. $borderBottomColor = $this->_readColor($objStyle->getBorders()->getBottom()->colorIndex);
  540. $objStyle->getBorders()->getBottom()->getColor()->setRGB($borderBottomColor['rgb']);
  541. $borderLeftColor = $this->_readColor($objStyle->getBorders()->getLeft()->colorIndex);
  542. $objStyle->getBorders()->getLeft()->getColor()->setRGB($borderLeftColor['rgb']);
  543. }
  544. }
  545. // treat MSODRAWINGGROUP records, workbook-level Escher
  546. if (!$this->_readDataOnly && $this->_drawingGroupData) {
  547. $escherWorkbook = new PHPExcel_Shared_Escher();
  548. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorkbook);
  549. $escherWorkbook = $reader->load($this->_drawingGroupData);
  550. // debug Escher stream
  551. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  552. //$debug->load($this->_drawingGroupData);
  553. }
  554. // Parse the individual sheets
  555. foreach ($this->_sheets as $sheet) {
  556. // check if sheet should be skipped
  557. if (isset($this->_loadSheetsOnly) && !in_array($sheet['name'], $this->_loadSheetsOnly)) {
  558. continue;
  559. }
  560. // add sheet to PHPExcel object
  561. $this->_phpSheet = $this->_phpExcel->createSheet();
  562. $this->_phpSheet->setTitle($sheet['name']);
  563. $this->_phpSheet->setSheetState($sheet['sheetState']);
  564. $this->_pos = $sheet['offset'];
  565. // Initialize isFitToPages. May change after reading SHEETPR record.
  566. $this->_isFitToPages = false;
  567. // Initialize drawingData
  568. $this->_drawingData = '';
  569. // Initialize objs
  570. $this->_objs = array();
  571. // Initialize shared formula parts
  572. $this->_sharedFormulaParts = array();
  573. // Initialize shared formulas
  574. $this->_sharedFormulas = array();
  575. while ($this->_pos < $this->_dataSize) {
  576. $code = $this->_GetInt2d($this->_data, $this->_pos);
  577. switch ($code) {
  578. case self::XLS_Type_BOF:
  579. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  580. $recordData = substr($this->_data, $this->_pos + 4, $length);
  581. // move stream pointer to next record
  582. $this->_pos += 4 + $length;
  583. // do not use this version information for anything
  584. // it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
  585. // offset: 2; size: 2; type of the following data
  586. $substreamType = $this->_GetInt2d($recordData, 2);
  587. if ($substreamType != self::XLS_Worksheet) {
  588. break 2;
  589. }
  590. break;
  591. case self::XLS_Type_PRINTGRIDLINES: $this->_readPrintGridlines(); break;
  592. case self::XLS_Type_DEFAULTROWHEIGHT: $this->_readDefaultRowHeight(); break;
  593. case self::XLS_Type_SHEETPR: $this->_readSheetPr(); break;
  594. case self::XLS_Type_HORIZONTALPAGEBREAKS: $this->_readHorizontalPageBreaks(); break;
  595. case self::XLS_Type_VERTICALPAGEBREAKS: $this->_readVerticalPageBreaks(); break;
  596. case self::XLS_Type_HEADER: $this->_readHeader(); break;
  597. case self::XLS_Type_FOOTER: $this->_readFooter(); break;
  598. case self::XLS_Type_HCENTER: $this->_readHcenter(); break;
  599. case self::XLS_Type_VCENTER: $this->_readVcenter(); break;
  600. case self::XLS_Type_LEFTMARGIN: $this->_readLeftMargin(); break;
  601. case self::XLS_Type_RIGHTMARGIN: $this->_readRightMargin(); break;
  602. case self::XLS_Type_TOPMARGIN: $this->_readTopMargin(); break;
  603. case self::XLS_Type_BOTTOMMARGIN: $this->_readBottomMargin(); break;
  604. case self::XLS_Type_PAGESETUP: $this->_readPageSetup(); break;
  605. case self::XLS_Type_PROTECT: $this->_readProtect(); break;
  606. case self::XLS_Type_PASSWORD: $this->_readPassword(); break;
  607. case self::XLS_Type_DEFCOLWIDTH: $this->_readDefColWidth(); break;
  608. case self::XLS_Type_COLINFO: $this->_readColInfo(); break;
  609. case self::XLS_Type_DIMENSION: $this->_readDefault(); break;
  610. case self::XLS_Type_ROW: $this->_readRow(); break;
  611. case self::XLS_Type_DBCELL: $this->_readDefault(); break;
  612. case self::XLS_Type_RK: $this->_readRk(); break;
  613. case self::XLS_Type_LABELSST: $this->_readLabelSst(); break;
  614. case self::XLS_Type_MULRK: $this->_readMulRk(); break;
  615. case self::XLS_Type_NUMBER: $this->_readNumber(); break;
  616. case self::XLS_Type_FORMULA: $this->_readFormula(); break;
  617. case self::XLS_Type_SHAREDFMLA: $this->_readSharedFmla(); break;
  618. case self::XLS_Type_BOOLERR: $this->_readBoolErr(); break;
  619. case self::XLS_Type_MULBLANK: $this->_readMulBlank(); break;
  620. case self::XLS_Type_LABEL: $this->_readLabel(); break;
  621. case self::XLS_Type_BLANK: $this->_readBlank(); break;
  622. case self::XLS_Type_MSODRAWING: $this->_readMsoDrawing(); break;
  623. case self::XLS_Type_OBJ: $this->_readObj(); break;
  624. case self::XLS_Type_WINDOW2: $this->_readWindow2(); break;
  625. case self::XLS_Type_SCL: $this->_readScl(); break;
  626. case self::XLS_Type_PANE: $this->_readPane(); break;
  627. case self::XLS_Type_MERGEDCELLS: $this->_readMergedCells(); break;
  628. case self::XLS_Type_HYPERLINK: $this->_readHyperLink(); break;
  629. case self::XLS_Type_SHEETLAYOUT: $this->_readSheetLayout(); break;
  630. case self::XLS_Type_RANGEPROTECTION: $this->_readRangeProtection(); break;
  631. //case self::XLS_Type_IMDATA: $this->_readImData(); break;
  632. case self::XLS_Type_CONTINUE: $this->_readContinue(); break;
  633. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  634. default: $this->_readDefault(); break;
  635. }
  636. }
  637. // treat MSODRAWING records, sheet-level Escher
  638. if (!$this->_readDataOnly && $this->_drawingData) {
  639. $escherWorksheet = new PHPExcel_Shared_Escher();
  640. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorksheet);
  641. $escherWorksheet = $reader->load($this->_drawingData);
  642. // debug Escher stream
  643. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  644. //$debug->load($this->_drawingData);
  645. // get all spContainers in one long array, so they can be mapped to OBJ records
  646. $allSpContainers = $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
  647. }
  648. // treat OBJ records
  649. foreach ($this->_objs as $n => $obj) {
  650. // the first shape container never has a corresponding OBJ record, hence $n + 1
  651. $spContainer = $allSpContainers[$n + 1];
  652. // we skip all spContainers that are a part of a group shape since we cannot yet handle those
  653. if ($spContainer->getNestingLevel() > 1) {
  654. continue;
  655. }
  656. // calculate the width and height of the shape
  657. list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($spContainer->getStartCoordinates());
  658. list($endColumn, $endRow) = PHPExcel_Cell::coordinateFromString($spContainer->getEndCoordinates());
  659. $startOffsetX = $spContainer->getStartOffsetX();
  660. $startOffsetY = $spContainer->getStartOffsetY();
  661. $endOffsetX = $spContainer->getEndOffsetX();
  662. $endOffsetY = $spContainer->getEndOffsetY();
  663. $width = PHPExcel_Shared_Excel5::getDistanceX($this->_phpSheet, $startColumn, $startOffsetX, $endColumn, $endOffsetX);
  664. $height = PHPExcel_Shared_Excel5::getDistanceY($this->_phpSheet, $startRow, $startOffsetY, $endRow, $endOffsetY);
  665. // calculate offsetX and offsetY of the shape
  666. $offsetX = $startOffsetX * PHPExcel_Shared_Excel5::sizeCol($this->_phpSheet, $startColumn) / 1024;
  667. $offsetY = $startOffsetY * PHPExcel_Shared_Excel5::sizeRow($this->_phpSheet, $startRow) / 256;
  668. switch ($obj['type']) {
  669. case 0x08:
  670. // picture
  671. // get index to BSE entry (1-based)
  672. $BSEindex = $spContainer->getOPT(0x0104);
  673. $BSECollection = $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection();
  674. $BSE = $BSECollection[$BSEindex - 1];
  675. $blipType = $BSE->getBlipType();
  676. // need check because some blip types are not supported by Escher reader such as EMF
  677. if ($blip = $BSE->getBlip()) {
  678. $ih = imagecreatefromstring($blip->getData());
  679. $drawing = new PHPExcel_Worksheet_MemoryDrawing();
  680. $drawing->setImageResource($ih);
  681. // width, height, offsetX, offsetY
  682. $drawing->setResizeProportional(false);
  683. $drawing->setWidth($width);
  684. $drawing->setHeight($height);
  685. $drawing->setOffsetX($offsetX);
  686. $drawing->setOffsetY($offsetY);
  687. switch ($blipType) {
  688. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG:
  689. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
  690. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG);
  691. break;
  692. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG:
  693. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
  694. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG);
  695. break;
  696. }
  697. $drawing->setWorksheet($this->_phpSheet);
  698. $drawing->setCoordinates($spContainer->getStartCoordinates());
  699. }
  700. break;
  701. default:
  702. // other object type
  703. break;
  704. }
  705. }
  706. // treat SHAREDFMLA records
  707. if ($this->_version == self::XLS_BIFF8) {
  708. foreach ($this->_sharedFormulaParts as $cell => $baseCell) {
  709. $formula = $this->_getFormulaFromStructure($this->_sharedFormulas[$baseCell], $cell);
  710. $this->_phpSheet->getCell($cell)->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
  711. }
  712. }
  713. }
  714. // add the named ranges (defined names)
  715. foreach ($this->_definedname as $definedName) {
  716. if ($definedName['isBuiltInName']) {
  717. switch ($definedName['name']) {
  718. case pack('C', 0x06):
  719. // print area
  720. // in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2
  721. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  722. foreach ($ranges as $range) {
  723. // $range should look like this one of these
  724. // Foo!$C$7:$J$66
  725. // Bar!$A$1:$IV$2
  726. $explodes = explode('!', $range);
  727. if (count($explodes) == 2) {
  728. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
  729. $extractedRange = $explodes[1];
  730. $extractedRange = str_replace('$', '', $extractedRange);
  731. $docSheet->getPageSetup()->setPrintArea($extractedRange);
  732. }
  733. }
  734. }
  735. break;
  736. case pack('C', 0x07):
  737. // print titles (repeating rows)
  738. // Assuming BIFF8, there are 3 cases
  739. // 1. repeating rows
  740. // formula looks like this: Sheet!$A$1:$IV$2
  741. // rows 1-2 repeat
  742. // 2. repeating columns
  743. // formula looks like this: Sheet!$A$1:$B$65536
  744. // columns A-B repeat
  745. // 3. both repeating rows and repeating columns
  746. // formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2
  747. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  748. foreach ($ranges as $range) {
  749. // $range should look like this one of these
  750. // Sheet!$A$1:$B$65536
  751. // Sheet!$A$1:$IV$2
  752. $explodes = explode('!', $range);
  753. if (count($explodes) == 2) {
  754. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
  755. $extractedRange = $explodes[1];
  756. $extractedRange = str_replace('$', '', $extractedRange);
  757. $coordinateStrings = explode(':', $extractedRange);
  758. if (count($coordinateStrings) == 2) {
  759. list($firstColumn, $firstRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[0]);
  760. list($lastColumn, $lastRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[1]);
  761. if ($firstColumn == 'A' and $lastColumn == 'IV') {
  762. // then we have repeating rows
  763. $docSheet->getPageSetup()->setRowsToRepeatAtTop(array($firstRow, $lastRow));
  764. } elseif ($firstRow == 1 and $lastRow == 65536) {
  765. // then we have repeating columns
  766. $docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array($firstColumn, $lastColumn));
  767. }
  768. }
  769. }
  770. }
  771. }
  772. break;
  773. }
  774. } else {
  775. // Extract range
  776. $explodes = explode('!', $definedName['formula']);
  777. if (count($explodes) == 2) {
  778. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
  779. $extractedRange = $explodes[1];
  780. $extractedRange = str_replace('$', '', $extractedRange);
  781. $this->_phpExcel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $docSheet, $extractedRange, false) );
  782. }
  783. }
  784. }
  785. }
  786. return $this->_phpExcel;
  787. }
  788. /**
  789. * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
  790. */
  791. private function _readDefault()
  792. {
  793. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  794. $recordData = substr($this->_data, $this->_pos + 4, $length);
  795. // move stream pointer to next record
  796. $this->_pos += 4 + $length;
  797. }
  798. /**
  799. * FILEPASS
  800. *
  801. * This record is part of the File Protection Block. It
  802. * contains information about the read/write password of the
  803. * file. All record contents following this record will be
  804. * encrypted.
  805. *
  806. * -- "OpenOffice.org's Documentation of the Microsoft
  807. * Excel File Format"
  808. */
  809. private function _readFilepass()
  810. {
  811. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  812. $recordData = substr($this->_data, $this->_pos + 4, $length);
  813. // move stream pointer to next record
  814. $this->_pos += 4 + $length;
  815. throw new Exception('Cannot read encrypted file');
  816. }
  817. /**
  818. * CODEPAGE
  819. *
  820. * This record stores the text encoding used to write byte
  821. * strings, stored as MS Windows code page identifier.
  822. *
  823. * -- "OpenOffice.org's Documentation of the Microsoft
  824. * Excel File Format"
  825. */
  826. private function _readCodepage()
  827. {
  828. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  829. $recordData = substr($this->_data, $this->_pos + 4, $length);
  830. // move stream pointer to next record
  831. $this->_pos += 4 + $length;
  832. // offset: 0; size: 2; code page identifier
  833. $codepage = $this->_GetInt2d($recordData, 0);
  834. switch ($codepage) {
  835. case 367: // ASCII
  836. $this->_codepage ="ASCII";
  837. break;
  838. case 437: //OEM US
  839. $this->_codepage ="CP437";
  840. break;
  841. case 720: //OEM Arabic
  842. // currently not supported by libiconv
  843. $this->_codepage = "";
  844. break;
  845. case 737: //OEM Greek
  846. $this->_codepage ="CP737";
  847. break;
  848. case 775: //OEM Baltic
  849. $this->_codepage ="CP775";
  850. break;
  851. case 850: //OEM Latin I
  852. $this->_codepage ="CP850";
  853. break;
  854. case 852: //OEM Latin II (Central European)
  855. $this->_codepage ="CP852";
  856. break;
  857. case 855: //OEM Cyrillic
  858. $this->_codepage ="CP855";
  859. break;
  860. case 857: //OEM Turkish
  861. $this->_codepage ="CP857";
  862. break;
  863. case 858: //OEM Multilingual Latin I with Euro
  864. $this->_codepage ="CP858";
  865. break;
  866. case 860: //OEM Portugese
  867. $this->_codepage ="CP860";
  868. break;
  869. case 861: //OEM Icelandic
  870. $this->_codepage ="CP861";
  871. break;
  872. case 862: //OEM Hebrew
  873. $this->_codepage ="CP862";
  874. break;
  875. case 863: //OEM Canadian (French)
  876. $this->_codepage ="CP863";
  877. break;
  878. case 864: //OEM Arabic
  879. $this->_codepage ="CP864";
  880. break;
  881. case 865: //OEM Nordic
  882. $this->_codepage ="CP865";
  883. break;
  884. case 866: //OEM Cyrillic (Russian)
  885. $this->_codepage ="CP866";
  886. break;
  887. case 869: //OEM Greek (Modern)
  888. $this->_codepage ="CP869";
  889. break;
  890. case 874: //ANSI Thai
  891. $this->_codepage ="CP874";
  892. break;
  893. case 932: //ANSI Japanese Shift-JIS
  894. $this->_codepage ="CP932";
  895. break;
  896. case 936: //ANSI Chinese Simplified GBK
  897. $this->_codepage ="CP936";
  898. break;
  899. case 949: //ANSI Korean (Wansung)
  900. $this->_codepage ="CP949";
  901. break;
  902. case 950: //ANSI Chinese Traditional BIG5
  903. $this->_codepage ="CP950";
  904. break;
  905. case 1200: //UTF-16 (BIFF8)
  906. $this->_codepage ="UTF-16LE";
  907. break;
  908. case 1250:// ANSI Latin II (Central European)
  909. $this->_codepage ="CP1250";
  910. break;
  911. case 1251: //ANSI Cyrillic
  912. $this->_codepage ="CP1251";
  913. break;
  914. case 1252: //ANSI Latin I (BIFF4-BIFF7)
  915. $this->_codepage ="CP1252";
  916. break;
  917. case 1253: //ANSI Greek
  918. $this->_codepage ="CP1253";
  919. break;
  920. case 1254: //ANSI Turkish
  921. $this->_codepage ="CP1254";
  922. break;
  923. case 1255: //ANSI Hebrew
  924. $this->_codepage ="CP1255";
  925. break;
  926. case 1256: //ANSI Arabic
  927. $this->_codepage ="CP1256";
  928. break;
  929. case 1257: //ANSI Baltic
  930. $this->_codepage ="CP1257";
  931. break;
  932. case 1258: //ANSI Vietnamese
  933. $this->_codepage ="CP1258";
  934. break;
  935. case 1361: //ANSI Korean (Johab)
  936. $this->_codepage ="CP1361";
  937. break;
  938. case 10000: //Apple Roman
  939. $this->_codepage = 'MAC';
  940. break;
  941. case 32768: //Apple Roman
  942. $this->_codepage = 'MAC';
  943. break;
  944. case 32769: //ANSI Latin I (BIFF2-BIFF3)
  945. // currently not supported by libiconv
  946. $this->_codepage = "";
  947. break;
  948. }
  949. }
  950. /**
  951. * DATEMODE
  952. *
  953. * This record specifies the base date for displaying date
  954. * values. All dates are stored as count of days past this
  955. * base date. In BIFF2-BIFF4 this record is part of the
  956. * Calculation Settings Block. In BIFF5-BIFF8 it is
  957. * stored in the Workbook Globals Substream.
  958. *
  959. * -- "OpenOffice.org's Documentation of the Microsoft
  960. * Excel File Format"
  961. */
  962. private function _readDateMode()
  963. {
  964. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  965. $recordData = substr($this->_data, $this->_pos + 4, $length);
  966. // move stream pointer to next record
  967. $this->_pos += 4 + $length;
  968. // offset: 0; size: 2; 0 = base 1900, 1 = base 1904
  969. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
  970. if (ord($recordData{0}) == 1) {
  971. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
  972. }
  973. }
  974. /**
  975. * Read a FONT record
  976. */
  977. private function _readFont()
  978. {
  979. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  980. $recordData = substr($this->_data, $this->_pos + 4, $length);
  981. // move stream pointer to next record
  982. $this->_pos += 4 + $length;
  983. if (!$this->_readDataOnly) {
  984. $objFont = new PHPExcel_Style_Font();
  985. // offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
  986. $size = $this->_GetInt2d($recordData, 0);
  987. $objFont->setSize($size / 20);
  988. // offset: 2; size: 2; option flags
  989. // bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
  990. // bit: 1; mask 0x0002; italic
  991. $isItalic = (0x0002 & $this->_GetInt2d($recordData, 2)) >> 1;
  992. if ($isItalic) $objFont->setItalic(true);
  993. // bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
  994. // bit: 3; mask 0x0008; strike
  995. $isStrike = (0x0008 & $this->_GetInt2d($recordData, 2)) >> 3;
  996. if ($isStrike) $objFont->setStrikethrough(true);
  997. // offset: 4; size: 2; colour index
  998. $colorIndex = $this->_GetInt2d($recordData, 4);
  999. $objFont->colorIndex = $colorIndex;
  1000. // offset: 6; size: 2; font weight
  1001. $weight = $this->_GetInt2d($recordData, 6);
  1002. switch ($weight) {
  1003. case 0x02BC:
  1004. $objFont->setBold(true);
  1005. break;
  1006. }
  1007. // offset: 8; size: 2; escapement type
  1008. $escapement = $this->_GetInt2d($recordData, 8);
  1009. switch ($escapement) {
  1010. case 0x0001:
  1011. $objFont->setSuperScript(true);
  1012. break;
  1013. case 0x0002:
  1014. $objFont->setSubScript(true);
  1015. break;
  1016. }
  1017. // offset: 10; size: 1; underline type
  1018. $underlineType = ord($recordData{10});
  1019. switch ($underlineType) {
  1020. case 0x00:
  1021. break; // no underline
  1022. case 0x01:
  1023. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  1024. break;
  1025. case 0x02:
  1026. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
  1027. break;
  1028. case 0x21:
  1029. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING);
  1030. break;
  1031. case 0x22:
  1032. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING);
  1033. break;
  1034. }
  1035. // offset: 11; size: 1; font family
  1036. // offset: 12; size: 1; character set
  1037. // offset: 13; size: 1; not used
  1038. // offset: 14; size: var; font name
  1039. if ($this->_version == self::XLS_BIFF8) {
  1040. $string = $this->_readUnicodeStringShort(substr($recordData, 14));
  1041. } else {
  1042. $string = $this->_readByteStringShort(substr($recordData, 14));
  1043. }
  1044. $objFont->setName($string['value']);
  1045. $this->_objFonts[] = $objFont;
  1046. }
  1047. }
  1048. /**
  1049. * FORMAT
  1050. *
  1051. * This record contains information about a number format.
  1052. * All FORMAT records occur together in a sequential list.
  1053. *
  1054. * In BIFF2-BIFF4 other records referencing a FORMAT record
  1055. * contain a zero-based index into this list. From BIFF5 on
  1056. * the FORMAT record contains the index itself that will be
  1057. * used by other records.
  1058. *
  1059. * -- "OpenOffice.org's Documentation of the Microsoft
  1060. * Excel File Format"
  1061. */
  1062. private function _readFormat()
  1063. {
  1064. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1065. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1066. // move stream pointer to next record
  1067. $this->_pos += 4 + $length;
  1068. if (!$this->_readDataOnly) {
  1069. $indexCode = $this->_GetInt2d($recordData, 0);
  1070. if ($this->_version == self::XLS_BIFF8) {
  1071. $string = $this->_readUnicodeStringLong(substr($recordData, 2));
  1072. } else {
  1073. // BIFF7
  1074. $string = $this->_readByteStringShort(substr($recordData, 2));
  1075. }
  1076. $formatString = $string['value'];
  1077. $this->_formats[$indexCode] = $formatString;
  1078. }
  1079. }
  1080. /**
  1081. * XF - Extended Format
  1082. *
  1083. * This record contains formatting information for cells, rows, columns or styles.
  1084. * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF
  1085. * and 1 cell XF.
  1086. * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
  1087. * and XF record 15 is a cell XF
  1088. * We only read the first cell style XF and skip the remaining cell style XF records
  1089. * We read all cell XF records.
  1090. *
  1091. * -- "OpenOffice.org's Documentation of the Microsoft
  1092. * Excel File Format"
  1093. */
  1094. private function _readXf()
  1095. {
  1096. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1097. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1098. // move stream pointer to next record
  1099. $this->_pos += 4 + $length;
  1100. $objStyle = new PHPExcel_Style();
  1101. if (!$this->_readDataOnly) {
  1102. // offset: 0; size: 2; Index to FONT record
  1103. if ($this->_GetInt2d($recordData, 0) < 4) {
  1104. $fontIndex = $this->_GetInt2d($recordData, 0);
  1105. } else {
  1106. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  1107. // check the OpenOffice documentation of the FONT record
  1108. $fontIndex = $this->_GetInt2d($recordData, 0) - 1;
  1109. }
  1110. $objStyle->setFont($this->_objFonts[$fontIndex]);
  1111. // offset: 2; size: 2; Index to FORMAT record
  1112. $numberFormatIndex = $this->_GetInt2d($recordData, 2);
  1113. if (isset($this->_formats[$numberFormatIndex])) {
  1114. // then we have user-defined format code
  1115. $numberformat = array('code' => $this->_formats[$numberFormatIndex]);
  1116. } elseif (($code = PHPExcel_Style_NumberFormat::builtInFormatCode($numberFormatIndex)) !== '') {
  1117. // then we have built-in format code
  1118. $numberformat = array('code' => $code);
  1119. } else {
  1120. // we set the general format code
  1121. $numberformat = array('code' => 'General');
  1122. }
  1123. $objStyle->getNumberFormat()->setFormatCode($numberformat['code']);
  1124. // offset: 4; size: 2; XF type, cell protection, and parent style XF
  1125. // bit 2-0; mask 0x0007; XF_TYPE_PROT
  1126. $xfTypeProt = $this->_GetInt2d($recordData, 4);
  1127. // bit 0; mask 0x01; 1 = cell is locked
  1128. $isLocked = (0x01 & $xfTypeProt) >> 0;
  1129. $objStyle->getProtection()->setLocked($isLocked ?
  1130. PHPExcel_Style_Protection::PROTECTION_INHERIT : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1131. // bit 1; mask 0x02; 1 = Formula is hidden
  1132. $isHidden = (0x02 & $xfTypeProt) >> 1;
  1133. $objStyle->getProtection()->setHidden($isHidden ?
  1134. PHPExcel_Style_Protection::PROTECTION_PROTECTED : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1135. // bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
  1136. $isCellStyleXf = (0x04 & $xfTypeProt) >> 2;
  1137. // offset: 6; size: 1; Alignment and text break
  1138. // bit 2-0, mask 0x07; horizontal alignment
  1139. $horAlign = (0x07 & ord($recordData{6})) >> 0;
  1140. switch ($horAlign) {
  1141. case 0:
  1142. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_GENERAL);
  1143. break;
  1144. case 1:
  1145. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1146. break;
  1147. case 2:
  1148. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1149. break;
  1150. case 3:
  1151. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  1152. break;
  1153. case 5:
  1154. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  1155. break;
  1156. case 6:
  1157. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS);
  1158. break;
  1159. }
  1160. // bit 3, mask 0x08; wrap text
  1161. $wrapText = (0x08 & ord($recordData{6})) >> 3;
  1162. switch ($wrapText) {
  1163. case 0:
  1164. $objStyle->getAlignment()->setWrapText(false);
  1165. break;
  1166. case 1:
  1167. $objStyle->getAlignment()->setWrapText(true);
  1168. break;
  1169. }
  1170. // bit 6-4, mask 0x70; vertical alignment
  1171. $vertAlign = (0x70 & ord($recordData{6})) >> 4;
  1172. switch ($vertAlign) {
  1173. case 0:
  1174. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
  1175. break;
  1176. case 1:
  1177. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1178. break;
  1179. case 2:
  1180. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
  1181. break;
  1182. case 3:
  1183. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY);
  1184. break;
  1185. }
  1186. if ($this->_version == self::XLS_BIFF8) {
  1187. // offset: 7; size: 1; XF_ROTATION: Text rotation angle
  1188. $angle = ord($recordData{7});
  1189. $rotation = 0;
  1190. if ($angle <= 90) {
  1191. $rotation = $angle;
  1192. } else if ($angle <= 180) {
  1193. $rotation = 90 - $angle;
  1194. } else if ($angle == 255) {
  1195. $rotation = -165;
  1196. }
  1197. $objStyle->getAlignment()->setTextRotation($rotation);
  1198. // offset: 8; size: 1; Indentation, shrink to cell size, and text direction
  1199. // bit: 3-0; mask: 0x0F; indent level
  1200. $indent = (0x0F & ord($recordData{8})) >> 0;
  1201. $objStyle->getAlignment()->setIndent($indent);
  1202. // bit: 4; mask: 0x10; 1 = shrink content to fit into cell
  1203. $shrinkToFit = (0x10 & ord($recordData{8})) >> 4;
  1204. switch ($shrinkToFit) {
  1205. case 0:
  1206. $objStyle->getAlignment()->setShrinkToFit(false);
  1207. break;
  1208. case 1:
  1209. $objStyle->getAlignment()->setShrinkToFit(true);
  1210. break;
  1211. }
  1212. // offset: 9; size: 1; Flags used for attribute groups
  1213. // offset: 10; size: 4; Cell border lines and background area
  1214. // bit: 3-0; mask: 0x0000000F; left style
  1215. if ($bordersLeftStyle = $this->_mapBorderStyle((0x0000000F & $this->_GetInt4d($recordData, 10)) >> 0)) {
  1216. $objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
  1217. }
  1218. // bit: 7-4; mask: 0x000000F0; right style
  1219. if ($bordersRightStyle = $this->_mapBorderStyle((0x000000F0 & $this->_GetInt4d($recordData, 10)) >> 4)) {
  1220. $objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
  1221. }
  1222. // bit: 11-8; mask: 0x00000F00; top style
  1223. if ($bordersTopStyle = $this->_mapBorderStyle((0x00000F00 & $this->_GetInt4d($recordData, 10)) >> 8)) {
  1224. $objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
  1225. }
  1226. // bit: 15-12; mask: 0x0000F000; bottom style
  1227. if ($bordersBottomStyle = $this->_mapBorderStyle((0x0000F000 & $this->_GetInt4d($recordData, 10)) >> 12)) {
  1228. $objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
  1229. }
  1230. // bit: 22-16; mask: 0x007F0000; left color
  1231. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & $this->_GetInt4d($recordData, 10)) >> 16;
  1232. // bit: 29-23; mask: 0x3F800000; right color
  1233. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & $this->_GetInt4d($recordData, 10)) >> 23;
  1234. // offset: 14; size: 4;
  1235. // bit: 6-0; mask: 0x0000007F; top color
  1236. $objStyle->getBorders()->getTop()->colorIndex = (0x0000007F & $this->_GetInt4d($recordData, 14)) >> 0;
  1237. // bit: 13-7; mask: 0x00003F80; bottom color
  1238. $objStyle->getBorders()->getBottom()->colorIndex = (0x00003F80 & $this->_GetInt4d($recordData, 14)) >> 7;
  1239. // bit: 31-26; mask: 0xFC000000 fill pattern
  1240. if ($fillType = $this->_mapFillPattern((0xFC000000 & $this->_GetInt4d($recordData, 14)) >> 26)) {
  1241. $objStyle->getFill()->setFillType($fillType);
  1242. }
  1243. // offset: 18; size: 2; pattern and background colour
  1244. // bit: 6-0; mask: 0x007F; color index for pattern color
  1245. $objStyle->getFill()->startcolorIndex = (0x007F & $this->_GetInt2d($recordData, 18)) >> 0;
  1246. // bit: 13-7; mask: 0x3F80; color index for pattern background
  1247. $objStyle->getFill()->endcolorIndex = (0x3F80 & $this->_GetInt2d($recordData, 18)) >> 7;
  1248. } else {
  1249. // BIFF5
  1250. // offset: 7; size: 1; Text orientation and flags
  1251. $orientationAndFlags = ord($recordData{7});
  1252. // bit: 1-0; mask: 0x03; XF_ORIENTATION: Text orientation
  1253. $xfOrientation = (0x03 & $orientationAndFlags) >> 0;
  1254. switch ($xfOrientation) {
  1255. case 0:
  1256. $objStyle->getAlignment()->setTextRotation(0);
  1257. break;
  1258. case 1:
  1259. $objStyle->getAlignment()->setTextRotation(-165);
  1260. break;
  1261. case 2:
  1262. $objStyle->getAlignment()->setTextRotation(90);
  1263. break;
  1264. case 3:
  1265. $objStyle->getAlignment()->setTextRotation(-90);
  1266. break;
  1267. }
  1268. // offset: 8; size: 4; cell border lines and background area
  1269. $borderAndBackground = $this->_GetInt4d($recordData, 8);
  1270. // bit: 6-0; mask: 0x0000007F; color index for pattern color
  1271. $objStyle->getFill()->startcolorIndex = (0x0000007F & $borderAndBackground) >> 0;
  1272. // bit: 13-7; mask: 0x00003F80; color index for pattern background
  1273. $objStyle->getFill()->endcolorIndex = (0x00003F80 & $borderAndBackground) >> 7;
  1274. // bit: 21-16; mask: 0x003F0000; fill pattern
  1275. $objStyle->getFill()->setFillType($this->_mapFillPattern((0x003F0000 & $borderAndBackground) >> 16));
  1276. // bit: 24-22; mask: 0x01C00000; bottom line style
  1277. $objStyle->getBorders()->getBottom()->setBorderStyle($this->_mapBorderStyle((0x01C00000 & $borderAndBackground) >> 22));
  1278. // bit: 31-25; mask: 0xFE000000; bottom line color
  1279. $objStyle->getBorders()->getBottom()->colorIndex = (0xFE000000 & $borderAndBackground) >> 25;
  1280. // offset: 12; size: 4; cell border lines
  1281. $borderLines = $this->_GetInt4d($recordData, 12);
  1282. // bit: 2-0; mask: 0x00000007; top line style
  1283. $objStyle->getBorders()->getTop()->setBorderStyle($this->_mapBorderStyle((0x00000007 & $borderLines) >> 0));
  1284. // bit: 5-3; mask: 0x00000038; left line style
  1285. $objStyle->getBorders()->getLeft()->setBorderStyle($this->_mapBorderStyle((0x00000038 & $borderLines) >> 3));
  1286. // bit: 8-6; mask: 0x000001C0; right line style
  1287. $objStyle->getBorders()->getRight()->setBorderStyle($this->_mapBorderStyle((0x000001C0 & $borderLines) >> 6));
  1288. // bit: 15-9; mask: 0x0000FE00; top line color index
  1289. $objStyle->getBorders()->getTop()->colorIndex = (0x0000FE00 & $borderLines) >> 9;
  1290. // bit: 22-16; mask: 0x007F0000; left line color index
  1291. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & $borderLines) >> 16;
  1292. // bit: 29-23; mask: 0x3F800000; right line color index
  1293. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & $borderLines) >> 23;
  1294. }
  1295. // add cellStyleXf or cellXf and update mapping
  1296. if ($isCellStyleXf) {
  1297. // we only read one style XF record which is always the first
  1298. if ($this->_xfIndex == 0) {
  1299. $this->_phpExcel->addCellStyleXf($objStyle);
  1300. $this->_mapCellStyleXfIndex[$this->_xfIndex] = 0;
  1301. }
  1302. } else {
  1303. // we read all cell XF records
  1304. $this->_phpExcel->addCellXf($objStyle);
  1305. $this->_mapCellXfIndex[$this->_xfIndex] = count($this->_phpExcel->getCellXfCollection()) - 1;
  1306. }
  1307. // update XF index for when we read next record
  1308. ++$this->_xfIndex;
  1309. }
  1310. }
  1311. /**
  1312. * Read STYLE record
  1313. */
  1314. private function _readStyle()
  1315. {
  1316. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1317. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1318. // move stream pointer to next record
  1319. $this->_pos += 4 + $length;
  1320. if (!$this->_readDataOnly) {
  1321. // offset: 0; size: 2; index to XF record and flag for built-in style
  1322. $ixfe = $this->_GetInt2d($recordData, 0);
  1323. // bit: 11-0; mask 0x0FFF; index to XF record
  1324. $xfIndex = (0x0FFF & $ixfe) >> 0;
  1325. // bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style
  1326. $isBuiltIn = (bool) ((0x8000 & $ixfe) >> 15);
  1327. if ($isBuiltIn) {
  1328. // offset: 2; size: 1; identifier for built-in style
  1329. $builtInId = ord($recordData{2});
  1330. switch ($builtInId) {
  1331. case 0x00:
  1332. // currently, we are not using this for anything
  1333. break;
  1334. default:
  1335. break;
  1336. }
  1337. } else {
  1338. // user-defined; not supported by PHPExcel
  1339. }
  1340. }
  1341. }
  1342. /**
  1343. * Read PALETTE record
  1344. */
  1345. private function _readPalette()
  1346. {
  1347. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1348. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1349. // move stream pointer to next record
  1350. $this->_pos += 4 + $length;
  1351. if (!$this->_readDataOnly) {
  1352. // offset: 0; size: 2; number of following colors
  1353. $nm = $this->_GetInt2d($recordData, 0);
  1354. // list of RGB colors
  1355. for ($i = 0; $i < $nm; ++$i) {
  1356. $rgb = substr($recordData, 2 + 4 * $i, 4);
  1357. $this->_palette[] = $this->_readRGB($rgb);
  1358. }
  1359. }
  1360. }
  1361. /**
  1362. * SHEET
  1363. *
  1364. * This record is located in the Workbook Globals
  1365. * Substream and represents a sheet inside the workbook.
  1366. * One SHEET record is written for each sheet. It stores the
  1367. * sheet name and a stream offset to the BOF record of the
  1368. * respective Sheet Substream within the Workbook Stream.
  1369. *
  1370. * -- "OpenOffice.org's Documentation of the Microsoft
  1371. * Excel File Format"
  1372. */
  1373. private function _readSheet()
  1374. {
  1375. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1376. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1377. // move stream pointer to next record
  1378. $this->_pos += 4 + $length;
  1379. // offset: 0; size: 4; absolute stream position of the BOF record of the sheet
  1380. $rec_offset = $this->_GetInt4d($recordData, 0);
  1381. // offset: 4; size: 1; sheet state
  1382. $rec_typeFlag = ord($recordData{4});
  1383. switch (ord($recordData{4})) {
  1384. case 0x00: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE; break;
  1385. case 0x01: $sheetState = PHPExcel_Worksheet::SHEETSTATE_HIDDEN; break;
  1386. case 0x02: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VERYHIDDEN; break;
  1387. default: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE; break;
  1388. }
  1389. // offset: 5; size: 1; sheet type
  1390. $rec_visibilityFlag = ord($recordData{5});
  1391. // offset: 6; size: var; sheet name
  1392. if ($this->_version == self::XLS_BIFF8) {
  1393. $string = $this->_readUnicodeStringShort(substr($recordData, 6));
  1394. $rec_name = $string['value'];
  1395. } elseif ($this->_version == self::XLS_BIFF7) {
  1396. $string = $this->_readByteStringShort(substr($recordData, 6));
  1397. $rec_name = $string['value'];
  1398. }
  1399. $this->_sheets[] = array(
  1400. 'name' => $rec_name,
  1401. 'offset' => $rec_offset,
  1402. 'sheetState' => $sheetState,
  1403. );
  1404. }
  1405. /**
  1406. * Read EXTERNALBOOK record
  1407. */
  1408. private function _readExternalBook()
  1409. {
  1410. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1411. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1412. // move stream pointer to next record
  1413. $this->_pos += 4 + $length;
  1414. // offset within record data
  1415. $offset = 0;
  1416. // there are 4 types of records
  1417. if (strlen($recordData) > 4) {
  1418. // external reference
  1419. // offset: 0; size: 2; number of sheet names ($nm)
  1420. $nm = $this->_GetInt2d($recordData, 0);
  1421. $offset += 2;
  1422. // offset: 2; size: var; encoded URL without sheet name (Unicode string, 16-bit length)
  1423. $encodedUrlString = $this->_readUnicodeStringLong(substr($recordData, 2));
  1424. $offset += $encodedUrlString['size'];
  1425. // offset: var; size: var; list of $nm sheet names (Unicode strings, 16-bit length)
  1426. $externalSheetNames = array();
  1427. for ($i = 0; $i < $nm; ++$i) {
  1428. $externalSheetNameString = $this->_readUnicodeStringLong(substr($recordData, $offset));
  1429. $externalSheetNames[] = $externalSheetNameString['value'];
  1430. $offset += $externalSheetNameString['size'];
  1431. }
  1432. // store the record data
  1433. $this->_externalBooks[] = array(
  1434. 'type' => 'external',
  1435. 'encodedUrl' => $encodedUrlString['value'],
  1436. 'externalSheetNames' => $externalSheetNames,
  1437. );
  1438. } elseif (substr($recordData, 2, 2) == pack('CC', 0x01, 0x04)) {
  1439. // internal reference
  1440. // offset: 0; size: 2; number of sheet in this document
  1441. // offset: 2; size: 2; 0x01 0x04
  1442. $this->_externalBooks[] = array(
  1443. 'type' => 'internal',
  1444. );
  1445. } elseif (substr($recordData, 0, 4) == pack('VCC', 0x0001, 0x01, 0x3A)) {
  1446. // add-in function
  1447. // offset: 0; size: 2; 0x0001
  1448. $this->_externalBooks[] = array(
  1449. 'type' => 'addInFunction',
  1450. );
  1451. } elseif (substr($recordData, 0, 2) == pack('V', 0x0000)) {
  1452. // DDE links, OLE links
  1453. // offset: 0; size: 2; 0x0000
  1454. // offset: 2; size: var; encoded source document name
  1455. $this->_externalBooks[] = array(
  1456. 'type' => 'DDEorOLE',
  1457. );
  1458. }
  1459. }
  1460. /**
  1461. * Read EXTERNSHEET record
  1462. */
  1463. private function _readExternSheet()
  1464. {
  1465. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1466. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1467. // move stream pointer to next record
  1468. $this->_pos += 4 + $length;
  1469. // external sheet references provided for named cells
  1470. if ($this->_version == self::XLS_BIFF8) {
  1471. // offset: 0; size: 2; number of following ref structures
  1472. $nm = $this->_GetInt2d($recordData, 0);
  1473. for ($i = 0; $i < $nm; ++$i) {
  1474. $this->_ref[] = array(
  1475. // offset: 2 + 6 * $i; index to EXTERNALBOOK record
  1476. 'externalBookIndex' => $this->_GetInt2d($recordData, 2 + 6 * $i),
  1477. // offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
  1478. 'firstSheetIndex' => $this->_GetInt2d($recordData, 4 + 6 * $i),
  1479. // offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
  1480. 'lastSheetIndex' => $this->_GetInt2d($recordData, 6 + 6 * $i),
  1481. );
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * DEFINEDNAME
  1487. *
  1488. * This record is part of a Link Table. It contains the name
  1489. * and the token array of an internal defined name. Token
  1490. * arrays of defined names contain tokens with aberrant
  1491. * token classes.
  1492. *
  1493. * -- "OpenOffice.org's Documentation of the Microsoft
  1494. * Excel File Format"
  1495. */
  1496. private function _readDefinedName()
  1497. {
  1498. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1499. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1500. // move stream pointer to next record
  1501. $this->_pos += 4 + $length;
  1502. if ($this->_version == self::XLS_BIFF8) {
  1503. // retrieves named cells
  1504. // offset: 0; size: 2; option flags
  1505. $opts = $this->_GetInt2d($recordData, 0);
  1506. // bit: 5; mask: 0x0020; 0 = user-defined name, 1 = built-in-name
  1507. $isBuiltInName = (0x0020 & $opts) >> 5;
  1508. // offset: 2; size: 1; keyboard shortcut
  1509. // offset: 3; size: 1; length of the name (character count)
  1510. $nlen = ord($recordData{3});
  1511. // offset: 4; size: 2; size of the formula data (it can happen that this is zero)
  1512. $flen = $this->_GetInt2d($recordData, 4);
  1513. // offset: 14; size: var; Name (Unicode string without length field)
  1514. $string = $this->_readUnicodeString(substr($recordData, 14), $nlen);
  1515. // offset: var; size: $flen; formula data
  1516. $offset = 14 + $string['size'];
  1517. $formulaStructure = pack('v', $flen) . substr($recordData, $offset, $flen);
  1518. try {
  1519. $formula = $this->_getFormulaFromStructure($formulaStructure);
  1520. } catch (Exception $e) {
  1521. $formula = '';
  1522. }
  1523. $this->_definedname[] = array(
  1524. 'isBuiltInName' => $isBuiltInName,
  1525. 'name' => $string['value'],
  1526. 'formula' => $formula,
  1527. );
  1528. }
  1529. }
  1530. /**
  1531. * Read MSODRAWINGGROUP record
  1532. */
  1533. private function _readMsoDrawingGroup()
  1534. {
  1535. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1536. // get spliced record data
  1537. $splicedRecordData = $this->_getSplicedRecordData();
  1538. $recordData = $splicedRecordData['recordData'];
  1539. $this->_drawingGroupData .= $recordData;
  1540. }
  1541. /**
  1542. * SST - Shared String Table
  1543. *
  1544. * This record contains a list of all strings used anywhere
  1545. * in the workbook. Each string occurs only once. The
  1546. * workbook uses indexes into the list to reference the
  1547. * strings.
  1548. *
  1549. * -- "OpenOffice.org's Documentation of the Microsoft
  1550. * Excel File Format"
  1551. **/
  1552. private function _readSst()
  1553. {
  1554. // offset within (spliced) record data
  1555. $pos = 0;
  1556. // get spliced record data
  1557. $splicedRecordData = $this->_getSplicedRecordData();
  1558. $recordData = $splicedRecordData['recordData'];
  1559. $spliceOffsets = $splicedRecordData['spliceOffsets'];
  1560. // offset: 0; size: 4; total number of strings in the workbook
  1561. $pos += 4;
  1562. // offset: 4; size: 4; number of following strings ($nm)
  1563. $nm = $this->_GetInt4d($recordData, 4);
  1564. $pos += 4;
  1565. // loop through the Unicode strings (16-bit length)
  1566. for ($i = 0; $i < $nm; ++$i) {
  1567. // number of characters in the Unicode string
  1568. $numChars = $this->_GetInt2d($recordData, $pos);
  1569. $pos += 2;
  1570. // option flags
  1571. $optionFlags = ord($recordData{$pos});
  1572. ++$pos;
  1573. // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
  1574. $isCompressed = (($optionFlags & 0x01) == 0) ;
  1575. // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
  1576. $hasAsian = (($optionFlags & 0x04) != 0);
  1577. // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
  1578. $hasRichText = (($optionFlags & 0x08) != 0);
  1579. if ($hasRichText) {
  1580. // number of Rich-Text formatting runs
  1581. $formattingRuns = $this->_GetInt2d($recordData, $pos);
  1582. $pos += 2;
  1583. }
  1584. if ($hasAsian) {
  1585. // size of Asian phonetic setting
  1586. $extendedRunLength = $this->_GetInt4d($recordData, $pos);
  1587. $pos += 4;
  1588. }
  1589. // expected byte length of character array if not split
  1590. $len = ($isCompressed) ? $numChars : $numChars * 2;
  1591. // look up limit position
  1592. foreach ($spliceOffsets as $spliceOffset) {
  1593. if ($pos < $spliceOffset) {
  1594. $limitpos = $spliceOffset;
  1595. break;
  1596. }
  1597. }
  1598. if ($pos + $len <= $limitpos) {
  1599. // character array is not split between records
  1600. $retstr = substr($recordData, $pos, $len);
  1601. $pos += $len;
  1602. } else {
  1603. // character array is split between records
  1604. // first part of character array
  1605. $retstr = substr($recordData, $pos, $limitpos - $pos);
  1606. $bytesRead = $limitpos - $pos;
  1607. // remaining characters in Unicode string
  1608. $charsLeft = $numChars - (($isCompressed) ? $bytesRead : ($bytesRead / 2));
  1609. $pos = $limitpos;
  1610. // keep reading the characters
  1611. while ($charsLeft > 0) {
  1612. // look up next limit position, in case the string span more than one continue record
  1613. foreach ($spliceOffsets as $spliceOffset) {
  1614. if ($pos < $spliceOffset) {
  1615. $limitpos = $spliceOffset;
  1616. break;
  1617. }
  1618. }
  1619. // repeated option flags
  1620. // OpenOffice.org documentation 5.21
  1621. $option = ord($recordData{$pos});
  1622. ++$pos;
  1623. if ($isCompressed && ($option == 0)) {
  1624. // 1st fragment compressed
  1625. // this fragment compressed
  1626. $len = min($charsLeft, $limitpos - $pos);
  1627. $retstr .= substr($recordData, $pos, $len);
  1628. $charsLeft -= $len;
  1629. $isCompressed = true;
  1630. } elseif (!$isCompressed && ($option != 0)) {
  1631. // 1st fragment uncompressed
  1632. // this fragment uncompressed
  1633. $len = min($charsLeft * 2, $limitpos - $pos);
  1634. $retstr .= substr($recordData, $pos, $len);
  1635. $charsLeft -= $len / 2;
  1636. $isCompressed = false;
  1637. } elseif (!$isCompressed && ($option == 0)) {
  1638. // 1st fragment uncompressed
  1639. // this fragment compressed
  1640. $len = min($charsLeft, $limitpos - $pos);
  1641. for ($j = 0; $j < $len; ++$j) {
  1642. $retstr .= $recordData{$pos + $j} . chr(0);
  1643. }
  1644. $charsLeft -= $len;
  1645. $isCompressed = false;
  1646. } else {
  1647. // 1st fragment compressed
  1648. // this fragment uncompressed
  1649. $newstr = '';
  1650. for ($j = 0; $j < strlen($retstr); ++$j) {
  1651. $newstr .= $retstr[$j] . chr(0);
  1652. }
  1653. $retstr = $newstr;
  1654. $len = min($charsLeft * 2, $limitpos - $pos);
  1655. $retstr .= substr($recordData, $pos, $len);
  1656. $charsLeft -= $len / 2;
  1657. $isCompressed = false;
  1658. }
  1659. $pos += $len;
  1660. }
  1661. }
  1662. // convert to UTF-8
  1663. $retstr = $this->_encodeUTF16($retstr, $isCompressed);
  1664. // read additional Rich-Text information, if any
  1665. $fmtRuns = array();
  1666. if ($hasRichText) {
  1667. // list of formatting runs
  1668. for ($j = 0; $j < $formattingRuns; ++$j) {
  1669. // first formatted character; zero-based
  1670. $charPos = $this->_GetInt2d($recordData, $pos + $j * 4);
  1671. // index to font record
  1672. $fontIndex = $this->_GetInt2d($recordData, $pos + 2 + $j * 4);
  1673. $fmtRuns[] = array(
  1674. 'charPos' => $charPos,
  1675. 'fontIndex' => $fontIndex,
  1676. );
  1677. }
  1678. $pos += 4 * $formattingRuns;
  1679. }
  1680. // read additional Asian phonetics information, if any
  1681. if ($hasAsian) {
  1682. // For Asian phonetic settings, we skip the extended string data
  1683. $pos += $extendedRunLength;
  1684. }
  1685. // store the shared sting
  1686. $this->_sst[] = array(
  1687. 'value' => $retstr,
  1688. 'fmtRuns' => $fmtRuns,
  1689. );
  1690. }
  1691. // _getSplicedRecordData() takes care of moving current position in data stream
  1692. }
  1693. /**
  1694. * Read PRINTGRIDLINES record
  1695. */
  1696. private function _readPrintGridlines()
  1697. {
  1698. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1699. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1700. // move stream pointer to next record
  1701. $this->_pos += 4 + $length;
  1702. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1703. // offset: 0; size: 2; 0 = do not print sheet grid lines; 1 = print sheet gridlines
  1704. $printGridlines = (bool) $this->_GetInt2d($recordData, 0);
  1705. $this->_phpSheet->setPrintGridlines($printGridlines);
  1706. }
  1707. }
  1708. /**
  1709. * Read DEFAULTROWHEIGHT record
  1710. */
  1711. private function _readDefaultRowHeight()
  1712. {
  1713. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1714. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1715. // move stream pointer to next record
  1716. $this->_pos += 4 + $length;
  1717. // offset: 0; size: 2; option flags
  1718. // offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
  1719. $height = $this->_GetInt2d($recordData, 2);
  1720. $this->_phpSheet->getDefaultRowDimension()->setRowHeight($height / 20);
  1721. }
  1722. /**
  1723. * Read SHEETPR record
  1724. */
  1725. private function _readSheetPr()
  1726. {
  1727. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1728. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1729. // move stream pointer to next record
  1730. $this->_pos += 4 + $length;
  1731. // offset: 0; size: 2
  1732. // bit: 6; mask: 0x0040; 0 = outline buttons above outline group
  1733. $isSummaryBelow = (0x0040 & $this->_GetInt2d($recordData, 0)) >> 6;
  1734. $this->_phpSheet->setShowSummaryBelow($isSummaryBelow);
  1735. // bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
  1736. $isSummaryRight = (0x0080 & $this->_GetInt2d($recordData, 0)) >> 7;
  1737. $this->_phpSheet->setShowSummaryRight($isSummaryRight);
  1738. // bit: 8; mask: 0x100; 0 = scale printout in percent, 1 = fit printout to number of pages
  1739. // this corresponds to radio button setting in page setup dialog in Excel
  1740. $this->_isFitToPages = (bool) ((0x0100 & $this->_GetInt2d($recordData, 0)) >> 8);
  1741. }
  1742. /**
  1743. * Read HORIZONTALPAGEBREAKS record
  1744. */
  1745. private function _readHorizontalPageBreaks()
  1746. {
  1747. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1748. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1749. // move stream pointer to next record
  1750. $this->_pos += 4 + $length;
  1751. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1752. // offset: 0; size: 2; number of the following row index structures
  1753. $nm = $this->_GetInt2d($recordData, 0);
  1754. // offset: 2; size: 6 * $nm; list of $nm row index structures
  1755. for ($i = 0; $i < $nm; ++$i) {
  1756. $r = $this->_GetInt2d($recordData, 2 + 6 * $i);
  1757. $cf = $this->_GetInt2d($recordData, 2 + 6 * $i + 2);
  1758. $cl = $this->_GetInt2d($recordData, 2 + 6 * $i + 4);
  1759. // not sure why two column indexes are necessary?
  1760. $this->_phpSheet->setBreakByColumnAndRow($cf, $r, PHPExcel_Worksheet::BREAK_ROW);
  1761. }
  1762. }
  1763. }
  1764. /**
  1765. * Read VERTICALPAGEBREAKS record
  1766. */
  1767. private function _readVerticalPageBreaks()
  1768. {
  1769. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1770. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1771. // move stream pointer to next record
  1772. $this->_pos += 4 + $length;
  1773. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  1774. // offset: 0; size: 2; number of the following column index structures
  1775. $nm = $this->_GetInt2d($recordData, 0);
  1776. // offset: 2; size: 6 * $nm; list of $nm row index structures
  1777. for ($i = 0; $i < $nm; ++$i) {
  1778. $c = $this->_GetInt2d($recordData, 2 + 6 * $i);
  1779. $rf = $this->_GetInt2d($recordData, 2 + 6 * $i + 2);
  1780. $rl = $this->_GetInt2d($recordData, 2 + 6 * $i + 4);
  1781. // not sure why two row indexes are necessary?
  1782. $this->_phpSheet->setBreakByColumnAndRow($c, $rf, PHPExcel_Worksheet::BREAK_COLUMN);
  1783. }
  1784. }
  1785. }
  1786. /**
  1787. * Read HEADER record
  1788. */
  1789. private function _readHeader()
  1790. {
  1791. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1792. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1793. // move stream pointer to next record
  1794. $this->_pos += 4 + $length;
  1795. if (!$this->_readDataOnly) {
  1796. // offset: 0; size: var
  1797. // realized that $recordData can be empty even when record exists
  1798. if ($recordData) {
  1799. if ($this->_version == self::XLS_BIFF8) {
  1800. $string = $this->_readUnicodeStringLong($recordData);
  1801. } else {
  1802. $string = $this->_readByteStringShort($recordData);
  1803. }
  1804. $this->_phpSheet->getHeaderFooter()->setOddHeader($string['value']);
  1805. $this->_phpSheet->getHeaderFooter()->setEvenHeader($string['value']);
  1806. }
  1807. }
  1808. }
  1809. /**
  1810. * Read FOOTER record
  1811. */
  1812. private function _readFooter()
  1813. {
  1814. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1815. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1816. // move stream pointer to next record
  1817. $this->_pos += 4 + $length;
  1818. if (!$this->_readDataOnly) {
  1819. // offset: 0; size: var
  1820. // realized that $recordData can be empty even when record exists
  1821. if ($recordData) {
  1822. if ($this->_version == self::XLS_BIFF8) {
  1823. $string = $this->_readUnicodeStringLong($recordData);
  1824. } else {
  1825. $string = $this->_readByteStringShort($recordData);
  1826. }
  1827. $this->_phpSheet->getHeaderFooter()->setOddFooter($string['value']);
  1828. $this->_phpSheet->getHeaderFooter()->setEvenFooter($string['value']);
  1829. }
  1830. }
  1831. }
  1832. /**
  1833. * Read HCENTER record
  1834. */
  1835. private function _readHcenter()
  1836. {
  1837. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1838. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1839. // move stream pointer to next record
  1840. $this->_pos += 4 + $length;
  1841. if (!$this->_readDataOnly) {
  1842. // offset: 0; size: 2; 0 = print sheet left aligned, 1 = print sheet centered horizontally
  1843. $isHorizontalCentered = (bool) $this->_GetInt2d($recordData, 0);
  1844. $this->_phpSheet->getPageSetup()->setHorizontalCentered($isHorizontalCentered);
  1845. }
  1846. }
  1847. /**
  1848. * Read VCENTER record
  1849. */
  1850. private function _readVcenter()
  1851. {
  1852. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1853. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1854. // move stream pointer to next record
  1855. $this->_pos += 4 + $length;
  1856. if (!$this->_readDataOnly) {
  1857. // offset: 0; size: 2; 0 = print sheet aligned at top page border, 1 = print sheet vertically centered
  1858. $isVerticalCentered = (bool) $this->_GetInt2d($recordData, 0);
  1859. $this->_phpSheet->getPageSetup()->setVerticalCentered($isVerticalCentered);
  1860. }
  1861. }
  1862. /**
  1863. * Read LEFTMARGIN record
  1864. */
  1865. private function _readLeftMargin()
  1866. {
  1867. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1868. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1869. // move stream pointer to next record
  1870. $this->_pos += 4 + $length;
  1871. if (!$this->_readDataOnly) {
  1872. // offset: 0; size: 8
  1873. $this->_phpSheet->getPageMargins()->setLeft($this->_extractNumber($recordData));
  1874. }
  1875. }
  1876. /**
  1877. * Read RIGHTMARGIN record
  1878. */
  1879. private function _readRightMargin()
  1880. {
  1881. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1882. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1883. // move stream pointer to next record
  1884. $this->_pos += 4 + $length;
  1885. if (!$this->_readDataOnly) {
  1886. // offset: 0; size: 8
  1887. $this->_phpSheet->getPageMargins()->setRight($this->_extractNumber($recordData));
  1888. }
  1889. }
  1890. /**
  1891. * Read TOPMARGIN record
  1892. */
  1893. private function _readTopMargin()
  1894. {
  1895. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1896. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1897. // move stream pointer to next record
  1898. $this->_pos += 4 + $length;
  1899. if (!$this->_readDataOnly) {
  1900. // offset: 0; size: 8
  1901. $this->_phpSheet->getPageMargins()->setTop($this->_extractNumber($recordData));
  1902. }
  1903. }
  1904. /**
  1905. * Read BOTTOMMARGIN record
  1906. */
  1907. private function _readBottomMargin()
  1908. {
  1909. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1910. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1911. // move stream pointer to next record
  1912. $this->_pos += 4 + $length;
  1913. if (!$this->_readDataOnly) {
  1914. // offset: 0; size: 8
  1915. $this->_phpSheet->getPageMargins()->setBottom($this->_extractNumber($recordData));
  1916. }
  1917. }
  1918. /**
  1919. * Read PAGESETUP record
  1920. */
  1921. private function _readPageSetup()
  1922. {
  1923. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1924. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1925. // move stream pointer to next record
  1926. $this->_pos += 4 + $length;
  1927. if (!$this->_readDataOnly) {
  1928. // offset: 0; size: 2; paper size
  1929. $paperSize = $this->_GetInt2d($recordData, 0);
  1930. // offset: 2; size: 2; scaling factor
  1931. $scale = $this->_GetInt2d($recordData, 2);
  1932. // offset: 6; size: 2; fit worksheet width to this number of pages, 0 = use as many as needed
  1933. $fitToWidth = $this->_GetInt2d($recordData, 6);
  1934. // offset: 8; size: 2; fit worksheet height to this number of pages, 0 = use as many as needed
  1935. $fitToHeight = $this->_GetInt2d($recordData, 8);
  1936. // offset: 10; size: 2; option flags
  1937. // bit: 1; mask: 0x0002; 0=landscape, 1=portrait
  1938. $isPortrait = (0x0002 & $this->_GetInt2d($recordData, 10)) >> 1;
  1939. // bit: 2; mask: 0x0004; 1= paper size, scaling factor, paper orient. not init
  1940. // when this bit is set, do not use flags for those properties
  1941. $isNotInit = (0x0004 & $this->_GetInt2d($recordData, 10)) >> 2;
  1942. if (!$isNotInit) {
  1943. $this->_phpSheet->getPageSetup()->setPaperSize($paperSize);
  1944. switch ($isPortrait) {
  1945. case 0: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); break;
  1946. case 1: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); break;
  1947. }
  1948. $this->_phpSheet->getPageSetup()->setScale($scale, false);
  1949. $this->_phpSheet->getPageSetup()->setFitToPage((bool) $this->_isFitToPages);
  1950. $this->_phpSheet->getPageSetup()->setFitToWidth($fitToWidth, false);
  1951. $this->_phpSheet->getPageSetup()->setFitToHeight($fitToHeight, false);
  1952. }
  1953. // offset: 16; size: 8; header margin (IEEE 754 floating-point value)
  1954. $marginHeader = $this->_extractNumber(substr($recordData, 16, 8));
  1955. $this->_phpSheet->getPageMargins()->setHeader($marginHeader);
  1956. // offset: 24; size: 8; footer margin (IEEE 754 floating-point value)
  1957. $marginFooter = $this->_extractNumber(substr($recordData, 24, 8));
  1958. $this->_phpSheet->getPageMargins()->setFooter($marginFooter);
  1959. }
  1960. }
  1961. /**
  1962. * PROTECT - Sheet protection (BIFF2 through BIFF8)
  1963. * if this record is omitted, then it also means no sheet protection
  1964. */
  1965. private function _readProtect()
  1966. {
  1967. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1968. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1969. // move stream pointer to next record
  1970. $this->_pos += 4 + $length;
  1971. if (!$this->_readDataOnly) {
  1972. // offset: 0; size: 2;
  1973. // bit 0, mask 0x01; sheet protection
  1974. $isSheetProtected = (0x01 & $this->_GetInt2d($recordData, 0)) >> 0;
  1975. switch ($isSheetProtected) {
  1976. case 0: break;
  1977. case 1: $this->_phpSheet->getProtection()->setSheet(true); break;
  1978. }
  1979. }
  1980. }
  1981. /**
  1982. * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
  1983. */
  1984. private function _readPassword()
  1985. {
  1986. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  1987. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1988. // move stream pointer to next record
  1989. $this->_pos += 4 + $length;
  1990. if (!$this->_readDataOnly) {
  1991. // offset: 0; size: 2; 16-bit hash value of password
  1992. $password = strtoupper(dechex($this->_GetInt2d($recordData, 0))); // the hashed password
  1993. $this->_phpSheet->getProtection()->setPassword($password, true);
  1994. }
  1995. }
  1996. /**
  1997. * Read DEFCOLWIDTH record
  1998. */
  1999. private function _readDefColWidth()
  2000. {
  2001. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2002. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2003. // move stream pointer to next record
  2004. $this->_pos += 4 + $length;
  2005. // offset: 0; size: 2; default column width
  2006. $width = $this->_GetInt2d($recordData, 0);
  2007. if ($width != 8) {
  2008. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width);
  2009. }
  2010. }
  2011. /**
  2012. * Read COLINFO record
  2013. */
  2014. private function _readColInfo()
  2015. {
  2016. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2017. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2018. // move stream pointer to next record
  2019. $this->_pos += 4 + $length;
  2020. if (!$this->_readDataOnly) {
  2021. // offset: 0; size: 2; index to first column in range
  2022. $fc = $this->_GetInt2d($recordData, 0); // first column index
  2023. // offset: 2; size: 2; index to last column in range
  2024. $lc = $this->_GetInt2d($recordData, 2); // first column index
  2025. // offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
  2026. $width = $this->_GetInt2d($recordData, 4);
  2027. // offset: 6; size: 2; index to XF record for default column formatting
  2028. $xfIndex = $this->_GetInt2d($recordData, 6);
  2029. // offset: 8; size: 2; option flags
  2030. // bit: 0; mask: 0x0001; 1= columns are hidden
  2031. $isHidden = (0x0001 & $this->_GetInt2d($recordData, 8)) >> 0;
  2032. // bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
  2033. $level = (0x0700 & $this->_GetInt2d($recordData, 8)) >> 8;
  2034. // bit: 12; mask: 0x1000; 1 = collapsed
  2035. $isCollapsed = (0x1000 & $this->_GetInt2d($recordData, 8)) >> 12;
  2036. // offset: 10; size: 2; not used
  2037. for ($i = $fc; $i <= $lc; ++$i) {
  2038. if ($lc == 255 || $lc == 256) {
  2039. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width / 256);
  2040. break;
  2041. }
  2042. $this->_phpSheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
  2043. $this->_phpSheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
  2044. $this->_phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  2045. $this->_phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
  2046. $this->_phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2047. }
  2048. }
  2049. }
  2050. /**
  2051. * ROW
  2052. *
  2053. * This record contains the properties of a single row in a
  2054. * sheet. Rows and cells in a sheet are divided into blocks
  2055. * of 32 rows.
  2056. *
  2057. * -- "OpenOffice.org's Documentation of the Microsoft
  2058. * Excel File Format"
  2059. */
  2060. private function _readRow()
  2061. {
  2062. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2063. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2064. // move stream pointer to next record
  2065. $this->_pos += 4 + $length;
  2066. if (!$this->_readDataOnly) {
  2067. // offset: 0; size: 2; index of this row
  2068. $r = $this->_GetInt2d($recordData, 0);
  2069. // offset: 2; size: 2; index to column of the first cell which is described by a cell record
  2070. // offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
  2071. // offset: 6; size: 2;
  2072. // bit: 14-0; mask: 0x7FF; height of the row, in twips = 1/20 of a point
  2073. $height = (0x7FF & $this->_GetInt2d($recordData, 6)) >> 0;
  2074. // bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
  2075. $useDefaultHeight = (0x8000 & $this->_GetInt2d($recordData, 6)) >> 15;
  2076. if (!$useDefaultHeight) {
  2077. $this->_phpSheet->getRowDimension($r + 1)->setRowHeight($height / 20);
  2078. }
  2079. // offset: 8; size: 2; not used
  2080. // offset: 10; size: 2; not used in BIFF5-BIFF8
  2081. // offset: 12; size: 4; option flags and default row formatting
  2082. // bit: 2-0: mask: 0x00000007; outline level of the row
  2083. $level = (0x00000007 & $this->_GetInt4d($recordData, 12)) >> 0;
  2084. $this->_phpSheet->getRowDimension($r + 1)->setOutlineLevel($level);
  2085. // bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
  2086. $isCollapsed = (0x00000010 & $this->_GetInt4d($recordData, 12)) >> 4;
  2087. $this->_phpSheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
  2088. // bit: 5; mask: 0x00000020; 1 = row is hidden
  2089. $isHidden = (0x00000020 & $this->_GetInt4d($recordData, 12)) >> 5;
  2090. $this->_phpSheet->getRowDimension($r + 1)->setVisible(!$isHidden);
  2091. // bit: 7; mask: 0x00000080; 1 = row has explicit format
  2092. $hasExplicitFormat = (0x00000080 & $this->_GetInt4d($recordData, 12)) >> 7;
  2093. // bit: 27-16; mask: 0x0FFF0000; only applies when hasExplicitFormat = 1; index to XF record
  2094. $xfIndex = (0x0FFF0000 & $this->_GetInt4d($recordData, 12)) >> 16;
  2095. if ($hasExplicitFormat) {
  2096. $this->_phpSheet->getRowDimension($r + 1)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2097. }
  2098. }
  2099. }
  2100. /**
  2101. * Read RK record
  2102. * This record represents a cell that contains an RK value
  2103. * (encoded integer or floating-point value). If a
  2104. * floating-point value cannot be encoded to an RK value,
  2105. * a NUMBER record will be written. This record replaces the
  2106. * record INTEGER written in BIFF2.
  2107. *
  2108. * -- "OpenOffice.org's Documentation of the Microsoft
  2109. * Excel File Format"
  2110. */
  2111. private function _readRk()
  2112. {
  2113. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2114. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2115. // move stream pointer to next record
  2116. $this->_pos += 4 + $length;
  2117. // offset: 0; size: 2; index to row
  2118. $row = $this->_GetInt2d($recordData, 0);
  2119. // offset: 2; size: 2; index to column
  2120. $column = $this->_GetInt2d($recordData, 2);
  2121. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2122. // Read cell?
  2123. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2124. // offset: 4; size: 2; index to XF record
  2125. $xfIndex = $this->_GetInt2d($recordData, 4);
  2126. // offset: 6; size: 4; RK value
  2127. $rknum = $this->_GetInt4d($recordData, 6);
  2128. $numValue = $this->_GetIEEE754($rknum);
  2129. // add style information
  2130. if (!$this->_readDataOnly) {
  2131. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2132. }
  2133. // add cell
  2134. $this->_phpSheet->setCellValueExplicit($columnString . ($row + 1), $numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  2135. }
  2136. }
  2137. /**
  2138. * Read LABELSST record
  2139. * This record represents a cell that contains a string. It
  2140. * replaces the LABEL record and RSTRING record used in
  2141. * BIFF2-BIFF5.
  2142. *
  2143. * -- "OpenOffice.org's Documentation of the Microsoft
  2144. * Excel File Format"
  2145. */
  2146. private function _readLabelSst()
  2147. {
  2148. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2149. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2150. // move stream pointer to next record
  2151. $this->_pos += 4 + $length;
  2152. // offset: 0; size: 2; index to row
  2153. $row = $this->_GetInt2d($recordData, 0);
  2154. // offset: 2; size: 2; index to column
  2155. $column = $this->_GetInt2d($recordData, 2);
  2156. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2157. // Read cell?
  2158. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2159. // offset: 4; size: 2; index to XF record
  2160. $xfIndex = $this->_GetInt2d($recordData, 4);
  2161. // offset: 6; size: 4; index to SST record
  2162. $index = $this->_GetInt4d($recordData, 6);
  2163. // add cell
  2164. if (($fmtRuns = $this->_sst[$index]['fmtRuns']) && !$this->_readDataOnly) {
  2165. // then we should treat as rich text
  2166. $richText = new PHPExcel_RichText($this->_phpSheet->getCell($columnString . ($row + 1)));
  2167. $charPos = 0;
  2168. for ($i = 0; $i <= count($this->_sst[$index]['fmtRuns']); ++$i) {
  2169. if (isset($fmtRuns[$i])) {
  2170. $text = mb_substr($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos, 'UTF-8');
  2171. $charPos = $fmtRuns[$i]['charPos'];
  2172. } else {
  2173. $text = mb_substr($this->_sst[$index]['value'], $charPos, mb_strlen($this->_sst[$index]['value']), 'UTF-8');
  2174. }
  2175. if (mb_strlen($text) > 0) {
  2176. if ($i == 0) { // first text run, no style
  2177. $richText->createText($text);
  2178. } else {
  2179. $textRun = $richText->createTextRun($text);
  2180. if (isset($fmtRuns[$i - 1])) {
  2181. if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
  2182. $fontIndex = $fmtRuns[$i - 1]['fontIndex'];
  2183. } else {
  2184. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  2185. // check the OpenOffice documentation of the FONT record
  2186. $fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
  2187. }
  2188. $textRun->setFont(clone $this->_objFonts[$fontIndex]);
  2189. }
  2190. }
  2191. }
  2192. }
  2193. } else {
  2194. $this->_phpSheet->setCellValueExplicit($columnString . ($row + 1), $this->_sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING);
  2195. }
  2196. // add style information
  2197. if (!$this->_readDataOnly) {
  2198. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2199. }
  2200. }
  2201. }
  2202. /**
  2203. * Read MULRK record
  2204. * This record represents a cell range containing RK value
  2205. * cells. All cells are located in the same row.
  2206. *
  2207. * -- "OpenOffice.org's Documentation of the Microsoft
  2208. * Excel File Format"
  2209. */
  2210. private function _readMulRk()
  2211. {
  2212. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2213. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2214. // move stream pointer to next record
  2215. $this->_pos += 4 + $length;
  2216. // offset: 0; size: 2; index to row
  2217. $row = $this->_GetInt2d($recordData, 0);
  2218. // offset: 2; size: 2; index to first column
  2219. $colFirst = $this->_GetInt2d($recordData, 2);
  2220. // offset: var; size: 2; index to last column
  2221. $colLast = $this->_GetInt2d($recordData, $length - 2);
  2222. $columns = $colLast - $colFirst + 1;
  2223. // offset within record data
  2224. $offset = 4;
  2225. for ($i = 0; $i < $columns; ++$i) {
  2226. $columnString = PHPExcel_Cell::stringFromColumnIndex($colFirst + $i);
  2227. // Read cell?
  2228. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2229. // offset: var; size: 2; index to XF record
  2230. $xfIndex = $this->_GetInt2d($recordData, $offset);
  2231. // offset: var; size: 4; RK value
  2232. $numValue = $this->_GetIEEE754($this->_GetInt4d($recordData, $offset + 2));
  2233. if (!$this->_readDataOnly) {
  2234. // add style
  2235. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2236. }
  2237. // add cell value
  2238. $this->_phpSheet->setCellValueExplicit($columnString . ($row + 1), $numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  2239. }
  2240. $offset += 6;
  2241. }
  2242. }
  2243. /**
  2244. * Read NUMBER record
  2245. * This record represents a cell that contains a
  2246. * floating-point value.
  2247. *
  2248. * -- "OpenOffice.org's Documentation of the Microsoft
  2249. * Excel File Format"
  2250. */
  2251. private function _readNumber()
  2252. {
  2253. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2254. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2255. // move stream pointer to next record
  2256. $this->_pos += 4 + $length;
  2257. // offset: 0; size: 2; index to row
  2258. $row = $this->_GetInt2d($recordData, 0);
  2259. // offset: 2; size 2; index to column
  2260. $column = $this->_GetInt2d($recordData, 2);
  2261. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2262. // Read cell?
  2263. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2264. // offset 4; size: 2; index to XF record
  2265. $xfIndex = $this->_GetInt2d($recordData, 4);
  2266. $numValue = $this->_extractNumber(substr($recordData, 6, 8));
  2267. // add cell style
  2268. if (!$this->_readDataOnly) {
  2269. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2270. }
  2271. // add cell value
  2272. $this->_phpSheet->setCellValueExplicit($columnString . ($row + 1), $numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  2273. }
  2274. }
  2275. /**
  2276. * Read FORMULA record + perhaps a following STRING record if formula result is a string
  2277. * This record contains the token array and the result of a
  2278. * formula cell.
  2279. *
  2280. * -- "OpenOffice.org's Documentation of the Microsoft
  2281. * Excel File Format"
  2282. */
  2283. private function _readFormula()
  2284. {
  2285. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2286. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2287. // move stream pointer to next record
  2288. $this->_pos += 4 + $length;
  2289. // offset: 0; size: 2; row index
  2290. $row = $this->_GetInt2d($recordData, 0);
  2291. // offset: 2; size: 2; col index
  2292. $column = $this->_GetInt2d($recordData, 2);
  2293. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2294. // Read cell?
  2295. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2296. // offset: 20: size: variable; formula structure
  2297. $formulaStructure = substr($recordData, 20);
  2298. // offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
  2299. $options = $this->_GetInt2d($recordData, 14);
  2300. // bit: 0; mask: 0x0001; 1 = recalculate always
  2301. // bit: 1; mask: 0x0002; 1 = calculate on open
  2302. // bit: 2; mask: 0x0008; 1 = part of a shared formula
  2303. $isPartOfSharedFormula = (bool) (0x0008 & $options);
  2304. // WARNING:
  2305. // We can apparently not rely on $isPartOfSharedFormula. Even when $isPartOfSharedFormula = true
  2306. // the formula data may be ordinary formula data, therefore we need to check
  2307. // explicitly for the tExp token (0x01)
  2308. $isPartOfSharedFormula = $isPartOfSharedFormula && ord($formulaStructure{2}) == 0x01;
  2309. if ($isPartOfSharedFormula) {
  2310. // part of shared formula which means there will be a formula with a tExp token and nothing else
  2311. // get the base cell, grab tExp token
  2312. $baseRow = $this->_GetInt2d($formulaStructure, 3);
  2313. $baseCol = $this->_GetInt2d($formulaStructure, 5);
  2314. $this->_baseCell = PHPExcel_Cell::stringFromColumnIndex($baseCol). ($baseRow + 1);
  2315. // formula is added to this cell after the sheet has been read
  2316. $this->_sharedFormulaParts[$columnString . ($row + 1)] = $this->_baseCell;
  2317. }
  2318. // offset: 16: size: 4; not used
  2319. // offset: 4; size: 2; XF index
  2320. $xfIndex = $this->_GetInt2d($recordData, 4);
  2321. // offset: 6; size: 8; result of the formula
  2322. if ( (ord($recordData{6}) == 0)
  2323. && (ord($recordData{12}) == 255)
  2324. && (ord($recordData{13}) == 255) ) {
  2325. // String formula. Result follows in appended STRING record
  2326. $dataType = PHPExcel_Cell_DataType::TYPE_STRING;
  2327. // read possible SHAREDFMLA record
  2328. $code = $this->_GetInt2d($this->_data, $this->_pos);
  2329. if ($code == self::XLS_Type_SHAREDFMLA) {
  2330. $this->_readSharedFmla();
  2331. }
  2332. // read STRING record
  2333. $value = $this->_readString();
  2334. } elseif ((ord($recordData{6}) == 1)
  2335. && (ord($recordData{12}) == 255)
  2336. && (ord($recordData{13}) == 255)) {
  2337. // Boolean formula. Result is in +2; 0=false, 1=true
  2338. $dataType = PHPExcel_Cell_DataType::TYPE_BOOL;
  2339. $value = (bool) ord($recordData{8});
  2340. } elseif ((ord($recordData{6}) == 2)
  2341. && (ord($recordData{12}) == 255)
  2342. && (ord($recordData{13}) == 255)) {
  2343. // Error formula. Error code is in +2
  2344. $dataType = PHPExcel_Cell_DataType::TYPE_ERROR;
  2345. $value = $this->_mapErrorCode(ord($recordData{8}));
  2346. } elseif ((ord($recordData{6}) == 3)
  2347. && (ord($recordData{12}) == 255)
  2348. && (ord($recordData{13}) == 255)) {
  2349. // Formula result is a null string
  2350. $dataType = PHPExcel_Cell_DataType::TYPE_NULL;
  2351. $value = '';
  2352. } else {
  2353. // forumla result is a number, first 14 bytes like _NUMBER record
  2354. $dataType = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  2355. $value = $this->_extractNumber(substr($recordData, 6, 8));
  2356. }
  2357. // add cell style
  2358. if (!$this->_readDataOnly) {
  2359. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2360. }
  2361. // store the formula
  2362. if (!$isPartOfSharedFormula) {
  2363. // not part of shared formula
  2364. // add cell value. If we can read formula, populate with formula, otherwise just used cached value
  2365. try {
  2366. if ($this->_version != self::XLS_BIFF8) {
  2367. throw new Exception('Not BIFF8. Can only read BIFF8 formulas');
  2368. }
  2369. $formula = $this->_getFormulaFromStructure($formulaStructure); // get formula in human language
  2370. $this->_phpSheet->getCell($columnString . ($row + 1))->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
  2371. } catch (Exception $e) {
  2372. $this->_phpSheet->setCellValueExplicit($columnString . ($row + 1), $value, $dataType);
  2373. }
  2374. } else {
  2375. if ($this->_version == self::XLS_BIFF8) {
  2376. // do nothing at this point, formula id added later in the code
  2377. } else {
  2378. $this->_phpSheet->setCellValueExplicit($columnString . ($row + 1), $value, $dataType);
  2379. }
  2380. }
  2381. // store the cached calculated value
  2382. $this->_phpSheet->getCell($columnString . ($row + 1))->setCalculatedValue($value);
  2383. }
  2384. }
  2385. /**
  2386. * Read a SHAREDFMLA record. This function just stores the binary shared formula in the reader,
  2387. * which usually contains relative references.
  2388. * These will be used to construct the formula in each shared formula part after the sheet is read.
  2389. */
  2390. private function _readSharedFmla()
  2391. {
  2392. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2393. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2394. // move stream pointer to next record
  2395. $this->_pos += 4 + $length;
  2396. // offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything
  2397. $cellRange = substr($recordData, 0, 6);
  2398. $cellRange = $this->_readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax
  2399. // offset: 6, size: 1; not used
  2400. // offset: 7, size: 1; number of existing FORMULA records for this shared formula
  2401. $no = ord($recordData{7});
  2402. // offset: 8, size: var; Binary token array of the shared formula
  2403. $formula = substr($recordData, 8);
  2404. // at this point we only store the shared formula for later use
  2405. $this->_sharedFormulas[$this->_baseCell] = $formula;
  2406. }
  2407. /**
  2408. * Read a STRING record from current stream position and advance the stream pointer to next record
  2409. * This record is used for storing result from FORMULA record when it is a string, and
  2410. * it occurs directly after the FORMULA record
  2411. *
  2412. * @return string The string contents as UTF-8
  2413. */
  2414. private function _readString()
  2415. {
  2416. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2417. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2418. // move stream pointer to next record
  2419. $this->_pos += 4 + $length;
  2420. if ($this->_version == self::XLS_BIFF8) {
  2421. $string = $this->_readUnicodeStringLong($recordData);
  2422. $value = $string['value'];
  2423. } else {
  2424. $string = $this->_readByteStringLong($recordData);
  2425. $value = $string['value'];
  2426. }
  2427. return $value;
  2428. }
  2429. /**
  2430. * Read BOOLERR record
  2431. * This record represents a Boolean value or error value
  2432. * cell.
  2433. *
  2434. * -- "OpenOffice.org's Documentation of the Microsoft
  2435. * Excel File Format"
  2436. */
  2437. private function _readBoolErr()
  2438. {
  2439. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2440. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2441. // move stream pointer to next record
  2442. $this->_pos += 4 + $length;
  2443. // offset: 0; size: 2; row index
  2444. $row = $this->_GetInt2d($recordData, 0);
  2445. // offset: 2; size: 2; column index
  2446. $column = $this->_GetInt2d($recordData, 2);
  2447. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2448. // Read cell?
  2449. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2450. // offset: 4; size: 2; index to XF record
  2451. $xfIndex = $this->_GetInt2d($recordData, 4);
  2452. // offset: 6; size: 1; the boolean value or error value
  2453. $boolErr = ord($recordData{6});
  2454. // offset: 7; size: 1; 0=boolean; 1=error
  2455. $isError = ord($recordData{7});
  2456. switch ($isError) {
  2457. case 0: // boolean
  2458. $value = (bool) $boolErr;
  2459. // add cell value
  2460. $this->_phpSheet->getCell($columnString . ($row + 1))->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_BOOL);
  2461. break;
  2462. case 1: // error type
  2463. $value = $this->_mapErrorCode($boolErr);
  2464. // add cell value
  2465. $this->_phpSheet->getCell($columnString . ($row + 1))->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_ERROR);
  2466. break;
  2467. }
  2468. // add cell style
  2469. if (!$this->_readDataOnly) {
  2470. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2471. }
  2472. }
  2473. }
  2474. /**
  2475. * Read MULBLANK record
  2476. * This record represents a cell range of empty cells. All
  2477. * cells are located in the same row
  2478. *
  2479. * -- "OpenOffice.org's Documentation of the Microsoft
  2480. * Excel File Format"
  2481. */
  2482. private function _readMulBlank()
  2483. {
  2484. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2485. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2486. // move stream pointer to next record
  2487. $this->_pos += 4 + $length;
  2488. // offset: 0; size: 2; index to row
  2489. $row = $this->_GetInt2d($recordData, 0);
  2490. // offset: 2; size: 2; index to first column
  2491. $fc = $this->_GetInt2d($recordData, 2);
  2492. // offset: 4; size: 2 x nc; list of indexes to XF records
  2493. // add style information
  2494. if (!$this->_readDataOnly) {
  2495. for ($i = 0; $i < $length / 2 - 3; ++$i) {
  2496. $columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i);
  2497. // Read cell?
  2498. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2499. $xfIndex = $this->_GetInt2d($recordData, 4 + 2 * $i);
  2500. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2501. }
  2502. }
  2503. }
  2504. // offset: 6; size 2; index to last column (not needed)
  2505. }
  2506. /**
  2507. * Read LABEL record
  2508. * This record represents a cell that contains a string. In
  2509. * BIFF8 it is usually replaced by the LABELSST record.
  2510. * Excel still uses this record, if it copies unformatted
  2511. * text cells to the clipboard.
  2512. *
  2513. * -- "OpenOffice.org's Documentation of the Microsoft
  2514. * Excel File Format"
  2515. */
  2516. private function _readLabel()
  2517. {
  2518. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2519. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2520. // move stream pointer to next record
  2521. $this->_pos += 4 + $length;
  2522. // offset: 0; size: 2; index to row
  2523. $row = $this->_GetInt2d($recordData, 0);
  2524. // offset: 2; size: 2; index to column
  2525. $column = $this->_GetInt2d($recordData, 2);
  2526. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2527. // Read cell?
  2528. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2529. // offset: 4; size: 2; XF index
  2530. $xfIndex = $this->_GetInt2d($recordData, 4);
  2531. // add cell value
  2532. // todo: what if string is very long? continue record
  2533. if ($this->_version == self::XLS_BIFF8) {
  2534. $string = $this->_readUnicodeStringLong(substr($recordData, 6));
  2535. $value = $string['value'];
  2536. } else {
  2537. $string = $this->_readByteStringLong(substr($recordData, 6));
  2538. $value = $string['value'];
  2539. }
  2540. $this->_phpSheet->setCellValueExplicit($columnString . ($row + 1), $value, PHPExcel_Cell_DataType::TYPE_STRING);
  2541. // add cell style
  2542. if (!$this->_readDataOnly) {
  2543. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2544. }
  2545. }
  2546. }
  2547. /**
  2548. * Read BLANK record
  2549. */
  2550. private function _readBlank()
  2551. {
  2552. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2553. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2554. // move stream pointer to next record
  2555. $this->_pos += 4 + $length;
  2556. // offset: 0; size: 2; row index
  2557. $row = $this->_GetInt2d($recordData, 0);
  2558. // offset: 2; size: 2; col index
  2559. $col = $this->_GetInt2d($recordData, 2);
  2560. $columnString = PHPExcel_Cell::stringFromColumnIndex($col);
  2561. // Read cell?
  2562. if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2563. // offset: 4; size: 2; XF index
  2564. $xfIndex = $this->_GetInt2d($recordData, 4);
  2565. // add style information
  2566. if (!$this->_readDataOnly) {
  2567. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2568. }
  2569. }
  2570. }
  2571. /**
  2572. * Read MSODRAWING record
  2573. */
  2574. private function _readMsoDrawing()
  2575. {
  2576. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2577. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2578. // move stream pointer to next record
  2579. $this->_pos += 4 + $length;
  2580. $this->_drawingData .= $recordData;
  2581. }
  2582. /**
  2583. * Read OBJ record
  2584. */
  2585. private function _readObj()
  2586. {
  2587. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2588. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2589. // move stream pointer to next record
  2590. $this->_pos += 4 + $length;
  2591. if ($this->_readDataOnly || $this->_version != self::XLS_BIFF8) {
  2592. return;
  2593. }
  2594. // recordData consists of an array of subrecords looking like this:
  2595. // ft: 2 bytes; id number
  2596. // cb: 2 bytes; size in bytes of following data
  2597. // data: var; subrecord data
  2598. // for now, we are just interested in the second subrecord containing the object type
  2599. $ot = $this->_GetInt2d($recordData, 4);
  2600. $this->_objs[] = array(
  2601. 'type' => $ot,
  2602. );
  2603. }
  2604. /**
  2605. * Read WINDOW2 record
  2606. */
  2607. private function _readWindow2()
  2608. {
  2609. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2610. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2611. // move stream pointer to next record
  2612. $this->_pos += 4 + $length;
  2613. // offset: 0; size: 2; option flags
  2614. $options = $this->_GetInt2d($recordData, 0);
  2615. // bit: 1; mask: 0x0002; 0 = do not show gridlines, 1 = show gridlines
  2616. $showGridlines = (bool) ((0x0002 & $options) >> 1);
  2617. $this->_phpSheet->setShowGridlines($showGridlines);
  2618. // bit: 3; mask: 0x0008; 0 = panes are not frozen, 1 = panes are frozen
  2619. $this->_frozen = (bool) ((0x0008 & $options) >> 3);
  2620. // bit: 6; mask: 0x0040; 0 = columns from left to right, 1 = columns from right to left
  2621. $this->_phpSheet->setRightToLeft((bool)((0x0040 & $options) >> 6));
  2622. // bit: 10; mask: 0x0400; 0 = sheet not active, 1 = sheet active
  2623. $isActive = (bool) ((0x0400 & $options) >> 10);
  2624. if ($isActive) {
  2625. $this->_phpExcel->setActiveSheetIndex($this->_phpExcel->getIndex($this->_phpSheet));
  2626. }
  2627. }
  2628. /**
  2629. * Read SCL record
  2630. */
  2631. private function _readScl()
  2632. {
  2633. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2634. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2635. // move stream pointer to next record
  2636. $this->_pos += 4 + $length;
  2637. // offset: 0; size: 2; numerator of the view magnification
  2638. $numerator = $this->_GetInt2d($recordData, 0);
  2639. // offset: 2; size: 2; numerator of the view magnification
  2640. $denumerator = $this->_GetInt2d($recordData, 2);
  2641. // set the zoom scale (in percent)
  2642. $this->_phpSheet->getSheetView()->setZoomScale($numerator * 100 / $denumerator);
  2643. }
  2644. /**
  2645. * Read PANE record
  2646. */
  2647. private function _readPane()
  2648. {
  2649. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2650. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2651. // move stream pointer to next record
  2652. $this->_pos += 4 + $length;
  2653. if (!$this->_readDataOnly) {
  2654. // offset: 0; size: 2; position of vertical split
  2655. $px = $this->_GetInt2d($recordData, 0);
  2656. // offset: 2; size: 2; position of horizontal split
  2657. $py = $this->_GetInt2d($recordData, 2);
  2658. if ($this->_frozen) {
  2659. // frozen panes
  2660. $this->_phpSheet->freezePane(PHPExcel_Cell::stringFromColumnIndex($px) . ($py + 1));
  2661. } else {
  2662. // unfrozen panes; split windows; not supported by PHPExcel core
  2663. }
  2664. }
  2665. }
  2666. /**
  2667. * MERGEDCELLS
  2668. *
  2669. * This record contains the addresses of merged cell ranges
  2670. * in the current sheet.
  2671. *
  2672. * -- "OpenOffice.org's Documentation of the Microsoft
  2673. * Excel File Format"
  2674. */
  2675. private function _readMergedCells()
  2676. {
  2677. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2678. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2679. // move stream pointer to next record
  2680. $this->_pos += 4 + $length;
  2681. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  2682. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($recordData);
  2683. foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress) {
  2684. $this->_phpSheet->mergeCells($cellRangeAddress);
  2685. }
  2686. }
  2687. }
  2688. /**
  2689. * Read HYPERLINK record
  2690. */
  2691. private function _readHyperLink()
  2692. {
  2693. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2694. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2695. // move stream pointer forward to next record
  2696. $this->_pos += 4 + $length;
  2697. if (!$this->_readDataOnly) {
  2698. // offset: 0; size: 8; cell range address of all cells containing this hyperlink
  2699. try {
  2700. $cellRange = $this->_readBIFF8CellRangeAddressFixed($recordData, 0, 8);
  2701. } catch (Exception $e) {
  2702. return;
  2703. }
  2704. // offset: 8, size: 16; GUID of StdLink
  2705. // offset: 24, size: 4; unknown value
  2706. // offset: 28, size: 4; option flags
  2707. // bit: 0; mask: 0x00000001; 0 = no link or extant, 1 = file link or URL
  2708. $isFileLinkOrUrl = (0x00000001 & $this->_GetInt2d($recordData, 28)) >> 0;
  2709. // bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL
  2710. $isAbsPathOrUrl = (0x00000001 & $this->_GetInt2d($recordData, 28)) >> 1;
  2711. // bit: 2 (and 4); mask: 0x00000014; 0 = no description
  2712. $hasDesc = (0x00000014 & $this->_GetInt2d($recordData, 28)) >> 2;
  2713. // bit: 3; mask: 0x00000008; 0 = no text, 1 = has text
  2714. $hasText = (0x00000008 & $this->_GetInt2d($recordData, 28)) >> 3;
  2715. // bit: 7; mask: 0x00000080; 0 = no target frame, 1 = has target frame
  2716. $hasFrame = (0x00000080 & $this->_GetInt2d($recordData, 28)) >> 7;
  2717. // bit: 8; mask: 0x00000100; 0 = file link or URL, 1 = UNC path (inc. server name)
  2718. $isUNC = (0x00000100 & $this->_GetInt2d($recordData, 28)) >> 8;
  2719. // offset within record data
  2720. $offset = 32;
  2721. if ($hasDesc) {
  2722. // offset: 32; size: var; character count of description text
  2723. $dl = $this->_GetInt4d($recordData, 32);
  2724. // offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated
  2725. $desc = $this->_encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false);
  2726. $offset += 4 + 2 * $dl;
  2727. }
  2728. if ($hasFrame) {
  2729. $fl = $this->_GetInt4d($recordData, $offset);
  2730. $offset += 4 + 2 * $fl;
  2731. }
  2732. // detect type of hyperlink (there are 4 types)
  2733. $hyperlinkType = null;
  2734. if ($isUNC) {
  2735. $hyperlinkType = 'UNC';
  2736. } else if (!$isFileLinkOrUrl) {
  2737. $hyperlinkType = 'workbook';
  2738. } else if (ord($recordData{$offset}) == 0x03) {
  2739. $hyperlinkType = 'local';
  2740. } else if (ord($recordData{$offset}) == 0xE0) {
  2741. $hyperlinkType = 'URL';
  2742. }
  2743. switch ($hyperlinkType) {
  2744. case 'URL':
  2745. // section 5.58.2: Hyperlink containing a URL
  2746. // e.g. http://example.org/index.php
  2747. // offset: var; size: 16; GUID of URL Moniker
  2748. $offset += 16;
  2749. // offset: var; size: 4; size (in bytes) of character array of the URL including trailing zero word
  2750. $us = $this->_GetInt4d($recordData, $offset);
  2751. $offset += 4;
  2752. // offset: var; size: $us; character array of the URL, no Unicode string header, always 16-bit characters, zero-terminated
  2753. $url = $this->_encodeUTF16(substr($recordData, $offset, $us - 2), false);
  2754. $url .= $hasText ? '#' : '';
  2755. $offset += $us;
  2756. break;
  2757. case 'local':
  2758. // section 5.58.3: Hyperlink to local file
  2759. // examples:
  2760. // mydoc.txt
  2761. // ../../somedoc.xls#Sheet!A1
  2762. // offset: var; size: 16; GUI of File Moniker
  2763. $offset += 16;
  2764. // offset: var; size: 2; directory up-level count.
  2765. $upLevelCount = $this->_GetInt2d($recordData, $offset);
  2766. $offset += 2;
  2767. // offset: var; size: 4; character count of the shortened file path and name, including trailing zero word
  2768. $sl = $this->_GetInt4d($recordData, $offset);
  2769. $offset += 4;
  2770. // offset: var; size: sl; character array of the shortened file path and name in 8.3-DOS-format (compressed Unicode string)
  2771. $shortenedFilePath = substr($recordData, $offset, $sl);
  2772. $shortenedFilePath = $this->_encodeUTF16($shortenedFilePath, true);
  2773. $shortenedFilePath = substr($shortenedFilePath, 0, -1); // remove trailing zero
  2774. $offset += $sl;
  2775. // offset: var; size: 24; unknown sequence
  2776. $offset += 24;
  2777. // extended file path
  2778. // offset: var; size: 4; size of the following file link field including string lenth mark
  2779. $sz = $this->_GetInt4d($recordData, $offset);
  2780. $offset += 4;
  2781. // only present if $sz > 0
  2782. if ($sz > 0) {
  2783. // offset: var; size: 4; size of the character array of the extended file path and name
  2784. $xl = $this->_GetInt4d($recordData, $offset);
  2785. $offset += 4;
  2786. // offset: var; size 2; unknown
  2787. $offset += 2;
  2788. // offset: var; size $xl; character array of the extended file path and name.
  2789. $extendedFilePath = substr($recordData, $offset, $xl);
  2790. $extendedFilePath = $this->_encodeUTF16($extendedFilePath, false);
  2791. $offset += $xl;
  2792. }
  2793. // construct the path
  2794. $url = str_repeat('..\\', $upLevelCount);
  2795. $url .= ($sz > 0) ?
  2796. $extendedFilePath : $shortenedFilePath; // use extended path if available
  2797. $url .= $hasText ? '#' : '';
  2798. break;
  2799. case 'UNC':
  2800. // section 5.58.4: Hyperlink to a File with UNC (Universal Naming Convention) Path
  2801. // todo: implement
  2802. return;
  2803. case 'workbook':
  2804. // section 5.58.5: Hyperlink to the Current Workbook
  2805. // e.g. Sheet2!B1:C2, stored in text mark field
  2806. $url = 'sheet://';
  2807. break;
  2808. default:
  2809. return;
  2810. }
  2811. if ($hasText) {
  2812. // offset: var; size: 4; character count of text mark including trailing zero word
  2813. $tl = $this->_GetInt4d($recordData, $offset);
  2814. $offset += 4;
  2815. // offset: var; size: var; character array of the text mark without the # sign, no Unicode header, always 16-bit characters, zero-terminated
  2816. $text = $this->_encodeUTF16(substr($recordData, $offset, 2 * ($tl - 1)), false);
  2817. $url .= $text;
  2818. }
  2819. // apply the hyperlink to all the relevant cells
  2820. foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cellRange) as $coordinate) {
  2821. $this->_phpSheet->getCell($coordinate)->getHyperLink()->setUrl($url);
  2822. }
  2823. }
  2824. }
  2825. /**
  2826. * Read SHEETLAYOUT record. Stores sheet tab color information.
  2827. */
  2828. private function _readSheetLayout()
  2829. {
  2830. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2831. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2832. // move stream pointer to next record
  2833. $this->_pos += 4 + $length;
  2834. // local pointer in record data
  2835. $offset = 0;
  2836. if (!$this->_readDataOnly) {
  2837. // offset: 0; size: 2; repeated record identifier 0x0862
  2838. // offset: 2; size: 10; not used
  2839. // offset: 12; size: 4; size of record data
  2840. // Excel 2003 uses size of 0x14 (documented), Excel 2007 uses size of 0x28 (not documented?)
  2841. $sz = $this->_GetInt4d($recordData, 12);
  2842. switch ($sz) {
  2843. case 0x14:
  2844. // offset: 16; size: 2; color index for sheet tab
  2845. $colorIndex = $this->_GetInt2d($recordData, 16);
  2846. $color = $this->_readColor($colorIndex);
  2847. $this->_phpSheet->getTabColor()->setRGB($color['rgb']);
  2848. break;
  2849. case 0x28:
  2850. // TODO: Investigate structure for .xls SHEETLAYOUT record as saved by MS Office Excel 2007
  2851. return;
  2852. break;
  2853. }
  2854. }
  2855. }
  2856. /**
  2857. * Read RANGEPROTECTION record
  2858. * Reading of this record is based on Microsoft Office Excel 97-2000 Binary File Format Specification,
  2859. * where it is referred to as FEAT record
  2860. */
  2861. private function _readRangeProtection()
  2862. {
  2863. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2864. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2865. // move stream pointer to next record
  2866. $this->_pos += 4 + $length;
  2867. // local pointer in record data
  2868. $offset = 0;
  2869. if (!$this->_readDataOnly) {
  2870. $offset += 12;
  2871. // offset: 12; size: 2; shared feature type, 2 = enhanced protection, 4 = smart tag
  2872. $isf = $this->_GetInt2d($recordData, 12);
  2873. if ($isf != 2) {
  2874. // we only read FEAT records of type 2
  2875. return;
  2876. }
  2877. $offset += 2;
  2878. $offset += 5;
  2879. // offset: 19; size: 2; count of ref ranges this feature is on
  2880. $cref = $this->_GetInt2d($recordData, 19);
  2881. $offset += 2;
  2882. $offset += 6;
  2883. // offset: 27; size: 8 * $cref; list of cell ranges (like in hyperlink record)
  2884. $cellRanges = array();
  2885. for ($i = 0; $i < $cref; ++$i) {
  2886. try {
  2887. $cellRange = $this->_readBIFF8CellRangeAddressFixed(substr($recordData, 27 + 8 * $i, 8));
  2888. } catch (Exception $e) {
  2889. return;
  2890. }
  2891. $cellRanges[] = $cellRange;
  2892. $offset += 8;
  2893. }
  2894. // offset: var; size: var; variable length of feature specific data
  2895. $rgbFeat = substr($recordData, $offset);
  2896. $offset += 4;
  2897. // offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit)
  2898. $wPassword = $this->_GetInt4d($recordData, $offset);
  2899. $offset += 4;
  2900. // Apply range protection to sheet
  2901. if ($cellRanges) {
  2902. $this->_phpSheet->protectCells(implode(' ', $cellRanges), strtoupper(dechex($wPassword)), true);
  2903. }
  2904. }
  2905. }
  2906. /**
  2907. * Read IMDATA record
  2908. */
  2909. private function _readImData()
  2910. {
  2911. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2912. // get spliced record data
  2913. $splicedRecordData = $this->_getSplicedRecordData();
  2914. $recordData = $splicedRecordData['recordData'];
  2915. // UNDER CONSTRUCTION
  2916. // offset: 0; size: 2; image format
  2917. $cf = $this->_GetInt2d($recordData, 0);
  2918. // offset: 2; size: 2; environment from which the file was written
  2919. $env = $this->_GetInt2d($recordData, 2);
  2920. // offset: 4; size: 4; length of the image data
  2921. $lcb = $this->_GetInt4d($recordData, 4);
  2922. // offset: 8; size: var; image data
  2923. $iData = substr($recordData, 8);
  2924. switch ($cf) {
  2925. case 0x09: // Windows bitmap format
  2926. // BITMAPCOREINFO
  2927. // 1. BITMAPCOREHEADER
  2928. // offset: 0; size: 4; bcSize, Specifies the number of bytes required by the structure
  2929. $bcSize = $this->_GetInt4d($iData, 0);
  2930. var_dump($bcSize);
  2931. // offset: 4; size: 2; bcWidth, specifies the width of the bitmap, in pixels
  2932. $bcWidth = $this->_GetInt2d($iData, 4);
  2933. var_dump($bcWidth);
  2934. // offset: 6; size: 2; bcHeight, specifies the height of the bitmap, in pixels.
  2935. $bcHeight = $this->_GetInt2d($iData, 6);
  2936. var_dump($bcHeight);
  2937. $ih = imagecreatetruecolor($bcWidth, $bcHeight);
  2938. // offset: 8; size: 2; bcPlanes, specifies the number of planes for the target device. This value must be 1
  2939. // offset: 10; size: 2; bcBitCount specifies the number of bits-per-pixel. This value must be 1, 4, 8, or 24
  2940. $bcBitCount = $this->_GetInt2d($iData, 10);
  2941. var_dump($bcBitCount);
  2942. $rgbString = substr($iData, 12);
  2943. $rgbTriples = array();
  2944. while (strlen($rgbString) > 0) {
  2945. $rgbTriples[] = unpack('Cb/Cg/Cr', $rgbString);
  2946. $rgbString = substr($rgbString, 3);
  2947. }
  2948. $x = 0;
  2949. $y = 0;
  2950. foreach ($rgbTriples as $i => $rgbTriple) {
  2951. $color = imagecolorallocate($ih, $rgbTriple['r'], $rgbTriple['g'], $rgbTriple['b']);
  2952. imagesetpixel($ih, $x, $bcHeight - 1 - $y, $color);
  2953. $x = ($x + 1) % $bcWidth;
  2954. $y = $y + floor(($x + 1) / $bcWidth);
  2955. }
  2956. //imagepng($ih, 'image.png');
  2957. $drawing = new PHPExcel_Worksheet_Drawing();
  2958. $drawing->setPath($filename);
  2959. $drawing->setWorksheet($this->_phpSheet);
  2960. break;
  2961. case 0x02: // Windows metafile or Macintosh PICT format
  2962. case 0x0e: // native format
  2963. default;
  2964. break;
  2965. }
  2966. // _getSplicedRecordData() takes care of moving current position in data stream
  2967. }
  2968. /**
  2969. * Read a free CONTINUE record. Free CONTINUE record may be a camouflaged MSODRAWING record
  2970. * When MSODRAWING data on a sheet exceeds 8224 bytes, CONTINUE records are used instead. Undocumented.
  2971. * In this case, we must treat the CONTINUE record as a MSODRAWING record
  2972. */
  2973. private function _readContinue()
  2974. {
  2975. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  2976. $recordData = substr($this->_data, $this->_pos + 4, $length);
  2977. // move stream pointer to next record
  2978. $this->_pos += 4 + $length;
  2979. // check if we are reading drawing data
  2980. // this is in case a free CONTINUE record occurs in other circumstances we are unaware of
  2981. if ($this->_drawingData == '') {
  2982. return;
  2983. }
  2984. // check if record data is at least 4 bytes long, otherwise there is no chance this is MSODRAWING data
  2985. if (strlen($recordData) < 4) {
  2986. return;
  2987. }
  2988. // dirty check to see if CONTINUE record could be a camouflaged MSODRAWING record
  2989. // look inside CONTINUE record to see if it looks like a part of an Escher stream
  2990. // we know that Escher stream may be split at least at
  2991. // 0xF003 MsofbtSpgrContainer
  2992. // 0xF004 MsofbtSpContainer
  2993. // 0xF00D MsofbtClientTextbox
  2994. $validSplitPoints = array(0xF003, 0xF004, 0xF00D); // add identifiers if we find more
  2995. $splitPoint = $this->_GetInt2d($recordData, 2);
  2996. if (in_array($splitPoint, $validSplitPoints)) {
  2997. $this->_drawingData .= $recordData;
  2998. }
  2999. }
  3000. /**
  3001. * Reads a record from current position in data stream and continues reading data as long as CONTINUE
  3002. * records are found. Splices the record data pieces and returns the combined string as if record data
  3003. * is in one piece.
  3004. * Moves to next current position in data stream to start of next record different from a CONtINUE record
  3005. *
  3006. * @return array
  3007. */
  3008. private function _getSplicedRecordData()
  3009. {
  3010. $data = '';
  3011. $spliceOffsets = array();
  3012. $i = 0;
  3013. $spliceOffsets[0] = 0;
  3014. do {
  3015. ++$i;
  3016. // offset: 0; size: 2; identifier
  3017. $identifier = $this->_GetInt2d($this->_data, $this->_pos);
  3018. // offset: 2; size: 2; length
  3019. $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
  3020. $data .= substr($this->_data, $this->_pos + 4, $length);
  3021. $spliceOffsets[$i] = $spliceOffsets[$i - 1] + $length;
  3022. $this->_pos += 4 + $length;
  3023. $nextIdentifier = $this->_GetInt2d($this->_data, $this->_pos);
  3024. }
  3025. while ($nextIdentifier == self::XLS_Type_CONTINUE);
  3026. $splicedData = array(
  3027. 'recordData' => $data,
  3028. 'spliceOffsets' => $spliceOffsets,
  3029. );
  3030. return $splicedData;
  3031. }
  3032. /**
  3033. * Convert formula structure into human readable Excel formula like 'A3+A5*5'
  3034. *
  3035. * @param string $formulaStructure The complete binary data for the formula
  3036. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  3037. * @return string Human readable formula
  3038. */
  3039. private function _getFormulaFromStructure($formulaStructure, $baseCell = 'A1')
  3040. {
  3041. // offset: 0; size: 2; size of the following formula data
  3042. $sz = $this->_GetInt2d($formulaStructure, 0);
  3043. // offset: 2; size: sz
  3044. $formulaData = substr($formulaStructure, 2, $sz);
  3045. // for debug: dump the formula data
  3046. //echo '<xmp>';
  3047. //echo 'size: ' . $sz . "\n";
  3048. //echo 'the entire formula data: ';
  3049. //Debug::dump($formulaData);
  3050. //echo "\n----\n";
  3051. // offset: 2 + sz; size: variable (optional)
  3052. if (strlen($formulaStructure) > 2 + $sz) {
  3053. $additionalData = substr($formulaStructure, 2 + $sz);
  3054. // for debug: dump the additional data
  3055. //echo 'the entire additional data: ';
  3056. //Debug::dump($additionalData);
  3057. //echo "\n----\n";
  3058. } else {
  3059. $additionalData = '';
  3060. }
  3061. return $this->_getFormulaFromData($formulaData, $additionalData, $baseCell);
  3062. }
  3063. /**
  3064. * Take formula data and additional data for formula and return human readable formula
  3065. *
  3066. * @param string $formulaData The binary data for the formula itself
  3067. * @param string $additionalData Additional binary data going with the formula
  3068. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  3069. * @return string Human readable formula
  3070. */
  3071. private function _getFormulaFromData($formulaData, $additionalData = '', $baseCell = 'A1')
  3072. {
  3073. // start parsing the formula data
  3074. $tokens = array();
  3075. while (strlen($formulaData) > 0 and $token = $this->_getNextToken($formulaData, $baseCell)) {
  3076. $tokens[] = $token;
  3077. $formulaData = substr($formulaData, $token['size']);
  3078. // for debug: dump the token
  3079. //var_dump($token);
  3080. }
  3081. $formulaString = $this->_createFormulaFromTokens($tokens, $additionalData);
  3082. return $formulaString;
  3083. }
  3084. /**
  3085. * Take array of tokens together with additional data for formula and return human readable formula
  3086. *
  3087. * @param array $tokens
  3088. * @param array $additionalData Additional binary data going with the formula
  3089. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  3090. * @return string Human readable formula
  3091. */
  3092. private function _createFormulaFromTokens($tokens, $additionalData)
  3093. {
  3094. // empty formula?
  3095. if (count($tokens) == 0) {
  3096. return '';
  3097. }
  3098. $formulaStrings = array();
  3099. foreach ($tokens as $token) {
  3100. // initialize spaces
  3101. $space0 = isset($space0) ? $space0 : ''; // spaces before next token, not tParen
  3102. $space1 = isset($space1) ? $space1 : ''; // carriage returns before next token, not tParen
  3103. $space2 = isset($space2) ? $space2 : ''; // spaces before opening parenthesis
  3104. $space3 = isset($space3) ? $space3 : ''; // carriage returns before opening parenthesis
  3105. $space4 = isset($space4) ? $space4 : ''; // spaces before closing parenthesis
  3106. $space5 = isset($space5) ? $space5 : ''; // carriage returns before closing parenthesis
  3107. switch ($token['name']) {
  3108. case 'tAdd': // addition
  3109. case 'tConcat': // addition
  3110. case 'tDiv': // division
  3111. case 'tEQ': // equaltiy
  3112. case 'tGE': // greater than or equal
  3113. case 'tGT': // greater than
  3114. case 'tIsect': // intersection
  3115. case 'tLE': // less than or equal
  3116. case 'tList': // less than or equal
  3117. case 'tLT': // less than
  3118. case 'tMul': // multiplication
  3119. case 'tNE': // multiplication
  3120. case 'tPower': // power
  3121. case 'tRange': // range
  3122. case 'tSub': // subtraction
  3123. $op2 = array_pop($formulaStrings);
  3124. $op1 = array_pop($formulaStrings);
  3125. $formulaStrings[] = "$op1$space1$space0{$token['data']}$op2";
  3126. unset($space0, $space1);
  3127. break;
  3128. case 'tUplus': // unary plus
  3129. case 'tUminus': // unary minus
  3130. $op = array_pop($formulaStrings);
  3131. $formulaStrings[] = "$space1$space0{$token['data']}$op";
  3132. unset($space0, $space1);
  3133. break;
  3134. case 'tPercent': // percent sign
  3135. $op = array_pop($formulaStrings);
  3136. $formulaStrings[] = "$op$space1$space0{$token['data']}";
  3137. unset($space0, $space1);
  3138. break;
  3139. case 'tAttrVolatile': // indicates volatile function
  3140. case 'tAttrIf':
  3141. case 'tAttrSkip':
  3142. case 'tAttrChoose':
  3143. // token is only important for Excel formula evaluator
  3144. // do nothing
  3145. break;
  3146. case 'tAttrSpace': // space / carriage return
  3147. // space will be used when next token arrives, do not alter formulaString stack
  3148. switch ($token['data']['spacetype']) {
  3149. case 'type0':
  3150. $space0 = str_repeat(' ', $token['data']['spacecount']);
  3151. break;
  3152. case 'type1':
  3153. $space1 = str_repeat("\n", $token['data']['spacecount']);
  3154. break;
  3155. case 'type2':
  3156. $space2 = str_repeat(' ', $token['data']['spacecount']);
  3157. break;
  3158. case 'type3':
  3159. $space3 = str_repeat("\n", $token['data']['spacecount']);
  3160. break;
  3161. case 'type4':
  3162. $space4 = str_repeat(' ', $token['data']['spacecount']);
  3163. break;
  3164. case 'type5':
  3165. $space5 = str_repeat("\n", $token['data']['spacecount']);
  3166. break;
  3167. }
  3168. break;
  3169. case 'tAttrSum': // SUM function with one parameter
  3170. $op = array_pop($formulaStrings);
  3171. $formulaStrings[] = "{$space1}{$space0}SUM($op)";
  3172. unset($space0, $space1);
  3173. break;
  3174. case 'tFunc': // function with fixed number of arguments
  3175. case 'tFuncV': // function with variable number of arguments
  3176. $ops = array(); // array of operators
  3177. for ($i = 0; $i < $token['data']['args']; ++$i) {
  3178. $ops[] = array_pop($formulaStrings);
  3179. }
  3180. $ops = array_reverse($ops);
  3181. $formulaStrings[] = "$space1$space0{$token['data']['function']}(" . implode(',', $ops) . ")";
  3182. unset($space0, $space1);
  3183. break;
  3184. case 'tParen': // parenthesis
  3185. $expression = array_pop($formulaStrings);
  3186. $formulaStrings[] = "$space3$space2($expression$space5$space4)";
  3187. unset($space2, $space3, $space4, $space5);
  3188. break;
  3189. case 'tArray': // array constant
  3190. $constantArray = $this->_readBIFF8ConstantArray($additionalData);
  3191. $formulaStrings[] = $space1 . $space0 . $constantArray['value'];
  3192. $additionalData = substr($additionalData, $constantArray['size']); // bite of chunk of additional data
  3193. unset($space0, $space1);
  3194. break;
  3195. case 'tMemArea':
  3196. // bite off chunk of additional data
  3197. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($additionalData);
  3198. $additionalData = substr($additionalData, $cellRangeAddressList['size']);
  3199. $formulaStrings[] = "$space1$space0{$token['data']}";
  3200. unset($space0, $space1);
  3201. break;
  3202. case 'tArea': // cell range address
  3203. case 'tBool': // boolean
  3204. case 'tErr': // error code
  3205. case 'tInt': // integer
  3206. case 'tMemErr':
  3207. case 'tMemFunc':
  3208. case 'tMissArg':
  3209. case 'tName':
  3210. case 'tNum': // number
  3211. case 'tRef': // single cell reference
  3212. case 'tRef3d': // 3d cell reference
  3213. case 'tArea3d': // 3d cell range reference
  3214. case 'tRefN':
  3215. case 'tAreaN':
  3216. case 'tStr': // string
  3217. $formulaStrings[] = "$space1$space0{$token['data']}";
  3218. unset($space0, $space1);
  3219. break;
  3220. }
  3221. }
  3222. $formulaString = $formulaStrings[0];
  3223. // for debug: dump the human readable formula
  3224. //echo '----' . "\n";
  3225. //echo 'Formula: ' . $formulaString;
  3226. return $formulaString;
  3227. }
  3228. /**
  3229. * Fetch next token from binary formula data
  3230. *
  3231. * @param string Formula data
  3232. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  3233. * @return array
  3234. * @throws Exception
  3235. */
  3236. private function _getNextToken($formulaData, $baseCell = 'A1')
  3237. {
  3238. // offset: 0; size: 1; token id
  3239. $id = ord($formulaData[0]); // token id
  3240. $name = false; // initialize token name
  3241. switch ($id) {
  3242. case 0x03: $name = 'tAdd'; $size = 1; $data = '+'; break;
  3243. case 0x04: $name = 'tSub'; $size = 1; $data = '-'; break;
  3244. case 0x05: $name = 'tMul'; $size = 1; $data = '*'; break;
  3245. case 0x06: $name = 'tDiv'; $size = 1; $data = '/'; break;
  3246. case 0x07: $name = 'tPower'; $size = 1; $data = '^'; break;
  3247. case 0x08: $name = 'tConcat'; $size = 1; $data = '&'; break;
  3248. case 0x09: $name = 'tLT'; $size = 1; $data = '<'; break;
  3249. case 0x0A: $name = 'tLE'; $size = 1; $data = '<='; break;
  3250. case 0x0B: $name = 'tEQ'; $size = 1; $data = '='; break;
  3251. case 0x0C: $name = 'tGE'; $size = 1; $data = '>='; break;
  3252. case 0x0D: $name = 'tGT'; $size = 1; $data = '>'; break;
  3253. case 0x0E: $name = 'tNE'; $size = 1; $data = '<>'; break;
  3254. case 0x0F: $name = 'tIsect'; $size = 1; $data = ' '; break;
  3255. case 0x10: $name = 'tList'; $size = 1; $data = ','; break;
  3256. case 0x11: $name = 'tRange'; $size = 1; $data = ':'; break;
  3257. case 0x12: $name = 'tUplus'; $size = 1; $data = '+'; break;
  3258. case 0x13: $name = 'tUminus'; $size = 1; $data = '-'; break;
  3259. case 0x14: $name = 'tPercent'; $size = 1; $data = '%'; break;
  3260. case 0x15: // parenthesis
  3261. $name = 'tParen';
  3262. $size = 1;
  3263. $data = null;
  3264. break;
  3265. case 0x16: // missing argument
  3266. $name = 'tMissArg';
  3267. $size = 1;
  3268. $data = '';
  3269. break;
  3270. case 0x17: // string
  3271. $name = 'tStr';
  3272. // offset: 1; size: var; Unicode string, 8-bit string length
  3273. $string = $this->_readUnicodeStringShort(substr($formulaData, 1));
  3274. $size = 1 + $string['size'];
  3275. $data = $this->_UTF8toExcelDoubleQuoted($string['value']);
  3276. break;
  3277. case 0x19: // Special attribute
  3278. // offset: 1; size: 1; attribute type flags:
  3279. switch (ord($formulaData[1])) {
  3280. case 0x01:
  3281. $name = 'tAttrVolatile';
  3282. $size = 4;
  3283. $data = null;
  3284. break;
  3285. case 0x02:
  3286. $name = 'tAttrIf';
  3287. $size = 4;
  3288. $data = null;
  3289. break;
  3290. case 0x04:
  3291. $name = 'tAttrChoose';
  3292. // offset: 2; size: 2; number of choices in the CHOOSE function ($nc, number of parameters decreased by 1)
  3293. $nc = $this->_GetInt2d($formulaData, 2);
  3294. // offset: 4; size: 2 * $nc
  3295. // offset: 4 + 2 * $nc; size: 2
  3296. $size = 2 * $nc + 6;
  3297. $data = null;
  3298. break;
  3299. case 0x08:
  3300. $name = 'tAttrSkip';
  3301. $size = 4;
  3302. $data = null;
  3303. break;
  3304. case 0x10:
  3305. $name = 'tAttrSum';
  3306. $size = 4;
  3307. $data = null;
  3308. break;
  3309. case 0x40:
  3310. case 0x41:
  3311. $name = 'tAttrSpace';
  3312. $size = 4;
  3313. // offset: 2; size: 2; space type and position
  3314. switch (ord($formulaData[2])) {
  3315. case 0x00:
  3316. $spacetype = 'type0';
  3317. break;
  3318. case 0x01:
  3319. $spacetype = 'type1';
  3320. break;
  3321. case 0x02:
  3322. $spacetype = 'type2';
  3323. break;
  3324. case 0x03:
  3325. $spacetype = 'type3';
  3326. break;
  3327. case 0x04:
  3328. $spacetype = 'type4';
  3329. break;
  3330. case 0x05:
  3331. $spacetype = 'type5';
  3332. break;
  3333. default:
  3334. throw new Exception('Unrecognized space type in tAttrSpace token');
  3335. break;
  3336. }
  3337. // offset: 3; size: 1; number of inserted spaces/carriage returns
  3338. $spacecount = ord($formulaData[3]);
  3339. $data = array('spacetype' => $spacetype, 'spacecount' => $spacecount);
  3340. break;
  3341. default:
  3342. throw new Exception('Unrecognized attribute flag in tAttr token');
  3343. break;
  3344. }
  3345. break;
  3346. case 0x1C: // error code
  3347. // offset: 1; size: 1; error code
  3348. $name = 'tErr';
  3349. $size = 2;
  3350. $data = $this->_mapErrorCode(ord($formulaData[1]));
  3351. break;
  3352. case 0x1D: // boolean
  3353. // offset: 1; size: 1; 0 = false, 1 = true;
  3354. $name = 'tBool';
  3355. $size = 2;
  3356. $data = ord($formulaData[1]) ? 'TRUE' : 'FALSE';
  3357. break;
  3358. case 0x1E: // integer
  3359. // offset: 1; size: 2; unsigned 16-bit integer
  3360. $name = 'tInt';
  3361. $size = 3;
  3362. $data = $this->_GetInt2d($formulaData, 1);
  3363. break;
  3364. case 0x1F: // number
  3365. // offset: 1; size: 8;
  3366. $name = 'tNum';
  3367. $size = 9;
  3368. $data = $this->_extractNumber(substr($formulaData, 1));
  3369. $data = str_replace(',', '.', (string)$data); // in case non-English locale
  3370. break;
  3371. case 0x40: // array constant
  3372. case 0x60: // array constant
  3373. // offset: 1; size: 7; not used
  3374. $name = 'tArray';
  3375. $size = 8;
  3376. $data = null;
  3377. break;
  3378. case 0x41: // function with fixed number of arguments
  3379. $name = 'tFunc';
  3380. $size = 3;
  3381. // offset: 1; size: 2; index to built-in sheet function
  3382. switch ($this->_GetInt2d($formulaData, 1)) {
  3383. case 2: $function = 'ISNA'; $args = 1; break;
  3384. case 3: $function = 'ISERROR'; $args = 1; break;
  3385. case 10: $function = 'NA'; $args = 0; break;
  3386. case 15: $function = 'SIN'; $args = 1; break;
  3387. case 16: $function = 'COS'; $args = 1; break;
  3388. case 17: $function = 'TAN'; $args = 1; break;
  3389. case 18: $function = 'ATAN'; $args = 1; break;
  3390. case 19: $function = 'PI'; $args = 0; break;
  3391. case 20: $function = 'SQRT'; $args = 1; break;
  3392. case 21: $function = 'EXP'; $args = 1; break;
  3393. case 22: $function = 'LN'; $args = 1; break;
  3394. case 23: $function = 'LOG10'; $args = 1; break;
  3395. case 24: $function = 'ABS'; $args = 1; break;
  3396. case 25: $function = 'INT'; $args = 1; break;
  3397. case 26: $function = 'SIGN'; $args = 1; break;
  3398. case 27: $function = 'ROUND'; $args = 2; break;
  3399. case 30: $function = 'REPT'; $args = 2; break;
  3400. case 31: $function = 'MID'; $args = 3; break;
  3401. case 32: $function = 'LEN'; $args = 1; break;
  3402. case 33: $function = 'VALUE'; $args = 1; break;
  3403. case 34: $function = 'TRUE'; $args = 0; break;
  3404. case 35: $function = 'FALSE'; $args = 0; break;
  3405. case 38: $function = 'NOT'; $args = 1; break;
  3406. case 39: $function = 'MOD'; $args = 2; break;
  3407. case 40: $function = 'DCOUNT'; $args = 3; break;
  3408. case 41: $function = 'DSUM'; $args = 3; break;
  3409. case 42: $function = 'DAVERAGE'; $args = 3; break;
  3410. case 43: $function = 'DMIN'; $args = 3; break;
  3411. case 44: $function = 'DMAX'; $args = 3; break;
  3412. case 45: $function = 'DSTDEV'; $args = 3; break;
  3413. case 48: $function = 'TEXT'; $args = 2; break;
  3414. case 61: $function = 'MIRR'; $args = 3; break;
  3415. case 63: $function = 'RAND'; $args = 0; break;
  3416. case 65: $function = 'DATE'; $args = 3; break;
  3417. case 66: $function = 'TIME'; $args = 3; break;
  3418. case 67: $function = 'DAY'; $args = 1; break;
  3419. case 68: $function = 'MONTH'; $args = 1; break;
  3420. case 69: $function = 'YEAR'; $args = 1; break;
  3421. case 71: $function = 'HOUR'; $args = 1; break;
  3422. case 72: $function = 'MINUTE'; $args = 1; break;
  3423. case 73: $function = 'SECOND'; $args = 1; break;
  3424. case 74: $function = 'NOW'; $args = 0; break;
  3425. case 75: $function = 'AREAS'; $args = 1; break;
  3426. case 76: $function = 'ROWS'; $args = 1; break;
  3427. case 77: $function = 'COLUMNS'; $args = 1; break;
  3428. case 83: $function = 'TRANSPOSE'; $args = 1; break;
  3429. case 86: $function = 'TYPE'; $args = 1; break;
  3430. case 97: $function = 'ATAN2'; $args = 2; break;
  3431. case 98: $function = 'ASIN'; $args = 1; break;
  3432. case 99: $function = 'ACOS'; $args = 1; break;
  3433. case 105: $function = 'ISREF'; $args = 1; break;
  3434. case 111: $function = 'CHAR'; $args = 1; break;
  3435. case 112: $function = 'LOWER'; $args = 1; break;
  3436. case 113: $function = 'UPPER'; $args = 1; break;
  3437. case 114: $function = 'PROPER'; $args = 1; break;
  3438. case 117: $function = 'EXACT'; $args = 2; break;
  3439. case 118: $function = 'TRIM'; $args = 1; break;
  3440. case 119: $function = 'REPLACE'; $args = 4; break;
  3441. case 121: $function = 'CODE'; $args = 1; break;
  3442. case 126: $function = 'ISERR'; $args = 1; break;
  3443. case 127: $function = 'ISTEXT'; $args = 1; break;
  3444. case 128: $function = 'ISNUMBER'; $args = 1; break;
  3445. case 129: $function = 'ISBLANK'; $args = 1; break;
  3446. case 130: $function = 'T'; $args = 1; break;
  3447. case 131: $function = 'N'; $args = 1; break;
  3448. case 140: $function = 'DATEVALUE'; $args = 1; break;
  3449. case 141: $function = 'TIMEVALUE'; $args = 1; break;
  3450. case 142: $function = 'SLN'; $args = 3; break;
  3451. case 143: $function = 'SYD'; $args = 4; break;
  3452. case 162: $function = 'CLEAN'; $args = 1; break;
  3453. case 163: $function = 'MDETERM'; $args = 1; break;
  3454. case 164: $function = 'MINVERSE'; $args = 1; break;
  3455. case 165: $function = 'MMULT'; $args = 2; break;
  3456. case 184: $function = 'FACT'; $args = 1; break;
  3457. case 189: $function = 'DPRODUCT'; $args = 3; break;
  3458. case 190: $function = 'ISNONTEXT'; $args = 1; break;
  3459. case 195: $function = 'DSTDEVP'; $args = 3; break;
  3460. case 196: $function = 'DVARP'; $args = 3; break;
  3461. case 198: $function = 'ISLOGICAL'; $args = 1; break;
  3462. case 199: $function = 'DCOUNTA'; $args = 3; break;
  3463. case 207: $function = 'REPLACEB'; $args = 4; break;
  3464. case 210: $function = 'MIDB'; $args = 3; break;
  3465. case 211: $function = 'LENB'; $args = 1; break;
  3466. case 212: $function = 'ROUNDUP'; $args = 2; break;
  3467. case 213: $function = 'ROUNDDOWN'; $args = 2; break;
  3468. case 214: $function = 'ASC'; $args = 1; break;
  3469. case 215: $function = 'DBCS'; $args = 1; break;
  3470. case 221: $function = 'TODAY'; $args = 0; break;
  3471. case 229: $function = 'SINH'; $args = 1; break;
  3472. case 230: $function = 'COSH'; $args = 1; break;
  3473. case 231: $function = 'TANH'; $args = 1; break;
  3474. case 232: $function = 'ASINH'; $args = 1; break;
  3475. case 233: $function = 'ACOSH'; $args = 1; break;
  3476. case 234: $function = 'ATANH'; $args = 1; break;
  3477. case 235: $function = 'DGET'; $args = 3; break;
  3478. case 244: $function = 'INFO'; $args = 1; break;
  3479. case 252: $function = 'FREQUENCY'; $args = 2; break;
  3480. case 261: $function = 'ERROR.TYPE'; $args = 1; break;
  3481. case 271: $function = 'GAMMALN'; $args = 1; break;
  3482. case 273: $function = 'BINOMDIST'; $args = 4; break;
  3483. case 274: $function = 'CHIDIST'; $args = 2; break;
  3484. case 275: $function = 'CHIINV'; $args = 2; break;
  3485. case 276: $function = 'COMBIN'; $args = 2; break;
  3486. case 277: $function = 'CONFIDENCE'; $args = 3; break;
  3487. case 278: $function = 'CRITBINOM'; $args = 3; break;
  3488. case 279: $function = 'EVEN'; $args = 1; break;
  3489. case 280: $function = 'EXPONDIST'; $args = 3; break;
  3490. case 281: $function = 'FDIST'; $args = 3; break;
  3491. case 282: $function = 'FINV'; $args = 3; break;
  3492. case 283: $function = 'FISHER'; $args = 1; break;
  3493. case 284: $function = 'FISHERINV'; $args = 1; break;
  3494. case 285: $function = 'FLOOR'; $args = 2; break;
  3495. case 286: $function = 'GAMMADIST'; $args = 4; break;
  3496. case 287: $function = 'GAMMAINV'; $args = 3; break;
  3497. case 288: $function = 'CEILING'; $args = 2; break;
  3498. case 289: $function = 'HYPGEOMDIST'; $args = 4; break;
  3499. case 290: $function = 'LOGNORMDIST'; $args = 3; break;
  3500. case 291: $function = 'LOGINV'; $args = 3; break;
  3501. case 292: $function = 'NEGBINOMDIST'; $args = 3; break;
  3502. case 293: $function = 'NORMDIST'; $args = 4; break;
  3503. case 294: $function = 'NORMSDIST'; $args = 1; break;
  3504. case 295: $function = 'NORMINV'; $args = 3; break;
  3505. case 296: $function = 'NORMSINV'; $args = 1; break;
  3506. case 297: $function = 'STANDARDIZE'; $args = 3; break;
  3507. case 298: $function = 'ODD'; $args = 1; break;
  3508. case 299: $function = 'PERMUT'; $args = 2; break;
  3509. case 300: $function = 'POISSON'; $args = 3; break;
  3510. case 301: $function = 'TDIST'; $args = 3; break;
  3511. case 302: $function = 'WEIBULL'; $args = 4; break;
  3512. case 303: $function = 'SUMXMY2'; $args = 2; break;
  3513. case 304: $function = 'SUMX2MY2'; $args = 2; break;
  3514. case 305: $function = 'SUMX2PY2'; $args = 2; break;
  3515. case 306: $function = 'CHITEST'; $args = 2; break;
  3516. case 307: $function = 'CORREL'; $args = 2; break;
  3517. case 308: $function = 'COVAR'; $args = 2; break;
  3518. case 309: $function = 'FORECAST'; $args = 3; break;
  3519. case 310: $function = 'FTEST'; $args = 2; break;
  3520. case 311: $function = 'INTERCEPT'; $args = 2; break;
  3521. case 312: $function = 'PEARSON'; $args = 2; break;
  3522. case 313: $function = 'RSQ'; $args = 2; break;
  3523. case 314: $function = 'STEYX'; $args = 2; break;
  3524. case 315: $function = 'SLOPE'; $args = 2; break;
  3525. case 316: $function = 'TTEST'; $args = 4; break;
  3526. case 325: $function = 'LARGE'; $args = 2; break;
  3527. case 326: $function = 'SMALL'; $args = 2; break;
  3528. case 327: $function = 'QUARTILE'; $args = 2; break;
  3529. case 328: $function = 'PERCENTILE'; $args = 2; break;
  3530. case 331: $function = 'TRIMMEAN'; $args = 2; break;
  3531. case 332: $function = 'TINV'; $args = 2; break;
  3532. case 337: $function = 'POWER'; $args = 2; break;
  3533. case 342: $function = 'RADIANS'; $args = 1; break;
  3534. case 343: $function = 'DEGREES'; $args = 1; break;
  3535. case 346: $function = 'COUNTIF'; $args = 2; break;
  3536. case 347: $function = 'COUNTBLANK'; $args = 1; break;
  3537. case 350: $function = 'ISPMT'; $args = 4; break;
  3538. case 351: $function = 'DATEDIF'; $args = 3; break;
  3539. case 352: $function = 'DATESTRING'; $args = 1; break;
  3540. case 353: $function = 'NUMBERSTRING'; $args = 2; break;
  3541. case 360: $function = 'PHONETIC'; $args = 1; break;
  3542. default:
  3543. throw new Exception('Unrecognized function in formula');
  3544. break;
  3545. }
  3546. $data = array('function' => $function, 'args' => $args);
  3547. break;
  3548. case 0x22: // function with variable number of arguments
  3549. case 0x42: // function with variable number of arguments
  3550. case 0x62: // function with variable number of arguments
  3551. $name = 'tFuncV';
  3552. $size = 4;
  3553. // offset: 1; size: 1; number of arguments
  3554. $args = ord($formulaData[1]);
  3555. // offset: 2: size: 2; index to built-in sheet function
  3556. switch ($this->_GetInt2d($formulaData, 2)) {
  3557. case 0: $function = 'COUNT'; break;
  3558. case 1: $function = 'IF'; break;
  3559. case 4: $function = 'SUM'; break;
  3560. case 5: $function = 'AVERAGE'; break;
  3561. case 6: $function = 'MIN'; break;
  3562. case 7: $function = 'MAX'; break;
  3563. case 8: $function = 'ROW'; break;
  3564. case 9: $function = 'COLUMN'; break;
  3565. case 11: $function = 'NPV'; break;
  3566. case 12: $function = 'STDEV'; break;
  3567. case 13: $function = 'DOLLAR'; break;
  3568. case 14: $function = 'FIXED'; break;
  3569. case 28: $function = 'LOOKUP'; break;
  3570. case 29: $function = 'INDEX'; break;
  3571. case 36: $function = 'AND'; break;
  3572. case 37: $function = 'OR'; break;
  3573. case 46: $function = 'VAR'; break;
  3574. case 49: $function = 'LINEST'; break;
  3575. case 50: $function = 'TREND'; break;
  3576. case 51: $function = 'LOGEST'; break;
  3577. case 52: $function = 'GROWTH'; break;
  3578. case 56: $function = 'PV'; break;
  3579. case 57: $function = 'FV'; break;
  3580. case 58: $function = 'NPER'; break;
  3581. case 59: $function = 'PMT'; break;
  3582. case 60: $function = 'RATE'; break;
  3583. case 62: $function = 'IRR'; break;
  3584. case 64: $function = 'MATCH'; break;
  3585. case 70: $function = 'WEEKDAY'; break;
  3586. case 78: $function = 'OFFSET'; break;
  3587. case 82: $function = 'SEARCH'; break;
  3588. case 100: $function = 'CHOOSE'; break;
  3589. case 101: $function = 'HLOOKUP'; break;
  3590. case 102: $function = 'VLOOKUP'; break;
  3591. case 109: $function = 'LOG'; break;
  3592. case 115: $function = 'LEFT'; break;
  3593. case 116: $function = 'RIGHT'; break;
  3594. case 120: $function = 'SUBSTITUTE'; break;
  3595. case 124: $function = 'FIND'; break;
  3596. case 125: $function = 'CELL'; break;
  3597. case 144: $function = 'DDB'; break;
  3598. case 148: $function = 'INDIRECT'; break;
  3599. case 167: $function = 'IPMT'; break;
  3600. case 168: $function = 'PPMT'; break;
  3601. case 169: $function = 'COUNTA'; break;
  3602. case 183: $function = 'PRODUCT'; break;
  3603. case 193: $function = 'STDEVP'; break;
  3604. case 194: $function = 'VARP'; break;
  3605. case 197: $function = 'TRUNC'; break;
  3606. case 204: $function = 'USDOLLAR'; break;
  3607. case 205: $function = 'FINDB'; break;
  3608. case 206: $function = 'SEARCHB'; break;
  3609. case 208: $function = 'LEFTB'; break;
  3610. case 209: $function = 'RIGHTB'; break;
  3611. case 216: $function = 'RANK'; break;
  3612. case 219: $function = 'ADDRESS'; break;
  3613. case 220: $function = 'DAYS360'; break;
  3614. case 222: $function = 'VDB'; break;
  3615. case 227: $function = 'MEDIAN'; break;
  3616. case 228: $function = 'SUMPRODUCT'; break;
  3617. case 247: $function = 'DB'; break;
  3618. case 269: $function = 'AVEDEV'; break;
  3619. case 270: $function = 'BETADIST'; break;
  3620. case 272: $function = 'BETAINV'; break;
  3621. case 317: $function = 'PROB'; break;
  3622. case 318: $function = 'DEVSQ'; break;
  3623. case 319: $function = 'GEOMEAN'; break;
  3624. case 320: $function = 'HARMEAN'; break;
  3625. case 321: $function = 'SUMSQ'; break;
  3626. case 322: $function = 'KURT'; break;
  3627. case 323: $function = 'SKEW'; break;
  3628. case 324: $function = 'ZTEST'; break;
  3629. case 329: $function = 'PERCENTRANK'; break;
  3630. case 330: $function = 'MODE'; break;
  3631. case 336: $function = 'CONCATENATE'; break;
  3632. case 344: $function = 'SUBTOTAL'; break;
  3633. case 345: $function = 'SUMIF'; break;
  3634. case 354: $function = 'ROMAN'; break;
  3635. case 358: $function = 'GETPIVOTDATA'; break;
  3636. case 359: $function = 'HYPERLINK'; break;
  3637. case 361: $function = 'AVERAGEA'; break;
  3638. case 362: $function = 'MAXA'; break;
  3639. case 363: $function = 'MINA'; break;
  3640. case 364: $function = 'STDEVPA'; break;
  3641. case 365: $function = 'VARPA'; break;
  3642. case 366: $function = 'STDEVA'; break;
  3643. case 367: $function = 'VARA'; break;
  3644. default:
  3645. throw new Exception('Unrecognized function in formula');
  3646. break;
  3647. }
  3648. $data = array('function' => $function, 'args' => $args);
  3649. break;
  3650. case 0x23: // index to defined name
  3651. case 0x43:
  3652. $name = 'tName';
  3653. $size = 5;
  3654. // offset: 1; size: 2; one-based index to definedname record
  3655. $definedNameIndex = $this->_GetInt2d($formulaData, 1) - 1;
  3656. // offset: 2; size: 2; not used
  3657. $data = $this->_definedname[$definedNameIndex]['name'];
  3658. break;
  3659. case 0x24: // single cell reference e.g. A5
  3660. case 0x44:
  3661. case 0x64:
  3662. $name = 'tRef';
  3663. $size = 5;
  3664. $data = $this->_readBIFF8CellAddress(substr($formulaData, 1, 4));
  3665. break;
  3666. case 0x25: // cell range reference to cells in the same sheet
  3667. case 0x45:
  3668. case 0x65:
  3669. $name = 'tArea';
  3670. $size = 9;
  3671. $data = $this->_readBIFF8CellRangeAddress(substr($formulaData, 1, 8));
  3672. break;
  3673. case 0x26:
  3674. case 0x46:
  3675. $name = 'tMemArea';
  3676. // offset: 1; size: 4; not used
  3677. // offset: 5; size: 2; size of the following subexpression
  3678. $subSize = $this->_GetInt2d($formulaData, 5);
  3679. $size = 7 + $subSize;
  3680. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  3681. break;
  3682. case 0x47:
  3683. $name = 'tMemErr';
  3684. // offset: 1; size: 4; not used
  3685. // offset: 5; size: 2; size of the following subexpression
  3686. $subSize = $this->_GetInt2d($formulaData, 5);
  3687. $size = 7 + $subSize;
  3688. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  3689. break;
  3690. case 0x29:
  3691. case 0x49:
  3692. $name = 'tMemFunc';
  3693. // offset: 1; size: 2; size of the following subexpression
  3694. $subSize = $this->_GetInt2d($formulaData, 1);
  3695. $size = 3 + $subSize;
  3696. $data = $this->_getFormulaFromData(substr($formulaData, 3, $subSize));
  3697. break;
  3698. case 0x2C: // Relative reference, used in shared formulas and some other places
  3699. case 0x4C:
  3700. case 0x6C:
  3701. $name = 'tRefN';
  3702. $size = 5;
  3703. $data = $this->_readBIFF8CellAddressB(substr($formulaData, 1, 4), $baseCell);
  3704. break;
  3705. case 0x2D:
  3706. case 0x4D:
  3707. case 0x6D:
  3708. $name = 'tAreaN';
  3709. $size = 9;
  3710. $data = $this->_readBIFF8CellRangeAddressB(substr($formulaData, 1, 8), $baseCell);
  3711. break;
  3712. case 0x3A: // 3d reference to cell
  3713. case 0x5A:
  3714. $name = 'tRef3d';
  3715. $size = 7;
  3716. // offset: 1; size: 2; index to REF entry
  3717. $sheetRange = $this->_readSheetRangeByRefIndex($this->_GetInt2d($formulaData, 1));
  3718. // offset: 3; size: 4; cell address
  3719. $cellAddress = $this->_readBIFF8CellAddress(substr($formulaData, 3, 4));
  3720. $data = "$sheetRange!$cellAddress";
  3721. break;
  3722. case 0x3B: // 3d reference to cell range
  3723. case 0x5B:
  3724. $name = 'tArea3d';
  3725. $size = 11;
  3726. // offset: 1; size: 2; index to REF entry
  3727. $sheetRange = $this->_readSheetRangeByRefIndex($this->_GetInt2d($formulaData, 1));
  3728. // offset: 3; size: 8; cell address
  3729. $cellRangeAddress = $this->_readBIFF8CellRangeAddress(substr($formulaData, 3, 8));
  3730. $data = "$sheetRange!$cellRangeAddress";
  3731. break;
  3732. // case 0x39: // don't know how to deal with
  3733. default:
  3734. throw new Exception('Unrecognized token ' . sprintf('%02X', $id) . ' in formula');
  3735. break;
  3736. }
  3737. return array(
  3738. 'id' => $id,
  3739. 'name' => $name,
  3740. 'size' => $size,
  3741. 'data' => $data,
  3742. );
  3743. }
  3744. /**
  3745. * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
  3746. * section 3.3.4
  3747. *
  3748. * @param string $cellAddressStructure
  3749. * @return string
  3750. */
  3751. private function _readBIFF8CellAddress($cellAddressStructure)
  3752. {
  3753. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  3754. $row = $this->_GetInt2d($cellAddressStructure, 0) + 1;
  3755. // offset: 2; size: 2; index to column or column offset + relative flags
  3756. // bit: 7-0; mask 0x00FF; column index
  3757. $column = PHPExcel_Cell::stringFromColumnIndex(0x00FF & $this->_GetInt2d($cellAddressStructure, 2));
  3758. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  3759. if (!(0x4000 & $this->_GetInt2d($cellAddressStructure, 2))) {
  3760. $column = '$' . $column;
  3761. }
  3762. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  3763. if (!(0x8000 & $this->_GetInt2d($cellAddressStructure, 2))) {
  3764. $row = '$' . $row;
  3765. }
  3766. return $column . $row;
  3767. }
  3768. /**
  3769. * Reads a cell address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  3770. * to indicate offsets from a base cell
  3771. * section 3.3.4
  3772. *
  3773. * @param string $cellAddressStructure
  3774. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  3775. * @return string
  3776. */
  3777. private function _readBIFF8CellAddressB($cellAddressStructure, $baseCell = 'A1')
  3778. {
  3779. list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
  3780. $baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
  3781. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  3782. $rowIndex = $this->_GetInt2d($cellAddressStructure, 0);
  3783. $row = $this->_GetInt2d($cellAddressStructure, 0) + 1;
  3784. // offset: 2; size: 2; index to column or column offset + relative flags
  3785. // bit: 7-0; mask 0x00FF; column index
  3786. $colIndex = 0x00FF & $this->_GetInt2d($cellAddressStructure, 2);
  3787. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  3788. if (!(0x4000 & $this->_GetInt2d($cellAddressStructure, 2))) {
  3789. $column = PHPExcel_Cell::stringFromColumnIndex($colIndex);
  3790. $column = '$' . $column;
  3791. } else {
  3792. $colIndex = ($colIndex <= 127) ? $colIndex : $colIndex - 256;
  3793. $column = PHPExcel_Cell::stringFromColumnIndex($baseCol + $colIndex);
  3794. }
  3795. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  3796. if (!(0x8000 & $this->_GetInt2d($cellAddressStructure, 2))) {
  3797. $row = '$' . $row;
  3798. } else {
  3799. $rowIndex = ($rowIndex <= 32767) ? $rowIndex : $rowIndex - 65536;
  3800. $row = $baseRow + $rowIndex;
  3801. }
  3802. return $column . $row;
  3803. }
  3804. /**
  3805. * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
  3806. * always fixed range
  3807. * section 2.5.14
  3808. *
  3809. * @param string $subData
  3810. * @return string
  3811. * @throws Exception
  3812. */
  3813. private function _readBIFF5CellRangeAddressFixed($subData)
  3814. {
  3815. // offset: 0; size: 2; index to first row
  3816. $fr = $this->_GetInt2d($subData, 0) + 1;
  3817. // offset: 2; size: 2; index to last row
  3818. $lr = $this->_GetInt2d($subData, 2) + 1;
  3819. // offset: 4; size: 1; index to first column
  3820. $fc = ord($subData{4});
  3821. // offset: 5; size: 1; index to last column
  3822. $lc = ord($subData{5});
  3823. // check values
  3824. if ($fr > $lr || $fc > $lc) {
  3825. throw new Exception('Not a cell range address');
  3826. }
  3827. // column index to letter
  3828. $fc = PHPExcel_Cell::stringFromColumnIndex($fc);
  3829. $lc = PHPExcel_Cell::stringFromColumnIndex($lc);
  3830. if ($fr == $lr and $fc == $lc) {
  3831. return "$fc$fr";
  3832. }
  3833. return "$fc$fr:$lc$lr";
  3834. }
  3835. /**
  3836. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or 'A1'
  3837. * always fixed range
  3838. * section 2.5.14
  3839. *
  3840. * @param string $subData
  3841. * @return string
  3842. * @throws Exception
  3843. */
  3844. private function _readBIFF8CellRangeAddressFixed($subData)
  3845. {
  3846. // offset: 0; size: 2; index to first row
  3847. $fr = $this->_GetInt2d($subData, 0) + 1;
  3848. // offset: 2; size: 2; index to last row
  3849. $lr = $this->_GetInt2d($subData, 2) + 1;
  3850. // offset: 4; size: 2; index to first column
  3851. $fc = $this->_GetInt2d($subData, 4);
  3852. // offset: 6; size: 2; index to last column
  3853. $lc = $this->_GetInt2d($subData, 6);
  3854. // check values
  3855. if ($fr > $lr || $fc > $lc) {
  3856. throw new Exception('Not a cell range address');
  3857. }
  3858. // column index to letter
  3859. $fc = PHPExcel_Cell::stringFromColumnIndex($fc);
  3860. $lc = PHPExcel_Cell::stringFromColumnIndex($lc);
  3861. if ($fr == $lr and $fc == $lc) {
  3862. return "$fc$fr";
  3863. }
  3864. return "$fc$fr:$lc$lr";
  3865. }
  3866. /**
  3867. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
  3868. * there are flags indicating whether column/row index is relative
  3869. * section 3.3.4
  3870. *
  3871. * @param string $subData
  3872. * @return string
  3873. */
  3874. private function _readBIFF8CellRangeAddress($subData)
  3875. {
  3876. // todo: if cell range is just a single cell, should this funciton
  3877. // not just return e.g. 'A1' and not 'A1:A1' ?
  3878. // offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
  3879. $fr = $this->_GetInt2d($subData, 0) + 1;
  3880. // offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
  3881. $lr = $this->_GetInt2d($subData, 2) + 1;
  3882. // offset: 4; size: 2; index to first column or column offset + relative flags
  3883. // bit: 7-0; mask 0x00FF; column index
  3884. $fc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & $this->_GetInt2d($subData, 4));
  3885. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  3886. if (!(0x4000 & $this->_GetInt2d($subData, 4))) {
  3887. $fc = '$' . $fc;
  3888. }
  3889. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  3890. if (!(0x8000 & $this->_GetInt2d($subData, 4))) {
  3891. $fr = '$' . $fr;
  3892. }
  3893. // offset: 6; size: 2; index to last column or column offset + relative flags
  3894. // bit: 7-0; mask 0x00FF; column index
  3895. $lc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & $this->_GetInt2d($subData, 6));
  3896. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  3897. if (!(0x4000 & $this->_GetInt2d($subData, 6))) {
  3898. $lc = '$' . $lc;
  3899. }
  3900. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  3901. if (!(0x8000 & $this->_GetInt2d($subData, 6))) {
  3902. $lr = '$' . $lr;
  3903. }
  3904. return "$fc$fr:$lc$lr";
  3905. }
  3906. /**
  3907. * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  3908. * to indicate offsets from a base cell
  3909. * section 3.3.4
  3910. *
  3911. * @param string $subData
  3912. * @param string $baseCell Base cell
  3913. * @return string Cell range address
  3914. */
  3915. private function _readBIFF8CellRangeAddressB($subData, $baseCell = 'A1')
  3916. {
  3917. list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
  3918. $baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
  3919. // TODO: if cell range is just a single cell, should this funciton
  3920. // not just return e.g. 'A1' and not 'A1:A1' ?
  3921. // offset: 0; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
  3922. $frIndex = $this->_GetInt2d($subData, 0);
  3923. $frIndex = ($frIndex <= 32767) ? $frIndex : $frIndex - 65536;
  3924. $fr = $baseRow + $frIndex;
  3925. // offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
  3926. $lrIndex = $this->_GetInt2d($subData, 2);
  3927. $lrIndex = ($lrIndex <= 32767) ? $lrIndex : $lrIndex - 65536;
  3928. $lr = $baseRow + $lrIndex;
  3929. // offset: 4; size: 2; relative index to first column
  3930. // bit: 7-0; mask 0x00FF; column index
  3931. $fcIndex = 0x00FF & $this->_GetInt2d($subData, 4);
  3932. $fcIndex = ($fcIndex <= 127) ? $fcIndex : $fcIndex - 256;
  3933. $fc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $fcIndex);
  3934. // offset: 6; size: 2; relative index to first column
  3935. // bit: 7-0; mask 0x00FF; column index
  3936. $lcIndex = 0x00FF & $this->_GetInt2d($subData, 6);
  3937. $lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
  3938. $lc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $lcIndex);
  3939. return "$fc$fr:$lc$lr";
  3940. }
  3941. /**
  3942. * Read BIFF8 cell range address list
  3943. * section 2.5.15
  3944. *
  3945. * @param string $subData
  3946. * @return array
  3947. */
  3948. private function _readBIFF8CellRangeAddressList($subData)
  3949. {
  3950. $cellRangeAddresses = array();
  3951. // offset: 0; size: 2; number of the following cell range addresses
  3952. $nm = $this->_GetInt2d($subData, 0);
  3953. $offset = 2;
  3954. // offset: 2; size: 8 * $nm; list of $nm (fixed) cell range addresses
  3955. for ($i = 0; $i < $nm; ++$i) {
  3956. $cellRangeAddresses[] = $this->_readBIFF8CellRangeAddressFixed(substr($subData, $offset, 8));
  3957. $offset += 8;
  3958. }
  3959. return array(
  3960. 'size' => 2 + 8 * $nm,
  3961. 'cellRangeAddresses' => $cellRangeAddresses,
  3962. );
  3963. }
  3964. /**
  3965. * Get a sheet range like Sheet1:Sheet3 from REF index
  3966. * Note: If there is only one sheet in the range, one gets e.g Sheet1
  3967. * It can also happen that the REF structure uses the -1 (FFFF) code to indicate deleted sheets,
  3968. * in which case an exception is thrown
  3969. *
  3970. * @param int $index
  3971. * @return string|false
  3972. * @throws Exception
  3973. */
  3974. private function _readSheetRangeByRefIndex($index)
  3975. {
  3976. if (isset($this->_ref[$index])) {
  3977. $type = $this->_externalBooks[$this->_ref[$index]['externalBookIndex']]['type'];
  3978. switch ($type) {
  3979. case 'internal':
  3980. // check if we have a deleted 3d reference
  3981. if ($this->_ref[$index]['firstSheetIndex'] == 0xFFFF or $this->_ref[$index]['lastSheetIndex'] == 0xFFFF) {
  3982. throw new Exception('Deleted sheet reference');
  3983. }
  3984. // we have normal sheet range (collapsed or uncollapsed)
  3985. $firstSheetName = $this->_sheets[$this->_ref[$index]['firstSheetIndex']]['name'];
  3986. $lastSheetName = $this->_sheets[$this->_ref[$index]['lastSheetIndex']]['name'];
  3987. if ($firstSheetName == $lastSheetName) {
  3988. // collapsed sheet range
  3989. $sheetRange = $firstSheetName;
  3990. } else {
  3991. $sheetRange = "$firstSheetName:$lastSheetName";
  3992. }
  3993. // escape the single-quotes
  3994. $sheetRange = str_replace("'", "''", $sheetRange);
  3995. // if there are special characters, we need to enclose the range in single-quotes
  3996. // todo: check if we have identified the whole set of special characters
  3997. // it seems that the following characters are not accepted for sheet names
  3998. // and we may assume that they are not present: []*/:\?
  3999. if (preg_match("/[ !\"@#ÂŁ$%&{()}<>=+'|^,;-]/", $sheetRange)) {
  4000. $sheetRange = "'$sheetRange'";
  4001. }
  4002. return $sheetRange;
  4003. break;
  4004. default:
  4005. // TODO: external sheet support
  4006. throw new Exception('Excel5 reader only supports internal sheets in fomulas');
  4007. break;
  4008. }
  4009. }
  4010. return false;
  4011. }
  4012. /**
  4013. * read BIFF8 constant value array from array data
  4014. * returns e.g. array('value' => '{1,2;3,4}', 'size' => 40}
  4015. * section 2.5.8
  4016. *
  4017. * @param string $arrayData
  4018. * @return array
  4019. */
  4020. private function _readBIFF8ConstantArray($arrayData)
  4021. {
  4022. // offset: 0; size: 1; number of columns decreased by 1
  4023. $nc = ord($arrayData[0]);
  4024. // offset: 1; size: 2; number of rows decreased by 1
  4025. $nr = $this->_GetInt2d($arrayData, 1);
  4026. $size = 3; // initialize
  4027. $arrayData = substr($arrayData, 3);
  4028. // offset: 3; size: var; list of ($nc + 1) * ($nr + 1) constant values
  4029. $matrixChunks = array();
  4030. for ($r = 1; $r <= $nr + 1; ++$r) {
  4031. $items = array();
  4032. for ($c = 1; $c <= $nc + 1; ++$c) {
  4033. $constant = $this->_readBIFF8Constant($arrayData);
  4034. $items[] = $constant['value'];
  4035. $arrayData = substr($arrayData, $constant['size']);
  4036. $size += $constant['size'];
  4037. }
  4038. $matrixChunks[] = implode(',', $items); // looks like e.g. '1,"hello"'
  4039. }
  4040. $matrix = '{' . implode(';', $matrixChunks) . '}';
  4041. return array(
  4042. 'value' => $matrix,
  4043. 'size' => $size,
  4044. );
  4045. }
  4046. /**
  4047. * read BIFF8 constant value which may be 'Empty Value', 'Number', 'String Value', 'Boolean Value', 'Error Value'
  4048. * section 2.5.7
  4049. * returns e.g. array('value' => '5', 'size' => 9)
  4050. *
  4051. * @param string $valueData
  4052. * @return array
  4053. */
  4054. private function _readBIFF8Constant($valueData)
  4055. {
  4056. // offset: 0; size: 1; identifier for type of constant
  4057. $identifier = ord($valueData[0]);
  4058. switch ($identifier) {
  4059. case 0x00: // empty constant (what is this?)
  4060. $value = '';
  4061. $size = 9;
  4062. break;
  4063. case 0x01: // number
  4064. // offset: 1; size: 8; IEEE 754 floating-point value
  4065. $value = $this->_extractNumber(substr($valueData, 1, 8));
  4066. $size = 9;
  4067. break;
  4068. case 0x02: // string value
  4069. // offset: 1; size: var; Unicode string, 16-bit string length
  4070. $string = $this->_readUnicodeStringLong(substr($valueData, 1));
  4071. $value = '"' . $string['value'] . '"';
  4072. $size = 1 + $string['size'];
  4073. break;
  4074. case 0x04: // boolean
  4075. // offset: 1; size: 1; 0 = FALSE, 1 = TRUE
  4076. if (ord($valueData[1])) {
  4077. $value = 'TRUE';
  4078. } else {
  4079. $value = 'FALSE';
  4080. }
  4081. $size = 9;
  4082. break;
  4083. case 0x10: // error code
  4084. // offset: 1; size: 1; error code
  4085. $value = $this->_mapErrorCode(ord($valueData[1]));
  4086. $size = 9;
  4087. break;
  4088. }
  4089. return array(
  4090. 'value' => $value,
  4091. 'size' => $size,
  4092. );
  4093. }
  4094. /**
  4095. * Extract RGB color
  4096. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.4
  4097. *
  4098. * @param string $rgb Encoded RGB value (4 bytes)
  4099. * @return array
  4100. */
  4101. private function _readRGB($rgb)
  4102. {
  4103. // offset: 0; size 1; Red component
  4104. $r = ord($rgb{0});
  4105. // offset: 1; size: 1; Green component
  4106. $g = ord($rgb{1});
  4107. // offset: 2; size: 1; Blue component
  4108. $b = ord($rgb{2});
  4109. // HEX notation, e.g. 'FF00FC'
  4110. $rgb = sprintf('%02X', $r) . sprintf('%02X', $g) . sprintf('%02X', $b);
  4111. return array('rgb' => $rgb);
  4112. }
  4113. /**
  4114. * Read byte string (8-bit string length)
  4115. * OpenOffice documentation: 2.5.2
  4116. *
  4117. * @param string $subData
  4118. * @return array
  4119. */
  4120. private function _readByteStringShort($subData)
  4121. {
  4122. // offset: 0; size: 1; length of the string (character count)
  4123. $ln = ord($subData[0]);
  4124. // offset: 1: size: var; character array (8-bit characters)
  4125. $value = $this->_decodeCodepage(substr($subData, 1, $ln));
  4126. return array(
  4127. 'value' => $value,
  4128. 'size' => 1 + $ln, // size in bytes of data structure
  4129. );
  4130. }
  4131. /**
  4132. * Read byte string (16-bit string length)
  4133. * OpenOffice documentation: 2.5.2
  4134. *
  4135. * @param string $subData
  4136. * @return array
  4137. */
  4138. private function _readByteStringLong($subData)
  4139. {
  4140. // offset: 0; size: 2; length of the string (character count)
  4141. $ln = $this->_GetInt2d($subData, 0);
  4142. // offset: 2: size: var; character array (8-bit characters)
  4143. $value = $this->_decodeCodepage(substr($subData, 2));
  4144. //return $string;
  4145. return array(
  4146. 'value' => $value,
  4147. 'size' => 2 + $ln, // size in bytes of data structure
  4148. );
  4149. }
  4150. /**
  4151. * Extracts an Excel Unicode short string (8-bit string length)
  4152. * OpenOffice documentation: 2.5.3
  4153. * function will automatically find out where the Unicode string ends.
  4154. *
  4155. * @param string $subData
  4156. * @return array
  4157. */
  4158. private function _readUnicodeStringShort($subData)
  4159. {
  4160. $value = '';
  4161. // offset: 0: size: 1; length of the string (character count)
  4162. $characterCount = ord($subData[0]);
  4163. $string = $this->_readUnicodeString(substr($subData, 1), $characterCount);
  4164. // add 1 for the string length
  4165. $string['size'] += 1;
  4166. return $string;
  4167. }
  4168. /**
  4169. * Extracts an Excel Unicode long string (16-bit string length)
  4170. * OpenOffice documentation: 2.5.3
  4171. * this function is under construction, needs to support rich text, and Asian phonetic settings
  4172. *
  4173. * @param string $subData
  4174. * @return array
  4175. */
  4176. private function _readUnicodeStringLong($subData)
  4177. {
  4178. $value = '';
  4179. // offset: 0: size: 2; length of the string (character count)
  4180. $characterCount = $this->_GetInt2d($subData, 0);
  4181. $string = $this->_readUnicodeString(substr($subData, 2), $characterCount);
  4182. // add 2 for the string length
  4183. $string['size'] += 2;
  4184. return $string;
  4185. }
  4186. /**
  4187. * Read Unicode string with no string length field, but with known character count
  4188. * this function is under construction, needs to support rich text, and Asian phonetic settings
  4189. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3
  4190. *
  4191. * @param string $subData
  4192. * @param int $characterCount
  4193. * @return array
  4194. */
  4195. private function _readUnicodeString($subData, $characterCount)
  4196. {
  4197. $value = '';
  4198. // offset: 0: size: 1; option flags
  4199. // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  4200. $isCompressed = !((0x01 & ord($subData[0])) >> 0);
  4201. // bit: 2; mask: 0x04; Asian phonetic settings
  4202. $hasAsian = (0x04) & ord($subData[0]) >> 2;
  4203. // bit: 3; mask: 0x08; Rich-Text settings
  4204. $hasRichText = (0x08) & ord($subData[0]) >> 3;
  4205. // offset: 1: size: var; character array
  4206. // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  4207. // needs to be fixed
  4208. $value = $this->_encodeUTF16(substr($subData, 1, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
  4209. return array(
  4210. 'value' => $value,
  4211. 'size' => $isCompressed ? 1 + $characterCount : 1 + 2 * $characterCount, // the size in bytes including the option flags
  4212. );
  4213. }
  4214. /**
  4215. * Convert UTF-8 string to string surounded by double quotes. Used for explicit string tokens in formulas.
  4216. * Example: hello"world --> "hello""world"
  4217. *
  4218. * @param string $value UTF-8 encoded string
  4219. * @return string
  4220. */
  4221. private function _UTF8toExcelDoubleQuoted($value)
  4222. {
  4223. return '"' . str_replace('"', '""', $value) . '"';
  4224. }
  4225. /**
  4226. * Reads first 8 bytes of a string and return IEEE 754 float
  4227. *
  4228. * @param string $data Binary string that is at least 8 bytes long
  4229. * @return float
  4230. */
  4231. private function _extractNumber($data)
  4232. {
  4233. $rknumhigh = $this->_GetInt4d($data, 4);
  4234. $rknumlow = $this->_GetInt4d($data, 0);
  4235. $sign = ($rknumhigh & 0x80000000) >> 31;
  4236. $exp = ($rknumhigh & 0x7ff00000) >> 20;
  4237. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  4238. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  4239. $mantissalow2 = ($rknumlow & 0x7fffffff);
  4240. $value = $mantissa / pow( 2 , (20 - ($exp - 1023)));
  4241. if ($mantissalow1 != 0) {
  4242. $value += 1 / pow (2 , (21 - ($exp - 1023)));
  4243. }
  4244. $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  4245. if ($sign) {
  4246. $value = -1 * $value;
  4247. }
  4248. return $value;
  4249. }
  4250. private function _GetIEEE754($rknum)
  4251. {
  4252. if (($rknum & 0x02) != 0) {
  4253. $value = $rknum >> 2;
  4254. }
  4255. else {
  4256. // changes by mmp, info on IEEE754 encoding from
  4257. // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  4258. // The RK format calls for using only the most significant 30 bits
  4259. // of the 64 bit floating point value. The other 34 bits are assumed
  4260. // to be 0 so we use the upper 30 bits of $rknum as follows...
  4261. $sign = ($rknum & 0x80000000) >> 31;
  4262. $exp = ($rknum & 0x7ff00000) >> 20;
  4263. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  4264. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  4265. if ($sign) {
  4266. $value = -1 * $value;
  4267. }
  4268. //end of changes by mmp
  4269. }
  4270. if (($rknum & 0x01) != 0) {
  4271. $value /= 100;
  4272. }
  4273. return $value;
  4274. }
  4275. /**
  4276. * Get UTF-8 string from (compressed or uncompressed) UTF-16 string
  4277. *
  4278. * @param string $string
  4279. * @param bool $compressed
  4280. * @return string
  4281. */
  4282. private function _encodeUTF16($string, $compressed = '')
  4283. {
  4284. if ($compressed) {
  4285. $string = $this->_uncompressByteString($string);
  4286. }
  4287. $result = PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', 'UTF-16LE');
  4288. return $result;
  4289. }
  4290. /**
  4291. * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
  4292. *
  4293. * @param string $string
  4294. * @return string
  4295. */
  4296. private function _uncompressByteString($string)
  4297. {
  4298. $uncompressedString = '';
  4299. for ($i = 0; $i < strlen($string); ++$i) {
  4300. $uncompressedString .= $string[$i] . "\0";
  4301. }
  4302. return $uncompressedString;
  4303. }
  4304. /**
  4305. * Convert string to UTF-8. Only used for BIFF5.
  4306. *
  4307. * @param string $string
  4308. * @return string
  4309. */
  4310. private function _decodeCodepage($string)
  4311. {
  4312. $result = PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', $this->_codepage);
  4313. return $result;
  4314. }
  4315. /**
  4316. * Read 16-bit unsigned integer
  4317. *
  4318. * @param string $data
  4319. * @param int $pos
  4320. * @return int
  4321. */
  4322. private function _GetInt2d($data, $pos)
  4323. {
  4324. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8);
  4325. }
  4326. /**
  4327. * Read 32-bit signed integer
  4328. *
  4329. * @param string $data
  4330. * @param int $pos
  4331. * @return int
  4332. */
  4333. private function _GetInt4d($data, $pos)
  4334. {
  4335. //return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) |
  4336. // (ord($data[$pos + 2]) << 16) | (ord($data[$pos + 3]) << 24);
  4337. // FIX: represent numbers correctly on 64-bit system
  4338. // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  4339. $_or_24 = ord($data[$pos + 3]);
  4340. if ($_or_24 >= 128) {
  4341. // negative number
  4342. $_ord_24 = -abs((256 - $_or_24) << 24);
  4343. } else {
  4344. $_ord_24 = ($_or_24 & 127) << 24;
  4345. }
  4346. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24;
  4347. }
  4348. /**
  4349. * Read color
  4350. *
  4351. * @param int $color Indexed color
  4352. * @return array RGB color value, example: array('rgb' => 'FF0000')
  4353. */
  4354. private function _readColor($color)
  4355. {
  4356. if ($color <= 0x07 || $color >= 0x40) {
  4357. // special built-in color
  4358. $color = $this->_mapBuiltInColor($color);
  4359. } else if (isset($this->_palette) && isset($this->_palette[$color - 8])) {
  4360. // palette color, color index 0x08 maps to pallete index 0
  4361. $color = $this->_palette[$color - 8];
  4362. } else {
  4363. // default color table
  4364. if ($this->_version == self::XLS_BIFF8) {
  4365. $color = $this->_mapColor($color);
  4366. } else {
  4367. // BIFF5
  4368. $color = $this->_mapColorBIFF5($color);
  4369. }
  4370. }
  4371. return $color;
  4372. }
  4373. /**
  4374. * Map border style
  4375. * OpenOffice documentation: 2.5.11
  4376. *
  4377. * @param int $index
  4378. * @return string
  4379. */
  4380. private function _mapBorderStyle($index)
  4381. {
  4382. switch ($index) {
  4383. case 0x00: return PHPExcel_Style_Border::BORDER_NONE;
  4384. case 0x01: return PHPExcel_Style_Border::BORDER_THIN;
  4385. case 0x02: return PHPExcel_Style_Border::BORDER_MEDIUM;
  4386. case 0x03: return PHPExcel_Style_Border::BORDER_DASHED;
  4387. case 0x04: return PHPExcel_Style_Border::BORDER_DOTTED;
  4388. case 0x05: return PHPExcel_Style_Border::BORDER_THICK;
  4389. case 0x06: return PHPExcel_Style_Border::BORDER_DOUBLE;
  4390. case 0x07: return PHPExcel_Style_Border::BORDER_HAIR;
  4391. case 0x08: return PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
  4392. case 0x09: return PHPExcel_Style_Border::BORDER_DASHDOT;
  4393. case 0x0A: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
  4394. case 0x0B: return PHPExcel_Style_Border::BORDER_DASHDOTDOT;
  4395. case 0x0C: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
  4396. case 0x0D: return PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
  4397. default: return PHPExcel_Style_Border::BORDER_NONE;
  4398. }
  4399. }
  4400. /**
  4401. * Get fill pattern from index
  4402. * OpenOffice documentation: 2.5.12
  4403. *
  4404. * @param int $index
  4405. * @return string
  4406. */
  4407. private function _mapFillPattern($index)
  4408. {
  4409. switch ($index) {
  4410. case 0x00: return PHPExcel_Style_Fill::FILL_NONE;
  4411. case 0x01: return PHPExcel_Style_Fill::FILL_SOLID;
  4412. case 0x02: return PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
  4413. case 0x03: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
  4414. case 0x04: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
  4415. case 0x05: return PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
  4416. case 0x06: return PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
  4417. case 0x07: return PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
  4418. case 0x08: return PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
  4419. case 0x09: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
  4420. case 0x0A: return PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
  4421. case 0x0B: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
  4422. case 0x0C: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
  4423. case 0x0D: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
  4424. case 0x0E: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
  4425. case 0x0F: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
  4426. case 0x10: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
  4427. case 0x11: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
  4428. case 0x12: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
  4429. default: return PHPExcel_Style_Fill::FILL_NONE;
  4430. }
  4431. }
  4432. /**
  4433. * Map error code, e.g. '#N/A'
  4434. *
  4435. * @param int $subData
  4436. * @return string
  4437. */
  4438. private function _mapErrorCode($subData)
  4439. {
  4440. switch ($subData) {
  4441. case 0x00: return '#NULL!'; break;
  4442. case 0x07: return '#DIV/0!'; break;
  4443. case 0x0F: return '#VALUE!'; break;
  4444. case 0x17: return '#REF!'; break;
  4445. case 0x1D: return '#NAME?'; break;
  4446. case 0x24: return '#NUM!'; break;
  4447. case 0x2A: return '#N/A'; break;
  4448. default: return false;
  4449. }
  4450. }
  4451. /**
  4452. * Map built-in color to RGB value
  4453. *
  4454. * @param int $color Indexed color
  4455. * @return array
  4456. */
  4457. private function _mapBuiltInColor($color)
  4458. {
  4459. switch ($color) {
  4460. case 0x00: return array('rgb' => '000000');
  4461. case 0x01: return array('rgb' => 'FFFFFF');
  4462. case 0x02: return array('rgb' => 'FF0000');
  4463. case 0x03: return array('rgb' => '00FF00');
  4464. case 0x04: return array('rgb' => '0000FF');
  4465. case 0x05: return array('rgb' => 'FFFF00');
  4466. case 0x06: return array('rgb' => 'FF00FF');
  4467. case 0x07: return array('rgb' => '00FFFF');
  4468. case 0x40: return array('rgb' => '000000'); // system window text color
  4469. case 0x41: return array('rgb' => 'FFFFFF'); // system window background color
  4470. default: return array('rgb' => '000000');
  4471. }
  4472. }
  4473. /**
  4474. * Map color array from BIFF5 built-in color index
  4475. *
  4476. * @param int $subData
  4477. * @return array
  4478. */
  4479. private function _mapColorBIFF5($subData)
  4480. {
  4481. switch ($subData) {
  4482. case 0x08: return array('rgb' => '000000');
  4483. case 0x09: return array('rgb' => 'FFFFFF');
  4484. case 0x0A: return array('rgb' => 'FF0000');
  4485. case 0x0B: return array('rgb' => '00FF00');
  4486. case 0x0C: return array('rgb' => '0000FF');
  4487. case 0x0D: return array('rgb' => 'FFFF00');
  4488. case 0x0E: return array('rgb' => 'FF00FF');
  4489. case 0x0F: return array('rgb' => '00FFFF');
  4490. case 0x10: return array('rgb' => '800000');
  4491. case 0x11: return array('rgb' => '008000');
  4492. case 0x12: return array('rgb' => '000080');
  4493. case 0x13: return array('rgb' => '808000');
  4494. case 0x14: return array('rgb' => '800080');
  4495. case 0x15: return array('rgb' => '008080');
  4496. case 0x16: return array('rgb' => 'C0C0C0');
  4497. case 0x17: return array('rgb' => '808080');
  4498. case 0x18: return array('rgb' => '8080FF');
  4499. case 0x19: return array('rgb' => '802060');
  4500. case 0x1A: return array('rgb' => 'FFFFC0');
  4501. case 0x1B: return array('rgb' => 'A0E0F0');
  4502. case 0x1C: return array('rgb' => '600080');
  4503. case 0x1D: return array('rgb' => 'FF8080');
  4504. case 0x1E: return array('rgb' => '0080C0');
  4505. case 0x1F: return array('rgb' => 'C0C0FF');
  4506. case 0x20: return array('rgb' => '000080');
  4507. case 0x21: return array('rgb' => 'FF00FF');
  4508. case 0x22: return array('rgb' => 'FFFF00');
  4509. case 0x23: return array('rgb' => '00FFFF');
  4510. case 0x24: return array('rgb' => '800080');
  4511. case 0x25: return array('rgb' => '800000');
  4512. case 0x26: return array('rgb' => '008080');
  4513. case 0x27: return array('rgb' => '0000FF');
  4514. case 0x28: return array('rgb' => '00CFFF');
  4515. case 0x29: return array('rgb' => '69FFFF');
  4516. case 0x2A: return array('rgb' => 'E0FFE0');
  4517. case 0x2B: return array('rgb' => 'FFFF80');
  4518. case 0x2C: return array('rgb' => 'A6CAF0');
  4519. case 0x2D: return array('rgb' => 'DD9CB3');
  4520. case 0x2E: return array('rgb' => 'B38FEE');
  4521. case 0x2F: return array('rgb' => 'E3E3E3');
  4522. case 0x30: return array('rgb' => '2A6FF9');
  4523. case 0x31: return array('rgb' => '3FB8CD');
  4524. case 0x32: return array('rgb' => '488436');
  4525. case 0x33: return array('rgb' => '958C41');
  4526. case 0x34: return array('rgb' => '8E5E42');
  4527. case 0x35: return array('rgb' => 'A0627A');
  4528. case 0x36: return array('rgb' => '624FAC');
  4529. case 0x37: return array('rgb' => '969696');
  4530. case 0x38: return array('rgb' => '1D2FBE');
  4531. case 0x39: return array('rgb' => '286676');
  4532. case 0x3A: return array('rgb' => '004500');
  4533. case 0x3B: return array('rgb' => '453E01');
  4534. case 0x3C: return array('rgb' => '6A2813');
  4535. case 0x3D: return array('rgb' => '85396A');
  4536. case 0x3E: return array('rgb' => '4A3285');
  4537. case 0x3F: return array('rgb' => '424242');
  4538. default: return array('rgb' => '000000');
  4539. }
  4540. }
  4541. /**
  4542. * Map color array from BIFF8 built-in color index
  4543. *
  4544. * @param int $subData
  4545. * @return array
  4546. */
  4547. private function _mapColor($subData)
  4548. {
  4549. switch ($subData) {
  4550. case 0x08: return array('rgb' => '000000');
  4551. case 0x09: return array('rgb' => 'FFFFFF');
  4552. case 0x0A: return array('rgb' => 'FF0000');
  4553. case 0x0B: return array('rgb' => '00FF00');
  4554. case 0x0C: return array('rgb' => '0000FF');
  4555. case 0x0D: return array('rgb' => 'FFFF00');
  4556. case 0x0E: return array('rgb' => 'FF00FF');
  4557. case 0x0F: return array('rgb' => '00FFFF');
  4558. case 0x10: return array('rgb' => '800000');
  4559. case 0x11: return array('rgb' => '008000');
  4560. case 0x12: return array('rgb' => '000080');
  4561. case 0x13: return array('rgb' => '808000');
  4562. case 0x14: return array('rgb' => '800080');
  4563. case 0x15: return array('rgb' => '008080');
  4564. case 0x16: return array('rgb' => 'C0C0C0');
  4565. case 0x17: return array('rgb' => '808080');
  4566. case 0x18: return array('rgb' => '9999FF');
  4567. case 0x19: return array('rgb' => '993366');
  4568. case 0x1A: return array('rgb' => 'FFFFCC');
  4569. case 0x1B: return array('rgb' => 'CCFFFF');
  4570. case 0x1C: return array('rgb' => '660066');
  4571. case 0x1D: return array('rgb' => 'FF8080');
  4572. case 0x1E: return array('rgb' => '0066CC');
  4573. case 0x1F: return array('rgb' => 'CCCCFF');
  4574. case 0x20: return array('rgb' => '000080');
  4575. case 0x21: return array('rgb' => 'FF00FF');
  4576. case 0x22: return array('rgb' => 'FFFF00');
  4577. case 0x23: return array('rgb' => '00FFFF');
  4578. case 0x24: return array('rgb' => '800080');
  4579. case 0x25: return array('rgb' => '800000');
  4580. case 0x26: return array('rgb' => '008080');
  4581. case 0x27: return array('rgb' => '0000FF');
  4582. case 0x28: return array('rgb' => '00CCFF');
  4583. case 0x29: return array('rgb' => 'CCFFFF');
  4584. case 0x2A: return array('rgb' => 'CCFFCC');
  4585. case 0x2B: return array('rgb' => 'FFFF99');
  4586. case 0x2C: return array('rgb' => '99CCFF');
  4587. case 0x2D: return array('rgb' => 'FF99CC');
  4588. case 0x2E: return array('rgb' => 'CC99FF');
  4589. case 0x2F: return array('rgb' => 'FFCC99');
  4590. case 0x30: return array('rgb' => '3366FF');
  4591. case 0x31: return array('rgb' => '33CCCC');
  4592. case 0x32: return array('rgb' => '99CC00');
  4593. case 0x33: return array('rgb' => 'FFCC00');
  4594. case 0x34: return array('rgb' => 'FF9900');
  4595. case 0x35: return array('rgb' => 'FF6600');
  4596. case 0x36: return array('rgb' => '666699');
  4597. case 0x37: return array('rgb' => '969696');
  4598. case 0x38: return array('rgb' => '003366');
  4599. case 0x39: return array('rgb' => '339966');
  4600. case 0x3A: return array('rgb' => '003300');
  4601. case 0x3B: return array('rgb' => '333300');
  4602. case 0x3C: return array('rgb' => '993300');
  4603. case 0x3D: return array('rgb' => '993366');
  4604. case 0x3E: return array('rgb' => '333399');
  4605. case 0x3F: return array('rgb' => '333333');
  4606. default: return array('rgb' => '000000');
  4607. }
  4608. }
  4609. }