Search code examples
phpphpexcelphpspreadsheet

PHPSpreadsheet - Unable to get Cell Fill Color


I am working with PHPSpreadsheet to handle some tasks. What I am trying to do is get the fill color of a field.

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\Font;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("missingean.xlsx");

$allMissing = $spreadsheet->getSheet(1)->getStyle('B4');

echo "<pre>"; print_r($allMissing); echo "</pre>";

This is my code. Here is the fill response:

 [fill:protected] => PhpOffice\PhpSpreadsheet\Style\Fill Object
        (
            [startcolorIndex] => 
            [endcolorIndex] => 
            [fillType:protected] => none
            [rotation:protected] => 0
            [startColor:protected] => PhpOffice\PhpSpreadsheet\Style\Color Object
                (
                    [argb:protected] => FFFFFFFF
                    [isSupervisor:protected] => 1
                    [parent:protected] => PhpOffice\PhpSpreadsheet\Style\Fill Object
 *RECURSION*
                    [parentPropertyName:protected] => startColor
                )

            [endColor:protected] => PhpOffice\PhpSpreadsheet\Style\Color Object
                (
                    [argb:protected] => FF000000
                    [isSupervisor:protected] => 1
                    [parent:protected] => PhpOffice\PhpSpreadsheet\Style\Fill Object
 *RECURSION*
                    [parentPropertyName:protected] => endColor
                )

            [isSupervisor:protected] => 1
            [parent:protected] => PhpOffice\PhpSpreadsheet\Style\Style Object
 *RECURSION*
            [parentPropertyName:protected] => 
        )

As I understand, the endColor should be the fill color here. My issue, though, is that the ARGB value here is always FF000000, no matter which field I target. I have many yellow fields in my Xlsx-file and those are the ones of interest for me. Doesn't matter if I target a white cell or a yellow cell, the endColor stays the same.

Am I going about this wrongly or is there a trick to get this working?


Solution

  • I've also been having trouble with this one. At least for my Xlsx files, the background color appears to be stored as the StartColor. EndColor is aways coming back as white. Here is the code I used.

    $spreadsheet->getSheet(1)->getStyle('B4')->getFill()->getStartColor()->getRGB();