Hey Guys,

I’m trying to format an existing Excel worksheet using Excel Writer but it doesn’t work.

The Excel has been generated with Excel Writer and the first worksheet formatted.

I’m still using the created file and filling the worksheet. Once the worksheet contains 4 tables, I would like to add a new worksheet and format it with the same template - lines 102 to112 - (the short version here as “template”), but the formatting procedure doesn’t work.

I would like the template to be executed (setting of columns width).

This is a very light and short version of my script. Run it from the prompt window. Since there is here no upload option just answer for the first time to the question with “y”. This will create the Excel file. Fill the the cells A14, J14, S14 and AB14 with "Frame", run the script once more and answer "n" to the question.

Could somebody help me please?

use strict; use warnings; use Cwd; use OLE; use Win32::OLE::Const "Microsoft Excel"; use constant SCRIPT_VERSION => "1.0.0"; use Data::Dumper; use Excel::Writer::XLSX; my $directory = &getcwd; my $sheet; my $sheet1; my $alt; my $column = 0; my $tbCnt = 1; my $workbook; my $workbook1; my $excel; my $input; my $block; my $Excel_file = $directory . "\\Test_Excel" . ".xlsx"; #my $DIR = $ARGV[0]; #($DIR =~ s/^..// or $DIR =~ s/^.//); #--if excel file doesn't exist create it------ if (!-e $Excel_file) { print"\n"; #sleep (1); $excel = CreateObject OLE 'Excel.Application'; $excel -> {Visible} = 1; $Excel_file = $directory . "\\Test_Excel" . ".xlsx"; $workbook = Excel::Writer::XLSX -> new('Test_Excel.xlsx'); $sheet = $workbook -> add_worksheet('Test Report1'); $sheet1 = $sheet; $workbook1 = $workbook; &template; } #---otherwise open it------------------------- else { $excel = CreateObject OLE 'Excel.Application'; $excel -> {Visible} = 1; $workbook = $excel -> Workbooks -> Open ( "$Excel_file" ) or d +ie ( " can't open $Excel_file" ); #$sheet = $workbook -> Worksheets(1); $sheet = $workbook -> Worksheets("Test Report" . $workbook -> W +orksheets -> {Count}); $sheet -> Activate(); $excel -> ActiveWindow -> {FreezePanes} = "True"; $excel -> {DisplayAlerts} = 0; } #---blocs delimitation------------------------------- print "\nIs this a new Excel Document? (y/n): "; $input = <STDIN>; chomp $input; #$input_2 = 1; print"\n"; my @alphabet = ("A".."ZZ"); if ($input =~ /y/i) { $column = 0; #$sheet -> Range ( "D:E" ) -> {HorizontalAlignment} = xlHAlignCent +er; #$input_2 = 1; } elsif($input =~ /n/i) { $alt = $sheet -> Range( 'A14' )-> {Value}; print"value A14: $alt\n"; print"table's number: $tbCnt \n"; if($alt eq '') { $column = 0; } else { do { $column = $column+9; $alt = $sheet -> Range( $alphabet[$column]. '14' ) -> {Val +ue}; $tbCnt = $tbCnt + 1; print"value $alphabet[$column]14: $alt\n"; print"table's number: $tbCnt \n"; } until( $alt ne 'Frame'); } } #---new worksheet if the current already has 4 tables------- if($tbCnt == 5) { $tbCnt = 1; $column = 0; $sheet = $workbook -> Worksheets -> Add({after => $workbook -> Worksheets($workbook -> Worksheets -> {count +})}); $sheet -> {Name} = "Test Report" . $workbook -> Worksheets -> {Cou +nt}; &template; } #-----format---------------------------------------- sub template { for $block (0..3) { $sheet -> set_column ($column, 0, 40); $sheet -> set_column ($column+1, 0, 40); $sheet -> set_column ($column+2, 0, 40); $sheet -> set_column ($column+3, 0, 12); $sheet -> set_column ($column+4, 0, 12); $sheet -> set_column ($column+5, 0, 15); $sheet -> set_column ($column+6, 0, 60); $sheet -> set_column ($column+7, 0, 60); $sheet -> set_column ($column+8, 0, 1); $column = $column+9; } }

In reply to How to format a new worksheet in an existing Excel by Lauvia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.