Skip to content

Commit d25979f

Browse files
authored
Merge pull request #4508 from oleibman/writestyles
Xlsx Style Writer Minor Refactoring
2 parents 0dbe154 + cb0f9c0 commit d25979f

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1111

1212
- VSTACK and HSTACK. [Issue #4485](https://github.com/PHPOffice/PhpSpreadsheet/issues/4485) [PR #4492](https://github.com/PHPOffice/PhpSpreadsheet/pull/4492)
1313
- TOCOL and TOROW. [PR #4493](https://github.com/PHPOffice/PhpSpreadsheet/pull/4493)
14+
- Support Current Office Theme. [PR #4500](https://github.com/PHPOffice/PhpSpreadsheet/pull/4500)
1415

1516
### Removed
1617

@@ -26,12 +27,15 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2627

2728
### Deprecated
2829

29-
- Nothing yet.
30+
- Theme constants COLOR_SCHEME_2013_PLUS_NAME (use COLOR_SCHEME_2013_2022_NAME) and COLOR_SCHEME_2013_PLUS (use COLOR_SCHEME_2013_2022).
3031

3132
### Fixed
3233

3334
- Various Writers RichText TextElement Should Inherit Cell Style. [Issue #1154](https://github.com/PHPOffice/PhpSpreadsheet/issues/1154) [PR #4487](https://github.com/PHPOffice/PhpSpreadsheet/pull/4487)
3435
- Minor Changes to FILTER function. [PR #4491](https://github.com/PHPOffice/PhpSpreadsheet/pull/4491)
36+
- Allow Xlsx Reader/Writer to support Font Charset. [Issue #2760](https://github.com/PHPOffice/PhpSpreadsheet/issues/2760) [PR #4501](https://github.com/PHPOffice/PhpSpreadsheet/pull/4501)
37+
- AutoColor for LibreOffice Dark Mode [Discussion 4502](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4502) [PR #4503](https://github.com/PHPOffice/PhpSpreadsheet/pull/4503)
38+
- Xlsx Style Writer Minor Refactoring. [PR #4508](https://github.com/PHPOffice/PhpSpreadsheet/pull/4508)
3539

3640
## 2025-05-26 - 4.3.1
3741

src/PhpSpreadsheet/Writer/Xlsx.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,40 @@ public function getWriterPartWorksheet(): Worksheet
249249
return $this->writerPartWorksheet;
250250
}
251251

252+
public function createStyleDictionaries(): void
253+
{
254+
$this->styleHashTable->addFromSource(
255+
$this->getWriterPartStyle()->allStyles(
256+
$this->spreadSheet
257+
)
258+
);
259+
$this->stylesConditionalHashTable->addFromSource(
260+
$this->getWriterPartStyle()->allConditionalStyles(
261+
$this->spreadSheet
262+
)
263+
);
264+
$this->fillHashTable->addFromSource(
265+
$this->getWriterPartStyle()->allFills(
266+
$this->spreadSheet
267+
)
268+
);
269+
$this->fontHashTable->addFromSource(
270+
$this->getWriterPartStyle()->allFonts(
271+
$this->spreadSheet
272+
)
273+
);
274+
$this->bordersHashTable->addFromSource(
275+
$this->getWriterPartStyle()->allBorders(
276+
$this->spreadSheet
277+
)
278+
);
279+
$this->numFmtHashTable->addFromSource(
280+
$this->getWriterPartStyle()->allNumberFormats(
281+
$this->spreadSheet
282+
)
283+
);
284+
}
285+
252286
/**
253287
* Save PhpSpreadsheet to file.
254288
*
@@ -275,12 +309,7 @@ public function save($filename, int $flags = 0): void
275309
}
276310

277311
// Create styles dictionaries
278-
$this->styleHashTable->addFromSource($this->getWriterPartStyle()->allStyles($this->spreadSheet));
279-
$this->stylesConditionalHashTable->addFromSource($this->getWriterPartStyle()->allConditionalStyles($this->spreadSheet));
280-
$this->fillHashTable->addFromSource($this->getWriterPartStyle()->allFills($this->spreadSheet));
281-
$this->fontHashTable->addFromSource($this->getWriterPartStyle()->allFonts($this->spreadSheet));
282-
$this->bordersHashTable->addFromSource($this->getWriterPartStyle()->allBorders($this->spreadSheet));
283-
$this->numFmtHashTable->addFromSource($this->getWriterPartStyle()->allNumberFormats($this->spreadSheet));
312+
$this->createStyleDictionaries();
284313

285314
// Create drawing dictionary
286315
$this->drawingHashTable->addFromSource($this->getWriterPartDrawing()->allDrawings($this->spreadSheet));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Writer\Xlsx as XlsxWriter;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class StylesWriterTest extends TestCase
12+
{
13+
public function testStylesWriter(): void
14+
{
15+
$spreadsheet = new Spreadsheet();
16+
17+
$writer = new XlsxWriter($spreadsheet);
18+
$writer->createStyleDictionaries();
19+
$writerStyle = new XlsxWriter\Style($writer);
20+
$data = $writerStyle->writeStyles($spreadsheet);
21+
self::assertStringContainsString(
22+
'<fonts count="1"><font><b val="0"/><i val="0"/><strike val="0"/><u val="none"/><sz val="11"/><color rgb="FF000000"/><name val="Calibri"/></font></fonts>',
23+
$data
24+
);
25+
$spreadsheet->disconnectWorksheets();
26+
}
27+
}

0 commit comments

Comments
 (0)