Lauvia has asked for the wisdom of the Perl Monks concerning the following question:
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; } }
|
|---|