Skip to content

Problem with xls writer for conditional style #330

New issue

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

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

Already on GitHub? Sign in to your account

Closed
1 task done
A-contresens opened this issue Jan 10, 2018 · 3 comments
Closed
1 task done

Problem with xls writer for conditional style #330

A-contresens opened this issue Jan 10, 2018 · 3 comments
Labels
bug help wanted writer/xls Writer for MS BIFF-format (xls) spreadsheet files

Comments

@A-contresens
Copy link

A-contresens commented Jan 10, 2018

This is:

  • a bug report

What is the expected behavior?

Have same result for xlsx and xls writter

What is the current behavior?

This code works well with xlsx but does not works with the xls writer.

The conditional rules are applied but they doesn't have the colors information. The bold are ok.

What are the steps to reproduce?

<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\Fill;

$format = "xls";

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

$climateColors = array(
    "1" => "FF6666",
    "2" => "FF9966",
    "3" => "FFFF99",
    "4" => "99CC99",
    "5" => "66CC66",
);

foreach ($climateColors as $climateScore => $color) {
    $conditional = new Conditional();
    $conditional->setConditionType(Conditional::CONDITION_CELLIS)
    ->setOperatorType(Conditional::OPERATOR_EQUAL)
    ->addCondition($climateScore);
    $conditional->getStyle()->getFont()->getColor()->setARGB("FF".$color);
    $conditional->getStyle()->getFont()->setBold(true);
    $conditional->getStyle()->applyFromArray(
        ['fill' => [
                    'fillType' => Fill::FILL_SOLID,
                    'color' => ['argb' => "FF".$color],
                ],
            ]
    );
    $conditionalStyles[] = $conditional;
}

$sheet->setCellValue("A1",1);
$sheet->setCellValue("B1",2);
$sheet->setCellValue("C1",3);
$sheet->setCellValue("D1",4);
$sheet->setCellValue("E1",5);

$spreadsheet->getActiveSheet()->getStyle('A1:E1')->setConditionalStyles($conditionalStyles);

if($format == "xlsx"){
    $writer = new Xlsx($spreadsheet);
} else if ($format == "xls"){
    $writer = IOFactory::createWriter($spreadsheet, 'Xls');
}

$writer->save("/data/tmp/export/test.".$format);

Which versions of PhpSpreadsheet and PHP are affected?

php 7.1.4
PhpSpreadsheet 1.0.0

@PowerKiKi
Copy link
Member

PowerKiKi commented Jan 21, 2018

XLS format does not support custom colors, but only a few predefined one. From your list only FFFF99 is supported. That's a first issue that you might want to fix on your side.

But I can still confirm the bug for supported colors, with the following code:

<?php

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

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

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

$conditional = new Conditional();
$conditional->setConditionType(Conditional::CONDITION_CELLIS)
    ->setOperatorType(Conditional::OPERATOR_EQUAL)
    ->addCondition('1')
    ->getStyle()->applyFromArray(
        [
            'font' => [
                'bold' => true,
                'color' => ['argb' => "FF0000FF"],
            ],
            'fill' => [
                'fillType' => Fill::FILL_SOLID,
                'color' => ['argb' => "FFFF0000"],
            ],
        ]
    );

$sheet->getStyle('A1')->setConditionalStyles([$conditional]);
$sheet->setCellValue('A1', 1);

$writerXlsx = new Xlsx($spreadsheet);
$writerXls = new Xls($spreadsheet);

$writerXlsx->save("test.xlsx");
$writerXls->save("test.xls");

Expecting to see bue text over red background, like in XLSX, but don't get any background in XLS.

@PowerKiKi PowerKiKi added bug help wanted writer/xls Writer for MS BIFF-format (xls) spreadsheet files labels Jan 21, 2018
@stale
Copy link

stale bot commented Mar 22, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Mar 22, 2018
@stale stale bot closed this as completed Mar 29, 2018
@oleibman
Copy link
Collaborator

oleibman commented Jul 5, 2024

Should be fixed by PR #4030 and PR #4033.

@oleibman oleibman removed the stale label Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted writer/xls Writer for MS BIFF-format (xls) spreadsheet files
Development

No branches or pull requests

3 participants