如何使用 Win32::OLE 设置 Excel 公式?

发布于 2024-08-10 04:58:30 字数 1137 浏览 5 评论 0原文

谁能告诉我,为什么 ...->{FormulaR1C1} = '=SUMME( "R[-3]C:R[-1]C" )'; 不起作用。在应显示结果的单元格中,我得到“#Wert!” (也许是英语中的“价值”)。通过 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 } );

Can anybody tell me, why the ...->{FormulaR1C1} = '=SUMME( "R[-3]C:R[-1]C" )'; doesn't work. In the Cell where the result should appear I get "#Wert!" ( maybe "Value" in English ). With the WENN(IF)-formula I get what I expect.

#!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 } );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2024-08-17 04:58:30

您不需要围绕 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 } );

You do not need quotes around the SUM range. It should be explicit:

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

Additional point - your IF/WENN formula is incorrect. It is trying to compare the string "RC[-1]" to the number 5 and coming up with YES! THE STRING IS GREATER. It is not doing what you think it is doing... you should take out the quotes around the references here as well.

EDIT: Here's my version of you code, which runs without any errors. Changes are commented. Had to apply a few changes for the English version of Excel. Ran against 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 } );
别想她 2024-08-17 04:58:30

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

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

并使用 Z1S1 而不是 R1C1

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

但看起来在德语版本中我必须在 A1A1 之间进行选择代码>Z1S1 (R1C1) 表示法。

With the help of the perl-community.de I have now a solution:
I have to set

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

and use Z1S1 instead of R1C1

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

But it looks like that in the German version I have to choose between the A1 and the Z1S1 (R1C1) notation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文