Skip to content

Commit

Permalink
Merge pull request #4006 from oleibman/issue4004
Browse files Browse the repository at this point in the history
RTL Text Alignment in Xlsx Comment
  • Loading branch information
oleibman committed May 5, 2024
2 parents 4a7fa14 + 5d1d867 commit c56a583
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- 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)
- 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)
- Hidden Rows and Columns Tcpdf/Mpdf [PR #3945](https://github.com/PHPOffice/PhpSpreadsheet/pull/3945)
- RTL Text Alignment in Xlsx Comments [Issue #4004](https://github.com/PHPOffice/PhpSpreadsheet/issues/4004) [PR #4006](https://github.com/PHPOffice/PhpSpreadsheet/pull/4006)
- Protect Sheet But Allow Sort [Issue #3951](https://github.com/PHPOffice/PhpSpreadsheet/issues/3951) [PR #3956](https://github.com/PHPOffice/PhpSpreadsheet/pull/3956)
- Default Value for Conditional::$text [PR #3946](https://github.com/PHPOffice/PhpSpreadsheet/pull/3946)
- Table Filter Buttons [Issue #3988](https://github.com/PHPOffice/PhpSpreadsheet/issues/3988) [PR #3992](https://github.com/PHPOffice/PhpSpreadsheet/pull/3992)
Expand Down
10 changes: 10 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$fillColor = strtoupper(substr((string) $shape['fillcolor'], 1));
$column = null;
$row = null;
$textHAlign = null;
$fillImageRelId = null;
$fillImageTitle = '';

Expand All @@ -1149,8 +1150,17 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
if (is_array($temp)) {
$column = $temp[0];
}
$temp = $clientData->xpath('.//x:TextHAlign');
if (!empty($temp)) {
$textHAlign = $temp[0];
}
}
}
$rowx = (string) $row;
$colx = (string) $column;
if (is_numeric($rowx) && is_numeric($colx) && $textHAlign !== null) {
$docSheet->getComment([1 + (int) $colx, 1 + (int) $rowx], false)->setAlignment((string) $textHAlign);
}

$fillImageRelNode = $shape->xpath('.//v:fill/@o:relid');
if (is_array($fillImageRelNode) && !empty($fillImageRelNode)) {
Expand Down
6 changes: 4 additions & 2 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2626,7 +2626,7 @@ public function removeComment(CellAddress|string|array $cellCoordinate): self
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*/
public function getComment(CellAddress|string|array $cellCoordinate): Comment
public function getComment(CellAddress|string|array $cellCoordinate, bool $attachNew = true): Comment
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));

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

// If not, create a new comment.
$newComment = new Comment();
$this->comments[$cellAddress] = $newComment;
if ($attachNew) {
$this->comments[$cellAddress] = $newComment;
}

return $newComment;
}
Expand Down
15 changes: 15 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
use PhpOffice\PhpSpreadsheet\Comment;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class Comments extends WriterPart
{
private const VALID_HORIZONTAL_ALIGNMENT = [
Alignment::HORIZONTAL_CENTER,
Alignment::HORIZONTAL_DISTRIBUTED,
Alignment::HORIZONTAL_JUSTIFY,
Alignment::HORIZONTAL_LEFT,
Alignment::HORIZONTAL_RIGHT,
];

/**
* Write comments to XML format.
*
Expand Down Expand Up @@ -223,6 +232,12 @@ private function writeVMLComment(XMLWriter $objWriter, string $cellReference, Co
// x:AutoFill
$objWriter->writeElement('x:AutoFill', 'False');

// x:TextHAlign horizontal alignment of text
$alignment = strtolower($comment->getAlignment());
if (in_array($alignment, self::VALID_HORIZONTAL_ALIGNMENT, true)) {
$objWriter->writeElement('x:TextHAlign', ucfirst($alignment));
}

// x:Row
$objWriter->writeElement('x:Row', (string) ($row - 1));

Expand Down
1 change: 1 addition & 0 deletions tests/PhpSpreadsheetTests/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,6 @@ public function testRemoveComment(): void
self::assertArrayHasKey('A2', $comments1);
$sheet->removeComment('A2');
self::assertEmpty($sheet->getComments());
$spreadsheet->disconnectWorksheets();
}
}
37 changes: 37 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/CommentAlignmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class CommentAlignmentTest extends AbstractFunctional
{
public function testIssue4004(): void
{
$type = 'Xlsx';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getComment('A3')->getText()->createText('Comment');
$sheet->getComment('A4')->getText()->createText('שלום');
$sheet->getComment('A4')->setAlignment(Alignment::HORIZONTAL_RIGHT);

$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type);
$spreadsheet->disconnectWorksheets();

self::assertCount(1, $reloadedSpreadsheet->getAllSheets());

$rsheet = $reloadedSpreadsheet->getActiveSheet();
$comment1 = $rsheet->getComment('A3');
self::assertSame('Comment', $comment1->getText()->getPlainText());
self::assertSame('general', $comment1->getAlignment());
$comment2 = $rsheet->getComment('A4');
self::assertSame('שלום', $comment2->getText()->getPlainText());
self::assertSame('Right', $comment2->getAlignment());

$reloadedSpreadsheet->disconnectWorksheets();
}
}

0 comments on commit c56a583

Please sign in to comment.