Solved

How Do I remove spaces from an excel sheet using Perl module

Posted on 2010-08-14
6
718 Views
Last Modified: 2012-05-10
Hi,

I have an excel sheet, which has several leading and trailing spaces which I want to remove from the excel sheet and then use the excel sheet for some other work. Should I use Perl Spreadsheet ParseExcel SaveParser module?

 How should I do it?

0
Comment
Question by:sunny82
6 Comments
 

Expert Comment

by:Osvel_SQL
ID: 33437728
hi, I use the windows keyboard shortcut  "control + H" to replace a space with no space
0
 
LVL 1

Expert Comment

by:scarybot
ID: 33437781
Try this...
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit');  

my $Book = $Excel->Workbooks->Open("c:/worksheet.xls"); 

my $Sheet = $Book->Worksheets(1);

#change this to the number of rows/cols you want to change
foreach my $row (1..500) 
{
 foreach my $col (1..500)
 {
  next unless defined $Sheet->Cells($row,$col)->{'Value'};

   $Sheet->Cells($row,$col)->{'Value'} =~ m/^\s+(.*)\s+/;
   $Sheet->Cells($row,$col)->{'Value'} = $1;        
 }
}

$Book->Close;

Open in new window

0
 

Author Comment

by:sunny82
ID: 33437861
Hi,

Thx fr the reply. Can you pls send an example with unix modules like Spreadsheet Parseexcel and Spreadsheet Parseexcel Saveparser etc.. I cannot use a windows perl module like OLE in my code as it is a unix box.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 33438001
This should work (though I didn't test it so let me know if there are any errors)...
#!/usr/local/bin/perl
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;

my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $xls = $parser->Parse('Spreadsheet.xls') or die "could not parse xls: $!";

foreach my $sheet ($xls->worksheets) {
    my ($row_min, $row_max) = $sheet->row_range();
    my ($col_min, $col_max) = $sheet->col_range();
    for my $row ($row_min..$row_max) {
COL:
        for my $col ($col_min..$col_max) {
            my $val = $sheet->get_cell($row, $col);
            next COL unless $val;
            $val = $val->value;
            if ($val =~ m{^\s} or $val =~ m{\s$}) {
                $val =~ s{^\s+}{};
                $val =~ s{\s+$}{};
                $sheet->AddCell($row, $col, $val);
            }
        }
    }
}

$xls->SaveAs('Spreadsheet.xls');

Open in new window

0
 

Author Comment

by:sunny82
ID: 33438033
Thx.. I will try it and let you know, if not today, by Monday definitely..

Thx a lot as always..
0
 

Author Comment

by:sunny82
ID: 33446330
Thx..it worked great.. I am closing this..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now