Skip to content

Commit

Permalink
Remove Styles Property From Worksheet
Browse files Browse the repository at this point in the history
Styles are a property of Spreadsheet, not Worksheet. There is a Styles property in Worksheet, initially set to an empty array and *never* updated. This PR removes the property and the getStyles method of Worksheet. This is, technically, a breaking change, so it will be installed with PhpSpreadsheet V4 (PR PHPOffice#4240).
  • Loading branch information
oleibman committed Jan 28, 2025
1 parent 414f8a2 commit dd8fee5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
25 changes: 4 additions & 21 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Worksheet
/**
* Invalid characters in sheet title.
*/
private static array $invalidCharacters = ['*', ':', '/', '\\', '?', '[', ']'];
private const INVALID_CHARACTERS = ['*', ':', '/', '\\', '?', '[', ']'];

/**
* Parent spreadsheet.
Expand Down Expand Up @@ -157,13 +157,6 @@ class Worksheet
*/
private Protection $protection;

/**
* Collection of styles.
*
* @var Style[]
*/
private array $styles = [];

/**
* Conditional styles. Indexed by cell coordinate, e.g. 'A1'.
*/
Expand Down Expand Up @@ -399,7 +392,7 @@ public function getCellCollection(): Cells
*/
public static function getInvalidCharacters(): array
{
return self::$invalidCharacters;
return self::INVALID_CHARACTERS;
}

/**
Expand All @@ -417,7 +410,7 @@ private static function checkSheetCodeName(string $sheetCodeName): string
}
// Some of the printable ASCII characters are invalid: * : / \ ? [ ] and first and last characters cannot be a "'"
if (
(str_replace(self::$invalidCharacters, '', $sheetCodeName) !== $sheetCodeName)
(str_replace(self::INVALID_CHARACTERS, '', $sheetCodeName) !== $sheetCodeName)
|| (Shared\StringHelper::substring($sheetCodeName, -1, 1) == '\'')
|| (Shared\StringHelper::substring($sheetCodeName, 0, 1) == '\'')
) {
Expand All @@ -442,7 +435,7 @@ private static function checkSheetCodeName(string $sheetCodeName): string
private static function checkSheetTitle(string $sheetTitle): string
{
// Some of the printable ASCII characters are invalid: * : / \ ? [ ]
if (str_replace(self::$invalidCharacters, '', $sheetTitle) !== $sheetTitle) {
if (str_replace(self::INVALID_CHARACTERS, '', $sheetTitle) !== $sheetTitle) {
throw new Exception('Invalid character found in sheet title');
}

Expand Down Expand Up @@ -1392,16 +1385,6 @@ public function getColumnStyle(string $column): ?Style
);
}

/**
* Get styles.
*
* @return Style[]
*/
public function getStyles(): array
{
return $this->styles;
}

/**
* Get style for cell.
*
Expand Down
13 changes: 11 additions & 2 deletions tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ public function testMiscellaneous(): void
$invalid = Worksheet::getInvalidCharacters();
self::assertSame(['*', ':', '/', '\\', '?', '[', ']'], $invalid);
$worksheet = new Worksheet();
self::assertEmpty($worksheet->getStyles());
$coord1 = $worksheet->getCoordinates();
self::assertSame([], $coord1);
$worksheet->getCell('B3')->setValue(1);
$worksheet->getCell('G2')->setValue(2);
$coord2 = $worksheet->getCoordinates(false); // in order added
self::assertSame(['B3', 'G2'], $coord2);
$coord3 = $worksheet->getCoordinates(); // sorted by row then column
self::assertSame(['G2', 'B3'], $coord3);
$worksheet = new Worksheet();
$worksheet->disconnectCells();
self::assertSame([], $worksheet->getCoordinates());
$coord4 = $worksheet->getCoordinates();
self::assertSame([], $coord4);
}

public function testHighestColumn(): void
Expand Down

0 comments on commit dd8fee5

Please sign in to comment.