Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^6: Deleting entire column in an excel

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


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

use strict; use Spreadsheet::ParseExcel; use Data::Dumper; use Spreadsheet::WriteExcel; my $val=0; my $last_col; my $col=0; my $row=0; my @req_sfr_array; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('formatting.xls'); my $row1; my $col1; my $rowmin; my $colmin; if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $workbook = Spreadsheet::WriteExcel->new("perl.xls"); foreach my $worksheet ( $workbook->worksheets() ) { $worksheet1 = $workbook->addworksheet(); // if i write $workbook +->addworksheet($workbook) not changing the worksheet name my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); print DEBUG_LOG "PARSING WORKSHEET:", $worksheet->get_name(), "\n" +; print "\n"; print "ROW_MIN = $row_min, ROW_MAX = $row_max\n"; print "COL_MIN = $col_min, COL_MAX = $col_max\n"; print "\n"; for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless ($cell);; next unless ($cell->value() =~ /\S+/);; my $val = 0; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "\n"; $val = $cell->value(); if($val eq "Register") { $rowmin=$row; $colmin=$col; } $row1=$row-$rowmin; $col1=$col-$colmin $worksheet1->write($row1, $col1, "$val"); } } } Output file Reg number output 2. Correct 4. Wrong Input file Blank box Blank box Reg number. Output 2. Correct 4. Wrong

when i tried changing the worksheet name as commented above didn't change. Using this code how to delete a particular column in each sheet?

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

    That code gives these errors

    Scalar found where operator expected at excel.pl line 57, near "$colmin
                     $worksheet1"
            (Missing operator before $worksheet1?)
    Global symbol "$worksheet1" requires explicit package name at excel.pl line 27.
    Bareword "i" not allowed while "strict subs" in use at excel.pl line 27.
    syntax error at excel.pl line 27, near "i write"
    syntax error at excel.pl line 57, near "$colmin
                     $worksheet1"
    Global symbol "$worksheet1" requires explicit package name at excel.pl line 57.
    excel.pl had compilation errors.
    
    poj
      use strict; use Spreadsheet::ParseExcel; use Data::Dumper; use Spreadsheet::WriteExcel; my $val=0; my $last_col; my $col=0; my $row=0; my @req_sfr_array; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('formatting.xls'); my $row1; my $col1; my $rowmin; my $colmin; if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $workbook1 = Spreadsheet::WriteExcel->new("perl.xls"); foreach my $worksheet ( $workbook->worksheets() ) { $worksheet1 = $workbook1->addworksheet(); # if i write $workbo +ok->addworksheet($workbook) not changing the worksheet name my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); print DEBUG_LOG "PARSING WORKSHEET:", $worksheet->get_name(), "\n" +; print "\n"; print "ROW_MIN = $row_min, ROW_MAX = $row_max\n"; print "COL_MIN = $col_min, COL_MAX = $col_max\n"; print "\n"; for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless ($cell);; next unless ($cell->value() =~ /\S+/);; my $val = 0; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "\n"; $val = $cell->value(); if($val eq "Register") { $rowmin=$row; $colmin=$col; } $row1=$row-$rowmin; $col1=$col-$colmin; $worksheet1->write($row1, $col1, "$val"); } } } Output file Register output 2. Correct 4. Wrong Input file Blank box Blank box Register Output 2. Correct 4. Wrong

        Add the new sheet with a name parameter my $worksheet1 = $workbook->add_worksheet('Name');

        #!/usr/bin/perl use strict; use Spreadsheet::ParseExcel; use Data::Dumper; use Spreadsheet::WriteExcel; 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_min > 0 && $col_min > 0); # skip blank sheets print " PARSING WORKSHEET: $name ROW_MIN = $row_min, ROW_MAX = $row_max; COL_MIN = $col_min, COL_MAX = $col_max "; for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless defined $cell; my $val = $cell->value(); print " Row, Col = ($row, $col); Value = $val "; $worksheet1->write($row - $row_min, $col - $col_min, $val); } } }
        poj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found