以下脚本按预期工作。
use warnings;
use strict;
use Win32::Ole;
use Win32::OLE::Const 'Microsoft.Excel';
$Win32::OLE::Warn = 3;
my $excel = CreateObject Win32::OLE 'Excel.Application' or die;
$excel->{'Visible'} = 1;
my $workbook = $excel -> workbooks -> add(1);
my $sheet = $workbook -> sheets(1);
my $shape = $sheet -> shapes -> addChart;
my $chart = $shape -> chart;
$chart -> {chartType} = xlXYScatterSmoothNoMarkers;
$sheet -> cells(1,1) -> {value} = "x";
$sheet -> cells(1,2) -> {value} = "sin(x)";
my $row = 1;
for my $x (map {$_ / 10} (0..100)) {
$row ++;
$sheet -> cells($row, 1) ->{value} = $x;
$sheet -> cells($row, 2) ->{value} = sin($x);
}
$chart -> setSourceData(
$sheet->range(
$sheet->cells(1,2),
$sheet->cells(100,2)));
# This line works:
$chart -> SeriesCollection(1) -> {XValues} = '=Sheet1!$A$2:$A$102';
# Following doesn't work, fails with
# Win32::OLE(0.1709) error 0x80020003:
# in PROPERTYPUTREF "XValues"
#
# $chart -> SeriesCollection(1) -> {XValues} =
# $sheet->Range(
# $sheet->cells(2, 1 ),
# $sheet->cells(2, $row)
# );
$workbook -> {saved} = 1;
但如果我改变了行
$chart -> SeriesCollection(1) -> {XValues} = '=Sheet1!$A$2:$A$102';
与
$chart -> SeriesCollection(1) -> {XValues} =
$sheet->Range(
$sheet->cells(2, 1 ),
$sheet->cells(2, $row)
);
脚本失败并显示错误消息Win32::OLE(0.1709) error 0x80020003: Member not found in PROPERTYPUTREF "XValues" at ...
。
恕我直言,应该可以将一个OLE对象分配给一个OLE-Property(我以前已经这样做了,虽然不是XValues
属性)。