Skip to content

Commit 6d9c0a4

Browse files
authored
Merge pull request #4373 from YoheiZuho/feature/justifyLastLine
support justifyLastLine
2 parents 8c86caa + 53a984b commit 6d9c0a4

File tree

8 files changed

+138
-9
lines changed

8 files changed

+138
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

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

1415
### Removed

docs/topics/recipes.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,15 +1188,16 @@ quotePrefix | setQuotePrefix()
11881188

11891189
**\PhpOffice\PhpSpreadsheet\Style\Alignment**
11901190

1191-
Array key | Maps to property
1192-
------------|-------------------
1193-
horizontal | setHorizontal()
1194-
indent | setIndent()
1195-
readOrder | setReadOrder()
1196-
shrinkToFit | setShrinkToFit()
1197-
textRotation| setTextRotation()
1198-
vertical | setVertical()
1199-
wrapText | setWrapText()
1191+
Array key | Maps to property
1192+
----------------|-------------------
1193+
horizontal | setHorizontal()
1194+
justifyLastLine | setJustifyLastLine()
1195+
indent | setIndent()
1196+
readOrder | setReadOrder()
1197+
shrinkToFit | setShrinkToFit()
1198+
textRotation | setTextRotation()
1199+
vertical | setVertical()
1200+
wrapText | setWrapText()
12001201

12011202
**\PhpOffice\PhpSpreadsheet\Style\Border**
12021203

src/PhpSpreadsheet/Reader/Xlsx/Styles.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ public function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $align
265265
if ($horizontal !== '') {
266266
$alignment->setHorizontal($horizontal);
267267
}
268+
$justifyLastLine = (string) $this->getAttribute($alignmentXml, 'justifyLastLine');
269+
if ($justifyLastLine !== '') {
270+
$alignment->setJustifyLastLine(
271+
self::boolean($justifyLastLine)
272+
);
273+
}
268274
$vertical = (string) $this->getAttribute($alignmentXml, 'vertical');
269275
if ($vertical !== '') {
270276
$alignment->setVertical($vertical);

src/PhpSpreadsheet/Style/Alignment.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ class Alignment extends Supervisor
9797
*/
9898
protected ?string $horizontal = self::HORIZONTAL_GENERAL;
9999

100+
/**
101+
* Justify Last Line alignment.
102+
*/
103+
protected ?bool $justifyLastLine = null;
104+
100105
/**
101106
* Vertical alignment.
102107
*/
@@ -196,6 +201,9 @@ public function applyFromArray(array $styleArray): static
196201
if (isset($styleArray['horizontal'])) {
197202
$this->setHorizontal($styleArray['horizontal']);
198203
}
204+
if (isset($styleArray['justifyLastLine'])) {
205+
$this->setJustifyLastLine($styleArray['justifyLastLine']);
206+
}
199207
if (isset($styleArray['vertical'])) {
200208
$this->setVertical($styleArray['vertical']);
201209
}
@@ -255,6 +263,35 @@ public function setHorizontal(string $horizontalAlignment): static
255263
return $this;
256264
}
257265

266+
/**
267+
* Get Justify Last Line.
268+
*/
269+
public function getJustifyLastLine(): ?bool
270+
{
271+
if ($this->isSupervisor) {
272+
return $this->getSharedComponent()->getJustifyLastLine();
273+
}
274+
275+
return $this->justifyLastLine;
276+
}
277+
278+
/**
279+
* Set Justify Last Line.
280+
*
281+
* @return $this
282+
*/
283+
public function setJustifyLastLine(bool $justifyLastLine): static
284+
{
285+
if ($this->isSupervisor) {
286+
$styleArray = $this->getStyleArray(['justifyLastLine' => $justifyLastLine]);
287+
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
288+
} else {
289+
$this->justifyLastLine = $justifyLastLine;
290+
}
291+
292+
return $this;
293+
}
294+
258295
/**
259296
* Get Vertical.
260297
*/
@@ -475,6 +512,7 @@ public function getHashCode(): string
475512

476513
return md5(
477514
$this->horizontal
515+
. (($this->justifyLastLine === null) ? 'null' : ($this->justifyLastLine ? 't' : 'f'))
478516
. $this->vertical
479517
. $this->textRotation
480518
. ($this->wrapText ? 't' : 'f')
@@ -489,6 +527,7 @@ protected function exportArray1(): array
489527
{
490528
$exportedArray = [];
491529
$this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal());
530+
$this->exportArray2($exportedArray, 'justifyLastLine', $this->getJustifyLastLine());
492531
$this->exportArray2($exportedArray, 'indent', $this->getIndent());
493532
$this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder());
494533
$this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit());

src/PhpSpreadsheet/Writer/Xlsx/Style.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ private function writeCellStyleXf(XMLWriter $objWriter, \PhpOffice\PhpSpreadshee
457457
if ($vertical !== '') {
458458
$objWriter->writeAttribute('vertical', $vertical);
459459
}
460+
$justifyLastLine = $style->getAlignment()->getJustifyLastLine();
461+
if (is_bool($justifyLastLine)) {
462+
$objWriter->writeAttribute('justifyLastLine', (string) (int) $justifyLastLine);
463+
}
460464

461465
if ($style->getAlignment()->getTextRotation() >= 0) {
462466
$textRotation = $style->getAlignment()->getTextRotation();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
9+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
10+
11+
class AlignmentTest extends AbstractFunctional
12+
{
13+
private ?Spreadsheet $spreadsheet = null;
14+
15+
private ?Spreadsheet $reloadedSpreadsheet = null;
16+
17+
protected function tearDown(): void
18+
{
19+
if ($this->spreadsheet !== null) {
20+
$this->spreadsheet->disconnectWorksheets();
21+
$this->spreadsheet = null;
22+
}
23+
if ($this->reloadedSpreadsheet !== null) {
24+
$this->reloadedSpreadsheet->disconnectWorksheets();
25+
$this->reloadedSpreadsheet = null;
26+
}
27+
}
28+
29+
public function testJustifyLastLine(): void
30+
{
31+
$this->spreadsheet = new Spreadsheet();
32+
$sheet = $this->spreadsheet->getActiveSheet();
33+
$sheet->setCellValue('A1', 'ABC');
34+
$sheet->setCellValue('A2', 'DEF');
35+
$sheet->setCellValue('A3', 'GHI');
36+
$sheet->getStyle('A1')
37+
->getAlignment()
38+
->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED)
39+
->setJustifyLastLine(true);
40+
$sheet->getStyle('A2')
41+
->getAlignment()
42+
->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED)
43+
->setJustifyLastLine(false);
44+
$sheet->getStyle('A3')
45+
->getAlignment()
46+
->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED);
47+
$this->reloadedSpreadsheet = $this->writeAndReload($this->spreadsheet, 'Xlsx');
48+
$rsheet = $this->reloadedSpreadsheet->getActiveSheet();
49+
self::assertTrue(
50+
$rsheet->getStyle('A1')
51+
->getAlignment()
52+
->getJustifyLastLine()
53+
);
54+
self::assertFalse(
55+
$rsheet->getStyle('A2')
56+
->getAlignment()
57+
->getJustifyLastLine()
58+
);
59+
self::assertNull(
60+
$rsheet->getStyle('A3')
61+
->getAlignment()
62+
->getJustifyLastLine()
63+
);
64+
}
65+
}

tests/PhpSpreadsheetTests/Style/AlignmentTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ public function testHorizontal(): void
8484
self::assertEquals(0, $cell3->getStyle()->getAlignment()->getIndent());
8585
}
8686

87+
public function testJustifyLastLine(): void
88+
{
89+
$this->spreadsheet = new Spreadsheet();
90+
$sheet = $this->spreadsheet->getActiveSheet();
91+
$cell1 = $sheet->getCell('A1');
92+
$cell1->setValue('ABC');
93+
$cell1->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED);
94+
$cell1->getStyle()->getAlignment()->setJustifyLastLine(true);
95+
self::assertTrue($cell1->getStyle()->getAlignment()->getJustifyLastLine());
96+
}
97+
8798
public function testReadOrder(): void
8899
{
89100
$this->spreadsheet = new Spreadsheet();

tests/PhpSpreadsheetTests/Writer/Xlsx/Issue3443Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testNonDefaultAlign(): void
3737
$rsheet = $reloadedSpreadsheet->getActiveSheet();
3838
$expected1 = [
3939
'horizontal' => 'center',
40+
'justifyLastLine' => null,
4041
'indent' => 0,
4142
'readOrder' => 0,
4243
'shrinkToFit' => false,
@@ -78,6 +79,7 @@ public function testDefaultAlign(): void
7879
$rsheet = $reloadedSpreadsheet->getActiveSheet();
7980
$expected1 = [
8081
'horizontal' => 'general',
82+
'justifyLastLine' => null,
8183
'indent' => 0,
8284
'readOrder' => 0,
8385
'shrinkToFit' => false,

0 commit comments

Comments
 (0)