Skip to content

Commit

Permalink
Corrections for Xlsx Read Comments (#2329)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oleibman authored Oct 24, 2021
1 parent 1f08f16 commit 9512f54
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,10 @@ public function load(string $pFilename, int $flags = 0): Spreadsheet
// 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));
}
Expand Down
26 changes: 26 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/CommentTest.php
Original file line number Diff line number Diff line change
@@ -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 added tests/data/Reader/XLSX/issue.2316.xlsx
Binary file not shown.

0 comments on commit 9512f54

Please sign in to comment.