mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-17 12:10:19 +00:00
ba079b3967
Fix #1154, which went stale over 5 years ago, and is now reopened. RichText elements can be a Run, which sets its own style, or a TextElement, which doesn't. As the issue states, TextElement handling is inconsistent. This PR forces it to inherit the style of the cell. As implemented, this will change it to a Run when it is read in, but there should be no practical difference as far as the end-user is concerned. There is no change as far as Run is concerned. The user suggested 3 options - TextElement and Run both inherit for any unspecified style elements, TextElement inherits and Run does not, neither inherits. Option 2 makes most sense to me. Option 1 may not even be possible (we can't tell if, say, the user explicityl set Italic to false, or if that was just the default choice). Tests are added for all of Xlsx, Xls, and Html. Html reading of RichText elements is not well-supported. The tests are fairly difficult to understand. A new sample is added to demonstrate this change.
23 lines
635 B
PHP
23 lines
635 B
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
$helper->log('Create new Spreadsheet object');
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
$rtf = new RichText();
|
|
$rtf->createText('~Cell Style~');
|
|
$rtf->createTextRun('~RTF Style~')->getFont()?->setItalic(true);
|
|
$rtf->createText('~No Style~');
|
|
|
|
$sheet->getCell('A1')->setValue($rtf);
|
|
$sheet->getStyle('A1')->getFont()->setBold(true);
|
|
|
|
// Save
|
|
$helper->write($spreadsheet, __FILE__, ['Xlsx', 'Xls', 'Html']);
|