in reply to set_align() of merged cells in Spreadsheet::WriteExcel


Spreadsheet::WriteExcel is still based primarily on the Excel 5 file format. And in Excel 5 merged cells could only have a "Center" alignment. As such the alignments "right" and "merge" are mutually exclusive.

However, you can obtain the effect that you want be using the merge_cells() method. This gives you access to some of the features of Excel 97+ merging.

Specify the alignment as "right", ignore the "merge" property and force alignment using the merge_cells() method:

#!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("reload.xls"); my $worksheet = $workbook->addworksheet(); my $format = $workbook->addformat( align => 'right', border => 2 ); $worksheet->merge_cells("B4:D4"); $worksheet->write("B4", "Hello", $format); $worksheet->write("C4", "", $format); $worksheet->write("D4", "", $format);
The documentation and examples will be clearer on this in the next release.

--
John.

Replies are listed 'Best First'.
Re: Re: set_align() of merged cells in Spreadsheet::WriteExcel
by Anonymous Monk on Feb 11, 2002 at 00:48 UTC
    John,

    Wow - I'm more than a bit impressed to hear from the module author. Thanks for taking the time.

    I've tested your suggestion, and thought you might find some feedback useful:

    I'm designing for Excel 2000. In Excel 2000, the code you suggest results in the token being right aligned, but in the first cell, even though the specified cells appear merged.

    Similarly, changing the alignment to 'center' produces the token centred in the first cell.

    This therefore solves left-aligning, since specifying align => 'left' left-aligns the token in the first cell.

    To right align, your suggested code does disengage the Excel 95 'center' default, but I've had to add explicitly writing the token to the last cell with align => 'right', which right aligns the token in the last cell.

    And to centre-align, I use the merge_cells + set_merge combination of the original post.

    I hope this is useful for your next revision.

    Thanks, though, for an overall fab module.