Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support justifyLastLine #4373

Merged
merged 12 commits into from
Feb 23, 2025
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Added

- Support Justify Last Line. [Issue #4374](https://github.com/PHPOffice/PhpSpreadsheet/issues/4374) [PR #4373](https://github.com/PHPOffice/PhpSpreadsheet/pull/4373)
- Allow Spreadsheet clone. [PR #437-](https://github.com/PHPOffice/PhpSpreadsheet/pull/4370)

### Removed
Expand Down
19 changes: 10 additions & 9 deletions docs/topics/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1188,15 +1188,16 @@ quotePrefix | setQuotePrefix()

**\PhpOffice\PhpSpreadsheet\Style\Alignment**

Array key | Maps to property
------------|-------------------
horizontal | setHorizontal()
indent | setIndent()
readOrder | setReadOrder()
shrinkToFit | setShrinkToFit()
textRotation| setTextRotation()
vertical | setVertical()
wrapText | setWrapText()
Array key | Maps to property
----------------|-------------------
horizontal | setHorizontal()
justifyLastLine | setJustifyLastLine()
indent | setIndent()
readOrder | setReadOrder()
shrinkToFit | setShrinkToFit()
textRotation | setTextRotation()
vertical | setVertical()
wrapText | setWrapText()

**\PhpOffice\PhpSpreadsheet\Style\Border**

Expand Down
6 changes: 6 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ public function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $align
if ($horizontal !== '') {
$alignment->setHorizontal($horizontal);
}
$justifyLastLine = (string) $this->getAttribute($alignmentXml, 'justifyLastLine');
if ($justifyLastLine !== '') {
$alignment->setJustifyLastLine(
self::boolean($justifyLastLine)
);
}
$vertical = (string) $this->getAttribute($alignmentXml, 'vertical');
if ($vertical !== '') {
$alignment->setVertical($vertical);
Expand Down
39 changes: 39 additions & 0 deletions src/PhpSpreadsheet/Style/Alignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class Alignment extends Supervisor
*/
protected ?string $horizontal = self::HORIZONTAL_GENERAL;

/**
* Justify Last Line alignment.
*/
protected ?bool $justifyLastLine = null;

/**
* Vertical alignment.
*/
Expand Down Expand Up @@ -196,6 +201,9 @@ public function applyFromArray(array $styleArray): static
if (isset($styleArray['horizontal'])) {
$this->setHorizontal($styleArray['horizontal']);
}
if (isset($styleArray['justifyLastLine'])) {
$this->setJustifyLastLine($styleArray['justifyLastLine']);
}
if (isset($styleArray['vertical'])) {
$this->setVertical($styleArray['vertical']);
}
Expand Down Expand Up @@ -255,6 +263,35 @@ public function setHorizontal(string $horizontalAlignment): static
return $this;
}

/**
* Get Justify Last Line.
*/
public function getJustifyLastLine(): ?bool
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getJustifyLastLine();
}

return $this->justifyLastLine;
}

/**
* Set Justify Last Line.
*
* @return $this
*/
public function setJustifyLastLine(bool $justifyLastLine): static
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['justifyLastLine' => $justifyLastLine]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->justifyLastLine = $justifyLastLine;
}

return $this;
}

/**
* Get Vertical.
*/
Expand Down Expand Up @@ -475,6 +512,7 @@ public function getHashCode(): string

return md5(
$this->horizontal
. (($this->justifyLastLine === null) ? 'null' : ($this->justifyLastLine ? 't' : 'f'))
. $this->vertical
. $this->textRotation
. ($this->wrapText ? 't' : 'f')
Expand All @@ -489,6 +527,7 @@ protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal());
$this->exportArray2($exportedArray, 'justifyLastLine', $this->getJustifyLastLine());
$this->exportArray2($exportedArray, 'indent', $this->getIndent());
$this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder());
$this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit());
Expand Down
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ private function writeCellStyleXf(XMLWriter $objWriter, \PhpOffice\PhpSpreadshee
if ($vertical !== '') {
$objWriter->writeAttribute('vertical', $vertical);
}
$justifyLastLine = $style->getAlignment()->getJustifyLastLine();
if (is_bool($justifyLastLine)) {
$objWriter->writeAttribute('justifyLastLine', (string) (int) $justifyLastLine);
}

if ($style->getAlignment()->getTextRotation() >= 0) {
$textRotation = $style->getAlignment()->getTextRotation();
Expand Down
65 changes: 65 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/AlignmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

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

class AlignmentTest extends AbstractFunctional
{
private ?Spreadsheet $spreadsheet = null;

private ?Spreadsheet $reloadedSpreadsheet = null;

protected function tearDown(): void
{
if ($this->spreadsheet !== null) {
$this->spreadsheet->disconnectWorksheets();
$this->spreadsheet = null;
}
if ($this->reloadedSpreadsheet !== null) {
$this->reloadedSpreadsheet->disconnectWorksheets();
$this->reloadedSpreadsheet = null;
}
}

public function testJustifyLastLine(): void
{
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'ABC');
$sheet->setCellValue('A2', 'DEF');
$sheet->setCellValue('A3', 'GHI');
$sheet->getStyle('A1')
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED)
->setJustifyLastLine(true);
$sheet->getStyle('A2')
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED)
->setJustifyLastLine(false);
$sheet->getStyle('A3')
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED);
$this->reloadedSpreadsheet = $this->writeAndReload($this->spreadsheet, 'Xlsx');
$rsheet = $this->reloadedSpreadsheet->getActiveSheet();
self::assertTrue(
$rsheet->getStyle('A1')
->getAlignment()
->getJustifyLastLine()
);
self::assertFalse(
$rsheet->getStyle('A2')
->getAlignment()
->getJustifyLastLine()
);
self::assertNull(
$rsheet->getStyle('A3')
->getAlignment()
->getJustifyLastLine()
);
}
}
11 changes: 11 additions & 0 deletions tests/PhpSpreadsheetTests/Style/AlignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public function testHorizontal(): void
self::assertEquals(0, $cell3->getStyle()->getAlignment()->getIndent());
}

public function testJustifyLastLine(): void
{
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
$cell1 = $sheet->getCell('A1');
$cell1->setValue('ABC');
$cell1->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED);
$cell1->getStyle()->getAlignment()->setJustifyLastLine(true);
self::assertTrue($cell1->getStyle()->getAlignment()->getJustifyLastLine());
}

public function testReadOrder(): void
{
$this->spreadsheet = new Spreadsheet();
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/Issue3443Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testNonDefaultAlign(): void
$rsheet = $reloadedSpreadsheet->getActiveSheet();
$expected1 = [
'horizontal' => 'center',
'justifyLastLine' => null,
'indent' => 0,
'readOrder' => 0,
'shrinkToFit' => false,
Expand Down Expand Up @@ -78,6 +79,7 @@ public function testDefaultAlign(): void
$rsheet = $reloadedSpreadsheet->getActiveSheet();
$expected1 = [
'horizontal' => 'general',
'justifyLastLine' => null,
'indent' => 0,
'readOrder' => 0,
'shrinkToFit' => false,
Expand Down