excel - In Perl, how can I copy a subset of columns from an XLSX work sheet to another? -
i have .xlsx
file (only 1 sheet) 15 columns. want read specific columns, let's columns 3, 5, 11, 14 , write new excel sheet. in case cells of input files empty means don't have value.
here trying:
use warnings; use strict; use spreadsheet::parsexlsx; use excel::writer::xlsx; $parser = spreadsheet::parsexlsx->new; $workbook = $parser->parse("test.xlsx"); if ( !defined $workbook ) { die $parser->error(), ".\n"; } $worksheet = $workbook->worksheet('sheet1'); # here don't know how define row , column range specific column data. # trying data in array, can write in new .xlsx file. # function write data in new file sub writetoexcel { @fields = @_; $workbook = excel::writer::xlsx->new( 'report.xlsx' ); $worksheet = $workbook->add_worksheet(); $row = 0; $col = 0; $token ( @fields ) { $worksheet->write( $row, $col, $token ); $col++; } $row++; }
i followed question, no luck.
how can read specific columns .xlsx
file , write new .xlsx
file?
have never copied subset of columns array of arrays another?
here input sheet used this:
and, in output file after code run:
#!/usr/bin/env perl use strict; use warnings; use excel::writer::xlsx; use spreadsheet::parsexlsx; @cols = (1, 3); $reader = spreadsheet::parsexlsx->new; $bookin = $reader->parse($argv[0]); $sheetin = $bookin->worksheet('sheet1'); $writer = excel::writer::xlsx->new($argv[1]); $sheetout = $writer->add_worksheet('extract'); ($top, $bot) = $sheetin->row_range; $r ($top .. $bot) { $sheetout->write_row( $r, 0, # of course, need more work if want # preserve formulas, formats etc. left # you, left part of problem # unspecified. [ map $sheetin->get_cell($r, $_)->value, @cols ], ); } $writer->close;
Comments
Post a Comment