Monday, 15 September 2014

ms word - Clean way to write OLE code in Perl? -



ms word - Clean way to write OLE code in Perl? -

i'm working automating creation of word document in perl means of win32::ole. current code looks this, , it's leaving instances of winword.exe in memory:

my $range = $select->range; $table = $doc->tables->add( $range, scalar @rows, scalar @{ $rows[0] } ); $rownum ( 0 .. $#rows ) { $colnum ( 0 .. $#{ $rows[$rownum] } ) { @cellpos = ( $rownum + 1, $colnum + 1 ); $data = $rows[$rownum][$colnum]; $table->cell(@cellpos)->range->{'text'} = $data; 1; } }

however, if refactor code per microsoft recommendation visual studio .net, this:

my $range = $select->range; $tables = $doc->tables; $table = $tables->add( $range, scalar @rows, scalar @{ $rows[0] } ); $rownum ( 0 .. $#rows ) { $colnum ( 0 .. $#{ $rows[$rownum] } ) { @cellpos = ( $rownum + 1, $colnum + 1 ); $data = $rows[$rownum][$colnum]; $cell = $table->cell(@cellpos); $cell_range = $cell->range; $cell_range->{'text'} = $data; } }

that code job, it's awfully "noisy" mind. there cleaner way this?

it can improved marginally. there no need @cellpos , $data variables, , tidier extract reference current element of @rows utilize within inner loop.

my $range = $select->range; $tables = $doc->tables; $table = $tables->add( $range, scalar @rows, scalar @{ $rows[0] } ); $rownum ( 0 .. $#rows ) { $cols = $rows[$rownum]; $colnum ( 0 .. $#$cols ) { $cell = $table->cell($rownum + 1, $colnum + 1); $cell_range = $cell->range; $cell_range->{text} = $cols->[$colnum]; } }

perl ms-word interop ole

No comments:

Post a Comment