| 1 | package Spreadsheet::ParseExcel::SaveParser::Workbook; | ||
| 2 | |||
| 3 | ############################################################################### | ||
| 4 | # | ||
| 5 | # Spreadsheet::ParseExcel::SaveParser::Workbook - A class for SaveParser Workbooks. | ||
| 6 | # | ||
| 7 | # Used in conjunction with Spreadsheet::ParseExcel. | ||
| 8 | # | ||
| 9 | # Copyright (c) 2014 Douglas Wilson | ||
| 10 | # Copyright (c) 2009-2013 John McNamara | ||
| 11 | # Copyright (c) 2006-2008 Gabor Szabo | ||
| 12 | # Copyright (c) 2000-2006 Kawai Takanori | ||
| 13 | # | ||
| 14 | # perltidy with standard settings. | ||
| 15 | # | ||
| 16 | # Documentation after __END__ | ||
| 17 | # | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | |||
| 22 | use base 'Spreadsheet::ParseExcel::Workbook'; | ||
| 23 | our $VERSION = '0.66'; | ||
| 24 | |||
| 25 | #============================================================================== | ||
| 26 | # Spreadsheet::ParseExcel::SaveParser::Workbook | ||
| 27 | #============================================================================== | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my ( $sPkg, $oBook ) = @_; | ||
| 31 | return undef unless ( defined $oBook ); | ||
| 32 | my %oThis = %$oBook; | ||
| 33 | bless \%oThis, $sPkg; | ||
| 34 | |||
| 35 | # re-bless worksheets (and set their _Book properties !!!) | ||
| 36 | my $sWkP = ref($sPkg) || "$sPkg"; | ||
| 37 | $sWkP =~ s/Workbook$/Worksheet/; | ||
| 38 | map { bless( $_, $sWkP ); } @{ $oThis{Worksheet} }; | ||
| 39 | map { $_->{_Book} = \%oThis; } @{ $oThis{Worksheet} }; | ||
| 40 | return \%oThis; | ||
| 41 | } | ||
| 42 | |||
| 43 | #------------------------------------------------------------------------------ | ||
| 44 | # Parse (for Spreadsheet::ParseExcel::SaveParser::Workbook) | ||
| 45 | #------------------------------------------------------------------------------ | ||
| 46 | sub Parse { | ||
| 47 | my ( $sClass, $sFile, $oWkFmt ) = @_; | ||
| 48 | my $oBook = Spreadsheet::ParseExcel::Workbook->Parse( $sFile, $oWkFmt ); | ||
| 49 | bless $oBook, $sClass; | ||
| 50 | |||
| 51 | # re-bless worksheets (and set their _Book properties !!!) | ||
| 52 | my $sWkP = ref($sClass) || "$sClass"; | ||
| 53 | $sWkP =~ s/Workbook$/Worksheet/; | ||
| 54 | map { bless( $_, $sWkP ); } @{ $oBook->{Worksheet} }; | ||
| 55 | map { $_->{_Book} = $oBook; } @{ $oBook->{Worksheet} }; | ||
| 56 | return $oBook; | ||
| 57 | } | ||
| 58 | |||
| 59 | #------------------------------------------------------------------------------ | ||
| 60 | # SaveAs (for Spreadsheet::ParseExcel::SaveParser::Workbook) | ||
| 61 | #------------------------------------------------------------------------------ | ||
| 62 | sub SaveAs { | ||
| 63 | my ( $oBook, $sName ) = @_; | ||
| 64 | |||
| 65 | # Create a new Excel workbook | ||
| 66 | my $oWrEx = Spreadsheet::WriteExcel->new($sName); | ||
| 67 | $oWrEx->compatibility_mode(); | ||
| 68 | my %hFmt; | ||
| 69 | |||
| 70 | my $iNo = 0; | ||
| 71 | my @aAlH = ( | ||
| 72 | 'left', 'left', 'center', 'right', | ||
| 73 | 'fill', 'justify', 'merge', 'equal_space' | ||
| 74 | ); | ||
| 75 | my @aAlV = ( 'top', 'vcenter', 'bottom', 'vjustify', 'vequal_space' ); | ||
| 76 | |||
| 77 | foreach my $pFmt ( @{ $oBook->{Format} } ) { | ||
| 78 | my $oFmt = $oWrEx->addformat(); # Add Formats | ||
| 79 | unless ( $pFmt->{Style} ) { | ||
| 80 | $hFmt{$iNo} = $oFmt; | ||
| 81 | my $rFont = $pFmt->{Font}; | ||
| 82 | |||
| 83 | $oFmt->set_font( $rFont->{Name} ); | ||
| 84 | $oFmt->set_size( $rFont->{Height} ); | ||
| 85 | $oFmt->set_color( $rFont->{Color} ); | ||
| 86 | $oFmt->set_bold( $rFont->{Bold} ); | ||
| 87 | $oFmt->set_italic( $rFont->{Italic} ); | ||
| 88 | $oFmt->set_underline( $rFont->{Underline} ); | ||
| 89 | $oFmt->set_font_strikeout( $rFont->{Strikeout} ); | ||
| 90 | $oFmt->set_font_script( $rFont->{Super} ); | ||
| 91 | |||
| 92 | $oFmt->set_hidden( $rFont->{Hidden} ); #Add | ||
| 93 | |||
| 94 | $oFmt->set_locked( $pFmt->{Lock} ); | ||
| 95 | |||
| 96 | $oFmt->set_align( $aAlH[ $pFmt->{AlignH} ] ); | ||
| 97 | $oFmt->set_align( $aAlV[ $pFmt->{AlignV} ] ); | ||
| 98 | |||
| 99 | $oFmt->set_rotation( $pFmt->{Rotate} ); | ||
| 100 | |||
| 101 | $oFmt->set_num_format( | ||
| 102 | $oBook->{FmtClass}->FmtStringDef( $pFmt->{FmtIdx}, $oBook ) ); | ||
| 103 | |||
| 104 | $oFmt->set_text_wrap( $pFmt->{Wrap} ); | ||
| 105 | |||
| 106 | $oFmt->set_pattern( $pFmt->{Fill}->[0] ); | ||
| 107 | $oFmt->set_fg_color( $pFmt->{Fill}->[1] ) | ||
| 108 | if ( ( $pFmt->{Fill}->[1] >= 8 ) | ||
| 109 | && ( $pFmt->{Fill}->[1] <= 63 ) ); | ||
| 110 | $oFmt->set_bg_color( $pFmt->{Fill}->[2] ) | ||
| 111 | if ( ( $pFmt->{Fill}->[2] >= 8 ) | ||
| 112 | && ( $pFmt->{Fill}->[2] <= 63 ) ); | ||
| 113 | |||
| 114 | $oFmt->set_left( | ||
| 115 | ( $pFmt->{BdrStyle}->[0] > 7 ) ? 3 : $pFmt->{BdrStyle}->[0] ); | ||
| 116 | $oFmt->set_right( | ||
| 117 | ( $pFmt->{BdrStyle}->[1] > 7 ) ? 3 : $pFmt->{BdrStyle}->[1] ); | ||
| 118 | $oFmt->set_top( | ||
| 119 | ( $pFmt->{BdrStyle}->[2] > 7 ) ? 3 : $pFmt->{BdrStyle}->[2] ); | ||
| 120 | $oFmt->set_bottom( | ||
| 121 | ( $pFmt->{BdrStyle}->[3] > 7 ) ? 3 : $pFmt->{BdrStyle}->[3] ); | ||
| 122 | |||
| 123 | $oFmt->set_left_color( $pFmt->{BdrColor}->[0] ) | ||
| 124 | if ( ( $pFmt->{BdrColor}->[0] >= 8 ) | ||
| 125 | && ( $pFmt->{BdrColor}->[0] <= 63 ) ); | ||
| 126 | $oFmt->set_right_color( $pFmt->{BdrColor}->[1] ) | ||
| 127 | if ( ( $pFmt->{BdrColor}->[1] >= 8 ) | ||
| 128 | && ( $pFmt->{BdrColor}->[1] <= 63 ) ); | ||
| 129 | $oFmt->set_top_color( $pFmt->{BdrColor}->[2] ) | ||
| 130 | if ( ( $pFmt->{BdrColor}->[2] >= 8 ) | ||
| 131 | && ( $pFmt->{BdrColor}->[2] <= 63 ) ); | ||
| 132 | $oFmt->set_bottom_color( $pFmt->{BdrColor}->[3] ) | ||
| 133 | if ( ( $pFmt->{BdrColor}->[3] >= 8 ) | ||
| 134 | && ( $pFmt->{BdrColor}->[3] <= 63 ) ); | ||
| 135 | } | ||
| 136 | $iNo++; | ||
| 137 | } | ||
| 138 | for ( my $iSheet = 0 ; $iSheet < $oBook->{SheetCount} ; $iSheet++ ) { | ||
| 139 | my $oWkS = $oBook->{Worksheet}[$iSheet]; | ||
| 140 | my $oWrS = $oWrEx->addworksheet( $oWkS->{Name} ); | ||
| 141 | |||
| 142 | #Landscape | ||
| 143 | if ( !$oWkS->{Landscape} ) { # Landscape (0:Horizontal, 1:Vertical) | ||
| 144 | $oWrS->set_landscape(); | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | $oWrS->set_portrait(); | ||
| 148 | } | ||
| 149 | |||
| 150 | #Protect | ||
| 151 | if ( defined $oWkS->{Protect} ) | ||
| 152 | { # Protect ('':NoPassword, Password:Password) | ||
| 153 | if ( $oWkS->{Protect} ne '' ) { | ||
| 154 | $oWrS->protect( $oWkS->{Protect} ); | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | $oWrS->protect(); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | if ( $oWkS->{Scale} != 100 ) { | ||
| 161 | |||
| 162 | # Pages on fit with width and Heigt | ||
| 163 | $oWrS->fit_to_pages( $oWkS->{FitWidth}, $oWkS->{FitHeight} ); | ||
| 164 | |||
| 165 | #Print Scale and reset FitWidth/FitHeight | ||
| 166 | $oWrS->set_print_scale( $oWkS->{Scale} ); | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | |||
| 170 | #Print Scale | ||
| 171 | $oWrS->set_print_scale( $oWkS->{Scale} ); | ||
| 172 | |||
| 173 | # Pages on fit with width and Heigt | ||
| 174 | $oWrS->fit_to_pages( $oWkS->{FitWidth}, $oWkS->{FitHeight} ); | ||
| 175 | } | ||
| 176 | |||
| 177 | # Paper Size | ||
| 178 | $oWrS->set_paper( $oWkS->{PaperSize} ); | ||
| 179 | |||
| 180 | # Margin | ||
| 181 | $oWrS->set_margin_left( $oWkS->{LeftMargin} ); | ||
| 182 | $oWrS->set_margin_right( $oWkS->{RightMargin} ); | ||
| 183 | $oWrS->set_margin_top( $oWkS->{TopMargin} ); | ||
| 184 | $oWrS->set_margin_bottom( $oWkS->{BottomMargin} ); | ||
| 185 | |||
| 186 | # HCenter | ||
| 187 | $oWrS->center_horizontally() if ( $oWkS->{HCenter} ); | ||
| 188 | |||
| 189 | # VCenter | ||
| 190 | $oWrS->center_vertically() if ( $oWkS->{VCenter} ); | ||
| 191 | |||
| 192 | # Header, Footer | ||
| 193 | $oWrS->set_header( $oWkS->{Header}, $oWkS->{HeaderMargin} ); | ||
| 194 | $oWrS->set_footer( $oWkS->{Footer}, $oWkS->{FooterMargin} ); | ||
| 195 | |||
| 196 | # Print Area | ||
| 197 | if ( ref( $oBook->{PrintArea}[$iSheet] ) eq 'ARRAY' ) { | ||
| 198 | my $raP; | ||
| 199 | for $raP ( @{ $oBook->{PrintArea}[$iSheet] } ) { | ||
| 200 | $oWrS->print_area(@$raP); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | # Print Title | ||
| 205 | my $raW; | ||
| 206 | foreach $raW ( @{ $oBook->{PrintTitle}[$iSheet]->{Row} } ) { | ||
| 207 | $oWrS->repeat_rows(@$raW); | ||
| 208 | } | ||
| 209 | foreach $raW ( @{ $oBook->{PrintTitle}[$iSheet]->{Column} } ) { | ||
| 210 | $oWrS->repeat_columns(@$raW); | ||
| 211 | } | ||
| 212 | |||
| 213 | # Print Gridlines | ||
| 214 | if ( $oWkS->{PrintGrid} == 1 ) { | ||
| 215 | $oWrS->hide_gridlines(0); | ||
| 216 | } | ||
| 217 | else { | ||
| 218 | $oWrS->hide_gridlines(1); | ||
| 219 | } | ||
| 220 | |||
| 221 | # Print Headings | ||
| 222 | if ( $oWkS->{PrintHeaders} ) { | ||
| 223 | $oWrS->print_row_col_headers(); | ||
| 224 | } | ||
| 225 | |||
| 226 | # Horizontal Page Breaks | ||
| 227 | $oWrS->set_h_pagebreaks( @{ $oWkS->{HPageBreak} } ); | ||
| 228 | |||
| 229 | # Veritical Page Breaks | ||
| 230 | $oWrS->set_v_pagebreaks( @{ $oWkS->{VPageBreak} } ); | ||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | # PageStart => $oWkS->{PageStart}, # Page number for start | ||
| 235 | # UsePage => $oWkS->{UsePage}, # Use own start page number | ||
| 236 | # NoColor => $oWkS->{NoColor}, # Print in black-white | ||
| 237 | # Draft => $oWkS->{Draft}, # Print in draft mode | ||
| 238 | # Notes => $oWkS->{Notes}, # Print notes | ||
| 239 | # LeftToRight => $oWkS->{LeftToRight}, # Left to Right | ||
| 240 | |||
| 241 | |||
| 242 | for ( | ||
| 243 | my $iC = $oWkS->{MinCol} ; | ||
| 244 | defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; | ||
| 245 | $iC++ | ||
| 246 | ) | ||
| 247 | { | ||
| 248 | if ( defined $oWkS->{ColWidth}[$iC] ) { | ||
| 249 | if ( $oWkS->{ColWidth}[$iC] > 0 ) { | ||
| 250 | $oWrS->set_column( $iC, $iC, $oWkS->{ColWidth}[$iC] ) | ||
| 251 | ; #, undef, 1) ; | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | $oWrS->set_column( $iC, $iC, 0, undef, 1 ); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | my $merged_areas = $oWkS->get_merged_areas(); | ||
| 260 | my $merged_areas_h = {}; | ||
| 261 | if ($merged_areas) { | ||
| 262 | foreach my $range (@$merged_areas) { | ||
| 263 | $merged_areas_h->{$range->[0]}{$range->[1]} = $range; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | for ( | ||
| 268 | my $iR = $oWkS->{MinRow} ; | ||
| 269 | defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; | ||
| 270 | $iR++ | ||
| 271 | ) | ||
| 272 | { | ||
| 273 | $oWrS->set_row( $iR, $oWkS->{RowHeight}[$iR] ); | ||
| 274 | for ( | ||
| 275 | my $iC = $oWkS->{MinCol} ; | ||
| 276 | defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; | ||
| 277 | $iC++ | ||
| 278 | ) | ||
| 279 | { | ||
| 280 | |||
| 281 | my $oWkC = $oWkS->{Cells}[$iR][$iC]; | ||
| 282 | if ($oWkC) { | ||
| 283 | if ( $oWkC->{Merged} and exists $merged_areas_h->{$iR}{$iC} ) { | ||
| 284 | my $oFmtN = $oWrEx->addformat(); | ||
| 285 | $oFmtN->copy( $hFmt{ $oWkC->{FormatNo} } ); | ||
| 286 | $oWrS->merge_range ( | ||
| 287 | @{$merged_areas_h->{$iR}{$iC}}, | ||
| 288 | $oBook->{FmtClass} | ||
| 289 | ->TextFmt( $oWkC->{Val}, $oWkC->{Code} ), | ||
| 290 | $oFmtN | ||
| 291 | ); | ||
| 292 | } | ||
| 293 | else { | ||
| 294 | $oWrS->write( | ||
| 295 | $iR, | ||
| 296 | $iC, | ||
| 297 | $oBook->{FmtClass} | ||
| 298 | ->TextFmt( $oWkC->{Val}, $oWkC->{Code} ), | ||
| 299 | $hFmt{ $oWkC->{FormatNo} } | ||
| 300 | ); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | } | ||
| 306 | return $oWrEx; | ||
| 307 | } | ||
| 308 | |||
| 309 | #------------------------------------------------------------------------------ | ||
| 310 | # AddWorksheet (for Spreadsheet::ParseExcel::SaveParser::Workbook) | ||
| 311 | #------------------------------------------------------------------------------ | ||
| 312 | sub AddWorksheet { | ||
| 313 | my ( $oBook, $sName, %hAttr ) = @_; | ||
| 314 | $oBook->AddFormat if ( $#{ $oBook->{Format} } < 0 ); | ||
| 315 | $hAttr{Name} ||= $sName; | ||
| 316 | $hAttr{LeftMargin} ||= 0; | ||
| 317 | $hAttr{RightMargin} ||= 0; | ||
| 318 | $hAttr{TopMargin} ||= 0; | ||
| 319 | $hAttr{BottomMargin} ||= 0; | ||
| 320 | $hAttr{HeaderMargin} ||= 0; | ||
| 321 | $hAttr{FooterMargin} ||= 0; | ||
| 322 | $hAttr{FitWidth} ||= 0; | ||
| 323 | $hAttr{FitHeight} ||= 0; | ||
| 324 | $hAttr{PrintGrid} ||= 0; | ||
| 325 | my $oWkS = Spreadsheet::ParseExcel::SaveParser::Worksheet->new(%hAttr); | ||
| 326 | $oWkS->{_Book} = $oBook; | ||
| 327 | $oWkS->{_SheetNo} = $oBook->{SheetCount}; | ||
| 328 | $oBook->{Worksheet}[ $oBook->{SheetCount} ] = $oWkS; | ||
| 329 | $oBook->{SheetCount}++; | ||
| 330 | return $oWkS; #$oBook->{SheetCount} - 1; | ||
| 331 | } | ||
| 332 | |||
| 333 | #------------------------------------------------------------------------------ | ||
| 334 | # AddFont (for Spreadsheet::ParseExcel::SaveParser::Workbook) | ||
| 335 | #------------------------------------------------------------------------------ | ||
| 336 | sub AddFont { | ||
| 337 | my ( $oBook, %hAttr ) = @_; | ||
| 338 | $hAttr{Name} ||= 'Arial'; | ||
| 339 | $hAttr{Height} ||= 10; | ||
| 340 | $hAttr{Bold} ||= 0; | ||
| 341 | $hAttr{Italic} ||= 0; | ||
| 342 | $hAttr{Underline} ||= 0; | ||
| 343 | $hAttr{Strikeout} ||= 0; | ||
| 344 | $hAttr{Super} ||= 0; | ||
| 345 | push @{ $oBook->{Font} }, Spreadsheet::ParseExcel::Font->new(%hAttr); | ||
| 346 | return $#{ $oBook->{Font} }; | ||
| 347 | } | ||
| 348 | |||
| 349 | #------------------------------------------------------------------------------ | ||
| 350 | # AddFormat (for Spreadsheet::ParseExcel::SaveParser::Workbook) | ||
| 351 | #------------------------------------------------------------------------------ | ||
| 352 | sub AddFormat { | ||
| 353 | my ( $oBook, %hAttr ) = @_; | ||
| 354 | $hAttr{Fill} ||= [ 0, 0, 0 ]; | ||
| 355 | $hAttr{BdrStyle} ||= [ 0, 0, 0, 0 ]; | ||
| 356 | $hAttr{BdrColor} ||= [ 0, 0, 0, 0 ]; | ||
| 357 | $hAttr{AlignH} ||= 0; | ||
| 358 | $hAttr{AlignV} ||= 0; | ||
| 359 | $hAttr{Rotate} ||= 0; | ||
| 360 | $hAttr{Landscape} ||= 0; | ||
| 361 | $hAttr{FmtIdx} ||= 0; | ||
| 362 | |||
| 363 | if ( !defined( $hAttr{Font} ) ) { | ||
| 364 | my $oFont; | ||
| 365 | if ( defined $hAttr{FontNo} ) { | ||
| 366 | $oFont = $oBook->{Font}[ $hAttr{FontNo} ]; | ||
| 367 | } | ||
| 368 | elsif ( !defined $oFont ) { | ||
| 369 | if ( $#{ $oBook->{Font} } >= 0 ) { | ||
| 370 | $oFont = $oBook->{Font}[0]; | ||
| 371 | } | ||
| 372 | else { | ||
| 373 | my $iNo = $oBook->AddFont; | ||
| 374 | $oFont = $oBook->{Font}[$iNo]; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | $hAttr{Font} = $oFont; | ||
| 378 | } | ||
| 379 | push @{ $oBook->{Format} }, Spreadsheet::ParseExcel::Format->new(%hAttr); | ||
| 380 | return $#{ $oBook->{Format} }; | ||
| 381 | } | ||
| 382 | |||
| 383 | #------------------------------------------------------------------------------ | ||
| 384 | # AddCell (for Spreadsheet::ParseExcel::SaveParser::Workbook) | ||
| 385 | #------------------------------------------------------------------------------ | ||
| 386 | sub AddCell { | ||
| 387 | my ( $oBook, $iSheet, $iR, $iC, $sVal, $oCell, $sCode ) = @_; | ||
| 388 | my %rhKey; | ||
| 389 | $oCell ||= $oBook->{Worksheet}[$iSheet] | ||
| 390 | ->{Cells}[$iR][$iC]->{FormatNo} || 0; | ||
| 391 | my $iFmt = | ||
| 392 | ( UNIVERSAL::isa( $oCell, 'Spreadsheet::ParseExcel::Cell' ) ) | ||
| 393 | ? $oCell->{FormatNo} | ||
| 394 | : ( ref($oCell) ) ? 0 | ||
| 395 | : $oCell + 0; | ||
| 396 | $rhKey{FormatNo} = $iFmt; | ||
| 397 | $rhKey{Format} = $oBook->{Format}[$iFmt]; | ||
| 398 | $rhKey{Val} = $sVal; | ||
| 399 | $rhKey{Code} = $sCode || '_native_'; | ||
| 400 | $oBook->{_CurSheet} = $iSheet; | ||
| 401 | |||
| 402 | my $merged_areas = $oBook->{Worksheet}[$iSheet]->get_merged_areas(); | ||
| 403 | my $merged_areas_h = {}; | ||
| 404 | if ($merged_areas) { | ||
| 405 | foreach my $range (@$merged_areas) { | ||
| 406 | $merged_areas_h->{$range->[0]}{$range->[1]} = $range; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | |||
| 410 | my $oNewCell = | ||
| 411 | Spreadsheet::ParseExcel::_NewCell( $oBook, $iR, $iC, %rhKey ); | ||
| 412 | Spreadsheet::ParseExcel::_SetDimension( $oBook, $iR, $iC, $iC ); | ||
| 413 | |||
| 414 | $oNewCell->{Merged} = 1 | ||
| 415 | if exists $merged_areas_h->{$iR}{$iC}; | ||
| 416 | |||
| 417 | return $oNewCell; | ||
| 418 | } | ||
| 419 | |||
| 420 | 1; | ||
| 421 | |||
| 422 | __END__ |