Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Column fill not working #4285

Closed
1 of 8 tasks
ls-gh opened this issue Dec 19, 2024 · 3 comments · Fixed by #4289
Closed
1 of 8 tasks

Column fill not working #4285

ls-gh opened this issue Dec 19, 2024 · 3 comments · Fixed by #4289

Comments

@ls-gh
Copy link

ls-gh commented Dec 19, 2024

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

All cells in the column are colored.

What is the current behavior?

Blank cells are not colored.

xlsx-screenshot01

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Fill;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A5', 'ABC');
$sheet->getStyle('A:A')->getFill()->setFillType(Fill::FILL_SOLID)
  ->getStartColor()->setARGB('FFFFFF00');

$writer = new Xlsx($spreadsheet);
$writer->save('output.xlsx');

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

Which versions of PhpSpreadsheet and PHP are affected?

PHP 8.4.1
PhpSpreadsheet 3.6.0

@oleibman
Copy link
Collaborator

This is an admittedly kludgey solution, and I will look into whether something better can be done. In the meantime, after setting the column style the way you want, pick a cell with the correct styling and:

$xfIndex = $sheet->getCell('A5')->getXfIndex();
$sheet->getColumnDimension('A')->setXfIndex($xfIndex);

@ls-gh
Copy link
Author

ls-gh commented Dec 20, 2024

Thank you for your answer.

I found another workaround.
applyFromArray works as expected.

$sheet->getStyle('A:A')->getFill()->applyFromArray([
  'fillType' => 'solid',
  'startColor' => ['argb' => 'FFFFFF00'],
]);

@oleibman
Copy link
Collaborator

Thank you for sharing that information. Your solution is much less kludgey than mine. I confess that I do not understand why applyFromArray works differently than applying the style directly, but I can confirm that it does.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Dec 27, 2024
Fix PHPOffice#4285. Documentation change only - no changed code. It is not clear to me why using individual set options when styling an entire column works differently than applyFromArray, but the latter produces the expected result and the former doesn't. I will continue to research why. However, in the meantime, we can at least document that applyFromArray is preferred for this operation.
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jan 14, 2025
This is the second recent occasion where style for whole row or column is behaving unexpectedly (see issue PHPOffice#4285). For the earlier issue, a documentation update was made to show the preferred method of styling. Setting styles for row(s) or column(s) in that way will work just fine, however retrieving the style doesn't yield the expected result. Although that problem can be overcome with existing code, simpler methods are needed, and this PR adds methods getRowStyle and getColumnStyle to Worksheet. I will continue to investigate why this problem and the one from 4285 have unexpected results.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants