I looked at the docs for Spreadsheet::ParseXLSX (and Spreadsheet::ParseExcel) and Excel::Writer::XLSX, and found several functions that can help (especially if you don't mind a bare-bones approach). Assuming you create $workbook_i by parsing the import file into an S:P object and a $workbook_o when creating the E:W:X object, $workbook_i provides you with:

With the functions above, I wrote a crude filter in 84 lines. (I'd love to see if someone else has a better approach, by the way!)

#!/usr/bin/env perl use strict; use warnings; use Excel::Writer::XLSX; use Getopt::Long; use Spreadsheet::ParseXLSX; my @desired = ( qw/ qux foobar fubar /, ); my $infile = undef; my $outfile = undef; { my @temp_desired = (); GetOptions( q{keep:s} => \@temp_desired, q{input:s} => \$infile, q{output:s} => \$outfile, q{help} => sub { &help; }, ); if ( scalar @temp_desired ) { @desired = ( @temp_desired, ); } &help unless ( ( defined $infile and -e $infile ) and ( defined $outfile ) ); } my $parser_i = Spreadsheet::ParseXLSX->new(); my $workbook_i = $parser_i->parse($infile) or die qq{$infile}, q{: }, $parser_i->error(), qq{.\n}; my $workbook_o = Excel::Writer::XLSX->new($outfile) or die qq{$outfile}, q{: }, qq{Problem creating new Excel file: }, $!, qq{\n}; foreach my $worksheet_i ( $workbook_i->worksheets() ) { my $worksheet_o = $workbook_o->add_worksheet( $worksheet_i->get_name() ); my %ignore = (); my @row_range = $worksheet_i->row_range(); foreach my $i ( $row_range[0] .. $row_range[1] ) { if ( not $i ) { my @col_range = $worksheet_i->col_range(); foreach my $j ( $col_range[0] .. $col_range[1] ) { my $cell = $worksheet_i->get_cell( $i, $j ); if ( defined $cell ) { if ( not scalar grep { $_ eq $cell->unformatted() } @desired ) { $ignore{$j}++; next; } $worksheet_o->write( $i, $j, $cell->unformatted(), ); } } } else { my @col_range = $worksheet_i->col_range(); foreach my $j ( $col_range[0] .. $col_range[1] ) { next if ( defined $ignore{$j} ); my $cell = $worksheet_i->get_cell( $i, $j ); next unless ( defined $cell ); $worksheet_o->write( $i, $j, $cell->unformatted(), ); } } } } $workbook_o->close(); sub help { die qq{$0 --input file --output file [--keep s1] [--keep s2] ...\n}; }
It suffers from a number of limitations, including that it applies the list of values to keep to any worksheet present in the workbook (probably not what you want, but this was just a proof of concept) and it leaves blanks for excluded columns (again, just a proof of concept).

Hope that helps. (Look forward to if others have better code to do the same.)

Update: 2023-05-06

Sample command
./test.pl --input input.xlsx --output.xlsx

Sample input:

input.xlsx:

Sheet1
barquxfoobarfubarcorgegraultthudquuxxyzzywaldo
1Mrs. WhiteKitchenCandlestick44075139431135348
2Ms. ScarlettLoungeDagger750539473652920900
3Col. MustardLibraryRevolver69142213969067715

Sheet2
yabbadabbadoodah
1ABC
2DEF

Sample output:

output.xlsx:

Sheet1
 quxfoobarfubar      
 Mrs. WhiteKitchenCandlestick      
 Ms. ScarlettLoungeDagger      
 Col. MustardLibraryRevolver      

Sheet2
    
    
    


In reply to Re: Deleting a excel column by atcroft
in thread Deleting a excel column by MoodyDreams999

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.