1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
|
package Spreadsheet::ParseODS;
use strict;
use warnings;
use 5.010; # for "state"
use Archive::Zip ':ERROR_CODES';
use Moo 2;
use XML::Twig::XPath;
use Carp qw(croak);
use List::Util 'max';
our $VERSION = '0.39';
our @CARP_NOT = (qw(XML::Twig));
use Filter::signatures;
use feature 'signatures';
no warnings 'experimental::signatures';
use PerlX::Maybe;
use Scalar::Util 'weaken';
use Spreadsheet::ParseODS::Workbook;
use Spreadsheet::ParseODS::Worksheet;
use Spreadsheet::ParseODS::Cell;
use Spreadsheet::ParseODS::Styles;
use Spreadsheet::ParseODS::Settings;
=head1 NAME
Spreadsheet::ParseODS - read SXC and ODS files
=head1 SYNOPSIS
my $parser = Spreadsheet::ParseODS->new(
line_separator => "\n", # for multiline values
);
my $workbook = $parser->parse("$d/$file");
my $sheet = $workbook->worksheet('Sheet1');
=head1 WARNING
This module is not yet API-compatible with Spreadsheet::ParseXLSX
and Spreadsheet::ParseXLS. Method-level compatibility is planned, but there
always be differences in the values returned, for example for the cell
types.
=head1 METHODS
=head2 C<< ->new >>
=head3 Options
=over 4
=item *
B<line_separator> - the value to separate multi-line cell values with
=cut
has 'line_separator' => ( is => 'ro', default => "\n", );
=item *
B<readonly> - create the sheet as readonly, sharing Cells between repeated
rows. This uses less memory at the cost of not being able to modify the data
structure.
=cut
has 'readonly' => ( is => 'rw' );
=item *
B<NoTruncate> - legacy option not to truncate the sheets by stripping
empty columns from the right edge of a sheet. This option will likely be
renamed or moved.
=cut
has 'NoTruncate' => ( is => 'ro', default => 0, );
=item *
B<twig> - a premade L<XML::Twig::XPath> instance
=cut
has 'twig' => (
is => 'lazy',
default => sub {
XML::Twig::XPath->new(
no_xxe => 1,
keep_spaces => 1,
)
},
);
=back
=cut
# -----------------------------------------------------------------------------
# col2int (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
# converts a excel row letter into an int for use in an array
sub col2int {
my $result = 0;
my $str = shift;
my $incr = 1;
for ( my $i = length($str) ; $i > 0 ; $i-- ) {
my $char = substr( $str, $i - 1 );
my $curr += ord( lc($char) ) - ord('a') + 1;
$curr *= $incr;
$result += $curr;
$incr *= 26;
}
# this is one out as we range 0..x-1 not 1..x
$result--;
return $result;
}
# -----------------------------------------------------------------------------
# sheetRef (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
# -----------------------------------------------------------------------------
### sheetRef
# convert an excel letter-number address into a useful array address
# @note that also Excel uses X-Y notation, we normally use Y-X in arrays
# @args $str, excel coord eg. A2
# @returns an array - 2 elements - column, row, or undefined
#
sub sheetRef {
my $str = shift;
my @ret;
$str =~ m/^(\D+)(\d+)$/
or croak "Invalid cell address '$str'";
if ( $1 && $2 ) {
push( @ret, $2 - 1, col2int($1) );
}
if ( $ret[0] < 0 ) {
undef @ret;
}
return @ret;
}
sub _parse_printareas( $self, $printarea ) {
my $res = [];
while( $printarea =~ m!(?:'[^']+'|\w+)\.([A-Z]+)(\d+):(?:'[^']+'|\w+)\.([A-Z]+)(\d+)(?: |$)!gc) {
my( $w, $n, $e, $s ) = ($1,$2,$3,$4);
push @$res, [ $n-1, col2int($w), $s-1, col2int($e)];
};
return $res
}
=head2 C<< ->parse( %options ) >>
my $workbook = Spreadsheet::ParseODS->new()->parse( 'example.ods' );
Reads the spreadsheet into memory and returns the data as a
L<Spreadsheet::ParseODS::Workbook> object.
=head3 Options
=over 4
=item *
B<inputtype> - the type of file if passing a filehandle. Can be C<ods>, C<sxc>
, C<fods> or C<xml>.
=back
This method also takes the same options as the constructor.
=cut
sub _empty_cell( $self, $readonly, $is_merged ) {
state $merged_cell = Spreadsheet::ParseODS::Cell->new({
type => undef,
unformatted => undef,
value => undef,
formula => undef,
hyperlink => undef,
style => undef,
format => undef,
is_merged => 1,
is_hidden => undef,
});
state $empty_cell = Spreadsheet::ParseODS::Cell->new({
type => undef,
unformatted => undef,
value => undef,
formula => undef,
hyperlink => undef,
style => undef,
format => undef,
is_merged => undef,
is_hidden => undef,
});
if( $readonly ) {
return $is_merged ? $merged_cell : $empty_cell
} else {
return Spreadsheet::ParseODS::Cell->new({
type => undef,
unformatted => undef,
value => undef,
formula => undef,
hyperlink => undef,
style => undef,
format => undef,
is_merged => $is_merged,
is_hidden => undef,
});
}
}
sub parse( $self, $source, @options ) {
weaken $self;
my %options;
my $formatter;
if( @options % 2 == 0 ) {
%options = @options
} elsif( @options == 1 ) {
($formatter) = @options;
} else {
croak "Odd number of values passed to \%options hash";
};
if( $options{ attr }) {
die "We want to store cell attributes";
};
my $p = $self->twig;
my $readonly = $self->readonly;
if( exists $options{ readonly }) {
$readonly = $options{ readonly };
};
# Convert to ref, later
my %workbook = ();
my @worksheets = ();
my @sheet_order = ();
my %table_styles;
my $styles = Spreadsheet::ParseODS::Styles->new(); # the workbook style
my %settings;
my %handlers;
my %style_handlers;
my %setting_handlers;
$setting_handlers{ '//office:settings/config:config-item-set[@config:name="ooo:view-settings"]//config:config-item' } = sub {
my( $twig, $setting ) = @_;
if( $setting->att('config:name') eq 'ActiveTable' ) {
$settings{ active_sheet_name } = $setting->text;
};
};
$handlers{ "//office:automatic-styles/style:style" } =
$handlers{ "//office:styles/style:style" } =
$style_handlers{ "//office:automatic-styles/style:style" } =
$style_handlers{ "//office:styles/style:style" } = sub {
my( $twig, $style ) = @_;
my $style_name = $style->att('style:name');
my %style;
$style{ data_style_name } = $style->att('style:data-style-name');
($style{table_cell_properties}) = $style->findnodes('style:table-cell-properties');
if( my $prop = $style->first_child('style:table-properties')) {
my $display = $prop->att('table:display')
|| '';
my $tab_color = $prop->att('tableooo:tab-color');
$style{ display } = $display;
$style{ tab_color } = $tab_color;
};
$table_styles{ $style_name } = \%style;
};
$handlers{ "//office:styles" } =
$handlers{ "//office:automatic-styles" } =
$style_handlers{ "//office:styles" } =
$style_handlers{ "//office:automatic-styles" } = sub {
my( $twig, $style ) = @_;
$styles->read_from_twig( $style );
};
if( 0 ) {
# In case we have an FODS XML file where all sub-parts are contained within
# the same XML
$handlers{ "//office:styles" } = sub {
my( $twig, $style ) = @_;
$styles->read_from_twig( $style );
};
};
$handlers{ "table:table" } = sub {
my( $twig, $table ) = @_;
my $max_datarow = -1;
my $max_datacol = -1;
my @hidden_cols;
my @hidden_rows;
my @merged_areas;
my $tablename = $table->att('table:name');
my $tableref = $workbook{ $tablename } = [];
my $table_hidden = $table->att( 'table:visibility' ); # SXC
my $tab_color;
if( my $style_name = $table->att('table:style-name')) {
my $style = $table_styles{$style_name};
die "No style for '$style_name'" unless $style;
my $display = $style->{display}
|| '';
$table_hidden = $display eq 'false' ? 1 : undef;
$tab_color = $style->{tab_color};
};
my $print_areas;
# we currently only support one
if( my $print_area_attr = $table->att( 'table:print-ranges' )) {
$print_areas = $self->_parse_printareas($print_area_attr);
};
# Collect information on header columns
my @column_default_styles;
my ($header_col_start, $header_col_end) = (undef,undef);
my $colnum = -1;
for my $col ($table->findnodes('.//table:table-column')) {
$colnum++;
my $repeat = $col->att('table:number-columns-repeated') || 1;
if( my $style = $col->att('table:default-cell-style-name')) {
push @column_default_styles, ($style) x $repeat;
} else {
push @column_default_styles, (undef) x $repeat;
};
if( $col->parent->tag eq 'table:table-header-columns' ) {
$header_col_start = $colnum
unless defined $header_col_start;
$header_col_end = $colnum+$repeat-1;
};
$colnum += $repeat;
# if columns is hidden, add column number to @hidden_cols array for later use
my $col_visibility = $col->att('table:visibility') || '';
for (1..$repeat) {
push @hidden_cols, $col_visibility eq 'collapse';
};
};
my ($header_row_start, $header_row_end) = (undef,undef);
my @rows = $table->findnodes('.//table:table-row');
# Optimization hack: Find the last row that contains something
# This is necessary because a formatted column extends 1.000.000 rows
# downwards
my $last_payload_row = $#rows;
while( $last_payload_row >= 0
and !$rows[ $last_payload_row ]->findnodes('*[@office:value-type] | *[@table:value-type] | .//text:p')) {
$last_payload_row--
};
# Cut away the empty rows
$last_payload_row++ if $last_payload_row == -1;
splice @rows, $last_payload_row+1;
my $rownum = -1;
for my $row (@rows) {
$rownum++;
my $row_hidden = $row->att( 'table:visibility' ) || '';
my $rowref = [];
# Do we really only want to add a cell if it contains text?!
for my $cell ($row->findnodes("./table:table-cell | ./table:covered-table-cell")) {
my $colnum = @$rowref;
my $style_name = $cell->att('table:style-name')
|| $column_default_styles[ $colnum ];
# If there are repeats, they will respect
# changing styles anyway
my ($text);
my $type = $cell->att("office:value-type") # ODS
|| $cell->att("table:value-type") # SXC
|| '' ;
my ($unformatted) = grep { defined($_) }
$cell->att("office:value"), # ODS
$cell->att("table:value"), # SXC
$cell->att("office:date-value"), # ODS
$cell->att("table:date-value"), # SXC
$cell->att("office:time-value"), # ODS
$cell->att("table:time-value"), # SXC
;
my $formula = $cell->att("table:formula");
if( $formula ) {
$formula =~ s!^of:!!;
};
my $hyperlink;
my @hyperlink = $cell->findnodes('.//text:a');
if( @hyperlink ) {
$hyperlink = $hyperlink[0]->att('xlink:href');
};
my $repeat = $cell->att('table:number-columns-repeated') || 1;
my ($merge_source, $is_merged) = (undef, 0);
if( $cell->att('table:number-columns-spanned') || $cell->att('table:number-rows-spanned')) {
my $colspan = $cell->att('table:number-columns-spanned') || 0;
my $rowspan = $cell->att('table:number-rows-spanned') || 0;
push @merged_areas, [ $rownum, $colnum, $rownum + $rowspan -1, $colnum + $colspan -1 ];
$is_merged = 1;
} elsif( $cell->tag eq 'table:covered-table-cell') {
$is_merged = 1;
};
my @text = $cell->findnodes('text:p');
if( @text or $is_merged) {
$text = join $self->line_separator, map {
join '', map {
my $tag = $_->tag;
$tag eq '#PCDATA' ? $_->text
: $tag eq 'text:s' ? ' '
: $tag eq 'text:tab' ? "\t"
: $tag eq 'text:span' ? $_->text
: $tag eq 'text:a' ? $_->text
: warn "Unknown text tag " . $_->tag && ''
} $_->children;
} @text;
$max_datacol = max( $max_datacol, $#$rowref+$repeat );
} else {
$text = $unformatted;
};
# Yes, this is somewhat inefficient, but it saves us
# from later programming errors if we create/store
# references. We can always later turn this inside-out.
my $cell_obj;
if( $cell->is_empty ) {
$cell_obj = $self->_empty_cell( $readonly, $is_merged );
} else {
my $is_hidden;
my $f;
if( "Default" ne $style_name ) {
my $ts = $table_styles{ $style_name }
or croak "Internal error: Style '$style_name' encountered but not defined in XML";
my $s = $ts->{data_style_name};
# Find if the cell is protected/hidden
my ($cellprops) = $ts->{table_cell_properties};
if( $cellprops ) {
my $protect = $cellprops->att('style:cell-protect');
if( $protect ) {
$is_hidden = ($protect =~ /^(?:formula-hidden|hidden-and-protected)$/);
};
};
if( $s ) {
$f = $styles->styles->{ $s }->{format};
} else {
#warn "<<$style_name>>";
#warn "<<$s>>";
#use Data::Dumper;
#warn Dumper $styles->styles;
#die;
};
};
$cell_obj = Spreadsheet::ParseODS::Cell->new({
value => $text,
unformatted => defined $unformatted ? $unformatted : $text,
formula => $formula,
type => $type,
hyperlink => $hyperlink,
style => $style_name,
is_merged => $is_merged,
is_hidden => $is_hidden,
maybe 'format' => $f,
});
};
if( $readonly ) {
push @$rowref, ($cell_obj) x $repeat;
} else {
push @$rowref, $cell_obj;
for (2..$repeat) {
push @$rowref, (ref $cell_obj)->new( { %$cell_obj } );
};
};
};
# if number-rows-repeated is set, set $repeat_rows value accordingly for later use
my $row_repeat = $row->att('table:number-rows-repeated') || 1;
for my $r (1..$row_repeat) {
# clone the row unless there are no more repeated rows
#push @$tableref, $r < $row_repeat ? dclone( $rowref ) : $rowref;
# This is nasty but about 5 times faster than calling dclone()
if( $readonly ) {
push @$tableref, $rowref;
} else {
push @$tableref, $r < $row_repeat ? [map { bless { %$_ } => 'Spreadsheet::ParseODS::Cell'; } @$rowref ]: $rowref;
};
push @hidden_rows, $row_hidden;
$max_datarow++;
};
if( $row->parent->tag eq 'table:table-header-rows' ) {
$header_row_start = $#$tableref
unless defined $header_row_start;
$header_row_end = $#$tableref;
};
}
# truncate/expand table to $max_datarow and $max_datacol
if ( ! $self->NoTruncate ) {
$#{$tableref} = $max_datarow;
foreach ( @{$tableref} ) {
$#{$_} = $max_datacol;
}
}
@$tableref = ()
if $max_datacol < 0;
my $header_rows;
if( defined $header_row_start ) {
$header_rows = [$header_row_start, $header_row_end];
};
my $header_cols;
if( defined $header_col_start ) {
$header_cols = [$header_col_start, $header_col_end];
};
my $ws = Spreadsheet::ParseODS::Worksheet->new({
label => $tablename,
tab_color => $tab_color,
sheet_hidden => $table_hidden,
print_areas => $print_areas,
data => \@{$workbook{$tablename}},
col_min => 0,
col_max => $max_datacol || 0,
row_min => 0,
row_max => $max_datarow || 0,
header_rows => $header_rows,
header_cols => $header_cols,
hidden_rows => \@hidden_rows,
hidden_cols => \@hidden_cols,
table_styles => \%table_styles,
merged_areas => \@merged_areas,
});
# set up alternative data structure
push @worksheets, $ws;
$workbook{ $tablename } = $ws;
};
my $options = {};
# if we don't have an FODS monolithic file, read the styles separately
if( !$options{ inputtype } or $options{ inputtype } ne 'xml' ) {
my ($method, $xml) = $self->_open_xml_thing(
$source,
$options,
inputtype => $options{ inputtype },
member_file => 'styles.xml',
);
$p->setTwigHandlers( \%style_handlers );
$p->$method( $xml );
# read /settings.xml in addition, to fill stuff like ActiveSheet
($method, $xml) = $self->_open_xml_thing(
$source,
$options,
inputtype => $options{ inputtype },
member_file => 'settings.xml',
optional => 1,
);
if( defined $xml) {
$p->setTwigHandlers( \%setting_handlers );
$p->$method( $xml );
};
# Also maybe read /meta.xml for the remaining information
};
$p->setTwigHandlers( \%handlers );
my ($method, $xml) = $self->_open_xml_thing(
$source,
$options,
inputtype => $options{ inputtype },
member_file => 'content.xml',
);
$p->$method( $xml );
return Spreadsheet::ParseODS::Workbook->new(
%$options,
_worksheets => \%workbook,
_sheets => \@worksheets,
_settings => Spreadsheet::ParseODS::Settings->new( %settings ),
maybe _styles => $styles,
);
};
sub _open_xml_thing( $self, $source, $wb_info, %options ) {
my $ref = ref($source);
my $xml;
my $method = 'parse';
if( ! $ref ) {
# Specified by filename .
croak "Undef ODS source given"
unless defined $source;
$wb_info->{filename} = $source;
if( $source =~ m!(\.xml|\.fods)!i or ($options{ inputtype } and $options{ inputtype } =~ m!^(xml|fods)$! )) {
$method = 'parsefile';
$xml = $source;
} else {
$xml = $self->_open_sxc( $source, \%options );
};
} else {
if ( $ref eq 'SCALAR' ) {
# Specified by a scalar buffer.
# We create a copy here. Maybe we should be able to feed
# this to XML::Twig without creating (another) copy here?
# Or will CoW save us here anyway?
if( ($options{ inputtype } and $options{ inputtype } =~ m!^(xml|fods)$! )) {
$xml = $$source;
} else {
open my $fh, '<', $source;
$xml = $self->_open_sxc_fh( $fh, $options{member_file} );
};
} elsif ( $ref eq 'ARRAY' ) {
# Specified by file content
if( ($options{ inputtype } and $options{ inputtype } =~ m!^(xml|fods)$! )) {
$xml = join( '', @$source );
} else {
my $content = join( '', @$source );
open my $fh, '<', $content;
$xml = $self->_open_sxc_fh( $fh, $options{member_file} );
};
} else {
# Assume filehandle
# Kick off XML::Twig from Filehandle
#warn "Duplicated source";
open my $fh, '<&', $source;
$xml = $self->_open_sxc_fh( $fh, $options{ member_file });
}
}
return ($method, $xml)
}
sub _open_sxc {
my ($self, $sxc_file, $options_ref) = @_;
if( !$options_ref->{StrictErrors}) {
-f $sxc_file && -s *_ or return undef;
};
open my $fh, '<', $sxc_file
or croak "Couldn't open '$sxc_file': $!";
return $self->_open_sxc_fh( $fh, $options_ref->{member_file},
maybe optional => $options_ref->{optional}
);
}
sub _open_sxc_fh($self, $fh, $member, %options) {
my $zip = Archive::Zip->new();
my $status = $zip->readFromFileHandle($fh);
$status == AZ_OK
or croak "Read error from zip";
my $content = $zip->memberNamed($member);
if( ! defined $content ) {
if( $options{ optional }) {
return;
} else {
croak "Want to read $member' but it doesn't exist!";
}
}
$content->rewindData();
my $stream = $content->fh;
1 if eof($stream); # reset eof state of $stream?! Is that a bug? Where?
binmode $stream => ':gzip(none)';
return $stream
}
sub _build_styles( $self, $styles ) {
return Spreadsheet::ParseODS::Styles->new()
}
1;
=head1 REPOSITORY
The public repository of this module is
L<https://github.com/Corion/Spreadsheet-ReadSXC>.
=head1 SUPPORT
The public support forum of this module is L<https://perlmonks.org/>.
=head1 BUG TRACKER
Please report bugs in this module via the Github bug queue at
L<https://github.com/Corion/Spreadsheet-ReadSXC/issues>
=head1 AUTHOR
Max Maischein C<corion@cpan.org>
=head1 CONTRIBUTORS
H. Merijn Brand C<hmbrand@cpan.org>
Mohammad S Anwar C<manwar@cpan.org>
=head1 COPYRIGHT (c)
Copyright 2019-2024 by Max Maischein C<corion@cpan.org>.
=head1 LICENSE
This module is released under the same terms as Perl itself.
=cut
|