Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add TabColor and ColFmtNo support to ParseXLSX #26

Merged
merged 2 commits into from

2 participants

@felliott

Hello,

This patch adds two new features to ParseXLSX to make it compatible with ParseExcel. The first commit adds support for getting the tab color of the current worksheet. The second sets the undocumented ColFmtNo property of Spreadsheet::ParseExcel::Worksheet (see https://metacpan.org/source/DOUGW/Spreadsheet-ParseExcel-0.65/lib/Spreadsheet/ParseExcel.pm#L1367), which stores the column default formats. I have added tests for both features. Please let me know if there is anything you'd like me to fix or change. Thank you and have a great day!

Cheers,
Fitz

@doy
Owner
doy commented

Can you provide an example spreadsheet which demonstrates these features, so I can add it to the test suite?

@felliott
@doy
Owner
doy commented

Oh, I missed that. Yeah, if you could put that in a separate spreadsheet, that would be helpful - it's easier to keep track of that way, since Excel files don't diff well.

@felliott

I've split out the features into two new test files and rebased the commits on top of v0.15. Please let me know if I need to fix anything else. Thanks and have a great weekend!

Cheers,
Fitz

@doy doy merged commit 01f97f6 into doy:master
@doy
Owner
doy commented

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
16 lib/Spreadsheet/ParseXLSX.pm
@@ -136,6 +136,7 @@ sub _parse_sheet {
my @merged_cells;
+ my @column_formats;
my @column_widths;
my @row_heights;
@@ -195,8 +196,10 @@ sub _parse_sheet {
'col' => sub {
my ( $twig, $col ) = @_;
- $column_widths[ $_ - 1 ] = $col->att('width')
- for ( $col->att('min') .. $col->att('max') );
+ for my $colnum ($col->att('min')..$col->att('max')) {
+ $column_widths[$colnum - 1] = $col->att('width');
+ $column_formats[$colnum - 1] = $col->att('style');
+ }
$twig->purge;
},
@@ -226,6 +229,14 @@ sub _parse_sheet {
$twig->purge;
},
+ 'sheetPr/tabColor' => sub {
+ my ( $twig, $tab_color ) = @_;
+
+ $sheet->{TabColor} = $self->_color($sheet->{_Book}{Color}, $tab_color);
+
+ $twig->purge;
+ },
+
}
);
@@ -326,6 +337,7 @@ sub _parse_sheet {
$sheet->{ColWidth} = [
map { defined $_ ? 0+$_ : 0+$default_column_width } @column_widths
];
+ $sheet->{ColFmtNo} = \@column_formats;
}
View
25 t/column-formats.t
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+use Spreadsheet::ParseXLSX;
+
+my $wb = Spreadsheet::ParseXLSX->new->parse('t/data/column-formats.xlsx');
+my $ws = $wb->worksheet(0);
+
+ok(my $col_format_nos = $ws->{ColFmtNo});
+
+my @col_formats = map { $wb->{Format}[ $_ ] } @$col_format_nos;
+is_deeply($col_formats[0]->{Fill}, [1, '#FF0000', '#FFFFFF']);
+
+is($col_formats[1]->{AlignH}, 3);
+is($col_formats[1]->{AlignV}, 0);
+
+my $font = $col_formats[2]->{Font};
+is_deeply($font->{Name}, 'Arial');
+is_deeply($font->{Height}, 16);
+is_deeply($font->{Bold}, 1);
+
+
+done_testing;
View
BIN  t/data/column-formats.xlsx
Binary file not shown
View
BIN  t/data/tab-color.xlsx
Binary file not shown
View
15 t/tab-color.t
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+use Spreadsheet::ParseXLSX;
+
+my $wb = Spreadsheet::ParseXLSX->new->parse('t/data/tab-color.xlsx');
+my $ws1 = $wb->worksheet(0);
+is($ws1->get_tab_color, '#FF0000');
+
+my $ws2 = $wb->worksheet(1);
+is($ws2->get_tab_color, undef);
+
+done_testing;
Something went wrong with that request. Please try again.