Setting an Excel worksheet's custom page size (not the printable area) from Perl - 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".

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

How to create reports containing text and figures with MATLAB

I am using a MATLAB script to tune the control system on a machine. When the tuning is complete, I would like a report containing text (especially serial number, date/time and the values determined during tuning) and plots, especially transfer functions.
What do to you recommend?
Whatever solution I use should be compatible with the MATLAB compiler so I can distribute my solution to a team of field engineers.
Ideally the report will be a PDF document.
The MATLAB report generator does not seem to be the right product as it appears that I have to break up my script into little pieces and embed them in the report template. My script contains opportunities for the user to intervene and change values or reject the tune if plots don't look right and my hunch is that this will be difficult if the code runs from the report generator. Also, I fear code structure and maintainability will be lost if the code structure is determined by the requirements of the report template.
Please comment if my assumptions are wrong.
UPDATE
I have now switched to use the MATLAB Report Generator with release r2016b and it is working very well for my compiled code users. Unfortunately it means that colleagues who have a MATLAB licence need to buy the Report Generator too, to use my tools scripted.
As the MATLAB Report Generator's development manager, I am concerned that this question may leave the wrong impression about the Report Generator's capabilities.
For one thing, the Report Generator does not require you to break a script up into little pieces and run them inside a template. You can do this if you choose and in some circumstances, it makes sense, but it is not a requirement. In fact, many Report Generator applications use a MATLAB script or program to interact with a user, generate data in the MATLAB workspace, and as a final step, generate a report from the workspace data.
Moreover, as of the R2014b version, the MATLAB Report Generator comes with a document generation API, called the DOM API, that allows you to embed document generation statements in a MATLAB program. For example, you can programmatically create a document object, add and format text, paragraphs, tables, images, lists, and subdocuments, and output Microsoft Word, HTML, or PDF output, depending on the output type you select. You can even programmatically fill in the blanks in forms that you create, using Word or an HTML editor.
The API runs on Windows, Linux, and Mac platforms and generates Word and HTML output on all three, without the use of Word. On Windows, it uses Word under the hood to produce PDF output from the Word documents that it generates.
The latest release of the MATLAB Report Generator introduces a PowerPoint API with capabilities similar to the DOM API. If you need to include report generation in your MATLAB application, please don't rule out the MATLAB Report Generator based on past impressions. You may be surprised at just how powerful it has become.
I've done this quite a bit. You're right that MATLAB Report Generator is typically not a great solution. #Max suggests the right approach (automating Word through its COM interface), but I'd add a few extra comments and tips, based on my experiences.
Remember that if you're going with this solution, you are depending that your end-users will be running Windows, and have a copy of Office on their machine. If you want to ultimately produce a PDF report, that will need to be Office 2010 or above.
I would bet that you'll find it easier to automate the report generation in Excel rather than Word. Given that you're producing a report from MATLAB, you'll likely be wanting quite a lot of things in tables of numbers, which are easier to lay out in Excel.
If you are going to do it in Word, the easiest way is to first (without MATLAB) create a template .doc/.docx file, which contains any generic text that will be the same for all reports and blank tables for any information. Turn on track changes, and insert empty comments at each point that you will be filling in information. Then within your report creation routine in MATLAB, connect to Word and iterate through each comment, replacing it with whatever data you wish.
If you are learning to automate Excel from MATLAB, this page from the Excel Interop documentation is really helpful. There's an equivalent one for Word.
Unlike #Max, I've never had good results by saving figures to an .emf file and then inserting them. In theory that does preserve editability, but I've never found that valuable. Instead, get the figure looking right (and the right size) in MATLAB, then copy it to the clipboard with print(figHandle, 'dbitmap') and paste to Excel with Worksheet.Range('A1').PasteSpecial.
To save as a PDF, use Workbook.ExportAsFixedFormat('xlTypePDF', pathToOutputFile).
Hope that helps!
I think you are right about the report generator.
In my opinion the fastest/easiest approach would be to generate the report in a html document. For that you just need the figures and write a text file, conversion should be trivial.
Quite similar approach would be to create a Latex file. And then create a pdf from it - though for this you'd need to install latex on your deployed machines.
Lastly you could use the good integration of Java in Matlab. There are several libraries you could use - like this. But I wonder if all the complication will be worth it.
Have you considered driving Microsoft Word through its ActiveX interface? I've done this in compiled Matlab programs and it works well. Look at the Matlab help for actxserver(): The object you want to create is of type Word.Application.
Edit to add: To get figures into the document, save them as .emf files using the -dmeta argument to print(), then add them to the document like this:
WordServer.Selection.InlineShapes.AddPicture(fileName);

How to export a crystal report as a fixed character into a text file

I'm trying to export a formula with fixed characters spacing into a text file.
What is happening is that its a long set of fields that is longer then the report page in crystal can show. When running the report it places the additional fields under and it repeats for all the records. When I try to export it to a text file it is pushing it the same way instead of that one record being one whole line across on the text file like I would like it to be. How can this be done. New to crystal thanks in advance.
Since you're exporting to plaintext, I'm assuming you don't care what the format of the actual report is (since it is merely an intermediate player during the export process). If that's the case, then you can just expand the page size horizontally until it can accommodate all of your fields the way you want them.
Your page size is probably 8.5"x11" by default. Go into your Page Setup ("File" -> "Page Setup") and make it the size that suits your fields.
EDIT: If you're using an older version of CR, you may have to use a dummy printer that can accommodate large paper sizes. See here.

Excel::Writer::XLSX: Problems with LibreOffice

#!/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.

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.