| 1 | package Win32::Scsv; | ||
| 2 | $Win32::Scsv::VERSION = '0.41'; | ||
| 3 | 1 | ▶ | use 5.026; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Win32::Scsv - Convert from and to *.xls, *.csv using Win32::OLE | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use Win32::Scsv qw(set_handle_ole xls_2_csv xls_all_csv); | ||
| 13 | |||
| 14 | set_handle_ole; | ||
| 15 | |||
| 16 | xls_2_csv('Test1.xls' => 'dummy.csv'); | ||
| 17 | xls_2_csv('Abc.xls%Tab01' => 'data01.csv', { cpy => 'all' }); # copy values *AND* format... | ||
| 18 | xls_2_csv('Abc.xls%Tab02' => 'data02.csv', { cpy => 'val' }); # copy only values... | ||
| 19 | xls_2_csv('Abc.xls%Tab03' => 'data03.csv'); # ...same as { cpy => 'val' }, which is the default... | ||
| 20 | xls_2_csv('Abc.xls%Tab04' => 'data04.csv', { rmc => [ 'CR', 'LF' ] }); # remove CRLF from all cells... | ||
| 21 | xls_2_csv('Abc.xls%Tab05' => 'data05.csv', { clc => 1 }); # force recalculation... | ||
| 22 | |||
| 23 | xls_all_csv('Abc.xls' => 'result_*.csv', { cpy => 'all' }); # copy all sheets in one operation... | ||
| 24 | |||
| 25 | csv_2_xls('dummy.csv' => 'New.xlsx%Tab9', { | ||
| 26 | 'tpl' => 'Template.xls', | ||
| 27 | 'prt' => 1, | ||
| 28 | 'csz' => [ | ||
| 29 | ['H:H' => 13.71], | ||
| 30 | ['A:D' => 3], | ||
| 31 | ], | ||
| 32 | 'fmt' => [ | ||
| 33 | ['A:A' => '#,##0.000'], | ||
| 34 | ['B:B' => '\\<@\\>'], | ||
| 35 | ['C:C' => 'dd/mm/yyyy hh:mm:ss'], | ||
| 36 | ], | ||
| 37 | }); | ||
| 38 | |||
| 39 | =head1 AUTHOR | ||
| 40 | |||
| 41 | Klaus Eichner <klaus03@gmail.com> | ||
| 42 | |||
| 43 | =head1 COPYRIGHT AND LICENSE | ||
| 44 | |||
| 45 | Copyright (C) 2009-2011 by Klaus Eichner | ||
| 46 | |||
| 47 | All rights reserved. This program is free software; you can redistribute | ||
| 48 | it and/or modify it under the terms of the artistic license 2.0, | ||
| 49 | see http://www.opensource.org/licenses/artistic-license-2.0.php | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | 0 | ▶ | use Win32::OLE; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 54 | use Win32::OLE::Variant; | ||
| 55 | use Carp; | ||
| 56 | use File::Spec; | ||
| 57 | use File::Copy; | ||
| 58 | use File::Slurp; | ||
| 59 | use Win32::File qw(); | ||
| 60 | |||
| 61 | require Exporter; | ||
| 62 | our @ISA = qw(Exporter); | ||
| 63 | our @EXPORT = qw(); | ||
| 64 | our @EXPORT_OK = qw( | ||
| 65 | set_handle_obj set_handle_ole set_handle_quit get_hwnd | ||
| 66 | set_size_pos set_size_min set_size_max | ||
| 67 | xls_2_csv xls_all_csv xls_lst_csv csv_2_xls csv_lst_xls | ||
| 68 | get_book get_ver get_handle get_lang | ||
| 69 | get_last_row get_last_col XLRef get_sno | ||
| 70 | set_style_R1C1 set_style_default trf_style_R1C1 | ||
| 71 | ); | ||
| 72 | |||
| 73 | my $CXL_OpenXML = 51; # xlOpenXMLWorkbook | ||
| 74 | my $CXL_PasteVal = -4163; # xlPasteValues | ||
| 75 | my $CXL_PasteAll = -4104; # xlPasteAll | ||
| 76 | my $CXL_Csv = 6; # xlCSV | ||
| 77 | my $CXL_CalcMan = -4135; # xlCalculationManual | ||
| 78 | my $CXL_Previous = 2; # xlPrevious | ||
| 79 | my $CXL_ByRows = 1; # xlByRows | ||
| 80 | my $CXL_ByCols = 2; # xlByColumns | ||
| 81 | my $CXL_R1C1 = -4150; # xlR1C1 | ||
| 82 | my $CXL_Part = 2; # xlPart | ||
| 83 | my $CXL_Normal = -4143; # xlNormal | ||
| 84 | my $CXL_Maxi = -4137; # xlMaximized | ||
| 85 | my $CXL_Mini = -4140; # xlMinimized | ||
| 86 | |||
| 87 | my $VAR_False = Variant(VT_BOOL, 0); | ||
| 88 | my $VAR_True = Variant(VT_BOOL, 1); | ||
| 89 | |||
| 90 | my @C01_Lang = ( | ||
| 91 | [ 'EN' => 'SUM(1)', '1' ], | ||
| 92 | [ 'DE' => 'SUMME(1)', '1' ], | ||
| 93 | [ 'FR' => 'SOMME(1)', '1' ], | ||
| 94 | ); | ||
| 95 | |||
| 96 | my %C02_Func = ( | ||
| 97 | 'SUM' => { DE => 'SUMME', FR => 'SOMME' }, | ||
| 98 | 'SUMIF' => { DE => 'SUMMEWENN', FR => 'SOMME.SI' }, | ||
| 99 | ); | ||
| 100 | |||
| 101 | my $glb_handle; | ||
| 102 | my $glb_lang; | ||
| 103 | my $glb_ver; | ||
| 104 | my $glb_refst; | ||
| 105 | |||
| 106 | my @glb_sno; | ||
| 107 | |||
| 108 | sub get_sno { | ||
| 109 | \@glb_sno; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub set_handle_obj { | ||
| 113 | if ($glb_handle) { | ||
| 114 | croak "MSG-0010: Excel handle already active"; | ||
| 115 | } | ||
| 116 | |||
| 117 | $glb_handle = _fetch_excel_obj(); | ||
| 118 | _fetch_handle(); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub set_handle_ole { | ||
| 122 | if ($glb_handle) { | ||
| 123 | croak "MSG-0020: Excel handle already active"; | ||
| 124 | } | ||
| 125 | |||
| 126 | $glb_handle = _fetch_excel_ole(); | ||
| 127 | _fetch_handle(); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub set_handle_quit { | ||
| 131 | unless ($glb_handle) { | ||
| 132 | croak "MSG-0030: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 133 | } | ||
| 134 | |||
| 135 | $glb_handle->Quit; | ||
| 136 | } | ||
| 137 | |||
| 138 | END { | ||
| 139 | if ($glb_handle) { | ||
| 140 | $glb_handle->Quit; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _fetch_handle { | ||
| 145 | $glb_lang = _fetch_lang(); | ||
| 146 | $glb_ver = _fetch_ver(); | ||
| 147 | $glb_refst = $glb_handle->{ReferenceStyle}; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub get_hwnd { | ||
| 151 | unless ($glb_handle) { | ||
| 152 | croak "MSG-0040: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 153 | } | ||
| 154 | |||
| 155 | $glb_handle->{hwnd}; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub set_size_min { | ||
| 159 | unless ($glb_handle) { | ||
| 160 | croak "MSG-0050: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 161 | } | ||
| 162 | |||
| 163 | $glb_handle->{WindowState} = $CXL_Mini; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub set_size_max { | ||
| 167 | unless ($glb_handle) { | ||
| 168 | croak "MSG-0060: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 169 | } | ||
| 170 | |||
| 171 | $glb_handle->{WindowState} = $CXL_Maxi; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub set_size_pos { | ||
| 175 | my ($param) = @_; | ||
| 176 | $param //= {}; | ||
| 177 | |||
| 178 | my $lx = $param->{'lx'}; | ||
| 179 | my $ly = $param->{'ly'}; | ||
| 180 | |||
| 181 | my $px = $param->{'px'}; | ||
| 182 | my $py = $param->{'py'}; | ||
| 183 | |||
| 184 | unless ($glb_handle) { | ||
| 185 | croak "MSG-0070: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 186 | } | ||
| 187 | |||
| 188 | $glb_handle->{WindowState} = $CXL_Normal; | ||
| 189 | |||
| 190 | $glb_handle->{Width} = $lx if defined $lx; | ||
| 191 | $glb_handle->{Height} = $ly if defined $ly; | ||
| 192 | |||
| 193 | $glb_handle->{Left} = $px if defined $px; | ||
| 194 | $glb_handle->{Top} = $py if defined $py; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub get_handle { $glb_handle } | ||
| 198 | sub get_lang { $glb_lang } | ||
| 199 | sub get_ver { $glb_ver } | ||
| 200 | |||
| 201 | sub _fetch_excel_obj { | ||
| 202 | # use existing instance if Excel is already running | ||
| 203 | eval { Win32::OLE->GetActiveObject('Excel.Application') }; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _fetch_excel_ole { | ||
| 207 | # create a new OLE instance | ||
| 208 | Win32::OLE->new('Excel.Application', sub { $_[0]->Quit }); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub _fetch_ver { | ||
| 212 | unless ($glb_handle) { | ||
| 213 | croak "MSG-0080: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 214 | } | ||
| 215 | |||
| 216 | my $num = $glb_handle->Version; | ||
| 217 | |||
| 218 | my $ver = | ||
| 219 | $num eq '16.0' ? '2016' : | ||
| 220 | $num eq '15.0' ? '2013' : | ||
| 221 | $num eq '14.0' ? '2010' : | ||
| 222 | $num eq '12.0' ? '2007' : | ||
| 223 | $num eq '11.0' ? '2003' : | ||
| 224 | $num eq '10.0' ? '2002' : | ||
| 225 | $num eq '9.0' ? '2000' : | ||
| 226 | $num eq '8.0' ? '1997' : | ||
| 227 | $num eq '7.0' ? '1995' : '?'.$num; | ||
| 228 | |||
| 229 | return $ver; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub _fetch_lang { | ||
| 233 | unless ($glb_handle) { | ||
| 234 | croak "MSG-0090: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 235 | } | ||
| 236 | |||
| 237 | $glb_handle->{DisplayAlerts} = 0; | ||
| 238 | $glb_handle->{AskToUpdateLinks} = 0; | ||
| 239 | $glb_handle->{Calculation} = $CXL_CalcMan; | ||
| 240 | $glb_handle->{CalculateBeforeSave} = $VAR_False; | ||
| 241 | |||
| 242 | my %L1; | ||
| 243 | |||
| 244 | my $tmp_xls_book = $glb_handle->Workbooks->Add | ||
| 245 | or croak "MSG-0100: Can't Workbooks->Add"; | ||
| 246 | |||
| 247 | my $tmp_xls_sheet = $tmp_xls_book->Worksheets(1) | ||
| 248 | or croak "MSG-0110: Can't find Sheet '1' in new Workbook"; | ||
| 249 | |||
| 250 | my $line; | ||
| 251 | |||
| 252 | $line = 0; | ||
| 253 | |||
| 254 | for (@C01_Lang) { $line++; | ||
| 255 | $tmp_xls_sheet->Cells(1, $line)->{'Formula'} = '='.$_->[1]; | ||
| 256 | } | ||
| 257 | |||
| 258 | $tmp_xls_sheet->Calculate; | ||
| 259 | |||
| 260 | $line = 0; | ||
| 261 | |||
| 262 | for (@C01_Lang) { $line++; | ||
| 263 | if ($tmp_xls_sheet->Cells(1, $line)->{'Value'} eq $_->[2]) { | ||
| 264 | $L1{$_->[0]}++; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | $tmp_xls_book->Close; | ||
| 269 | |||
| 270 | my @L2 = sort keys %L1; | ||
| 271 | |||
| 272 | if (@L2 == 0) { | ||
| 273 | croak "MSG-0120: Can't find any language in (".join(', ', map { "'$_->[0]'" } @C01_Lang).")"; | ||
| 274 | } | ||
| 275 | |||
| 276 | unless (@L2 == 1) { | ||
| 277 | croak "MSG-0130: Found more than one language (".join(', ', map { "'$_'" } @L2).")"; | ||
| 278 | } | ||
| 279 | |||
| 280 | return $L2[0]; | ||
| 281 | } | ||
| 282 | |||
| 283 | # Comment by Klaus Eichner, 02-Oct-2016: | ||
| 284 | # ************************************** | ||
| 285 | # | ||
| 286 | # I have added 3 new functions set_style_R1C1(), set_style_default() and trf_style_R1C1(). | ||
| 287 | # | ||
| 288 | # Why, you might ask... | ||
| 289 | # | ||
| 290 | # ...because I had big problems with my German version of Excel crashing when using | ||
| 291 | # a non-trivial formula with Perl / Win32::OLE... | ||
| 292 | # | ||
| 293 | # ...it turned out that the default references (Style "A1") | ||
| 294 | # was too much to handle for my German Excel. In order for Excel not to crash, | ||
| 295 | # one better switches to the relative style ("R[-1]C[2]") | ||
| 296 | # | ||
| 297 | # Here is the StackOverflow article that got me on the right track: | ||
| 298 | # | ||
| 299 | # http://stackoverflow.com/questions/1674987/how-do-i-set-excel-formulas-with-win32ole#1675036 | ||
| 300 | # | ||
| 301 | # >> Without the quotes I get an errormessage: Win32::OLE(0.1709) error 0x80020009: | ||
| 302 | # >> "Ausnahmefehler aufgetreten" in PROPERTYPUT "FormulaR1C1" at | ||
| 303 | # >> C:\Dokumente und Einstellungen\pp\Eigene Dateien\excel.pl line 113 | ||
| 304 | # >> Just to check, you now have... $sheet->Range( 'G4' )->{FormulaR1C1} = '=SUMME(R[-3]C:R[-1]C)'; | ||
| 305 | # >> @ Joel : Yes. Update: considering Joel's commend, neither of the two formula works. | ||
| 306 | # >> With the help of the perl-community.de I have now a solution: I have to set | ||
| 307 | # >> $excel->{ReferenceStyle} = $xl->{xlR1C1}; | ||
| 308 | # >> and use Z1S1 instead of R1C1 | ||
| 309 | # >> =SUMME(Z(-2)S:Z(-1)S) | ||
| 310 | # >> But it looks like that in the German version I have to choose between the A1 and the Z1S1 (R1C1) notation. | ||
| 311 | # >> Sounds like this was your problem all along - strange. | ||
| 312 | # | ||
| 313 | # ...as for the French version of Excel, the following sums it up quite nicely: | ||
| 314 | # | ||
| 315 | # http://www.office-archive.com/4-excel/0262612dc88a206e.htm | ||
| 316 | # | ||
| 317 | # >> all our french-VBA code was translated to english-VBA. | ||
| 318 | # >> If you really want to stick to L1C1 references, beware of: | ||
| 319 | # >> "[" and "]" => "(" and ")" | ||
| 320 | # >> ";" => "," | ||
| 321 | # >> and of course "L" => "R" | ||
| 322 | |||
| 323 | sub set_style_R1C1 { | ||
| 324 | unless ($glb_handle) { | ||
| 325 | croak "MSG-0140: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 326 | } | ||
| 327 | |||
| 328 | $glb_handle->{ReferenceStyle} = $CXL_R1C1; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub set_style_default { | ||
| 332 | unless ($glb_handle) { | ||
| 333 | croak "MSG-0150: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 334 | } | ||
| 335 | |||
| 336 | $glb_handle->{ReferenceStyle} = $glb_refst; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub trf_style_R1C1 { | ||
| 340 | unless ($glb_handle) { | ||
| 341 | croak "MSG-0160: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 342 | } | ||
| 343 | |||
| 344 | my @result; | ||
| 345 | |||
| 346 | for (@_) { | ||
| 347 | my $t2; | ||
| 348 | |||
| 349 | if (m{\A = (.*) \z}xms) { | ||
| 350 | my $func_gen = uc($1); | ||
| 351 | |||
| 352 | if ($glb_lang eq 'EN') { | ||
| 353 | $t2 = $func_gen; | ||
| 354 | } | ||
| 355 | else { | ||
| 356 | my $item = $C02_Func{$func_gen} // croak "MSG-0170: Can't find function '$func_gen'"; | ||
| 357 | $t2 = $item->{$glb_lang} // croak "MSG-0180: Can't find function '$func_gen', language '$glb_lang'"; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | elsif (m{\A < ([^>]*) > \z}xms) { | ||
| 361 | my $adr_gen = uc($1); | ||
| 362 | |||
| 363 | if ($glb_lang eq 'EN') { | ||
| 364 | $t2 = $adr_gen; | ||
| 365 | } | ||
| 366 | elsif ($glb_lang eq 'DE') { | ||
| 367 | $t2 = $adr_gen =~ s{R}'Z'xmsgr =~ s{C}'S'xmsgr =~ s{\[}'('xmsgr =~ s{\]}')'xmsgr; | ||
| 368 | } | ||
| 369 | elsif ($glb_lang eq 'FR') { | ||
| 370 | $t2 = $adr_gen =~ s{R}'L'xmsgr =~ s{\[}'('xmsgr =~ s{\]}')'xmsgr; | ||
| 371 | } | ||
| 372 | else { | ||
| 373 | croak "MSG-0190: Invalid language '$glb_lang'"; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | elsif ($_ eq ',') { | ||
| 377 | if ($glb_lang eq 'EN') { | ||
| 378 | $t2 = ','; | ||
| 379 | } | ||
| 380 | elsif ($glb_lang eq 'DE') { | ||
| 381 | $t2 = ';'; | ||
| 382 | } | ||
| 383 | elsif ($glb_lang eq 'FR') { | ||
| 384 | $t2 = ';'; | ||
| 385 | } | ||
| 386 | else { | ||
| 387 | croak "MSG-0200: Invalid language '$glb_lang'"; | ||
| 388 | } | ||
| 389 | } | ||
| 390 | else { | ||
| 391 | croak "MSG-0210: Can't parse parameter '$_'"; | ||
| 392 | } | ||
| 393 | |||
| 394 | push @result, $t2; | ||
| 395 | } | ||
| 396 | |||
| 397 | return @result; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub get_last_row { | ||
| 401 | my $proxy = $_[0]->UsedRange->Find({ | ||
| 402 | What => '*', | ||
| 403 | SearchDirection => $CXL_Previous, | ||
| 404 | SearchOrder => $CXL_ByRows, | ||
| 405 | }); | ||
| 406 | |||
| 407 | $proxy ? $proxy->{'Row'} : 0; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub get_last_col { | ||
| 411 | my $proxy = $_[0]->UsedRange->Find({ | ||
| 412 | What => '*', | ||
| 413 | SearchDirection => $CXL_Previous, | ||
| 414 | SearchOrder => $CXL_ByCols, | ||
| 415 | }); | ||
| 416 | |||
| 417 | $proxy ? $proxy->{'Column'} : 0; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub get_book { | ||
| 421 | my $prm_xls_name = $_[0]; | ||
| 422 | |||
| 423 | unless (-f $prm_xls_name) { | ||
| 424 | croak "MSG-0220: Input name '$prm_xls_name' not found"; | ||
| 425 | } | ||
| 426 | |||
| 427 | unless ($glb_handle) { | ||
| 428 | croak "MSG-0230: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 429 | } | ||
| 430 | |||
| 431 | $glb_handle->{DisplayAlerts} = 0; | ||
| 432 | $glb_handle->{AskToUpdateLinks} = 0; | ||
| 433 | $glb_handle->{Calculation} = $CXL_CalcMan; | ||
| 434 | $glb_handle->{CalculateBeforeSave} = $VAR_False; | ||
| 435 | |||
| 436 | my $prm_xls_abs = $prm_xls_name eq '' ? '' : File::Spec->rel2abs($prm_xls_name) =~ s{/}'\\'xmsgr; | ||
| 437 | |||
| 438 | my $wrk_xls_book = $glb_handle->Workbooks->Open($prm_xls_abs) | ||
| 439 | or croak "MSG-0240: Can't Workbooks->Open '$prm_xls_abs'"; | ||
| 440 | |||
| 441 | return $wrk_xls_book; | ||
| 442 | } | ||
| 443 | |||
| 444 | sub xls_2_csv { _x2c('S', @_) } | ||
| 445 | sub xls_all_csv { _x2c('A', @_) } | ||
| 446 | sub xls_lst_csv { _x2c('L', @_) } | ||
| 447 | |||
| 448 | sub _x2c { | ||
| 449 | my $prm_mode = $_[0]; # can be 'S' (= Single), 'L' (= List) or 'A' (= All) | ||
| 450 | my ($prm_xls_name, $prm_xls_sheet) = $_[1] =~ m{\A ([^%]*) % ([^%]*) \z}xms ? ($1, $2) : ($_[1], undef); | ||
| 451 | |||
| 452 | unless (-f $prm_xls_name) { | ||
| 453 | croak "MSG-0250: Input name '$prm_xls_name' not found"; | ||
| 454 | } | ||
| 455 | |||
| 456 | my ($prm_xls_stem, $prm_xls_ext) = | ||
| 457 | $prm_xls_name eq '' ? ('', '') : | ||
| 458 | $prm_xls_name =~ m{\A (.*) \. (xls x?) \z}xmsi ? ($1, lc($2)) : | ||
| 459 | croak "MSG-0260: xls_name '$prm_xls_name' does not have an Excel extension of the right type (*.xls, *.xlsx)"; | ||
| 460 | |||
| 461 | my $prm_xls_abs = $prm_xls_name eq '' ? '' : File::Spec->rel2abs($prm_xls_name) =~ s{/}'\\'xmsgr; | ||
| 462 | |||
| 463 | unless ($glb_handle) { | ||
| 464 | croak "MSG-0270: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 465 | } | ||
| 466 | |||
| 467 | $glb_handle->{DisplayAlerts} = 0; | ||
| 468 | $glb_handle->{AskToUpdateLinks} = 0; | ||
| 469 | $glb_handle->{Calculation} = $CXL_CalcMan; | ||
| 470 | $glb_handle->{CalculateBeforeSave} = $VAR_False; | ||
| 471 | |||
| 472 | # ************************************************ | ||
| 473 | # ** Here we load the Excel file into Memory... ** | ||
| 474 | # ************************************************ | ||
| 475 | |||
| 476 | my $wrk_xls_book = $glb_handle->Workbooks->Open($prm_xls_abs) | ||
| 477 | or croak "MSG-0280: Can't Workbooks->Open '$prm_xls_abs'"; | ||
| 478 | |||
| 479 | my @PList; | ||
| 480 | my $ta_dir; | ||
| 481 | my $ta_leaf; | ||
| 482 | |||
| 483 | if ($prm_mode eq 'A') { | ||
| 484 | my $def_csv_name = $_[2]; | ||
| 485 | my $def_csv_abs = $def_csv_name eq '' ? '' : File::Spec->rel2abs($def_csv_name) =~ s{/}'\\'xmsgr; | ||
| 486 | |||
| 487 | my ($def_dir, $def_leaf) = $def_csv_abs =~ m{\A (.+) [\\/] ([^\\/]+) _ \* \. csv \z}xmsi ? ($1, $2) : | ||
| 488 | croak "MSG-0290: Can't parse (dir/_*.csv) from csv_abs = '$def_csv_abs'"; | ||
| 489 | |||
| 490 | $ta_dir = $def_dir; | ||
| 491 | $ta_leaf = $def_leaf; | ||
| 492 | |||
| 493 | unless (-d $def_dir) { | ||
| 494 | croak "MSG-0300: Output directory '$def_dir' not found"; | ||
| 495 | } | ||
| 496 | |||
| 497 | my $def_p_fmt = []; | ||
| 498 | my $def_p_cpy = 'val'; | ||
| 499 | my $def_p_rmc = []; | ||
| 500 | my $def_p_clc = 0; | ||
| 501 | |||
| 502 | if ($_[3]) { | ||
| 503 | for (sort keys $_[3]->%* ) { | ||
| 504 | my $v = $_[3]->{$_}; | ||
| 505 | |||
| 506 | if ($_ eq 'fmt') { $def_p_fmt = $v } | ||
| 507 | elsif ($_ eq 'cpy') { $def_p_cpy = $v } | ||
| 508 | elsif ($_ eq 'rmc') { $def_p_rmc = $v } | ||
| 509 | elsif ($_ eq 'clc') { $def_p_clc = $v } | ||
| 510 | else { | ||
| 511 | croak "MSG-0310: Invalid option ('$_')"; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | if (defined $prm_xls_sheet) { | ||
| 517 | croak "MSG-0320: Can't have a sheet name ('...%$prm_xls_sheet') when requesting all sheets"; | ||
| 518 | } | ||
| 519 | |||
| 520 | for (1..$wrk_xls_book->Sheets->Count) { | ||
| 521 | my $def_csv_sht = $def_leaf.'_'.sprintf('%03d', $_).'.csv'; | ||
| 522 | |||
| 523 | push @PList, { | ||
| 524 | 'sht' => $_, | ||
| 525 | 'nam' => $def_csv_sht, | ||
| 526 | 'csv' => $def_dir.'\\'.$def_csv_sht, | ||
| 527 | 'fmt' => $def_p_fmt, | ||
| 528 | 'cpy' => $def_p_cpy, | ||
| 529 | 'rmc' => $def_p_rmc, | ||
| 530 | 'clc' => $def_p_clc, | ||
| 531 | }; | ||
| 532 | } | ||
| 533 | |||
| 534 | # remove all existing *.CSV files | ||
| 535 | for (sort(read_dir($def_dir))) { | ||
| 536 | my $def_full = $def_dir.'\\'.$_; | ||
| 537 | |||
| 538 | next unless -f $def_full; | ||
| 539 | next unless m{\A \Q$def_leaf\E _ \d+ \. csv \z}xmsi; | ||
| 540 | |||
| 541 | unlink $def_full or croak "MSG-0330: Can't unlink csv_leaf '$def_full' because $!"; | ||
| 542 | } | ||
| 543 | } | ||
| 544 | elsif ($prm_mode eq 'L') { | ||
| 545 | if (defined $prm_xls_sheet) { | ||
| 546 | croak "MSG-0340: Can't have a sheet name ('...%$prm_xls_sheet') when requesting list sheets"; | ||
| 547 | } | ||
| 548 | |||
| 549 | for ($_[2]->@*) { | ||
| 550 | my $def_sheet = $_->[0]; | ||
| 551 | my $def_csv_name = $_->[1]; | ||
| 552 | my $def_csv_abs = $def_csv_name eq '' ? '' : File::Spec->rel2abs($def_csv_name) =~ s{/}'\\'xmsgr; | ||
| 553 | |||
| 554 | my $def_p_fmt = []; | ||
| 555 | my $def_p_cpy = 'val'; | ||
| 556 | my $def_p_rmc = []; | ||
| 557 | my $def_p_clc = 0; | ||
| 558 | |||
| 559 | if ($_->[2]) { | ||
| 560 | for my $key (sort keys $_->[2]->%* ) { | ||
| 561 | my $v = $_->[2]{$key}; | ||
| 562 | |||
| 563 | if ($key eq 'fmt') { $def_p_fmt = $v } | ||
| 564 | elsif ($key eq 'cpy') { $def_p_cpy = $v } | ||
| 565 | elsif ($key eq 'rmc') { $def_p_rmc = $v } | ||
| 566 | elsif ($key eq 'clc') { $def_p_clc = $v } | ||
| 567 | else { | ||
| 568 | croak "MSG-0350: Invalid option ('$key') for csv = '$def_csv_name'"; | ||
| 569 | } | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | my $def_csv_sht = $def_csv_abs =~ m{\A (.+) [\\/] ([^\\/]+) \z}xms ? $1 : ''; | ||
| 574 | |||
| 575 | push @PList, { | ||
| 576 | 'sht' => $def_sheet, | ||
| 577 | 'nam' => $def_csv_sht, | ||
| 578 | 'csv' => $def_csv_abs, | ||
| 579 | 'fmt' => $def_p_fmt, | ||
| 580 | 'cpy' => $def_p_cpy, | ||
| 581 | 'rmc' => $def_p_rmc, | ||
| 582 | 'clc' => $def_p_clc, | ||
| 583 | }; | ||
| 584 | |||
| 585 | # remove the CSV file (if it exists) | ||
| 586 | if (-e $def_csv_abs) { | ||
| 587 | unlink $def_csv_abs or croak "MSG-0360: Can't unlink csv_abs '$def_csv_abs' because $!"; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | } | ||
| 591 | elsif ($prm_mode eq 'S') { | ||
| 592 | my $def_csv_name = $_[2]; | ||
| 593 | my $def_csv_abs = $def_csv_name eq '' ? '' : File::Spec->rel2abs($def_csv_name) =~ s{/}'\\'xmsgr; | ||
| 594 | |||
| 595 | my $def_p_fmt = []; | ||
| 596 | my $def_p_cpy = 'val'; | ||
| 597 | my $def_p_rmc = []; | ||
| 598 | my $def_p_clc = 0; | ||
| 599 | |||
| 600 | if ($_[3]) { | ||
| 601 | for (sort keys $_[3]->%* ) { | ||
| 602 | my $v = $_[3]->{$_}; | ||
| 603 | |||
| 604 | if ($_ eq 'fmt') { $def_p_fmt = $v } | ||
| 605 | elsif ($_ eq 'cpy') { $def_p_cpy = $v } | ||
| 606 | elsif ($_ eq 'rmc') { $def_p_rmc = $v } | ||
| 607 | elsif ($_ eq 'clc') { $def_p_clc = $v } | ||
| 608 | else { | ||
| 609 | croak "MSG-0370: Invalid option ('$_')"; | ||
| 610 | } | ||
| 611 | } | ||
| 612 | } | ||
| 613 | |||
| 614 | $prm_xls_sheet //= 1; | ||
| 615 | |||
| 616 | my $def_csv_sht = $def_csv_abs =~ m{\A (.+) [\\/] ([^\\/]+) \z}xms ? $1 : ''; | ||
| 617 | |||
| 618 | push @PList, { | ||
| 619 | 'sht' => $prm_xls_sheet, | ||
| 620 | 'nam' => $def_csv_sht, | ||
| 621 | 'csv' => $def_csv_abs, | ||
| 622 | 'fmt' => $def_p_fmt, | ||
| 623 | 'cpy' => $def_p_cpy, | ||
| 624 | 'rmc' => $def_p_rmc, | ||
| 625 | 'clc' => $def_p_clc, | ||
| 626 | }; | ||
| 627 | |||
| 628 | # remove the CSV file (if it exists) | ||
| 629 | if (-e $def_csv_abs) { | ||
| 630 | unlink $def_csv_abs or croak "MSG-0390: Can't unlink csv_abs '$def_csv_abs' because $!"; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | else { | ||
| 634 | croak "MSG-0400: Assertion failure -- prm_mode = '$prm_mode', but extected ('A', 'L' or 'S')"; | ||
| 635 | } | ||
| 636 | |||
| 637 | @glb_sno = (); | ||
| 638 | |||
| 639 | for (@PList) { | ||
| 640 | my $wrk_xls_snum = $_->{'sht'}; | ||
| 641 | my $wrk_csv_nam = $_->{'nam'}; | ||
| 642 | my $wrk_csv_abs = $_->{'csv'}; | ||
| 643 | my $def_p_fmt = $_->{'fmt'}; | ||
| 644 | my $def_p_cpy = $_->{'cpy'}; | ||
| 645 | my $def_p_rmc = $_->{'rmc'}; | ||
| 646 | my $def_p_clc = $_->{'clc'}; | ||
| 647 | |||
| 648 | my $wrk_xls_sheet = $wrk_xls_book->Worksheets($wrk_xls_snum) | ||
| 649 | or croak "MSG-0410: Can't find Sheet '$wrk_xls_snum' in Workbook '$prm_xls_abs'"; | ||
| 650 | |||
| 651 | if ($prm_mode eq 'A') { | ||
| 652 | push @glb_sno, [ $wrk_xls_snum, $wrk_xls_sheet->Name, $wrk_csv_nam ]; | ||
| 653 | } | ||
| 654 | |||
| 655 | my $dat_special = | ||
| 656 | $def_p_cpy eq 'val' ? $CXL_PasteVal : | ||
| 657 | $def_p_cpy eq 'all' ? $CXL_PasteAll : | ||
| 658 | croak "MSG-0420: Invalid parameter cpy => ('$def_p_cpy'), expected ('val' or 'all') in Sheet ($wrk_xls_snum)"; | ||
| 659 | |||
| 660 | my @dat_list_rmc; | ||
| 661 | |||
| 662 | for (@$def_p_rmc) { | ||
| 663 | if (lc($_) eq 'tab') { | ||
| 664 | push @dat_list_rmc, [ "\x{09}" => '~!' ]; | ||
| 665 | } | ||
| 666 | elsif (lc($_) eq 'cr') { | ||
| 667 | push @dat_list_rmc, [ "\x{0a}" => '~*' ]; | ||
| 668 | } | ||
| 669 | elsif (lc($_) eq 'lf') { | ||
| 670 | push @dat_list_rmc, [ "\x{0d}" => '~+' ]; | ||
| 671 | } | ||
| 672 | else { | ||
| 673 | croak "MSG-0430: Invalid option rmc ('$_') in Sheet ($wrk_xls_snum)"; | ||
| 674 | } | ||
| 675 | } | ||
| 676 | |||
| 677 | my $wrk_csv_book = $glb_handle->Workbooks->Add or croak "MSG-0440: Can't Workbooks->Add"; | ||
| 678 | my $wrk_csv_sheet = $wrk_csv_book->Worksheets(1) or croak "MSG-0450: Can't find Sheet '1' in new Workbook"; | ||
| 679 | |||
| 680 | $wrk_xls_sheet->Activate; | ||
| 681 | $wrk_xls_sheet->{'Visible'} = $VAR_True; | ||
| 682 | |||
| 683 | if ($def_p_clc) { | ||
| 684 | $wrk_xls_sheet->Calculate; | ||
| 685 | } | ||
| 686 | |||
| 687 | $wrk_xls_sheet->Cells->AutoFilter; # This should, I hope, get rid of any AutoFilter... | ||
| 688 | $wrk_xls_sheet->Cells->Copy; | ||
| 689 | |||
| 690 | $wrk_csv_sheet->Activate; | ||
| 691 | $wrk_csv_sheet->{'Visible'} = $VAR_True; | ||
| 692 | $wrk_csv_sheet->Range('A1')->PasteSpecial($dat_special); # $CXL_PasteVal or $CXL_PasteAll | ||
| 693 | |||
| 694 | for (@dat_list_rmc) { | ||
| 695 | $wrk_csv_sheet->Cells->Replace({ | ||
| 696 | What => $_->[0], | ||
| 697 | Replacement => $_->[1], | ||
| 698 | LookAt => $CXL_Part, | ||
| 699 | SearchOrder => $CXL_ByRows, | ||
| 700 | MatchCase => $VAR_False, | ||
| 701 | }); | ||
| 702 | } | ||
| 703 | |||
| 704 | $wrk_csv_sheet->Columns($_->[0])->{NumberFormat} = $_->[1] for @$def_p_fmt; | ||
| 705 | |||
| 706 | $wrk_csv_book->SaveAs($wrk_csv_abs, $CXL_Csv); | ||
| 707 | $wrk_csv_book->Close; | ||
| 708 | } | ||
| 709 | |||
| 710 | $wrk_xls_book->Close; | ||
| 711 | |||
| 712 | if ($prm_mode eq 'A') { | ||
| 713 | my $tmp_csv_abs = $ta_dir.'\\'.$ta_leaf.'_'.sprintf('%03d', 0).'.csv'; | ||
| 714 | |||
| 715 | open my $ofh, '>', $tmp_csv_abs or croak "MSG-0460: Can't open > '$tmp_csv_abs' because $!"; | ||
| 716 | |||
| 717 | say {$ofh} 'SNo;Sheet;File'; | ||
| 718 | |||
| 719 | for (@glb_sno) { | ||
| 720 | say {$ofh} '', ($_->[0] =~ m{\A \d+ \z}xms ? sprintf('S%03d', $_->[0]) : 'T'.$_->[0]), ';', $_->[1], ';', $_->[2]; | ||
| 721 | } | ||
| 722 | |||
| 723 | close $ofh; | ||
| 724 | } | ||
| 725 | } | ||
| 726 | |||
| 727 | sub csv_2_xls { _c2x('S', @_) } | ||
| 728 | sub csv_lst_xls { _c2x('L', @_) } | ||
| 729 | |||
| 730 | sub _c2x { | ||
| 731 | my $prm_mode = $_[0]; # can be 'S' (= Single) or 'L' (= List) | ||
| 732 | |||
| 733 | my ($prm_xls_name, $prm_xls_sheet) = $_[2] =~ m{\A ([^%]*) % ([^%]*) \z}xms ? ($1, $2) : ($_[2], undef); | ||
| 734 | |||
| 735 | my ($prm_xls_stem, $prm_xls_ext) = | ||
| 736 | $prm_xls_name eq '' ? ('', '') : | ||
| 737 | $prm_xls_name =~ m{\A (.*) \. (xls x?) \z}xmsi ? ($1, lc($2)) : | ||
| 738 | croak "MSG-0470: xls_name '$prm_xls_name' does not have an Excel extension of the right type (*.xls, *.xlsx)"; | ||
| 739 | |||
| 740 | my $prm_def_tpl = ''; | ||
| 741 | |||
| 742 | my @PList; | ||
| 743 | |||
| 744 | if ($prm_mode eq 'L') { | ||
| 745 | if (defined $prm_xls_sheet) { | ||
| 746 | croak "MSG-0480: Can't have a sheet name ('...%$prm_xls_sheet') when requesting list sheets"; | ||
| 747 | } | ||
| 748 | |||
| 749 | for ($_[1]->@* ) { | ||
| 750 | my $def_csv_name = $_->[0]; | ||
| 751 | my $def_sheet = $_->[1]; | ||
| 752 | my $def_csv_abs = $def_csv_name eq '' ? '' : File::Spec->rel2abs($def_csv_name) =~ s{/}'\\'xmsgr; | ||
| 753 | |||
| 754 | my $def_p_fmt = []; | ||
| 755 | my $def_p_csz = []; | ||
| 756 | my $def_p_prt = 0; | ||
| 757 | |||
| 758 | for my $key (sort keys $_->[2]->%* ) { | ||
| 759 | my $v = $_->[2]->{$key}; | ||
| 760 | |||
| 761 | if ($key eq 'fmt') { $def_p_fmt = $v } | ||
| 762 | elsif ($key eq 'csz') { $def_p_csz = $v } | ||
| 763 | elsif ($key eq 'prt') { $def_p_prt = $v } | ||
| 764 | else { | ||
| 765 | croak "MSG-0490: Invalid option ('$key') for csv = '$def_csv_name'"; | ||
| 766 | } | ||
| 767 | } | ||
| 768 | |||
| 769 | my $def_csv_sht = $def_csv_abs =~ m{\A (.+) [\\/] ([^\\/]+) \z}xms ? $1 : ''; | ||
| 770 | |||
| 771 | push @PList, { | ||
| 772 | 'sht' => $def_sheet, | ||
| 773 | 'nam' => $def_csv_sht, | ||
| 774 | 'csv' => $def_csv_abs, | ||
| 775 | 'fmt' => $def_p_fmt, | ||
| 776 | 'csz' => $def_p_csz, | ||
| 777 | 'prt' => $def_p_prt, | ||
| 778 | }; | ||
| 779 | } | ||
| 780 | |||
| 781 | if ($_[3]) { | ||
| 782 | for (sort keys $_[3]->%* ) { | ||
| 783 | my $v = $_[3]->{$_}; | ||
| 784 | |||
| 785 | if ($_ eq 'tpl') { $prm_def_tpl = $v } | ||
| 786 | else { | ||
| 787 | croak "MSG-0500: Invalid option ('$_')"; | ||
| 788 | } | ||
| 789 | } | ||
| 790 | } | ||
| 791 | } | ||
| 792 | elsif ($prm_mode eq 'S') { | ||
| 793 | my $def_csv_name = $_[1]; | ||
| 794 | my $def_csv_abs = $def_csv_name eq '' ? '' : File::Spec->rel2abs($def_csv_name) =~ s{/}'\\'xmsgr; | ||
| 795 | |||
| 796 | my $def_p_fmt = []; | ||
| 797 | my $def_p_csz = []; | ||
| 798 | my $def_p_prt = 0; | ||
| 799 | |||
| 800 | if ($_[3]) { | ||
| 801 | for my $key (sort keys $_[3]->%* ) { | ||
| 802 | my $v = $_[3]{$key}; | ||
| 803 | |||
| 804 | if ($key eq 'fmt') { $def_p_fmt = $v } | ||
| 805 | elsif ($key eq 'csz') { $def_p_csz = $v } | ||
| 806 | elsif ($key eq 'prt') { $def_p_prt = $v } | ||
| 807 | elsif ($key eq 'tpl') { $prm_def_tpl = $v } | ||
| 808 | else { | ||
| 809 | croak "MSG-0510: Invalid option ('$key') for csv = '$def_csv_name'"; | ||
| 810 | } | ||
| 811 | } | ||
| 812 | } | ||
| 813 | |||
| 814 | $prm_xls_sheet //= 1; | ||
| 815 | |||
| 816 | my $def_csv_sht = $def_csv_abs =~ m{\A (.+) [\\/] ([^\\/]+) \z}xms ? $1 : ''; | ||
| 817 | |||
| 818 | push @PList, { | ||
| 819 | 'sht' => $prm_xls_sheet, | ||
| 820 | 'nam' => $def_csv_sht, | ||
| 821 | 'csv' => $def_csv_abs, | ||
| 822 | 'fmt' => $def_p_fmt, | ||
| 823 | 'csz' => $def_p_csz, | ||
| 824 | 'prt' => $def_p_prt, | ||
| 825 | }; | ||
| 826 | } | ||
| 827 | else { | ||
| 828 | croak "MSG-0530: Assertion failure -- prm_mode = '$prm_mode', but extected ('A', 'L' or 'S')"; | ||
| 829 | } | ||
| 830 | |||
| 831 | my $dat_format = | ||
| 832 | $prm_xls_ext eq 'xls' ? $CXL_Normal : | ||
| 833 | $prm_xls_ext eq 'xlsx' ? $CXL_OpenXML : | ||
| 834 | croak "Msg-0540: Assertion failure ext = ('$prm_xls_ext') not in ('xls', 'xlsx')"; | ||
| 835 | |||
| 836 | my $prm_xls_abs = $prm_xls_name eq '' ? '' : File::Spec->rel2abs($prm_xls_name) =~ s{/}'\\'xmsgr; | ||
| 837 | |||
| 838 | unless ($glb_handle) { | ||
| 839 | croak "MSG-0550: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 840 | } | ||
| 841 | |||
| 842 | my $prm_tpl_abs = $prm_def_tpl eq '' ? '' : File::Spec->rel2abs($prm_def_tpl) =~ s{/}'\\'xmsgr; | ||
| 843 | |||
| 844 | unless ($glb_handle) { | ||
| 845 | croak "MSG-0560: No Excel handle active -- maybe you forgot to call set_handle_obj or set_handle_ole"; | ||
| 846 | } | ||
| 847 | |||
| 848 | $glb_handle->{DisplayAlerts} = 0; | ||
| 849 | $glb_handle->{AskToUpdateLinks} = 0; | ||
| 850 | $glb_handle->{Calculation} = $CXL_CalcMan; | ||
| 851 | $glb_handle->{CalculateBeforeSave} = $VAR_False; | ||
| 852 | |||
| 853 | if ($prm_def_tpl eq '*') { | ||
| 854 | if (-e $prm_xls_abs) { | ||
| 855 | unlink $prm_xls_abs or croak "MSG-0570: Can't unlink '$prm_xls_abs' because $!"; | ||
| 856 | } | ||
| 857 | |||
| 858 | my $tmp_xls_book = $glb_handle->Workbooks->Add | ||
| 859 | or croak "MSG-0580: Can't Workbooks->Add"; | ||
| 860 | |||
| 861 | $tmp_xls_book->SaveAs($prm_xls_abs, $dat_format); | ||
| 862 | $tmp_xls_book->Close; | ||
| 863 | } | ||
| 864 | elsif ($prm_def_tpl eq '') { | ||
| 865 | unless (-f $prm_xls_abs) { | ||
| 866 | croak "MSG-0590: xls_name ('$prm_xls_abs') does not exist and template was not specified"; | ||
| 867 | } | ||
| 868 | } | ||
| 869 | else { | ||
| 870 | if (-e $prm_xls_abs) { | ||
| 871 | unlink $prm_xls_abs or croak "MSG-0600: Can't unlink '$prm_xls_abs' because $!"; | ||
| 872 | } | ||
| 873 | |||
| 874 | unless (-f $prm_tpl_abs) { | ||
| 875 | croak "MSG-0610: Can't find template '$prm_tpl_abs'"; | ||
| 876 | } | ||
| 877 | |||
| 878 | copy $prm_tpl_abs, $prm_xls_abs | ||
| 879 | or croak "MSG-0620: Can't copy tpl_name to xls_name ('$prm_tpl_abs', '$prm_xls_abs')"; | ||
| 880 | } | ||
| 881 | |||
| 882 | # Force "$xls_abs" to be RW -- i.e. remove the RO flag, if any... | ||
| 883 | # *************************************************************** | ||
| 884 | |||
| 885 | { | ||
| 886 | my $aflag; | ||
| 887 | |||
| 888 | unless (Win32::File::GetAttributes($prm_xls_abs, $aflag)) { | ||
| 889 | croak "MSG-0630: Can't get attributes from '$prm_xls_abs'"; | ||
| 890 | } | ||
| 891 | |||
| 892 | if ($aflag & Win32::File::READONLY()) { | ||
| 893 | unless (Win32::File::SetAttributes($prm_xls_abs, ($aflag & ~Win32::File::READONLY()))) { | ||
| 894 | croak "MSG-0640: Can't set attribute ('RW') for '$prm_xls_abs'"; | ||
| 895 | } | ||
| 896 | } | ||
| 897 | } | ||
| 898 | |||
| 899 | # ************************************************ | ||
| 900 | # ** Here we load the Excel file into Memory... ** | ||
| 901 | # ************************************************ | ||
| 902 | |||
| 903 | my $wrk_xls_book = $glb_handle->Workbooks->Open($prm_xls_abs) | ||
| 904 | or croak "MSG-0650: Can't Workbooks->Open '$prm_xls_abs'"; | ||
| 905 | |||
| 906 | for (@PList) { | ||
| 907 | my $wrk_xls_snum = $_->{'sht'}; | ||
| 908 | my $wrk_csv_nam = $_->{'nam'}; | ||
| 909 | my $wrk_csv_abs = $_->{'csv'}; | ||
| 910 | my $def_p_fmt = $_->{'fmt'}; | ||
| 911 | my $def_p_csz = $_->{'csz'}; | ||
| 912 | my $def_p_prt = $_->{'prt'}; | ||
| 913 | |||
| 914 | my $wrk_xls_sheet3 = $wrk_xls_book->ActiveSheet | ||
| 915 | or croak "MSG-0660: Can't find ActiveSheet in Workbook '$prm_xls_abs'"; | ||
| 916 | |||
| 917 | my $wrk_xls_sheet1 = $wrk_xls_book->Worksheets($wrk_xls_snum) | ||
| 918 | or croak "MSG-0670: Can't find Sheet '$wrk_xls_snum' in Workbook '$prm_xls_abs'"; | ||
| 919 | |||
| 920 | $wrk_xls_sheet1->Unprotect; # unprotect the sheet in any case... | ||
| 921 | $wrk_xls_sheet1->Activate; # "...->Activate" is necessary in order to allow "...Range('A1')->Select" later to be effective | ||
| 922 | |||
| 923 | my $wrk_window = $glb_handle->ActiveWindow; | ||
| 924 | |||
| 925 | $wrk_window->{ScrollColumn} = 1; | ||
| 926 | $wrk_window->{ScrollRow} = 1; | ||
| 927 | |||
| 928 | my $pos_row = $wrk_window->{ScrollRow}; | ||
| 929 | my $pos_col = $wrk_window->{ScrollColumn}; | ||
| 930 | |||
| 931 | # Here we copy the contents of the *.csv file into the *.xls file... | ||
| 932 | # ****************************************************************** | ||
| 933 | |||
| 934 | my $wrk_csv_book = $glb_handle->Workbooks->Open($wrk_csv_abs) | ||
| 935 | or croak "MSG-0680: Can't Workbooks->Open csv_abs '$wrk_csv_abs'"; | ||
| 936 | |||
| 937 | my $wrk_csv_sheet2 = $wrk_csv_book->Worksheets(1) | ||
| 938 | or croak "MSG-0690: Can't find Sheet #1 in csv_abs '$wrk_csv_abs'"; | ||
| 939 | |||
| 940 | $wrk_xls_sheet1->Cells->ClearContents; | ||
| 941 | $wrk_csv_sheet2->Cells->Copy; | ||
| 942 | $wrk_xls_sheet1->Range('A1')->PasteSpecial($CXL_PasteVal); | ||
| 943 | $wrk_xls_sheet1->Cells->EntireColumn->AutoFit; | ||
| 944 | |||
| 945 | $wrk_csv_book->Close; | ||
| 946 | |||
| 947 | $wrk_xls_sheet1->Columns($_->[0])->{NumberFormat} = $_->[1] for @$def_p_fmt; | ||
| 948 | $wrk_xls_sheet1->Columns($_->[0])->{ColumnWidth} = $_->[1] for @$def_p_csz; | ||
| 949 | |||
| 950 | $wrk_xls_sheet1->Cells($pos_row, $pos_col)->Select; | ||
| 951 | |||
| 952 | if ($def_p_prt) { | ||
| 953 | $wrk_xls_sheet1->Protect({ | ||
| 954 | DrawingObjects => $VAR_True, | ||
| 955 | Contents => $VAR_True, | ||
| 956 | Scenarios => $VAR_True, | ||
| 957 | }); | ||
| 958 | } | ||
| 959 | |||
| 960 | $wrk_xls_sheet3->Activate; | ||
| 961 | } | ||
| 962 | |||
| 963 | $wrk_xls_book->SaveAs($prm_xls_abs, $dat_format); # ...always use SaveAs(), never use Save() here ... | ||
| 964 | $wrk_xls_book->Close; | ||
| 965 | } | ||
| 966 | |||
| 967 | sub XLRef { | ||
| 968 | my ($col, $row) = @_; | ||
| 969 | $row //= ''; | ||
| 970 | |||
| 971 | my $c3 = int(($col - 1 - 26) / (26 * 26)); my $rem = $col - $c3 * 26 * 26; | ||
| 972 | my $c2 = int(($rem - 1) / 26); | ||
| 973 | my $c1 = $rem - $c2 * 26; | ||
| 974 | |||
| 975 | return ($c3 == 0 ? '' : chr($c3 + 64)).($c2 == 0 ? '' : chr($c2 + 64)).chr($c1 + 64).$row; | ||
| 976 | } | ||
| 977 | |||
| 978 | 1; | ||