开发者

Perl: Spreadsheet::WriteExcel not working with PERCENTILE FORMULA IN EXCEL

开发者 https://www.devze.com 2023-02-28 01:28 出处:网络
Below is my code use Spreadsheet::WriteExcel; # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new(\'ttif.xls\');

Below is my code

use 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! 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.

You can fix that by modifying the repeat_formula() arguments as follows:

$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. Read the documentation before you choose to migrate to the newer module to make sure it meets your requirements.

0

精彩评论

暂无评论...
验证码 换一张
取 消