Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: How to run an Excel Macro in Perl?

by WoodyWeaver (Monk)
on Jan 14, 2008 at 23:08 UTC ( [id://662399]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to run an Excel Macro in Perl?
in thread How to run an Excel Macro in Perl?

Its not that bad, whakka. I think he is trying, and I'll help him get started.

The way to convert your macro into perl is to use the perl excel instance you've created, and convert the VBScript references to object oriented function calls. Staring at the object browser (I'm not on a box that has excel on it right now, but its something like Tools/Macro/VBEditor then View/ObjectBrowser) to understand what the calls are doing and what they mean is very helpful. Adding perl print statements mixed between the Excel statements is also helpful.

You've set up my $Sheet = $Book->Worksheets(1); -- this is the first worksheet. You can now start manipulating it. For example, the first thing you do is to select a range of rows. If you look in the object browser, you will find that class "Worksheet" has property "Rows" as a range object. So, you can pick up that range as a perlish object as my $range = $Sheet->Rows("1:13"); The macro then selects that range $range->Select; -- but that might not be what you want. If you look in the browser, class Range has a lot of properties you can invoke -- and since you are selecting only to insert, you can do that directly. The object browser describes

expression.Insert(Shift, CopyOrigin) expression Required. An expression that returns a Range object. Shift Optional Variant. Specifies which way to shift the cells. Can +be one of the following XlInsertShiftDirection constants: xlShiftToRi +ght or xlShiftDown. If this argument is omitted, Microsoft Excel dec +ides to do what it thinks you really want. CopyOrigin Optional Variant. The copy origin.
Okay, not the greatest of doc, but given that xlDown is -4121 and that xlFormatFromLeftOrAbove is 0 (look them up in the object browser) your code becomes $range->Insert(-4121,0); or, since you didn't really need that range for anything else,
$Sheet->Rows("1:13")->Insert(-4121,0);
And so on. Now you can put in a print statement that says you did that line successfully. Even better, you can introduce a $Book->Save; so that after the train jumps the tracks, you can at least see if the spreadsheet looked like you expected up to that point.

By breaking out of the "load file, execute canned macro" you can gain a lot of leverage from perl. Sure, you can still have lines like

$worksheet->Range("A3:N3")->{Value} = [ "Point of Contact", "Project", "PAL Code", "Goal", "Earned Value Towards Goal", "Overall Goal Progress", "Start Date", "Finish Date", "Days Active", "Last Completed Task", "Date Completed", "Dollars", "Staff Size", "Unplanned Tasks", ]; $worksheet->Range("A3:N3")->{WrapText} = 1; $worksheet->Rows(3)->{RowHeight} = 36; $worksheet->Range("A3:N3")->{Font}->{Bold} = 1; $xls->ActiveWindow->{SplitRow} = 3; $xls->ActiveWindow->{SplitColumn} = 3; $xls->ActiveWindow->{FreezePanes} = 1;
which is nice, because it saves some tedium, but you can also do things like
### build a whole bunch of data like below... my $defects = Dashboard::safeSQLfunction( "SELECT COUNT(*) FROM userProcessLists WHERE masterID = ? AND EVpo +ints > 0 AND ((start IS NULL) OR (finish IS NULL))", $ref->{'processID'}); # and store it $worksheet->Range("A$row:N$row")->{Value} = [ $poc, $project, $code, $process, $evp, $overallPercentage, $start, + $finish, $duration, $lastDone, $lastCompletionDate, $dollars, $staff, $defects, ]; } ## end while (my $ref = $sth->fetchrow_hashref)
But ultimately, yeah, its hit the documentation to see how others have done it and what patterns to follow.

--woody

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-23 23:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found