I’m using the Perl module ODF::lpOD to add content to a template ODF document that I created using LibreOffice. The template contains, among other things, a table:
In the template, I enabled right-alignment in the rightmost row, but when I set the value of the cell using $cell->set_text($number)
, the alignment gets reset to left.
The closest I have come to a solution was creating a new style:
my $s = odf_create_style(
'table cell',
name => 'right-aligned-style',
);
$s->set_properties(
area => 'paragraph',
align => 'right',
);
and then assigning this style to the cell:
$cell->set_text($number);
$cell->set_style('right-aligned-style');
This does indeed achieve the alignment I want, but it removes the borders:
If I try to add the borders to the above style, I can, but they are in smaller box and clearly not where they should be.
It’s normally possible to “clone” a style, but apparently it’s not possible to clone a table cell style, otherwise I would clone the existing style and then simply change/add the alignment property.
Does anyone have the magic incantation to get this working?
I created a sample ODF document and an demonstration script which will open the document, set the value of cell B1, set a new style for cell B1, and then write a new document file.
The document contains a sample 2×2 table with 0.5pt borders. Cell B1 is right-aligned, not that it does any good. The document can be retrieved using this URL:
Link to sample document
The script below will set the value of cell B1 and also set the table cell style, but as I explained above, this removes the previous style from the cell and thus the borders disappear.
By default the script expects the sample input document to be in “./doc-with-table-ra.odt” and will write the output document to “./demo-ra-document.odt”. These can be overridden with -input <file> and -output <file>. The script expects that ODF::lpOD be available somewhere in Perl’s search path.
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use ODF::lpOD;
my $template_document_file = './doc-with-table-ra.odt';
my $output_document_file = './demo-ra-document.odt';
GetOptions(
'input=s' => $template_document_file,
'output=s' => $output_document_file,
) || die "error with options";
-f $template_document_file || die "template [$template_document_file] not found";
# open template
print "open [$template_document_file] ...n";
my $doc = odf_document->get($template_document_file) or die "failed to load template";
my $body = $doc->get_body;
# create table cell style with right alignment
print "create and register new table cell style...n";
my $tc_style_name = 'right-justified-table';
my $tc_style = odf_create_style(
'table cell',
name => $tc_style_name,
);
$tc_style->set_properties(
area => 'paragraph',
align => 'right',
);
$doc->register_style($tc_style);
# fetch table
print "fetch table...n";
my $table = $body->get_table_by_name('Table1');
# set a value in cell 'B1' (row=0, column=1)
print "get cell object...n";
my $cell = $table->get_cell(0, 1);
# set value of cell
my $value = 123.4;
print "set cell value to [$value]n";
$cell->set_text('123.4');
# assign style to cell
print "set cell style...n";
$cell->set_style($tc_style_name);
# write output document
print "save modified document to [$output_document_file]n";
$doc->save(target => $output_document_file);
exit 0;
treefrob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6