in reply to How to use Perl to assign different fill colors to columns in the same series of an Excel chart?

G'day CoVAX,

[Disclaimer: I'm not a regular user of Excel::Writer::XLSX — possibly last used a decade ago. Check any solutions by other monks; they may be much better.]

As your question revolved around colouring columns, I've just focussed on that aspect. I think the easiest thing would be to group the data by the required colours; that would avoid colouring individual cells. I believe the following does what you describe in terms of layout — I tested it by running this code as a standalone script and checking the pm_11166178.xlsx spreadsheet it created.

#!/usr/bin/env perl use v5.24; use warnings; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX::->new('pm_11166178.xlsx'); my $worksheet = $workbook->add_worksheet('C'); my @formats; { no warnings 'qw'; for my $colour (qw{#ED7D31 #4472C4 #00B050}) { push @formats, $workbook->add_format(bg_color => $colour); } } my @data = ( { token => [[qw{a b c}],[1,2,3]], row => 1, col => 0, format => $formats[0], }, { token => [[qw{d e f}],[4,5,6]], row => 1, col => 3, format => $formats[1], }, { token => [[qw{x y z}],[24,25,26]], row => 1, col => 6, format => $formats[2], }, ); for my $datum (@data) { $worksheet->write_col(@$datum{qw{row col token format}}); }

A hint for future reference: providing some ASCII art to describe the wanted layout can be better than a prosaic description (a picture paints a thousand words :-)

— Ken

  • Comment on Re: How to use Perl to assign different fill colors to columns in the same series of an Excel chart?
  • Select or Download Code