Skip to content

Commit 12e74f4

Browse files
committed
Ignore Theme When Tint is Also Specified
Theme colors subject to tints occur in the wild, and should not be targeted by this PR. Our use case is an unmodified Theme color. There is probably a decent case to be made for adding a Tint property to Color, but that can come later if desired. `Color::setHyperlinkTheme` will now try to fill in `rgb` as well as `theme` if it can locate an appropriate value in the Spreadsheet Theme. This will permit non-Xlsx formats to at least duplicate the color that Xlsx uses for hyperlinks. Setting `Color::rgb\argb` will reset `theme` to none. If you want to use both, do rgb first, then theme. This will, I hope, avoid surpises for end-users.
1 parent 4521864 commit 12e74f4

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

docs/topics/recipes.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ finding a specific document in a file repository or a document
2020
management system. For example Microsoft Sharepoint uses document
2121
metadata to search for a specific document in its document lists.
2222

23-
<details>
23+
<details markdown>
2424
<summary>Click here for details of Spreadsheet Document Properties</summary>
2525

2626
These are accessed in MS Excel from the "Info" option on the "File" menu:
@@ -67,7 +67,7 @@ $spreadsheet->getProperties()
6767

6868
You can choose which properties to set or ignore.
6969

70-
<details>
70+
<details markdown>
7171
<summary>Click here for details of Property Getters/Setters</summary>
7272

7373
PhpSpreadsheet provides specific getters/setters for a number of pre-defined properties.
@@ -90,7 +90,7 @@ PhpSpreadsheet provides specific getters/setters for a number of pre-defined pro
9090
9191
</details>
9292

93-
<details>
93+
<details markdown>
9494
<summary>Click here for details of Custom Properties</summary>
9595

9696
Additionally, PhpSpreadsheet supports the creation and reading of custom properties for those file formats that accept custom properties.
@@ -143,7 +143,7 @@ $spreadsheet->getProperties()
143143

144144
A Spreadsheet consists of (very rarely) none, one or more Worksheets. If you have 1 or more Worksheets, then one (and only one) of those Worksheets can be "Active" (viewed or updated) at a time, but there will always be an "Active" Worksheet (unless you explicitly delete all of the Worksheets in the Spreadsheet).
145145

146-
<details>
146+
<details markdown>
147147
<summary>Click here for details about Worksheets</summary>
148148

149149
When you create a new Spreadsheet in MS Excel, it creates the Spreadsheet with a single Worksheet ("Sheet1")
@@ -278,7 +278,7 @@ define your own values as long as they are a valid MS Excel format.
278278
PhpSpreadsheet also provides a number of Wizards to help you create
279279
Date, Time and DateTime format masks.
280280

281-
<details>
281+
<details markdown>
282282
<summary>Click here for an example of the Date/Time Wizards</summary>
283283

284284
```php
@@ -657,7 +657,8 @@ $spreadsheet->getActiveSheet()
657657
->getFont()
658658
->setHyperlinkTheme();
659659
```
660-
This will set underline (all formats) and text color (Xlsx only).
660+
This will set underline (all formats) and text color (always
661+
for Xlsx, and usually for other formats).
661662

662663
## Setting Printer Options for Excel files
663664

@@ -840,7 +841,9 @@ $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();
840841
$drawing->setName('PhpSpreadsheet logo');
841842
$drawing->setPath('./images/PhpSpreadsheet_logo.png');
842843
$drawing->setHeight(36);
843-
$spreadsheet->getActiveSheet()->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);
844+
$spreadsheet->getActiveSheet()
845+
->getHeaderFooter()
846+
->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);
844847
```
845848

846849
### Setting printing breaks on a row or column
@@ -2034,7 +2037,11 @@ $richText->createText('This invoice is ');
20342037
$payable = $richText->createTextRun('payable within thirty days after the end of the month');
20352038
$payable->getFont()->setBold(true);
20362039
$payable->getFont()->setItalic(true);
2037-
$payable->getFont()->setColor( new \PhpOffice\PhpSpreadsheet\Style\Color( \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN ) );
2040+
$payable->getFont()->setColor(
2041+
new \PhpOffice\PhpSpreadsheet\Style\Color(
2042+
\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN
2043+
)
2044+
);
20382045
$richText->createText(', unless specified otherwise on the invoice.');
20392046
$spreadsheet->getActiveSheet()->getCell('A18')->setValue($richText);
20402047
```

docs/topics/settings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ then the `setLocale()` method will return an error, and American English
4242
More details of the features available once a locale has been set,
4343
including a list of the languages and locales currently supported, can
4444
be found in [Locale Settings for
45-
Formulae](./recipes.md#locale-settings-for-formulae).
45+
Formulas](./recipes.md#locale-settings-for-formulas).
4646

4747
## HTTP client
4848

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ extra_css:
77
- extra/extra.css
88
extra_javascript:
99
- extra/extrajs.js
10+
markdown_extensions:
11+
- md_in_html

src/PhpSpreadsheet/Reader/Xlsx/Styles.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ public function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml):
104104
$attr = $this->getStyleAttributes($fontStyleXml->strike);
105105
$fontStyle->setStrikethrough(!isset($attr['val']) || self::boolean((string) $attr['val']));
106106
}
107-
$theme = $this->readColorTheme($fontStyleXml->color);
108-
if ($theme >= 0) {
109-
$fontStyle->getColor()->setTheme($theme);
110-
}
111107
$fontStyle->getColor()
112108
->setARGB(
113109
$this->readColor($fontStyleXml->color)
114110
);
111+
$theme = $this->readColorTheme($fontStyleXml->color);
112+
if ($theme >= 0) {
113+
$fontStyle->getColor()->setTheme($theme);
114+
}
115115

116116
if (isset($fontStyleXml->u)) {
117117
$attr = $this->getStyleAttributes($fontStyleXml->u);
@@ -408,8 +408,12 @@ public function readProtectionHidden(Style $docStyle, SimpleXMLElement $style):
408408
public function readColorTheme(SimpleXMLElement $color): int
409409
{
410410
$attr = $this->getStyleAttributes($color);
411+
$retVal = -1;
412+
if (isset($attr['theme']) && is_numeric((string) $attr['theme']) && !isset($attr['tint'])) {
413+
$retVal = (int) $attr['theme'];
414+
}
411415

412-
return isset($attr['theme']) ? (int) $attr['theme'] : -1;
416+
return $retVal;
413417
}
414418

415419
public function readColor(SimpleXMLElement $color, bool $background = false): string

src/PhpSpreadsheet/Style/Color.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public function getARGB(): ?string
248248
public function setARGB(?string $colorValue = self::COLOR_BLACK, bool $nullStringOkay = false): static
249249
{
250250
$this->hasChanged = true;
251+
$this->setTheme(-1);
251252
if (!$nullStringOkay || $colorValue !== '') {
252253
$colorValue = $this->validateColor($colorValue);
253254
if ($colorValue === '') {
@@ -464,6 +465,14 @@ public function setTheme(int $theme): self
464465

465466
public function setHyperlinkTheme(): self
466467
{
468+
$rgb = $this->getActiveSheet()
469+
->getParent()
470+
?->getTheme()
471+
->getThemeColors();
472+
if (is_array($rgb) && array_key_exists('hlink', $rgb)) {
473+
$this->setRGB($rgb['hlink']);
474+
}
475+
467476
return $this->setTheme(Theme::HYPERLINK_THEME);
468477
}
469478
}

tests/PhpSpreadsheetTests/Reader/Xlsx/HyperlinkTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public function testReadAndWriteHyperlinks(): void
5555
$rsheet2 = $reloadedSpreadsheet->getSheet(1);
5656
self::assertSame('link to other sheet', $rsheet2->getCell('A2')->getValue());
5757
self::assertSame("sheet://'Sheet One'!A1", $rsheet2->getCell('A2')->getHyperlink()->getUrl());
58+
self::assertSame(
59+
'FF0000FF',
60+
$rsheet2
61+
->getStyle('A2')
62+
->getFont()->getColor()->getARGB(),
63+
'argb is set in addition to theme'
64+
);
5865

5966
self::assertSame('external link', $rsheet2->getCell('A3')->getValue());
6067
self::assertSame('https://www.example.com', $rsheet2->getCell('A3')->getHyperlink()->getUrl());

0 commit comments

Comments
 (0)