Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Merging 2 xlsx files (UPDATED)

by thanos1983 (Parson)
on Sep 12, 2017 at 23:29 UTC ( [id://1199243]=note: print w/replies, xml ) Need Help??


in reply to Merging 2 xlsx files

Hello again mnakkach,

I was thinking again and again the case and I found my previous solution Re: Merging 2 xlsx files to your problem not correct.

Why it is not correct let's assume that you have 2 lines on first spreadsheet all in colum A and also on second spreadsheet two lines on column A. The old script will work just fine as long as all the data are still in column A. In case that the second spreadsheet (the appending) data are in column B, even if the column B has only on line e.g. B1 then after the process the B1 will be passed to B5. Why? simply because I did a wrong calculation of the keys in the hash.

To resolve this issue, but unfortunately only for columns A-Z see the solution bellow:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use Excel::Writer::XLSX; use Spreadsheet::Read qw( ReadData ); # read from all spreadsheets my @spreadsheets = ( 'test_file1.xlsx' , 'test_file2.xlsx' ); my %HoH; foreach my $spreadsheet (@spreadsheets) { my $book = ReadData ($spreadsheet); my @sheets = @$book[0]->{sheets}; foreach my $sheet (@sheets) { my @rows = Spreadsheet::Read::rows($book->[$sheet]); $HoH{$spreadsheet}{$sheet} = \@rows; } } my $outputXLSX = 'simple.xlsx'; my $workbook = Excel::Writer::XLSX->new( $outputXLSX ); my $worksheet = $workbook->add_worksheet(); my %hash; foreach my $file ( sort keys %HoH ) { foreach my $sheet ( sort keys %{ $HoH{$file} } ) { $workbook->add_worksheet() if $sheet > 1; foreach my $i (1 .. scalar @{$HoH{$file}{$sheet}}) { foreach my $j (1 .. scalar @{$HoH{$file}{$sheet}[$i-1]}) { next if ($HoH{$file}{$sheet}[$i-1][$j-1] // '') eq ''; if ( grep { $_ eq chr(64+$j) } keys %hash ) { if (grep { $_ eq $i} sort keys %{$hash{chr(64+$j)}}) { my $incrementRow = $i; while (grep { $_ eq $i} sort keys %{$hash{chr(64+$j)}}){ $incrementRow++; last if ($i ne $incrementRow); } $worksheet->write(chr(64+$j) . $incrementRow, ($HoH{$file}{$sheet}[$i-1][$j-1] // '')); $hash{chr(64+$j)}{$incrementRow} = ($HoH{$file}{$sheet}[$i-1][$j-1] // ''); } else { $worksheet->write(chr(64+$j) . $i, ($HoH{$file}{$sheet}[$i-1][$j-1] // '')); $hash{chr(64+$j)}{$i} = ($HoH{$file}{$sheet}[$i-1][$j-1] // ''); } } else { # say "Column Index: " . $j . " Column Character: " . chr( +64+$j) . " Text: " . ($HoH{$file}{$sheet}[$i-1][$j-1] // ''); $hash{chr(64+$j)}{$i} = ($HoH{$file}{$sheet}[$i-1][$j-1] // ''); $worksheet->write(chr(64+$j) . $i, ($HoH{$file}{$sheet}[$i-1][$j-1] // '')); } } } } } print Dumper \%hash; $workbook->close; # remove output file if you are # going to rerun the script # I used it for debuging purposes # unlink($outputXLSX); __END__ $ perl excel.pl $VAR1 = { 'Z' => { '2' => 'Line Z1 File 2', '1' => 'Line Z1 File 1', '5' => 'Line Z5 File 1', '6' => 'Line Z5 File 2' }, 'A' => { '2' => 'Line A1 file 2', '3' => 'Line A2 file 2', '1' => 'Line A1 file 1' }, 'B' => { '3' => 'Line B2 file 2', '1' => 'Line B1 File 1', '2' => 'Line B1 file 2' } };

Update: I finally managed to put it together a solution that works also for longer columns like AA. Hope this script will help other people in future.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Excel::Writer::XLSX; use Spreadsheet::Read qw( ReadData cr2cell ); # read from all spreadsheets my @spreadsheets = ( 'test_file1.xlsx' , 'test_file2.xlsx' ); my %HoH; foreach my $spreadsheet (@spreadsheets) { my $book = ReadData ($spreadsheet); my @sheets = @$book[0]->{sheets}; foreach my $sheet (@sheets) { my @rows = Spreadsheet::Read::rows($book->[$sheet]); $HoH{$spreadsheet}{$sheet} = \@rows; } } my $outputXLSX = 'simple.xlsx'; my $workbook = Excel::Writer::XLSX->new( $outputXLSX ); my $worksheet = $workbook->add_worksheet(); my %hash; foreach my $file ( sort keys %HoH ) { foreach my $sheet ( sort keys %{ $HoH{$file} } ) { $workbook->add_worksheet() if $sheet > 1; foreach my $i (1 .. scalar @{$HoH{$file}{$sheet}}) { foreach my $j (1 .. scalar @{$HoH{$file}{$sheet}[$i-1]}) { next if ($HoH{$file}{$sheet}[$i-1][$j-1] // '') eq ''; my $cell = cr2cell ($j, $i); my ($column, $row) = split /(?<=\D)(?=\d)/, $cell; if ( grep { $_ eq $column } keys %hash ) { if (grep { $_ eq $row} sort keys %{$hash{$column}}) { my $incrementRow = $row; while (grep { $_ eq $row} sort keys %{$hash{$column}}){ $incrementRow++; last if ($row ne $incrementRow); } write2ExcelFile($column, $incrementRow, ($HoH{$file}{$sheet}[$i-1][$j-1] // '')); } else { write2ExcelFile($column, $row, ($HoH{$file}{$sheet}[$i-1][$j-1] // '')); } } else { write2ExcelFile($column, $row, ($HoH{$file}{$sheet}[$i-1][$j-1] // '')); } } } } } print Dumper \%hash; $workbook->close; sub write2ExcelFile { my ( $column , $row , $data ) = @_; $worksheet->write($column . $row, $data); $hash{$column}{$row} = $data; return; } # remove output file if you are # going to rerun the script # I used it for debuging purposes # unlink($outputXLSX); __END__ $ perl excel.pl $VAR1 = { 'B' => { '3' => 'Line B2 file 2', '1' => 'Line B1 File 1', '2' => 'Line B1 file 2' }, 'AA' => { '2' => 'Line AA1 File 2', '1' => 'Line AA1 File 1' }, 'A' => { '3' => 'Line A2 file 2', '2' => 'Line A1 file 2', '1' => 'Line A1 file 1' }, 'Z' => { '1' => 'Line Z1 File 1', '2' => 'Line Z1 File 2', '5' => 'Line Z5 File 1', '6' => 'Line Z5 File 2' } };

I will try to resolve the issue with the columns but so far I am stack, I will update as soon as I have resolved it.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Merging 2 xlsx files (UPDATED)
by mnakkach (Novice) on Sep 14, 2017 at 04:53 UTC
    .

      Hello mnakkach,

      If you are trying to learn programming you really are not showing any effort. As the fellow Monks already said this is a not a free code forum. Show us some effort, show us your code and then you can ask for advice / code improvements etc.

      I will answer your question because I found it extremely easy. Did you read the documentation of the Excel::Writer::XLSX/write_comment( $row, $column, $string, ... ). Regarding reading the comments from you spread sheet, this is for you to figure it out. Show us the effort and we will try to help.

      Hope this helps, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1199243]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-28 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found