简体   繁体   English

Spreadsheet :: WriteExcel编写公式时,将其视为字符串

[英]Spreadsheet::WriteExcel while writing the formula, its considered as string

I am writing the VLOOKUP formula to an excel. 我正在将VLOOKUP公式编写为Excel。 i tried directly writing the formula using write_formula. 我尝试使用write_formula直接编写公式。 I used to get #VALUE! 我曾经获得#VALUE! error in excel when i hit ctrl+shift+enter in excel it works fine. 当我在excel中按ctrl + shift + enter时,excel中出现错误,它工作正常。 Also tried with store_formula & repeat formula same error. 还尝试了与store_formula和重复公式相同的错误。

Looks like the formula is considered as string. 看起来该公式被视为字符串。 Is there a way to overcome this problem? 有办法解决这个问题吗? Please help! 请帮忙!

my $i = 0;
foreach (@col){
my $op_mode_lookup = $worksheet1->store_formula("=VLOOKUP(B16,DB Sheet!A2:D5,".(${i}+1).",FALSE)");
.
.
.
$worksheet1->repeat_formula('B'.$row_count, $op_mode_lookup, undef);
$i++;
}

I tried replacing the "store_formula("=VLOOKUP(B16,DB Sheet!A2:D5,".(${i}+1).",FALSE)");" 我尝试替换“ store_formula(“ = VLOOKUP(B16,DB Sheet!A2:D5,”。($ {i} +1)。“,FALSE)”);“ with below still the same error. 与下面仍然是相同的错误。

store_formula('=VLOOKUP(B16,DB Sheet!A2:D5,'.(${i}+1).',FALSE)'); store_formula('= VLOOKUP(B16,DB Sheet!A2:D5,'。($ {i} +1)。',FALSE)'); store_formula('=VLOOKUP(B16,DB Sheet!A2:D5,2,FALSE)'); store_formula('= VLOOKUP(B16,DB Sheet!A2:D5,2,FALSE)');

Is there an alternative perl module for Spreadsheet::WriteExcel, which would overcome this issue? Spreadsheet :: WriteExcel是否有替代的perl模块,可以克服此问题?

There are several issues here: 这里有几个问题:

  1. The sheet name DB Sheet contains a space so Excel requires that it is enclosed in single quotes when used in formulas, like this: "=VLOOKUP(B16,'DB Sheet'!A2:D5,1,FALSE)" . 工作表名称DB Sheet包含一个空格,因此Excel在公式中使用时要求将其用单引号引起来,例如: "=VLOOKUP(B16,'DB Sheet'!A2:D5,1,FALSE)"
  2. You are using store_formula() repeatedly in a loop. 您正在循环中重复使用store_formula() This is inefficient and incorrect. 这是低效且不正确的。 The point of store_formula() is that the expensive formula parse is only done once and that the pre-parsed formula can then be reused via repeat_formula() . store_formula()的要点是,昂贵的公式解析仅进行一次,然后可以通过repeat_formula()重用预先解析的公式。 The is explained at some length in the documentation and shown in numerous examples . 在文档中进行了详细说明,在许多示例进行了说明 You should go back and the docs carefully and then try to apply the methodology correctly or avoid using it if it isn't something that you require. 您应该仔细查看文档,然后尝试正确地应用该方法,如果不需要,请避免使用它。
  3. There is a known (but not easily fixable) bug that can affect VLOOKUP formulas when used from Spreadsheet::WriteExcel. 从Spreadsheet :: WriteExcel使用时,有一个已知的(但不容易修复)错误会影响VLOOKUP公式。 A workaround is shown in the code below. 解决方法在下面的代码中显示。

So, fixing these issues would give something like this: 因此,解决这些问题将得到以下信息:

#!/usr/bin/perl -w

use strict;
use Spreadsheet::WriteExcel;

my $workbook  = Spreadsheet::WriteExcel->new( 'demo.xls' );
my $worksheet1 = $workbook->add_worksheet();
my $worksheet2 = $workbook->add_worksheet('DB Sheet');

$worksheet1->write( 'B16', 1 );

for my $row ( 0 .. 7 ) {
    $worksheet2->write( $row, 0, $row );
}

my $op_mode_lookup = $worksheet1->store_formula(
                     "=VLOOKUP(B16,'DB Sheet'!A2:D5,1,FALSE)");

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

my @col = (0, 1, 2, 3);
my $row_count = 1;

foreach ( @col ){
    $worksheet1->repeat_formula('B' . $row_count, $op_mode_lookup, undef);
    $row_count++;
}

__END__

However , I would advise avoiding all of this store_formula() code and workaround and just use Excel::Writer::XLSX instead. 但是 ,我建议避免使用所有此类store_formula()代码和变通办法,而应改用Excel :: Writer :: XLSX

It is API compatible with Spreadsheet::WriteExcel and has faster and non-buggy formula support. 它与Spreadsheet :: WriteExcel兼容,并且具有更快,更轻松的公式支持。

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

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