connerz has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Thanks in advance for attempts to answer. I would like to read a series of contiguous rows from a spread sheet. I see from some of the docs on msdn that using something like:
my$array = $Sheet->Range("3:3, 5:5, 7:7") -> {'Value'};
would operate on discrete rows. But I would like something to operate on say rows 7 to 21. Or ideally, given that the Excel spread sheet is open on the desktop, and the user has selected a range of cells, use the CurrentRange value for Range. I played with this a little, but ran into problems. Thanks again in advance. Geoff

Replies are listed 'Best First'.
Re: Using Win32:OLE on Excel
by cacharbe (Curate) on Jul 01, 2002 at 18:08 UTC
    Read this node. Everyting a growing monk needs to fulfill his daily allowance of OLE programming with Excel.

    More specifically, don't create a new sheet, set :

    my $Sheet = $Excel->{ActiveSheet};
    and you should be able to modify the last row, last column section in the tutorial to work within a given selection, vs. on a given sheet.

    Update::

    'course, you don't necessarily have to work with that chunk of code, since in this case the Application->Selection object returns a Range object, so you could always just do this:

    use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); $Win32::OLE::Warn = 3; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') or die "Ain't gonna work without an open worksheet\n"; my $Sheet = $Excel->{ActiveSheet}; my $array = $Excel->Selection->{Value}; foreach my $cell(@{$array}){ print $cell->[0]."\n"; }
    C-.

    ---
    Flex the Geek

      Thanks for your prompt answer, that did the trick. I also read that node you pointed out. Out of curiosity, how would one, given a set range of rows (say 7 - 21) get the Range object. The msdn docs gave a discrete set of rows but not much info on a contiguous range of rows. Thanks again.. Geoff
        That depends. *grin*

        If you know the Columns involved, then:

        my $Range = $Sheet->Range($col.$startrow.":".$col.$endrow);

        or if you just want an entire column:
        my $Range = $Sheet->Columns($colletter);
        or if you just want an entire row:
        my $Range = $Sheet->Rows($rownumber);

        There are, of course, a million ways to do it.

        C-.

        ---
        Flex the Geek

Re: Using Win32:OLE on Excel
by RMGir (Prior) on Jul 01, 2002 at 19:27 UTC
    Have you tried Spreadsheet::ParseExcel?

    Of course, that won't help you if you're trying to read DDE data from Excel, but if its static data, it might be simpler than working via the OLE interface.

    I haven't tried SS::PE myself. I use Spreadsheet::WriteExcel a lot, but that won't help you read the data at all.
    --
    Mike

      Thanks Mike, I'll have to check that out. I headed out in the win32:ole direction because that was the only way I knew of from my last stint with perl (3 years ago, the layers of rust are deep). Geoff