Skip to content

Commit c56a583

Browse files
authored
Merge pull request #4006 from oleibman/issue4004
RTL Text Alignment in Xlsx Comment
2 parents 4a7fa14 + 5d1d867 commit c56a583

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4141
- Unallocated Cells Affected by Column/Row Insert/Delete [Issue #3933](https://github.com/PHPOffice/PhpSpreadsheet/issues/3933) [PR #3940](https://github.com/PHPOffice/PhpSpreadsheet/pull/3940)
4242
- Invalid Builtin Defined Name in Xls Reader [Issue #3935](https://github.com/PHPOffice/PhpSpreadsheet/issues/3935) [PR #3942](https://github.com/PHPOffice/PhpSpreadsheet/pull/3942)
4343
- Hidden Rows and Columns Tcpdf/Mpdf [PR #3945](https://github.com/PHPOffice/PhpSpreadsheet/pull/3945)
44+
- RTL Text Alignment in Xlsx Comments [Issue #4004](https://github.com/PHPOffice/PhpSpreadsheet/issues/4004) [PR #4006](https://github.com/PHPOffice/PhpSpreadsheet/pull/4006)
4445
- Protect Sheet But Allow Sort [Issue #3951](https://github.com/PHPOffice/PhpSpreadsheet/issues/3951) [PR #3956](https://github.com/PHPOffice/PhpSpreadsheet/pull/3956)
4546
- Default Value for Conditional::$text [PR #3946](https://github.com/PHPOffice/PhpSpreadsheet/pull/3946)
4647
- Table Filter Buttons [Issue #3988](https://github.com/PHPOffice/PhpSpreadsheet/issues/3988) [PR #3992](https://github.com/PHPOffice/PhpSpreadsheet/pull/3992)

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
11321132
$fillColor = strtoupper(substr((string) $shape['fillcolor'], 1));
11331133
$column = null;
11341134
$row = null;
1135+
$textHAlign = null;
11351136
$fillImageRelId = null;
11361137
$fillImageTitle = '';
11371138

@@ -1149,8 +1150,17 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
11491150
if (is_array($temp)) {
11501151
$column = $temp[0];
11511152
}
1153+
$temp = $clientData->xpath('.//x:TextHAlign');
1154+
if (!empty($temp)) {
1155+
$textHAlign = $temp[0];
1156+
}
11521157
}
11531158
}
1159+
$rowx = (string) $row;
1160+
$colx = (string) $column;
1161+
if (is_numeric($rowx) && is_numeric($colx) && $textHAlign !== null) {
1162+
$docSheet->getComment([1 + (int) $colx, 1 + (int) $rowx], false)->setAlignment((string) $textHAlign);
1163+
}
11541164

11551165
$fillImageRelNode = $shape->xpath('.//v:fill/@o:relid');
11561166
if (is_array($fillImageRelNode) && !empty($fillImageRelNode)) {

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@ public function removeComment(CellAddress|string|array $cellCoordinate): self
26262626
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
26272627
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
26282628
*/
2629-
public function getComment(CellAddress|string|array $cellCoordinate): Comment
2629+
public function getComment(CellAddress|string|array $cellCoordinate, bool $attachNew = true): Comment
26302630
{
26312631
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));
26322632

@@ -2645,7 +2645,9 @@ public function getComment(CellAddress|string|array $cellCoordinate): Comment
26452645

26462646
// If not, create a new comment.
26472647
$newComment = new Comment();
2648-
$this->comments[$cellAddress] = $newComment;
2648+
if ($attachNew) {
2649+
$this->comments[$cellAddress] = $newComment;
2650+
}
26492651

26502652
return $newComment;
26512653
}

src/PhpSpreadsheet/Writer/Xlsx/Comments.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66
use PhpOffice\PhpSpreadsheet\Comment;
77
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
88
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
9+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
910

1011
class Comments extends WriterPart
1112
{
13+
private const VALID_HORIZONTAL_ALIGNMENT = [
14+
Alignment::HORIZONTAL_CENTER,
15+
Alignment::HORIZONTAL_DISTRIBUTED,
16+
Alignment::HORIZONTAL_JUSTIFY,
17+
Alignment::HORIZONTAL_LEFT,
18+
Alignment::HORIZONTAL_RIGHT,
19+
];
20+
1221
/**
1322
* Write comments to XML format.
1423
*
@@ -223,6 +232,12 @@ private function writeVMLComment(XMLWriter $objWriter, string $cellReference, Co
223232
// x:AutoFill
224233
$objWriter->writeElement('x:AutoFill', 'False');
225234

235+
// x:TextHAlign horizontal alignment of text
236+
$alignment = strtolower($comment->getAlignment());
237+
if (in_array($alignment, self::VALID_HORIZONTAL_ALIGNMENT, true)) {
238+
$objWriter->writeElement('x:TextHAlign', ucfirst($alignment));
239+
}
240+
226241
// x:Row
227242
$objWriter->writeElement('x:Row', (string) ($row - 1));
228243

tests/PhpSpreadsheetTests/CommentTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ public function testRemoveComment(): void
9696
self::assertArrayHasKey('A2', $comments1);
9797
$sheet->removeComment('A2');
9898
self::assertEmpty($sheet->getComments());
99+
$spreadsheet->disconnectWorksheets();
99100
}
100101
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
9+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
10+
11+
class CommentAlignmentTest extends AbstractFunctional
12+
{
13+
public function testIssue4004(): void
14+
{
15+
$type = 'Xlsx';
16+
$spreadsheet = new Spreadsheet();
17+
$sheet = $spreadsheet->getActiveSheet();
18+
$sheet->getComment('A3')->getText()->createText('Comment');
19+
$sheet->getComment('A4')->getText()->createText('שלום');
20+
$sheet->getComment('A4')->setAlignment(Alignment::HORIZONTAL_RIGHT);
21+
22+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type);
23+
$spreadsheet->disconnectWorksheets();
24+
25+
self::assertCount(1, $reloadedSpreadsheet->getAllSheets());
26+
27+
$rsheet = $reloadedSpreadsheet->getActiveSheet();
28+
$comment1 = $rsheet->getComment('A3');
29+
self::assertSame('Comment', $comment1->getText()->getPlainText());
30+
self::assertSame('general', $comment1->getAlignment());
31+
$comment2 = $rsheet->getComment('A4');
32+
self::assertSame('שלום', $comment2->getText()->getPlainText());
33+
self::assertSame('Right', $comment2->getAlignment());
34+
35+
$reloadedSpreadsheet->disconnectWorksheets();
36+
}
37+
}

0 commit comments

Comments
 (0)