Skip to content

[confusing] getStyle sets current selected fields #335

@chielsen

Description

@chielsen

As I only found out when looking at the source code, this doesn't work as expected:

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$first = $sheet->getStyle('A1:A10');
$second = $sheet->getStyle('A1:B10');

$first->applyFromArray(
	[
		'fill' => [
			'fillType'   => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
			'rotation'   => 90,
			'startcolor' => [
				'argb' => 'FFFFFF',
			],
			'endcolor'   => [
				'argb' => 'FFA0A0A0',
			],
		]
	]
);
$second->applyFromArray(
	['fill' =>
		 [
			 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
			 'color'    => ['argb' => 'FFe7c16a'],
		 ]
	]
);
$second->applyFromArray(['fill' =>
[
				'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
				'color' => ['argb' => 'FFe7c16a'],
			]
]);

The reason is that getStyle set the selected range. So when we do the second getStyle it overrides the first call. This is very confusion, especially since the name is getStyle

Proposal

It would be nice if the returned object holds the affected cells.
If that is too complicated, maybe just rename getStyle, so that it is clear it sets the selected cells.

Which versions of PhpSpreadsheet and PHP are affected?

1.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions