1

谁能告诉我,为什么...->{FormulaR1C1} = '=SUMME( "R[-3]C:R[-1]C" )';不起作用。在结果应该出现的单元格中,我得到“#Wert!” (也许是英文的“Value”)。使用 WENN(IF) 公式,我得到了我所期望的。

#!C:\Perl\bin\perl.exe
use warnings;
use strict;
use Win32::OLE qw;
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;
my $xl = Win32::OLE::Const -> Load( 'Microsoft Excel' );
my $excelfile = 'win32_ole_excel.xls';
my $excel = Win32::OLE -> GetActiveObject( 'Excel.Application' ) || Win32::OLE -> new( 'Excel.Application', 'Quit' ) or die $!;

my $workbook = $excel -> Workbooks -> Add();
my $sheet = $workbook -> Worksheets( 1 );
$sheet -> Activate;


$sheet->Range( 'A3' )->{Value} = 10;
$sheet->Range( 'B3' )->{FormulaR1C1} = '=WENN( "RC[-1]" > 5; "OK"; "Not OK")'; # IF(,,); workes fine


$sheet->Range( 'G1' )->{Value} = 3;
$sheet->Range( 'G2' )->{Value} = 7;
$sheet->Range( 'G3' )->{Value} = 6;
$sheet->Range( 'G4' )->{FormulaR1C1} = '=SUMME( "R[-3]C:R[-1]C" )'; # SUM(); doesn't work


$workbook -> SaveAs( { Filename => $excelfile, FileFormat => xlWorkbookNormal } );
4

2 回答 2

2

您不需要在 SUM 范围内加上引号。它应该是明确的:

=SUMME(R[-3]C:R[-1]C)

附加点-您的 IF/WENN 公式不正确。它正在尝试将字符串“RC[-1]”与数字 5 进行比较并得出 YES!字符串更大。它没有做你认为它正在做的事情......你也应该在此处删除引用周围的引号。

编辑:这是我的代码版本,运行时没有任何错误。更改已注释。必须对英文版的 Excel 进行一些更改。针对ActivePerl 5.10.1 Build 1006 运行

#!C:\Perl\bin\perl.exe
use warnings;
use strict;
# CHANGE - empty qw caused compilation error
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;
my $xl = Win32::OLE::Const -> Load( 'Microsoft Excel' );
# CHANGE - set path
my $excelfile = 'C:\win32_ole_excel.xls';
my $excel = Win32::OLE -> GetActiveObject( 'Excel.Application' ) || Win32::OLE -> new( 'Excel.Application', 'Quit' ) or die $!;

my $workbook = $excel -> Workbooks -> Add();
my $sheet = $workbook -> Worksheets( 1 );
$sheet -> Activate;

$sheet->Range( 'A3' )->{Value} = 10;
# CHANGE - Use IF, use commas, took quotes out around range
$sheet->Range( 'B3' )->{FormulaR1C1} = '=IF( RC[-1] > 5, OK, Not OK)'; # IF(,,); workes fine

$sheet->Range( 'G1' )->{Value} = 3;
$sheet->Range( 'G2' )->{Value} = 7;
$sheet->Range( 'G3' )->{Value} = 6;
# CHANGE - Use SUM, took quotes out around range
$sheet->Range( 'G4' )->{FormulaR1C1} = '=SUM(R[-3]C:R[-1]C)'; # SUM(); doesn't work

$workbook -> SaveAs( { Filename => $excelfile, FileFormat => xlWorkbookNormal } );
于 2009-11-04T16:36:46.583 回答
1

在perl-community.de的帮助下,我现在有了一个解决方案:我必须设置

$excel->{ReferenceStyle} = $xl->{xlR1C1};

并使用Z1S1而不是R1C1

=SUMME(Z(-2)S:Z(-1)S)

但在德语版本中,我必须A1Z1S1( R1C1) 符号之间进行选择。

于 2009-11-06T19:03:11.220 回答