简体   繁体   English

Perl:Spreadsheet :: WriteExcel无法与EXCEL中的PERCENTILE FORMULA一起使用

[英]Perl: Spreadsheet::WriteExcel not working with PERCENTILE FORMULA IN EXCEL

Below is my code 下面是我的代码

use Spreadsheet::WriteExcel; 使用Spreadsheet :: WriteExcel;

# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new('ttif.xls');
 $workbook->compatibility_mode();
# Add a worksheet
$worksheet = $workbook->add_worksheet();

#  Add and define a format
$format = $workbook->add_format(); # Add a format
$format->set_bold();
$format->set_color('blue');
$format->set_align('center');
my $row = 2;
my $count = 1;
my $lat = '28.580022';
my $long = '77.315362';
my $alt_range = 300;
my $ttf_range = 20;
my $gps_time  = 138955387;
$worksheet->write('A1',Latitude);
$worksheet->write('B1',Longitude);
$worksheet->write('C1',Alt);
$worksheet->write('D1',TTFF);
$worksheet->write('E1','GPS Time');
while ($count <= 15725)
{
$worksheet->write("A$row",$lat);
$worksheet->write("B$row",$long);
my $random_number = int(rand($alt_range));
$worksheet->write("C$row",$random_number);
$random_number = int(rand($ttf_range));
$worksheet->write("D$row",$random_number);
$worksheet->write("E$row",$gps_time);
    $worksheet->write("H$row",$count);
$worksheet->write("I$row","=H$row/100");
$count++;
$row++;
}
my $percentile = $worksheet->store_formula('=PERCENTILE(D:D,I1)');
my $row  = 2;
my $count = 1;
while ($count <=100)
{
    $worksheet->repeat_formula("J$row",$percentile,$format,'I1',"I$row");
$count++;
$row++;
}

When I open the generate file in excel instead of showing Percentile result it shows #VALUE! 当我在excel中打开生成文件而不显示百分比结果时,它显示#VALUE! error Kindly help 错误请帮助

The percentile () formula (in this case) isn't being parsed correctly and you are getting #VALUE as the result of the formula. percentile ()公式(在这种情况下)未正确解析,因此公式的结果为#VALUE

You can fix that by modifying the repeat_formula() arguments as follows: 您可以通过修改repeat_formula()参数来解决此问题,如下所示:

$worksheet->repeat_formula( "J$row", $percentile, $format, 'I1', "I$row",
    '_ref2d' => '_ref2dV' );

The extra pair of match/replace tokens at the end help the parser chose the correct formula class for the function in use. 最后,一对额外的匹配/替换标记对帮助解析器为使用中的函数选择正确的公式类。

Alternatively, you could try the Excel::Writer::XLSX module which uses the same API as Spreadsheet::WriteExcel but which doesn't suffer this type of problem with formulas and which doesn't require the store_formula ()/ repeat_formula () performance workaround. 另外,您可以尝试使用Excel :: Writer :: XLSX模块,该模块使用与Spreadsheet :: WriteExcel相同的API,但不会在公式中遇到此类问题,并且不需要store_formula ()/ repeat_formula ()性能变通办法。 Read the documentation before you choose to migrate to the newer module to make sure it meets your requirements. 在选择迁移到较新的模块之前,请先阅读文档,以确保它符合您的要求。

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

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