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.
This commit is contained in:
oleibman 2021-10-23 17:32:44 -07:00 committed by GitHub
parent 1f08f160ad
commit 9512f54cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 3 deletions

View File

@ -957,9 +957,10 @@ class Xlsx extends BaseReader
// Loop through contents
$contentPath = self::xpathNoFalse($commentsFile, 'com:commentList/com:comment');
foreach ($contentPath as $comment) {
$commentModel = $docSheet->getComment((string) $comment['ref']);
if (!empty($comment['authorId'])) {
$commentModel->setAuthor($authors[(int) $comment['authorId']]);
$commentx = $comment->attributes();
$commentModel = $docSheet->getComment((string) $commentx['ref']);
if (isset($commentx['authorId'])) {
$commentModel->setAuthor($authors[(int) $commentx['authorId']]);
}
$commentModel->setText($this->parseRichText($comment->children($mainNS)->text));
}

View File

@ -0,0 +1,26 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;
class CommentTest extends TestCase
{
public function testIssue2316(): void
{
$filename = 'tests/data/Reader/XLSX/issue.2316.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
$comment = $sheet->getComment('A1');
$commentString = (string) $comment;
self::assertStringContainsString('編號長度限制:', $commentString);
self::assertSame('jill.chen', $comment->getAuthor());
$comment = $sheet->getComment('E1');
$commentString = (string) $comment;
self::assertStringContainsString('若為宅配物流僅能選「純配送」', $commentString);
self::assertSame('Anderson Chen 陳宗棠', $comment->getAuthor());
}
}

Binary file not shown.