newperlbie has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, Thanks for the help on my previous perl to HTML query. Now,I am writing to an excel file and I have this code
my $workbook = Spreadsheet::WriteExcel->new("output.xls"); my $worksheet = $workbook->add_worksheet("overview"); my $colCount = 0; my $rowCount = 0; open my $tf, "output.txt" or die "Cannot open file"; while(my $line = <$tf>) { if ( $line =~ /.*versions are SAME.*/) { my $header1 = $workbook->addformat(bold => 0, color => 'green', size => 10, merge => 0, ); $worksheet->set_column($colCount, $colCount, 30); print $line; #this is a dummy line $worksheet->write($rowCount,$colCount,$line,$header1); $rowCount++; } }
All the lines from the text file are written to the excel but after a few lines(random) the coloring stops.The next lines are just written in black. What is my mistake?

Replies are listed 'Best First'.
Re: Perl 2 Excel
by Eily (Monsignor) on Aug 21, 2018 at 13:15 UTC

    Hello newperlbie, there are a few issues with your post that makes it harder to give you an answer:
    You didn't provide any input data, which means we can't reproduce the issue in the same conditions as you. Your sample of code also doesn't compile because it doesn't include the use Spreadsheet::WriteExcel; line. Asking if your includes are correct without showing them makes little sense. Also please do something about your identation, don't mix tabs and spaces, and align your code properly.

    More info on those points in How do I post a question effectively?

    That being said, I did reproduce the defect with 300 lines of dummy data (I replaced the regex by /./ because I was too lazy to write valid data). I took a wild guess: Excel somehow doesn't support that many formats (or there's a bug with adding so many formats), so I moved the format declaration out of the loop (it's always the same, so why redeclare it each time?).

    use strict; use warnings; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("output.xls"); my $worksheet = $workbook->add_worksheet("overview"); my $colCount = 0; my $rowCount = 0; open my $tf, "<", "input.txt" or die "Cannot open file: $!"; my $header1 = $workbook->addformat(bold => 0, color => 'green', size => 10, merge => 0, ); while(my $line = <$tf>) { if ( $line =~ /\w/) { $worksheet->set_column($colCount, $colCount, 30); print $line; #this is a dummy line $worksheet->write($rowCount,$colCount,$line,$header1); $rowCount++; } }
    It looks like it solved the issue. (BTW, you should indicate that you open the input file for reading with "<". And why did you name your input file "output.txt"?)

      Thank you!I moved the formatting out of the loop and it worked. the file is the output of another program, so!!

        So every program should have it's output named 'output' by default? :P

Re: Perl 2 Excel
by newperlbie (Acolyte) on Aug 21, 2018 at 12:54 UTC
    I am really wondering why the lines are written but not with color!! am I missing any includes ? I have use Spreadsheet::WriteExcel;

      I think you're running out of space in whatever table is used internally for ->add_format(). Compare the results of these two files:

      #!/usr/bin/perl use warnings; use strict; use Spreadsheet::WriteExcel; many_formats: { my $wb = Spreadsheet::WriteExcel->new('output_many.xls'); my $sheet = $wb->add_worksheet("overview"); foreach my $row (0..100) { local $\ = "\n"; print my $line = join('', map { ('A'..'Z')[rand 26] } 0 .. ran +d 16); my $header1 = $wb->add_format(bold=>0, color=>'green', size=>1 +0, merge=>0); $sheet->set_column(1,1,30); $sheet->write($row, 1, $line, $header1); } undef $wb; } shared_format: { my $wb = Spreadsheet::WriteExcel->new('output_shared.xls'); my $header1 = $wb->add_format(bold=>0, color=>'green', size=>10, m +erge=>0); my $sheet = $wb->add_worksheet("overview"); foreach my $row (0..100) { local $\ = "\n"; print my $line = join('', map { ('A'..'Z')[rand 26] } 0 .. ran +d 16); $sheet->set_column(1,1,30); $sheet->write($row, 1, $line, $header1); } undef $wb; }

      The only difference between the code is that in the first, like you, I create a new format for every row. That stops formatting after about 40 rows. For the second, I create one format, and apply it to every row... and when I open output_shared.xls in Excel, all the rows are properly formatted.

      (Also, since the documentation shows ->add_format(), that's what I used here, instead of the ->addformat() you used, though it didn't seem to affect my experiments, that I noticed.)

        I like responses like yours, which take the OP's code and try to breathe some life into it. In particular, you take some aim at providing some data set to work off of, which is critical if OP wants others to replicate their post. One thing I like about usenet is that we tried to do that on threads. (One thing I don't like about usenet was the meanspiritness and increased domination by topic trolls. I let my subscription lapse.)

        I did not see any difference in the 2 docs that pryrt's script produces. Everything seemed to be green. What follows are abridged output and then source:

        $ ./1.excel.pl TUAROKUNSDSIG JLUXOBCQYAIXDS YAKB ... UA OJGFXQYPVIVO UD IABE $ cat 1.excel.pl #!/usr/bin/perl -w use 5.011; use Spreadsheet::WriteExcel; many_formats: { my $wb = Spreadsheet::WriteExcel->new('1.output_many.xls'); my $sheet = $wb->add_worksheet("overview"); foreach my $row (0..100) { local $\ = "\n"; print my $line = join('', map { ('A'..'Z')[rand 26] } 0 .. ran +d 16); my $header1 = $wb->add_format(bold=>0, color=>'green', size=>1 +0, merge=>0); $sheet->set_column(1,1,30); $sheet->write($row, 1, $line, $header1); } undef $wb; } shared_format: { my $wb = Spreadsheet::WriteExcel->new('1.output_shared.xls'); my $header1 = $wb->add_format(bold=>0, color=>'green', size=>10, m +erge=>0); my $sheet = $wb->add_worksheet("overview"); foreach my $row (0..100) { local $\ = "\n"; print my $line = join('', map { ('A'..'Z')[rand 26] } 0 .. ran +d 16); $sheet->set_column(1,1,30); $sheet->write($row, 1, $line, $header1); } undef $wb; } __END__

        My question is: given OP's script, what qualifies as a well-conditioned data set for this problem? OP says that his input is the output of a program: why is it unpostable between readmore tags?

        yes,thanks again,moving the format out of the loop solved the issue!