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!

WIN32::OLE set up settings of my excel pivot chart didn't work . Why?

Status
Not open for further replies.

bman87

Technical User
Feb 23, 2011
1
0
0
DE
Hi @ all,

in recent days I try to talk about Perl with Excel 2007 (about WIN32::OLE).
I want to create a Pivot table + chart. But I couldn't create a chart title
respectively I couldn't set up the settings of my pivot chart (Title, name of
the x-axes ....) . Please help me....

For a better understanding my perl code:

#! perl -w

use warnings;
use strict;

use Win32::OLE ;

my $excel = Win32::OLE->new('Excel.Application');
$excel->{'Visible'} = 1;

my $workbook = $excel -> Workbooks -> Add;

my $sheet = $workbook->Sheets("Tabelle1");
$sheet -> Range('A1')->{'Value'} = "Temp [°C]";
$sheet -> Range('B1')->{'Value'} = "Vcc [V]";
$sheet -> Range('C1')->{'Value'} = "Vio [V]";
$sheet -> Range('D1')->{'Value'} = "Ivcc [mA]";

$sheet -> Range('A2:D3')->{'Value'} = [
['1', '2', '3', '4' ],
['5', '6', '7', '8' ],
];

my $sheet2 = $workbook-> Sheets -> Add;
$sheet -> select;

my $pivot=$workbook->PivotTableWizard( {
SourceType => 1,
SourceData => 'Tabelle1!Z1S1:Z3S4',
TableDestination => "Tabelle4!Z1S1",
TableName => "PivotTable1",
HasAutoFormat => 1
});

$pivot->PivotFields("Vcc [V]")->{Orientation} = 1; # 4=xlDataField
$pivot->PivotFields("Temp [°C]")->{Orientation} = 2; # 4=xlDataField
$pivot->PivotFields("Vio [V]")->{Orientation} = 1; # 4=xlDataField
$pivot->PivotFields("Ivcc [mA]")->{Orientation} = 4; # 4=xlDataField
$pivot->PivotFields("Summe von Ivcc [mA]")->{Function} = 2; #{Caption} =
"Mittelwert von Ivcc [mA]";

$sheet2->Range('F1')->Select;
my $chart=$sheet2->Shapes->AddChart(65);
$sheet2->chartObjects("Diagramm 1")->Activate;
$chart->{HasTitle} = 1;
$chart->ChartTitle->{Text} = "Some Title";

$excel -> Quit;

=======================================
end
=======================================
The lines

$chart->{HasTitle} = 1;
$chart->ChartTitle->{Text} = "Some Title";

didn't work. Why?

Some additional questions:

Where can I find information/documentation on the "Perl excel syntax"?
Can I control excel completely with the WIN32::OLE modul?

Thanks for working on this topic

Best Regards
 
Some ideas...

Please see an example where I create a graph based on a table:

Code:
use strict;


use Win32::OLE;
use Win32::OLE qw(in);
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Const 'Microsoft Office .* Object Library';
use constant xl3DColumnClustered => 54;
use constant xlCategory => 1;
use constant xlColumns => 2;
use constant xlLocationAsObject => 2;
use constant xlSeries => 3;
use constant xlValue => 2;

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
       || Win32::OLE->new('Excel.Application', 'Quit');
       
use constant xl3DColumnClustered => 54;
use constant xlCategory => 1;
use constant xlColumns => 2;
use constant xlLocationAsObject => 2;
use constant xlSeries => 3;
use constant xlValue => 2;

## open Excel file
my $Book = $Excel->Workbooks->Add();

## Prove you did it
$Book->Worksheets("Sheet1")->{Name} = "SUMMARY";


$Book->Worksheets("SUMMARY")-> Range('A2:F5')->{'Value'} = [
    ["MARKET",	"Data1",	"Data2",	"Data3",	"Data4",	"Data5"],	
    ["W.Portland",	"110",	"0",	"0",	"42",	"68",	"1"],
    ["W.Denver",	  "58",	  "30",	"40",	"32",	"24",	"26"],
    ["All",         "168",	"30", "40",	"74"  ,"92", "27"],
    ];
    
my $sheet2 = $Book->Sheets ->Add;
$Book->Worksheets("Sheet2")->{Name} = "My Graph";


 my $range; 
 
 foreach my $i(2..5) { 
          $range = "A2:F2\,A$i:F$i";
          #print "range\n";
    			$Excel->Sheets('My Graph')->Select(); 
    			$Excel->ActiveWorkbook->Charts()->Add();
    			$Excel->ActiveWorkbook->ActiveChart->{ChartType} = xl3DColumnClustered;
					$Excel->ActiveWorkbook->ActiveChart->SetSourceData({Source => $Excel->ActiveWorkbook->Sheets('SUMMARY')->Range($range), PlotBy => xlColumns});
					$Excel->ActiveWorkbook->ActiveChart->Location({Where => xlLocationAsObject, Name  => 'My Graph'});
 
					$Excel->ActiveWorkbook->ActiveChart->{HasTitle} = 1;
					$Excel->ActiveWorkbook->ActiveChart->Axes(xlCategory)->{HasTitle} = 1;
					$Excel->ActiveWorkbook->ActiveChart->Axes(xlCategory)->AxisTitle->Characters->{Text} = 'Axis Title';
					$Excel->ActiveWorkbook->ActiveChart->Axes(xlSeries)->{HasTitle} = 1;
					$Excel->ActiveWorkbook->ActiveChart->Axes(xlValue)->{HasTitle} = 1;
					$Excel->ActiveWorkbook->ActiveChart->ApplyDataLabels({AutoText => 1});
					my $chartname = $Excel->ActiveWorkbook->ActiveChart->Name;
					$Excel->ActiveWorkbook->ActiveChart->PlotArea->Select();
          $Excel->ActiveWorkbook->ActiveChart->ApplyCustomType({ChartType => xl3DColumnClustered});
          $Excel->ActiveWorkbook->ActiveChart->ApplyDataLabels({AutoText => 1});
          $Excel->ActiveWorkbook->ActiveChart->{HasTitle} = 1;
					$Excel->ActiveWorkbook->ActiveChart->ChartTitle->Characters->{Text} = 'MARKET';
					
 	  }

	




$Book->SaveAs({Filename =>'C:\fromperl.xls',
              FileFormat => xlWorkbookNormal});  
$Book->Close();
$Excel->Quit();

I would recommend you to record a macro using Excel, then edit the macro and after that convert it using PDK-> VBA Script Converter, it helps a lot




dmazzini
GSM/UMTS System and Telecomm Consultant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top