Excel::Writer::XLSX: Problems with LibreOffice - perl

#!/usr/bin/env perl
use warnings;
use strict;
use Excel::Writer::XLSX;
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );
my $worksheet = $workbook->add_worksheet();
$worksheet->write_formula( 'A1', '=SUM(1, 2, 3, 4)' );
$workbook->close();
Until now this worked fine with LibreOffice. But today this doesn't work any more ( the value in A1 is 0 ) - maybe due to some LibreOffice-updates (LibreOffice 3.5 Build-ID: 350m1(Build:402)).
Does the occasionally in the following part of the Excel::Writer::XLSX documentation refer to such kind of situations?
If required, it is also possible to specify the calculated value of the formula. This is occasionally necessary when working with non-Excel applications that don't calculate the value of the formula. The calculated $value is added at the end of the argument list

Excel::Writer::XLSX doesn't calculate the value of a formula that it writes. Instead it inserts 0 and allows the user to specify the value if required.
This isn't a great solution but it is the best that can be done since calculating arbitrary complex formulae is beyond the scope of the module.
And, in general Excel and other third party applications will recalculate formula values for display.

It's a setting in LibreOffice. Here is a solution (quote from here):
LibreOffice intentionally does not recalculate older spreadsheets,
because as formulas are updated from version to version or between
different spreadsheet programs, the results can be different. Go to
Tools – Options – LibreOffice Calc, under 'Recalculation on file
load', change the two drop-downs, 'Excel 2007 and newer' and 'ODF
Spreadsheet (not saved by LibreOffice)', to 'Always recalculate'.
Click Ok, close the spreadsheet and LibreOffice. Now open the file in
LibreOffice and you should see that the formulas have recalculated.
Also go to Tools – Cell Contents and be sure that AutoCalculate is selected.

Related

Column Width autofit functionality using perl

I have generated the excel sheet using set of csv files using perl script.In that i need to set width of the column as auto-fit.What 'm suppose to do i've got The way to do this in the module called OLE i don't know how to do this you please help me?
There is no way to set a file's column to "AutoFit" because that is not a setting of the file. Excel calculates this and sets a column width at run-time.
One option is to calculate the "AutoFit" width yourself based on your data, and then set the column width to the value that you calculate.
This discussion gives some workaround code that does that.
Another option is to use Win32::OLE to set the column to AutoFit. How to do this is not documented in the module itself; you would have to search around the Microsoft OLE documentation to find the appropriate command.
Update: This site appears to show how to use AutoFit with Win32::OLE.
You could generate your Excel file with Excel::Writer::XLSX module, and adjust the column width with set_column method
Have a look at the autofit.pl example here

Using Spreadsheet::XLSX, how do I copy formats from one worksheet to another?

I'm using Perl v5.12.3 on Mac OSX Lion. I'm using the latest version of the Spreadsheet::XLSX module. How do you copy existing worksheet row, column, and cell formats from one XLSX file to a new file? For cells, I'm currently trying
my $cell = $oldWorksheet -> {Cells} [$row] [$col];
...
$newWorksheet->write( $newWorksheetCurRow, $col, $val, $cell->{Format} )
But it isn't working. For example, the background colors aren't getting copied and I don't even think "{Format}" is a valid attribute of the cell.
Spreadsheet::XLSX doesn't read cell formats so it isn't possible to copy a worksheet formatting like this.
As far as I know there isn't any Perl module, apart from Win32::OLE that reads formatting from an XLSX file.
I'm working on one but formatting support is several months away.
If it's like SpreadSheet::WriteExcel/ParseExcel, then you have to do it all manually. That is, create a new format on the writer side, then copy in all the attributes of the format from the parsed side.

Setting an Excel worksheet's custom page size (not the printable area) from Perl

Long story short, all I'm really attempting to do is print my reports on half sheets. I had Kinko's chop a pack of printer paper in half and my laser printer is perfectly happy to suck them in and print the reports properly, if the paper size of the Excel report is set to exactly 8.5" x 5.64".
That can be done easily in Excel, but it's the one and only adjustment, in my project, I wasn't able to automate with Perl using Spreadsheet::WriteExcel. The CPAN documentation states that you can pick from some of the default sizes normally available with Excel, but doesn't provide an option to specify your own paper size.
Even if you establish the custom size you need in Excel beforehand, making it available in future spreadsheets, as one of your selectable paper sizes, there doesn't seem to be an index, using set_paper($index), that would specify that newly established custom size.
Thank you in advance!
#!/usr/local/gnu/bin/perl --
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $repWB = Spreadsheet::WriteExcel->new('../tmp/test.xls');
my $repWS = $repWB->add_worksheet('AA');
$repWS->set_paper(1);
Since VBScript can do many of these things natively, maybe you could try embedding the necessary VBScript into your main Perl script using Inline:WSC.
You could determine the needed VBScript by recording an Excel macro of you setting the print size. Then embed that code into your main Perl script.
I am the author of Spreadsheet::writeExcel.
As far as I know there isn't an option in Excel to set a custom paper size. This is usually set in the printer (correct me if I am wrong).
Excel can store printer information along with the workbook data so there may be a workaround.
Can you send me a single sheet workbook with data in cell A1 only and a copy with the custom page set and I'll take a look to see if it is possible.
P.S. The available option B5 (index 13) in landscape mode is fairly close: 8.27" x5.83". Or the undocumented "Organiser L" (index 129) which should be half Letter size: 8.5" x 5.5".

Perl Excel preview problem in Outlook

I am generating Excel report in Perl.
I am using the Formula in the cell, it's working fine, but in Outlook when I see through preview file, the cell is showing something like Spreadsheet::WriteExcel::Format=HASH(0x87d6d04) instead of total.
I am using simple forumulas only, like =sum(B1:B10) or =sum(A1,B2).
How to fix this?
outlook excel preview
You probably need to use the write_formula method rather than the plain write one.
For example,
$worksheet->write_formula(1, 0, '=SIN(B1:B10)');
From the documentation on CPAN for Spreadsheet::WriteExcel
In your code:
$worksheet->write(..., $format05,$font );
You have an unnecessary trailing $fontat the end of that method call which is being passed to write_formula() (via write()) as an optional result for the formula.
That is what is showing up as the formula result in Outlook.

How to write formula in Excel sheet which is stored in the perl variable

use strict
use Spreadsheet::WriteExcel;
my $Workbook = Spreadsheet::WriteExcel->new('cw.xls');
my $Worksheet = $Workbook->add_worksheet('MRD');
my $Formula= "A10+B10/2";
$Worksheet->write_formula(1,2,'='.($Formula));
exit;
Error getting:
Couldn't parse formula: =A10+B10/2;
Note : In the Formula that I am using contain across sheet data calling.
When I am Pasting it in the excel sheet manually its working fine but through program I am unable to put it in the sheet.
Please if any one came across this problem or has solution let me know.
If you see the documentation of Spreadsheet::WriteExcel module, it states in the DIAGNOSTICS section that:
Couldn't parse formula ...
There are a large number of warnings which relate to badly formed formulas and functions. See the "FORMULAS AND FUNCTIONS IN EXCEL" section for suggestions on how to avoid these errors. You should also check the formula in Excel to ensure that it is valid.
There is no problem storing a formula in a variable and passing it to write_formula(). In fact your own snippet above proves that it works.
So the problem is with the formula that you are trying to write. Since you haven't posted the formula it is hard to guess what the problem might be but here are some formula debugging tips from the docs:
Verify that the formula works in Excel (or Gnumeric or OpenOffice.org).
Ensure that it isn't on the Caveats list shown above. (In the docs).
Ensure that cell references and formula names are in uppercase.
Ensure that you are using ':' as the range operator, A1:A4.
Ensure that you are using ',' as the union operator, SUM(1,2,3).
Ensure that the function is in the above table. (In the docs).
P.S. The leading '=' isn't required when you are using write_formula() so you can just use:
$Worksheet->write_formula(1, 2, $Formula);