PageRenderTime 79ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/PHPExcel/Reader/Excel5.php

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