Skip to content

Commit 9512f54

Browse files
authored
Corrections for Xlsx Read Comments (#2329)
This change was suggested by issue #2316. There was a problem reading Xlsx comments which appeared with release 18.0 but which was already fixed in master. So no source change was needed to fix the issue, but I thought we should at least add the test case to our unit tests. In developing that case, I discovered that, although comment text was read correctly, there was a problem with comment author. In fact, there were two problems. One was new, with the namespacing changes - as in several other cases, the namespaced attribute `authorId` needed some special handling. However, another problem was much older - the code was checking `!empty($comment['authorId'])`, eliminating consideration of authorId=0, and should instead have been checking `isset`. Both problems are now fixed, and tested.
1 parent 1f08f16 commit 9512f54

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,10 @@ public function load(string $pFilename, int $flags = 0): Spreadsheet
957957
// Loop through contents
958958
$contentPath = self::xpathNoFalse($commentsFile, 'com:commentList/com:comment');
959959
foreach ($contentPath as $comment) {
960-
$commentModel = $docSheet->getComment((string) $comment['ref']);
961-
if (!empty($comment['authorId'])) {
962-
$commentModel->setAuthor($authors[(int) $comment['authorId']]);
960+
$commentx = $comment->attributes();
961+
$commentModel = $docSheet->getComment((string) $commentx['ref']);
962+
if (isset($commentx['authorId'])) {
963+
$commentModel->setAuthor($authors[(int) $commentx['authorId']]);
963964
}
964965
$commentModel->setText($this->parseRichText($comment->children($mainNS)->text));
965966
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CommentTest extends TestCase
9+
{
10+
public function testIssue2316(): void
11+
{
12+
$filename = 'tests/data/Reader/XLSX/issue.2316.xlsx';
13+
$reader = new Xlsx();
14+
$spreadsheet = $reader->load($filename);
15+
16+
$sheet = $spreadsheet->getActiveSheet();
17+
$comment = $sheet->getComment('A1');
18+
$commentString = (string) $comment;
19+
self::assertStringContainsString('編號長度限制:', $commentString);
20+
self::assertSame('jill.chen', $comment->getAuthor());
21+
$comment = $sheet->getComment('E1');
22+
$commentString = (string) $comment;
23+
self::assertStringContainsString('若為宅配物流僅能選「純配送」', $commentString);
24+
self::assertSame('Anderson Chen 陳宗棠', $comment->getAuthor());
25+
}
26+
}
13.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)