简体   繁体   English

如何让Perl的Spreadsheet :: WriteExcel用VLOOKUP创建公式?

[英]How do I get Perl's Spreadsheet::WriteExcel to create formulas with VLOOKUP?

I'm having difficulty with Spreadsheet::WriteExcel and formulas that use VLOOKUP . 我在使用Spreadsheet :: WriteExcel和使用VLOOKUP公式时遇到了困难。 The following test script populates a worksheet with some data and tries to create a VLOOKUP formula. 以下测试脚本使用一些数据填充工作表,并尝试创建VLOOKUP公式。 When I open the resulting Excel file, the formula results are displayed as #VALUE! 当我打开生成的Excel文件时,公式结果显示为#VALUE! . If I manually edit any of the cells containing formulas (press F2 and then just ENTER without changing anything), I can get Excel to evaluate the formula properly. 如果我手动编辑任何包含公式的单元格(按F2然后只输入ENTER而不更改任何内容),我可以让Excel正确评估公式。 Any idea what going wrong? 知道出了什么问题吗?

For what it's worth, if I open the same file in OpenOffice, the formulas work fine. 对于它的价值,如果我在OpenOffice中打开相同的文件,公式工作正常。

use strict;
use warnings;
use Spreadsheet::WriteExcel;

my $wb = Spreadsheet::WriteExcel->new('foo.xls');
my $ws = $wb->add_worksheet;

for my $r (0 .. 9){
    for my $c (0 .. 4){
        $ws->write($r, $c, $r * 10 + $c);
    }
    $ws->write($r, 10, $r * 10);
    my $formula = sprintf('=VLOOKUP(K%s, A1:B10, 2, FALSE)', $r + 1);
    $ws->write( $r, 11, $formula );
    # $ws->write_formula( $r, 11, $formula ); # Does not help either.
}

Version info: 版本信息:

  • Excel 2007 SP2. Excel 2007 SP2。
  • Spreadsheet::WriteExcel: tried both 2.25 and 2.37. Spreadsheet :: WriteExcel:尝试了2.25和2.37。

I am the author of Spreadsheet::WriteExcel. 我是Spreadsheet :: WriteExcel的作者。

This is a known error with the formula parser and certain formula types in WriteExcel. 这是公式解析器和WriteExcel中的某些公式类型的已知错误。 You can work around it using store_formula() and repeat_formula() as shown below: 您可以使用store_formula()repeat_formula()来解决它,如下所示:

use strict;
use warnings;
use Spreadsheet::WriteExcel;

my $wb = Spreadsheet::WriteExcel->new('foo.xls');
my $ws = $wb->add_worksheet;

my $formula = $ws->store_formula('=VLOOKUP(K1, A1:B10, 2, FALSE)');

# Workaround for VLOOKUP bug in WriteExcel.
@$formula = map {s/_ref2d/_ref2dV/;$_} @$formula;

for my $r (0 .. 9){
    for my $c (0 .. 4){
        $ws->write($r, $c, $r * 10 + $c);
    }
    $ws->write($r, 10, $r * 10);

    $ws->repeat_formula( $r, 11, $formula, undef, qr/^K1$/, 'K' . ($r +1) );
}

I'm maintainer of writeexcel rubygem. 我是writeexcel ruby​​gem的维护者。 for example, ruby code is below. 例如,ruby代码如下。

require 'rubygems'
require 'writeexcel'

wb = WriteExcel.new('fooruby.xls')
ws = wb.add_worksheet

formula = ws.store_formula('=VLOOKUP(K1, A1:B10, 2, FALSE)')

# Workaround for VLOOKUP bug in WriteExcel.
formula.map! {|f| f.sub(/_ref2d/, '_ref2dV') }

(0..9).each do |row|
  (0..4).each { |col| ws.write(row, col, row * 10 + col) }
  ws.write(row, 10, row * 10)
  ws.repeat_formula(row, 11, formula, nil, /^K1$/, "K#{row+1}" )
end

wb.close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM