PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/excellib.class.php

https://bitbucket.org/moodle/moodle
PHP | 896 lines | 471 code | 86 blank | 339 comment | 89 complexity | 7671ab58eec231ee00c8ce5eacc83814 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Excel writer abstraction layer.
  18. *
  19. * @copyright (C) 2001-3001 Eloy Lafuente (stronk7) {@link http://contiento.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once("$CFG->libdir/phpspreadsheet/vendor/autoload.php");
  25. use \PhpOffice\PhpSpreadsheet\Spreadsheet;
  26. use \PhpOffice\PhpSpreadsheet\IOFactory;
  27. use \PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  28. use \PhpOffice\PhpSpreadsheet\Cell\DataType;
  29. use \PhpOffice\PhpSpreadsheet\Shared\Date;
  30. use \PhpOffice\PhpSpreadsheet\Style\Alignment;
  31. use \PhpOffice\PhpSpreadsheet\Style\Border;
  32. use \PhpOffice\PhpSpreadsheet\Style\Fill;
  33. use \PhpOffice\PhpSpreadsheet\Style\Font;
  34. use \PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  35. use \PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  36. use \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  37. /**
  38. * Define and operate over one Moodle Workbook.
  39. *
  40. * This class acts as a wrapper around another library
  41. * maintaining Moodle functions isolated from underlying code.
  42. *
  43. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  44. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45. * @package moodlecore
  46. */
  47. class MoodleExcelWorkbook {
  48. /** @var PhpSpreadsheet */
  49. protected $objspreadsheet;
  50. /** @var string */
  51. protected $filename;
  52. /** @var string format type */
  53. protected $type;
  54. /**
  55. * Constructs one Moodle Workbook.
  56. *
  57. * @param string $filename The name of the file
  58. * @param string $type file format type used to be 'Xls or Xlsx' but now only 'Xlsx'
  59. */
  60. public function __construct($filename, $type = 'Xlsx') {
  61. global $CFG;
  62. $this->objspreadsheet = new Spreadsheet();
  63. $this->objspreadsheet->removeSheetByIndex(0);
  64. $this->filename = $filename;
  65. if (strtolower($type) === 'Xls') {
  66. debugging('Xls is no longer supported, using Xlsx instead');
  67. $this->type = 'Xlsx';
  68. } else {
  69. $this->type = 'Xlsx';
  70. }
  71. }
  72. /**
  73. * Create one Moodle Worksheet
  74. *
  75. * @param string $name Name of the sheet
  76. * @return MoodleExcelWorksheet
  77. */
  78. public function add_worksheet($name = '') {
  79. return new MoodleExcelWorksheet($name, $this->objspreadsheet);
  80. }
  81. /**
  82. * Create one cell Format.
  83. *
  84. * @param array $properties array of properties [name]=value;
  85. * valid names are set_XXXX existing
  86. * functions without the set_ part
  87. * i.e: [bold]=1 for set_bold(1)...Optional!
  88. * @return MoodleExcelFormat
  89. */
  90. public function add_format($properties = array()) {
  91. return new MoodleExcelFormat($properties);
  92. }
  93. /**
  94. * Close the Moodle Workbook
  95. */
  96. public function close() {
  97. global $CFG;
  98. foreach ($this->objspreadsheet->getAllSheets() as $sheet) {
  99. $sheet->setSelectedCells('A1');
  100. }
  101. $this->objspreadsheet->setActiveSheetIndex(0);
  102. $filename = preg_replace('/\.xlsx?$/i', '', $this->filename);
  103. $mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  104. $filename = $filename.'.xlsx';
  105. if (is_https()) { // HTTPS sites - watch out for IE! KB812935 and KB316431.
  106. header('Cache-Control: max-age=10');
  107. header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  108. header('Pragma: ');
  109. } else { //normal http - prevent caching at all cost
  110. header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
  111. header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  112. header('Pragma: no-cache');
  113. }
  114. if (core_useragent::is_ie() || core_useragent::is_edge()) {
  115. $filename = rawurlencode($filename);
  116. } else {
  117. $filename = s($filename);
  118. }
  119. header('Content-Type: '.$mimetype);
  120. header('Content-Disposition: attachment;filename="'.$filename.'"');
  121. $objwriter = IOFactory::createWriter($this->objspreadsheet, $this->type);
  122. $objwriter->save('php://output');
  123. }
  124. /**
  125. * Not required to use.
  126. * @param string $filename Name of the downloaded file
  127. */
  128. public function send($filename) {
  129. $this->filename = $filename;
  130. }
  131. }
  132. /**
  133. * Define and operate over one Worksheet.
  134. *
  135. * This class acts as a wrapper around another library
  136. * maintaining Moodle functions isolated from underlying code.
  137. *
  138. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  139. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  140. * @package core
  141. */
  142. class MoodleExcelWorksheet {
  143. /** @var Worksheet */
  144. protected $worksheet;
  145. /**
  146. * Constructs one Moodle Worksheet.
  147. *
  148. * @param string $name The name of the file
  149. * @param Spreadsheet $workbook The internal Workbook object we are creating.
  150. */
  151. public function __construct($name, Spreadsheet $workbook) {
  152. // Replace any characters in the name that Excel cannot cope with.
  153. $name = strtr(trim($name, "'"), '[]*/\?:', ' ');
  154. // Shorten the title if necessary.
  155. $name = core_text::substr($name, 0, 31);
  156. // After the substr, we might now have a single quote on the end.
  157. $name = trim($name, "'");
  158. if ($name === '') {
  159. // Name is required!
  160. $name = 'Sheet'.($workbook->getSheetCount()+1);
  161. }
  162. $this->worksheet = new Worksheet($workbook, $name);
  163. $this->worksheet->setPrintGridlines(false);
  164. $workbook->addSheet($this->worksheet);
  165. }
  166. /**
  167. * Write one string somewhere in the worksheet.
  168. *
  169. * @param integer $row Zero indexed row
  170. * @param integer $col Zero indexed column
  171. * @param string $str The string to write
  172. * @param mixed $format The XF format for the cell
  173. */
  174. public function write_string($row, $col, $str, $format = null) {
  175. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  176. $col += 1;
  177. $this->worksheet->getStyleByColumnAndRow($col, $row + 1)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
  178. $this->worksheet->setCellValueExplicitByColumnAndRow($col, $row + 1, $str, DataType::TYPE_STRING);
  179. $this->apply_format($row, $col, $format);
  180. }
  181. /**
  182. * Write one number somewhere in the worksheet.
  183. *
  184. * @param integer $row Zero indexed row
  185. * @param integer $col Zero indexed column
  186. * @param float $num The number to write
  187. * @param mixed $format The XF format for the cell
  188. */
  189. public function write_number($row, $col, $num, $format = null) {
  190. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  191. $col += 1;
  192. $this->worksheet->getStyleByColumnAndRow($col, $row + 1)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_GENERAL);
  193. $this->worksheet->setCellValueExplicitByColumnAndRow($col, $row + 1, $num, DataType::TYPE_NUMERIC);
  194. $this->apply_format($row, $col, $format);
  195. }
  196. /**
  197. * Write one url somewhere in the worksheet.
  198. *
  199. * @param integer $row Zero indexed row
  200. * @param integer $col Zero indexed column
  201. * @param string $url The url to write
  202. * @param mixed $format The XF format for the cell
  203. */
  204. public function write_url($row, $col, $url, $format = null) {
  205. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  206. $col += 1;
  207. $this->worksheet->setCellValueByColumnAndRow($col, $row + 1, $url);
  208. $this->worksheet->getCellByColumnAndRow($col, $row + 1)->getHyperlink()->setUrl($url);
  209. $this->apply_format($row, $col, $format);
  210. }
  211. /**
  212. * Write one date somewhere in the worksheet.
  213. * @param integer $row Zero indexed row
  214. * @param integer $col Zero indexed column
  215. * @param int $date The date to write in UNIX timestamp format
  216. * @param mixed $format The XF format for the cell
  217. */
  218. public function write_date($row, $col, $date, $format = null) {
  219. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  220. $col += 1;
  221. $getdate = usergetdate($date);
  222. $exceldate = Date::FormattedPHPToExcel(
  223. $getdate['year'],
  224. $getdate['mon'],
  225. $getdate['mday'],
  226. $getdate['hours'],
  227. $getdate['minutes'],
  228. $getdate['seconds']
  229. );
  230. $this->worksheet->setCellValueByColumnAndRow($col, $row + 1, $exceldate);
  231. $style = $this->worksheet->getStyleByColumnAndRow($col, $row + 1);
  232. $style->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_XLSX22);
  233. $this->apply_format($row, $col, $format);
  234. }
  235. /**
  236. * Write one formula somewhere in the worksheet.
  237. *
  238. * @param integer $row Zero indexed row
  239. * @param integer $col Zero indexed column
  240. * @param string $formula The formula to write
  241. * @param mixed $format The XF format for the cell
  242. */
  243. public function write_formula($row, $col, $formula, $format = null) {
  244. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  245. $col += 1;
  246. $this->worksheet->setCellValueExplicitByColumnAndRow($col, $row + 1, $formula, DataType::TYPE_FORMULA);
  247. $this->apply_format($row, $col, $format);
  248. }
  249. /**
  250. * Write one blank somewhere in the worksheet.
  251. *
  252. * @param integer $row Zero indexed row
  253. * @param integer $col Zero indexed column
  254. * @param mixed $format The XF format for the cell
  255. */
  256. public function write_blank($row, $col, $format = null) {
  257. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  258. $col += 1;
  259. $this->worksheet->setCellValueByColumnAndRow($col, $row + 1, '');
  260. $this->apply_format($row, $col, $format);
  261. }
  262. /**
  263. * Write anything somewhere in the worksheet,
  264. * type will be automatically detected.
  265. *
  266. * @param integer $row Zero indexed row
  267. * @param integer $col Zero indexed column
  268. * @param mixed $token What we are writing
  269. * @param mixed $format The XF format for the cell
  270. */
  271. public function write($row, $col, $token, $format = null) {
  272. // Analyse what are we trying to send.
  273. if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
  274. // Match number
  275. return $this->write_number($row, $col, $token, $format);
  276. } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
  277. // Match http or ftp URL
  278. return $this->write_url($row, $col, $token, '', $format);
  279. } elseif (preg_match("/^mailto:/", $token)) {
  280. // Match mailto:
  281. return $this->write_url($row, $col, $token, '', $format);
  282. } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
  283. // Match internal or external sheet link
  284. return $this->write_url($row, $col, $token, '', $format);
  285. } elseif (preg_match("/^=/", $token)) {
  286. // Match formula
  287. return $this->write_formula($row, $col, $token, $format);
  288. } elseif (preg_match("/^@/", $token)) {
  289. // Match formula
  290. return $this->write_formula($row, $col, $token, $format);
  291. } elseif ($token == '') {
  292. // Match blank
  293. return $this->write_blank($row, $col, $format);
  294. } else {
  295. // Default: match string
  296. return $this->write_string($row, $col, $token, $format);
  297. }
  298. }
  299. /**
  300. * Sets the height (and other settings) of one row.
  301. *
  302. * @param integer $row The row to set
  303. * @param integer $height Height we are giving to the row (null to set just format without setting the height)
  304. * @param mixed $format The optional format we are giving to the row
  305. * @param bool $hidden The optional hidden attribute
  306. * @param integer $level The optional outline level (0-7)
  307. */
  308. public function set_row($row, $height, $format = null, $hidden = false, $level = 0) {
  309. if ($level < 0) {
  310. $level = 0;
  311. } else if ($level > 7) {
  312. $level = 7;
  313. }
  314. if (isset($height)) {
  315. $this->worksheet->getRowDimension($row + 1)->setRowHeight($height);
  316. }
  317. $this->worksheet->getRowDimension($row + 1)->setVisible(!$hidden);
  318. $this->worksheet->getRowDimension($row + 1)->setOutlineLevel($level);
  319. $this->apply_row_format($row, $format);
  320. }
  321. /**
  322. * Sets the width (and other settings) of one column.
  323. *
  324. * @param integer $firstcol first column on the range
  325. * @param integer $lastcol last column on the range
  326. * @param integer $width width to set (null to set just format without setting the width)
  327. * @param mixed $format The optional format to apply to the columns
  328. * @param bool $hidden The optional hidden attribute
  329. * @param integer $level The optional outline level (0-7)
  330. */
  331. public function set_column($firstcol, $lastcol, $width, $format = null, $hidden = false, $level = 0) {
  332. if ($level < 0) {
  333. $level = 0;
  334. } else if ($level > 7) {
  335. $level = 7;
  336. }
  337. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  338. $i = $firstcol + 1;
  339. while ($i <= $lastcol + 1) {
  340. if (isset($width)) {
  341. $this->worksheet->getColumnDimensionByColumn($i)->setWidth($width);
  342. }
  343. $this->worksheet->getColumnDimensionByColumn($i)->setVisible(!$hidden);
  344. $this->worksheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  345. $this->apply_column_format($i, $format);
  346. $i++;
  347. }
  348. }
  349. /**
  350. * Set the option to hide grid lines on the printed page.
  351. */
  352. public function hide_gridlines() {
  353. // Not implemented - always off.
  354. }
  355. /**
  356. * Set the option to hide gridlines on the worksheet (as seen on the screen).
  357. */
  358. public function hide_screen_gridlines() {
  359. $this->worksheet->setShowGridlines(false);
  360. }
  361. /**
  362. * Insert an image in a worksheet.
  363. *
  364. * @param integer $row The row we are going to insert the bitmap into
  365. * @param integer $col The column we are going to insert the bitmap into
  366. * @param string $bitmap The bitmap filename
  367. * @param integer $x The horizontal position (offset) of the image inside the cell.
  368. * @param integer $y The vertical position (offset) of the image inside the cell.
  369. * @param integer $scalex The horizontal scale
  370. * @param integer $scaley The vertical scale
  371. */
  372. public function insert_bitmap($row, $col, $bitmap, $x = 0, $y = 0, $scalex = 1, $scaley = 1) {
  373. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  374. $col += 1;
  375. $objdrawing = new Drawing();
  376. $objdrawing->setPath($bitmap);
  377. $objdrawing->setCoordinates(Coordinate::stringFromColumnIndex($col) . ($row + 1));
  378. $objdrawing->setOffsetX($x);
  379. $objdrawing->setOffsetY($y);
  380. $objdrawing->setWorksheet($this->worksheet);
  381. if ($scale_x != 1) {
  382. $objdrawing->setResizeProportional(false);
  383. $objdrawing->getWidth($objdrawing->getWidth() * $scalex);
  384. }
  385. if ($scale_y != 1) {
  386. $objdrawing->setResizeProportional(false);
  387. $objdrawing->setHeight($objdrawing->getHeight() * $scaley);
  388. }
  389. }
  390. /**
  391. * Merges the area given by its arguments.
  392. *
  393. * @param integer $firstrow First row of the area to merge
  394. * @param integer $firstcol First column of the area to merge
  395. * @param integer $lastrow Last row of the area to merge
  396. * @param integer $lastcol Last column of the area to merge
  397. */
  398. public function merge_cells($firstrow, $firstcol, $lastrow, $lastcol) {
  399. // For PhpSpreadsheet library, the column indexes start on 1 (instead of 0 as before).
  400. $this->worksheet->mergeCellsByColumnAndRow($firstcol + 1, $firstrow + 1, $lastcol + 1, $lastrow + 1);
  401. }
  402. protected function apply_format($row, $col, $format = null) {
  403. if (!$format) {
  404. $format = new MoodleExcelFormat();
  405. } else if (is_array($format)) {
  406. $format = new MoodleExcelFormat($format);
  407. }
  408. $this->worksheet->getStyleByColumnAndRow($col, $row + 1)->applyFromArray($format->get_format_array());
  409. }
  410. protected function apply_column_format($col, $format = null) {
  411. if (!$format) {
  412. $format = new MoodleExcelFormat();
  413. } else if (is_array($format)) {
  414. $format = new MoodleExcelFormat($format);
  415. }
  416. $this->worksheet->getStyle(Coordinate::stringFromColumnIndex($col))->applyFromArray($format->get_format_array());
  417. }
  418. protected function apply_row_format($row, $format = null) {
  419. if (!$format) {
  420. $format = new MoodleExcelFormat();
  421. } else if (is_array($format)) {
  422. $format = new MoodleExcelFormat($format);
  423. }
  424. $this->worksheet->getStyle($row + 1)->applyFromArray($format->get_format_array());
  425. }
  426. }
  427. /**
  428. * Define and operate over one Format.
  429. *
  430. * A big part of this class acts as a wrapper over other libraries
  431. * maintaining Moodle functions isolated from underlying code.
  432. *
  433. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  434. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  435. * @package moodlecore
  436. */
  437. class MoodleExcelFormat {
  438. /** @var array */
  439. protected $format = array();
  440. /**
  441. * Constructs one Moodle Format.
  442. *
  443. * @param array $properties
  444. */
  445. public function __construct($properties = array()) {
  446. // If we have something in the array of properties, compute them
  447. foreach($properties as $property => $value) {
  448. if(method_exists($this,"set_$property")) {
  449. $aux = 'set_'.$property;
  450. $this->$aux($value);
  451. }
  452. }
  453. }
  454. /**
  455. * Returns standardised Excel format array.
  456. * @private
  457. *
  458. * @return array
  459. */
  460. public function get_format_array() {
  461. return $this->format;
  462. }
  463. /**
  464. * Set the size of the text in the format (in pixels).
  465. * By default all texts in generated sheets are 10pt.
  466. *
  467. * @param integer $size Size of the text (in points)
  468. */
  469. public function set_size($size) {
  470. $this->format['font']['size'] = $size;
  471. }
  472. /**
  473. * Set weight of the format.
  474. *
  475. * @param integer $weight Weight for the text, 0 maps to 400 (normal text),
  476. * 1 maps to 700 (bold text). Valid range is: 100-1000.
  477. * It's Optional, default is 1 (bold).
  478. */
  479. public function set_bold($weight = 1) {
  480. if ($weight == 1) {
  481. $weight = 700;
  482. }
  483. $this->format['font']['bold'] = ($weight > 400);
  484. }
  485. /**
  486. * Set underline of the format.
  487. *
  488. * @param integer $underline The value for underline. Possible values are:
  489. * 1 => underline, 2 => double underline
  490. */
  491. public function set_underline($underline) {
  492. if ($underline == 1) {
  493. $this->format['font']['underline'] = Font::UNDERLINE_SINGLE;
  494. } else if ($underline == 2) {
  495. $this->format['font']['underline'] = Font::UNDERLINE_DOUBLE;
  496. } else {
  497. $this->format['font']['underline'] = Font::UNDERLINE_NONE;
  498. }
  499. }
  500. /**
  501. * Set italic of the format.
  502. */
  503. public function set_italic() {
  504. $this->format['font']['italic'] = true;
  505. }
  506. /**
  507. * Set strikeout of the format.
  508. */
  509. public function set_strikeout() {
  510. $this->format['font']['strikethrough'] = true;
  511. }
  512. /**
  513. * Set outlining of the format.
  514. */
  515. public function set_outline() {
  516. // Not implemented.
  517. }
  518. /**
  519. * Set shadow of the format.
  520. */
  521. public function set_shadow() {
  522. // Not implemented.
  523. }
  524. /**
  525. * Set the script of the text.
  526. *
  527. * @param integer $script The value for script type. Possible values are:
  528. * 1 => superscript, 2 => subscript
  529. */
  530. public function set_script($script) {
  531. if ($script == 1) {
  532. $this->format['font']['superscript'] = true;
  533. } else if ($script == 2) {
  534. $this->format['font']['subscript'] = true;
  535. } else {
  536. $this->format['font']['superscript'] = false;
  537. $this->format['font']['subscript'] = false;
  538. }
  539. }
  540. /**
  541. * Set color of the format. Used to specify the color of the text to be formatted.
  542. *
  543. * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
  544. */
  545. public function set_color($color) {
  546. $this->format['font']['color']['rgb'] = $this->parse_color($color);
  547. }
  548. /**
  549. * Standardise colour name.
  550. *
  551. * @param mixed $color name of the color (i.e.: 'blue', 'red', etc..), or an integer (range is [8...63]).
  552. * @return string the RGB color value
  553. */
  554. protected function parse_color($color) {
  555. if (strpos($color, '#') === 0) {
  556. // No conversion should be needed.
  557. return substr($color, 1);
  558. }
  559. if ($color > 7 and $color < 53) {
  560. $numbers = array(
  561. 8 => 'black',
  562. 12 => 'blue',
  563. 16 => 'brown',
  564. 15 => 'cyan',
  565. 23 => 'gray',
  566. 17 => 'green',
  567. 11 => 'lime',
  568. 14 => 'magenta',
  569. 18 => 'navy',
  570. 53 => 'orange',
  571. 33 => 'pink',
  572. 20 => 'purple',
  573. 10 => 'red',
  574. 22 => 'silver',
  575. 9 => 'white',
  576. 13 => 'yellow',
  577. );
  578. if (isset($numbers[$color])) {
  579. $color = $numbers[$color];
  580. } else {
  581. $color = 'black';
  582. }
  583. }
  584. $colors = array(
  585. 'aqua' => '00FFFF',
  586. 'black' => '000000',
  587. 'blue' => '0000FF',
  588. 'brown' => 'A52A2A',
  589. 'cyan' => '00FFFF',
  590. 'fuchsia' => 'FF00FF',
  591. 'gray' => '808080',
  592. 'grey' => '808080',
  593. 'green' => '00FF00',
  594. 'lime' => '00FF00',
  595. 'magenta' => 'FF00FF',
  596. 'maroon' => '800000',
  597. 'navy' => '000080',
  598. 'orange' => 'FFA500',
  599. 'olive' => '808000',
  600. 'pink' => 'FAAFBE',
  601. 'purple' => '800080',
  602. 'red' => 'FF0000',
  603. 'silver' => 'C0C0C0',
  604. 'teal' => '008080',
  605. 'white' => 'FFFFFF',
  606. 'yellow' => 'FFFF00',
  607. );
  608. if (isset($colors[$color])) {
  609. return($colors[$color]);
  610. }
  611. return($colors['black']);
  612. }
  613. /**
  614. * Not used.
  615. *
  616. * @param mixed $color
  617. */
  618. public function set_fg_color($color) {
  619. // Not implemented.
  620. }
  621. /**
  622. * Set background color of the cell.
  623. *
  624. * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
  625. */
  626. public function set_bg_color($color) {
  627. if (!isset($this->format['fill']['fillType'])) {
  628. $this->format['fill']['fillType'] = Fill::FILL_SOLID;
  629. }
  630. $this->format['fill']['color']['rgb'] = $this->parse_color($color);
  631. }
  632. /**
  633. * Set the cell fill pattern.
  634. *
  635. * @deprecated use set_bg_color() instead.
  636. * @param integer
  637. */
  638. public function set_pattern($pattern=1) {
  639. if ($pattern > 0) {
  640. if (!isset($this->format['fill']['color']['rgb'])) {
  641. $this->set_bg_color('black');
  642. }
  643. } else {
  644. unset($this->format['fill']['color']['rgb']);
  645. unset($this->format['fill']['fillType']);
  646. }
  647. }
  648. /**
  649. * Set text wrap of the format.
  650. */
  651. public function set_text_wrap() {
  652. $this->format['alignment']['wrapText'] = true;
  653. }
  654. /**
  655. * Set the cell alignment of the format.
  656. *
  657. * @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
  658. */
  659. public function set_align($location) {
  660. if (in_array($location, array('left', 'centre', 'center', 'right', 'fill', 'merge', 'justify', 'equal_space'))) {
  661. $this->set_h_align($location);
  662. } else if (in_array($location, array('top', 'vcentre', 'vcenter', 'bottom', 'vjustify', 'vequal_space'))) {
  663. $this->set_v_align($location);
  664. }
  665. }
  666. /**
  667. * Set the cell horizontal alignment of the format.
  668. *
  669. * @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
  670. */
  671. public function set_h_align($location) {
  672. switch ($location) {
  673. case 'left':
  674. $this->format['alignment']['horizontal'] = Alignment::HORIZONTAL_LEFT;
  675. break;
  676. case 'center':
  677. case 'centre':
  678. $this->format['alignment']['horizontal'] = Alignment::HORIZONTAL_CENTER;
  679. break;
  680. case 'right':
  681. $this->format['alignment']['horizontal'] = Alignment::HORIZONTAL_RIGHT;
  682. break;
  683. case 'justify':
  684. $this->format['alignment']['horizontal'] = Alignment::HORIZONTAL_JUSTIFY;
  685. break;
  686. default:
  687. $this->format['alignment']['horizontal'] = Alignment::HORIZONTAL_GENERAL;
  688. }
  689. }
  690. /**
  691. * Set the cell vertical alignment of the format.
  692. *
  693. * @param string $location alignment for the cell ('top', 'bottom', 'center', 'justify')
  694. */
  695. public function set_v_align($location) {
  696. switch ($location) {
  697. case 'top':
  698. $this->format['alignment']['vertical'] = Alignment::VERTICAL_TOP;
  699. break;
  700. case 'vcentre':
  701. case 'vcenter':
  702. case 'centre':
  703. case 'center':
  704. $this->format['alignment']['vertical'] = Alignment::VERTICAL_CENTER;
  705. break;
  706. case 'vjustify':
  707. case 'justify':
  708. $this->format['alignment']['vertical'] = Alignment::VERTICAL_JUSTIFY;
  709. break;
  710. default:
  711. $this->format['alignment']['vertical'] = Alignment::VERTICAL_BOTTOM;
  712. }
  713. }
  714. /**
  715. * Set the top border of the format.
  716. *
  717. * @param integer $style style for the cell. 1 => thin, 2 => thick
  718. */
  719. public function set_top($style) {
  720. if ($style == 1) {
  721. $this->format['borders']['top']['borderStyle'] = Border::BORDER_THIN;
  722. } else if ($style == 2) {
  723. $this->format['borders']['top']['borderStyle'] = Border::BORDER_THICK;
  724. } else {
  725. $this->format['borders']['top']['borderStyle'] = Border::BORDER_NONE;
  726. }
  727. }
  728. /**
  729. * Set the bottom border of the format.
  730. *
  731. * @param integer $style style for the cell. 1 => thin, 2 => thick
  732. */
  733. public function set_bottom($style) {
  734. if ($style == 1) {
  735. $this->format['borders']['bottom']['borderStyle'] = Border::BORDER_THIN;
  736. } else if ($style == 2) {
  737. $this->format['borders']['bottom']['borderStyle'] = Border::BORDER_THICK;
  738. } else {
  739. $this->format['borders']['bottom']['borderStyle'] = Border::BORDER_NONE;
  740. }
  741. }
  742. /**
  743. * Set the left border of the format.
  744. *
  745. * @param integer $style style for the cell. 1 => thin, 2 => thick
  746. */
  747. public function set_left($style) {
  748. if ($style == 1) {
  749. $this->format['borders']['left']['borderStyle'] = Border::BORDER_THIN;
  750. } else if ($style == 2) {
  751. $this->format['borders']['left']['borderStyle'] = Border::BORDER_THICK;
  752. } else {
  753. $this->format['borders']['left']['borderStyle'] = Border::BORDER_NONE;
  754. }
  755. }
  756. /**
  757. * Set the right border of the format.
  758. *
  759. * @param integer $style style for the cell. 1 => thin, 2 => thick
  760. */
  761. public function set_right($style) {
  762. if ($style == 1) {
  763. $this->format['borders']['right']['borderStyle'] = Border::BORDER_THIN;
  764. } else if ($style == 2) {
  765. $this->format['borders']['right']['borderStyle'] = Border::BORDER_THICK;
  766. } else {
  767. $this->format['borders']['right']['borderStyle'] = Border::BORDER_NONE;
  768. }
  769. }
  770. /**
  771. * Set cells borders to the same style.
  772. *
  773. * @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
  774. */
  775. public function set_border($style) {
  776. $this->set_top($style);
  777. $this->set_bottom($style);
  778. $this->set_left($style);
  779. $this->set_right($style);
  780. }
  781. /**
  782. * Set the numerical format of the format.
  783. * It can be date, time, currency, etc...
  784. *
  785. * @param mixed $numformat The numeric format
  786. */
  787. public function set_num_format($numformat) {
  788. $numbers = array();
  789. $numbers[1] = '0';
  790. $numbers[2] = '0.00';
  791. $numbers[3] = '#,##0';
  792. $numbers[4] = '#,##0.00';
  793. $numbers[11] = '0.00E+00';
  794. $numbers[12] = '# ?/?';
  795. $numbers[13] = '# ??/??';
  796. $numbers[14] = 'mm-dd-yy';
  797. $numbers[15] = 'd-mmm-yy';
  798. $numbers[16] = 'd-mmm';
  799. $numbers[17] = 'mmm-yy';
  800. $numbers[22] = 'm/d/yy h:mm';
  801. $numbers[49] = '@';
  802. if ($numformat !== 0 and in_array($numformat, $numbers)) {
  803. $this->format['numberFormat']['formatCode'] = $numformat;
  804. }
  805. if (!isset($numbers[$numformat])) {
  806. return;
  807. }
  808. $this->format['numberFormat']['formatCode'] = $numbers[$numformat];
  809. }
  810. }