Thread

Posted on Thu Apr 14 22:13:21 2005 by marfolarfo
Time Formula not working correctly when generated by spreadsheet::writeExcel
I am experiencing a rather strange problem when I set up a spreadsheet to compute the differences between two times using the spreadsheet::writeExcel perl module. The problem occurs when the times cross an hour boundry. For instance, 9:59 AM and 10:00 AM would show as 24.02 hours apart. I can not reproduce the problem by typing the data and formula directly into excel. However, if I copy the generated data and formula to a different place in the spreadsheet, I get the same results. The only thing that I found that would cause it to calculate correctly is to edit either of the two data times manually. Even if you just change it to what it currently is it will still fix the problem. Any ideas? Below I have attached a piece of code that will demonstrate the problem.
#!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; use Spreadsheet::WriteExcel::Utility; my $workbook = Spreadsheet::WriteExcel->new("test2.xls"); my $worksheet1 = $workbook->add_worksheet(); my $timeFormat = $workbook->add_format( size => 10, valign => 'vcenter', align => 'right', num_format => 'hh:mm AM/PM', ); my $numFormat = $workbook->add_format( size => 10, valign => 'vcenter', align => 'right', num_format => '0.00', ); my $formula = '=IF(OR(ISBLANK(' . xl_rowcol_to_cell(2, 0) . '),ISBLANK(' . xl_rowcol_to_cell(1, 0) . ')),0,((' . xl_rowcol_to_cell(2, 0) . '-' . xl_rowcol_to_cell(1, 0) . '+(' . xl_rowcol_to_cell(2, 0) . '<' . xl_rowcol_to_cell(1, 0) . ')))*24)'; # Add data and formula to worksheet. $worksheet1->write(0, 0, "worksheet1" ); $worksheet1->write(1, 0, "9:59 AM", $timeFormat ); $worksheet1->write(2, 0, "10:00 AM", $timeFormat ); $worksheet1->write_formula(1, 1, $formula, $numFormat);
Direct Responses: 344 | Write a response
Posted on Fri Apr 15 01:49:28 2005 by jmcnamara in response to 342
Re: Time Formula not working correctly when generated by spreadsheet::writeExcel

In your code you are writing strings, not times:

$worksheet1->write(1, 0, "9:59 AM", $timeFormat ); $worksheet1->write(2, 0, "10:00 AM", $timeFormat );

Instead you need to do something like this

$worksheet1->write_date_time(1, 0, "T09:59", $timeFormat); $worksheet1->write_date_time(2, 0, "T10:00", $timeFormat);

See the Dates in Excel and write_date_time() sections of the Spreadsheet::WriteExcel documentation for more details.

John.
--
Direct Responses: 348 | Write a response
Posted on Fri Apr 15 18:23:26 2005 by marfolarfo in response to 344
Re: Time Formula not working correctly when generated by spreadsheet::writeExcel
Thanks John.
Write a response