Files
oleibman ba079b3967 Various Writers RichText TextElement Should Inherit Cell Style
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.
2025-05-25 21:22:12 -07:00

90 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xls as XlsReader;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xls as XlsWriter;
use PHPUnit\Framework\TestCase;
class RichTextTest extends TestCase
{
private string $filename = '';
protected function teardown(): void
{
if ($this->filename !== '') {
unlink($this->filename);
}
}
public function testRichText(): void
{
$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);
$fontStyle = $sheet->getStyle('A1')->getFont();
self::assertTrue($fontStyle->getBold());
self::assertFalse($fontStyle->getItalic());
$a1Value = $sheet->getCell('A1')->getValue();
self::assertInstanceOf(RichText::class, $a1Value);
$elements = $a1Value->getRichTextElements();
self::assertCount(3, $elements);
self::assertNull($elements[0]->getFont());
$fontStyle = $elements[1]->getFont();
self::assertNotNull($fontStyle);
self::assertFalse($fontStyle->getBold());
self::assertTrue($fontStyle->getItalic());
self::assertNull($elements[0]->getFont());
$this->filename = File::temporaryFilename();
$writer = new XlsWriter($spreadsheet);
$writer->save($this->filename);
$spreadsheet->disconnectWorksheets();
$this->readfile();
}
private function readfile(): void
{
$reader = new XlsReader();
$spreadsheet = $reader->load($this->filename);
$sheet = $spreadsheet->getActiveSheet();
$fontStyle = $sheet->getStyle('A1')->getFont();
self::assertTrue($fontStyle->getBold());
self::assertFalse($fontStyle->getItalic());
$a1Value = $sheet->getCell('A1')->getValue();
self::assertInstanceOf(RichText::class, $a1Value);
$elements = $a1Value->getRichTextElements();
self::assertCount(3, $elements);
// write/read has changed text to run but no real difference
$fontStyle = $elements[0]->getFont();
self::assertNotNull($fontStyle);
self::assertTrue($fontStyle->getBold());
self::assertFalse($fontStyle->getItalic());
$fontStyle = $elements[1]->getFont();
self::assertNotNull($fontStyle);
self::assertFalse($fontStyle->getBold());
self::assertTrue($fontStyle->getItalic());
// write/read has changed text to run but no real difference
$fontStyle = $elements[2]->getFont();
self::assertNotNull($fontStyle);
self::assertTrue($fontStyle->getBold());
self::assertFalse($fontStyle->getItalic());
$spreadsheet->disconnectWorksheets();
}
}