Skip to content

Commit 6ab27d2

Browse files
committed
Xlsx Writer Rich Text and TYPE_STRING
Fix #476. Another in the "better late than never" series, closed as stale in June 2018. Xlsx Writer expects cells containing RichText to have DataType `TYPE_INLINE`; but the spreadsheet associated with the issue has the cell defined as `TYPE_STRING`. Change Writer to handle RichText TYPE_STRING appropriately.
1 parent 1b68270 commit 6ab27d2

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ private function writeCell(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksh
15281528

15291529
break;
15301530
case 's': // String
1531-
$this->writeCellString($objWriter, $mappedType, $cellValueString, $flippedStringTable);
1531+
$this->writeCellString($objWriter, $mappedType, ($cellValue instanceof RichText) ? $cellValue : $cellValueString, $flippedStringTable);
15321532

15331533
break;
15341534
case 'f': // Formula
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PhpOffice\PhpSpreadsheet\RichText\RichText;
9+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
10+
11+
class Issue476Test extends AbstractFunctional
12+
{
13+
public function testIssue476(): void
14+
{
15+
// RichText written in a way usually used only by strings.
16+
$reader = new XlsxReader();
17+
$spreadsheet = $reader->load('tests/data/Writer/XLSX/issue.476.xlsx');
18+
19+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
20+
$spreadsheet->disconnectWorksheets();
21+
22+
$sheet = $reloadedSpreadsheet->getActiveSheet();
23+
$richText = $sheet->getCell('A1')->getValue();
24+
self::assertInstanceOf(RichText::class, $richText);
25+
$plainText = $richText->getPlainText();
26+
self::assertSame("Art. 1A of the Geneva Refugee Convention and Protocol or other international or national instruments.\n", $plainText);
27+
28+
$reloadedSpreadsheet->disconnectWorksheets();
29+
}
30+
}

tests/data/Writer/XLSX/issue.476.xlsx

8.82 KB
Binary file not shown.

0 commit comments

Comments
 (0)