in reply to Re^6: XLS to CSV file with Text wrap
in thread XLS to CSV file with Text wrap

I am not sure why the text wrap is not happening

Probably because you are setting the column widths to the maximum length of the string. Also you are only applying a text wrap format to rows that match 'completed'. You need a default text wrap for the other rows. It looks like you want to create the new spreadsheet with the same column widths as the original. Try this

#!/usr/bin/perl use strict; use warnings; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('Project_status_tracking_DFT.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $workbook1 = Spreadsheet::WriteExcel->new('Ration_Status_ksh.xls'); my @color = qw(white green orange pink blue magenta silver); my @format; $format[0] = $workbook1->add_format(); $format[0]->set_text_wrap(); for my $i (1..@color){ $format[$i] = $workbook1->add_format(bg_color => $color[$i]); $format[$i]->set_text_wrap(); } for my $worksheet ( $workbook->worksheets() ) { my $worksheet1 = $workbook1->add_worksheet(); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my $col_widths = $worksheet->get_col_widths(); my $flag = 0; for my $row ( $row_min .. $row_max ) { my $fmt = $format[0]; # default format for complete row # store row in array my @value = (); for my $col ( $col_min .. $col_max ) { # set column width same as source $worksheet1->set_column($col, $col, $col_widths->[$col]) if ($ro +w == $row_min); my $cell = $worksheet->get_cell( $row, $col ); if (defined $cell) { my $value = $cell->value(); $value[$col] = $value; if ($value =~ /DFT Tasks for Ration/){ $flag = 1; } elsif ($value =~ /END OF RATION/){ $flag = 2; }; if ($value =~ m/Completed/i){ $fmt = $format[1] } } } # write row if ($flag){ for my $col ( $col_min .. $col_max ) { $worksheet1->write($row, $col, $value[$col],$fmt); } $flag = 0 if $flag == 2; } } }
poj

Replies are listed 'Best First'.
Re^8: XLS to CSV file with Text wrap
by kshitij (Sexton) on Sep 04, 2018 at 11:03 UTC
    Hi Poj ,

    This script is somewhat better and column width is being set properly but now the content is not getting split between /DFT Tasks for Ration/ and /END OF RATION/ . The generated XLS is starting from /DFT Taks for Ration/ till the end of file of original XLS. It should print the content between /DFT Tasks for Ration/ and /END OF RATION/ .

    Thanks and Regards

    Kshitij Kulshreshtha

      Try a less precise regex, for example

      /END\s+OF\s+RATION/i
      poj
        Hi Poj ,

        Somehow I have modified prior script and now the below stated script is finally achieving whatever I was expecting it to achieve. It is generating the Text wrapper new XLS with different colors . Only last problem I could see here -> Empty rows. Since we are generating multiple XLS based on the pattern match , new generated excels are having lots of empty rows. In the below script , how can we delete those empty rows ?

        #!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use Text::Wrap; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('Project_status_tracking_KSHITIJ.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $workbook1 = Spreadsheet::WriteExcel->new('_Status.xls'); my @color = qw(white green orange pink blue magenta gray); my @format; #my $format1 = $workbook1->add_format(bg_color => 'green' ); #$format1->set_text_wrap(); #$format1->set_align('vjustify'); for my $i (1..@color){ $format[$i] = $workbook1->add_format(bg_color => $color[$i], border = +> 1); # $format[$i]->set_align('vjustify'); $format[$i]->set_text_wrap(); } #my $format7 = $workbook1->add_format(bg_color => 'gray'); #my $format7->set_size(30); my $format7 = $workbook1->add_format(bg_color => 'gray' , size => 15 , + bold => 1 , border => 1 ); my $format8 = $workbook1->add_format(bg_color => 'gray' , size => 15 , + bold => 1 , border => 1); my $format9 = $workbook1->add_format(bg_color => 'orange' , size => 12 + , bold => 1 , border => 1); #$format7->set_size(30); for my $worksheet ( $workbook->worksheets() ) { my $worksheet1 = $workbook1->add_worksheet(); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); # set minimum column width my @width; $width[$_] = 10 for ($col_min .. $col_max); for my $row ( $row_min .. $row_max ) { my @value = (); my $fmt; # format for complete row # store row in array for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); if (defined $cell) { local $Text::Wrap::columns = 60; my $value = wrap("", "", $cell->value()); if ($value=~ /Project Ratio/ .. $value =~ /END OF RATION/ +){ $value[$col] = $value; if ($value[$col] =~ m/Completed/) { $fmt = $format[1]; } elsif ($value[$col] =~ m/In Progress/i){ $fmt = $format[2]; } elsif ($value[$col] =~ m/Planning/i) { $fmt = $format[3]; } elsif ($value[$col] =~ m/On Hold/i) { $fmt = $format[4]; } elsif ( $value[$col] =~ m/END OF/i) { $fmt = $format[5]; } elsif ( $value[$col] =~ m/Date assigned/i) { $fmt = $format8; } elsif ( $value[$col] =~ m/Project Homestead/i) { $fmt = $format7; } elsif ( $value[$col] =~ m/DFT Tasks for Homestead/i) { $fmt = $format9; } # determine max width for each column my $len = length($value[$col]); $width[$col] = $len if $len > $width[$col]; } } } # write row for my $col ( $col_min .. $col_max ) { $worksheet1->write($row, $col, $value[$col],$fmt); } } # set column widths for my $col ( $col_min .. $col_max ) { $worksheet1->set_column($col,$col,$width[$col]); $worksheet1->set_column('F:G', 58); $worksheet1->set_column('A:C', 30); #$worksheet1->set_column('C', 20); } }
        Thanks

        Kshitij