Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^10: Deleting entire column in an excel

by harishnv (Sexton)
on Mar 11, 2018 at 13:29 UTC ( [id://1210656]=note: print w/replies, xml ) Need Help??


in reply to Re^9: Deleting entire column in an excel
in thread Deleting entire column in an excel

how to delete a particular column? ( if a keyword in that cell is encountered then that entire column has to be deleted)

  • Comment on Re^10: Deleting entire column in an excel

Replies are listed 'Best First'.
Re^11: Deleting entire column in an excel
by poj (Abbot) on Mar 11, 2018 at 13:44 UTC

    Where is this keyword in your example data ?

    Output file 
    Register     output
    2.           Correct
    4.             Wrong
     
    
    Input file
    Blank box
    Blank box Register       Output
               2.            Correct
               4.             Wrong
    
    poj

      keyword is output. in my output xls file there will be only one column. The input file which i have told is an example. My xls file has more than 5 columns. Since i can't attach my file i gave this example.

        Assuming the keyword might appear in more than one column and anywhere in the row try this

        #!/usr/bin/perl use strict; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; my $KEYWORD = 'output'; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('formatting.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $workbook1 = Spreadsheet::WriteExcel->new("perl.xls"); foreach my $worksheet ( $workbook->worksheets() ) { my $name = $worksheet->get_name(); # create worksheet with same name my $worksheet1 = $workbook1->addworksheet($name); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); next unless ($row_max >= 0 && $col_max >= 0); # skip blank sheets print " PARSING WORKSHEET: $name ROW_MIN = $row_min, ROW_MAX = $row_max; COL_MIN = $col_min, COL_MAX = $col_max "; my $copyflag; my $col1 = 0; for my $col ( $col_min .. $col_max ) { $copyflag = 1; my @rowval = (); for my $row ( $row_min .. $row_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless defined $cell; my $val = $cell->value(); print "Col, Row, Value = ($col, $row, $val)\n"; $rowval[$row - $row_min] = $val; if ($val =~ /$KEYWORD/i){ print "$KEYWORD found in column $col\n"; $copyflag = 0; } } # copy to new sheet next if ($copyflag == 0); for my $row1 (0..$#rowval){ $worksheet1->write($row1, $col1, $rowval[$row1]); } ++$col1; } }
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found