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