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

I have a script which is querying a db and writing the results to an Excel file. I get as far as writing it all in correctly and inserting a pivot table like I want. However, when I try to select the Range for a cell in the Column field of the pivot table and run the 'Group' method on it, I can't get anything to work. I've tried a myriad of things, including passing arrays, refs to arrays, 0 for false and 1 for true, etc. I have got all kinds of errors from "Invalid number of parameters" to "Unable to get the Group property". Here is what I'm trying to do:
$FALSE = 0; $TRUE = 1;
...
my $Excel = &openExcel(); my $filename = "<filename>"; my $book = &openBook($Excel, $filename); my $ClosedSheet = $book->Worksheets("Closed");
...
my $closedRows = $ClosedSheet -> UsedRange -> Find({What => "*", Searc +hDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
...
$range = "A1:I" . $closedRows; my $closedRange = $ClosedSheet->Range($range); my $closedPivot = $ClosedSheet->PivotTableWizard(1, $closedRan +ge, $ClosedSheet->Cells($closedRows+2,1), "Closed SPR PivotTable"); # Set pivot fields... $closedPivot->AddFields("date_fixed"); $closedPivot->PivotFields("Product")->{Orientation} = 4; # + 4=xlDataField # Group by days #TODO my $thetargetcell = $ClosedSheet->Cells($closedRows+3, 1)- +>{Value}; print "target val: " . $thetargetcell . "\n"; $ClosedSheet->Cells($closedRows+3, 1)->Group( {Start => $T +RUE, End => $TRUE, By => 1, Periods => [$FALSE, $FALSE, $FALSE, $TRU +E, $FALSE, $FALSE, $FALSE]});
Sure, some of this is ugly, but that print statement does output the correct data, so I know I'm getting the correct cell. This is the equivalent VBA code I got from recording an Excel macro:
Range("A460").Select Selection.Group Start:=True, End:=True, By:=1, Periods:=Array(Fals +e, _ False, False, True, False, False, False)
If anyone knows anything, let me know.

Replies are listed 'Best First'.
Re: Win32::OLE Range->Group Method
by Anonymous Monk on Nov 02, 2010 at 11:31 UTC
    You can't group data in a pivot table in Excel, maybe you need to copy and paste (values only) so that it isn't a pivot table anymore? http://www.ozgrid.com/Excel/grouping-pivot-tables.htm
      It is not the pivot table itself (per se) that I am trying to group. Rather, I am trying to group the column field (comprised of dates, none of which are blanks as suggested by the previous link). I am absolutely sure this is possible, as I have both manually achieved this (right click a cell in the field and selected 'Group'), as well as through a very simple vba script (see code at bottom of original post). I have been trying to follow the documentation at MSDN documentation with no success.