Solved

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

Posted on 2010-08-14
6
721 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

803 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