Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a Cell with Spreadsheet::ParseExcel

Status
Not open for further replies.
Oct 27, 2002
21
0
0
US
Greetings all,
I found most of my solution here already for using Spreadsheet::parseExcel and with the code I got here, I can get the script to parse out each cells information and print it for me. What I am trying to do is have the user input two values and as it goes down each row, it will check the first value against Column 1 and if that is correct it will check the second input value against Column 3 and if those are both true, then I want the script to print out information from Column 4, 5 and 6 of the row it is on.

Example Data:

Start | Row1 | Row2 | Row3 | Row4 | Row5 | Row6 |
--------------------------------------------------
Col1 | 3 | Red | zz | 35 | M | Blue |
Col2 | 4 | Blue | ll | 22 | F | Brown |
Col2 | 6 | Blue | zz | 27 | M | Blue |
Col2 | 8 | Blue | ll | 36 | M | Brown |
Col2 | 3 | Blue | ll | 22 | F | Brown |
Col2 | 4 | Blue | zz | 45 | M | Blue |

So if the User input 3 and zz then the results would print
34
M
Blue

I have tried to get add some if statements to the below code but when I do, it just does a continual return and I have to break out of it to get it to stop so I know I am doing something simple wrong, just going nuts trying to figure it out.

I added the field values I was looking for in this script just to test it and then instead of having it print the column information, I just had it print "Match" and when I run it, it just continually prints MatchMatchMatchMatch etc..

TIA for any assistance:
Code:
CODE
===================================================
#! /usr/bin/perl
use strict;
use Spreadsheet::ParseExcel;
my $oExcel = new Spreadsheet::ParseExcel;
my $field1 = "3";
my $field2 = "zz";


my $oBook = $oExcel->Parse('netgear.xls');
my($iR, $iC, $oWkS, $oWkC);
for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++)

{
$oWkS = $oBook->{Worksheet}[$iSheet];

for (my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++)
{
for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; $iC++)
{
$oWkC = $oWkS->{Cells}[$iR][$iC];
if ($iC = 1)
    {
     if ($oWkC = $field1)
     {
     if ($iC = 3)
     {
     if($oWkC = $field2)
       {
       print "Match"
       }
     }
    }
   }
  }
 }
}
 
Try this:
Code:
#! /usr/bin/perl
use strict;
use Spreadsheet::ParseExcel;
my $oExcel = new Spreadsheet::ParseExcel;
my $field1 = "3";
my $field2 = "zz";


my $excel = Spreadsheet::ParseExcel::Workbook->Parse('netgear.xls');
    for my $sheet (@{$excel->{Worksheet}}) {
     	for my $row ($sheet->{MinRow} .. $sheet->{MaxRow}) {
		my $cell = $sheet->{Cells}[$row][1];
		if ($sheet->{Cells}[$row][1]->{Val} == $field1 && $sheet->{Cells}[$row][3]->{Val} eq $field2) {
			for (4..6){
				print $sheet->{Cells}[$row][$_]->{Val}, "\n";
			}
		}
	}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
you can get rid of this line
my $oExcel = new Spreadsheet::parseExcel;

it was left over from you code that I copied in.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Travs69,
Thank you so much for the quick response. It works great. Thank you so much for your assistance.
 
no problem!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top