how to right-align a table cell using ODF::lpOD?

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;

New contributor

treefrob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

how to right-align a table cell using ODF::lpOD?

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;

New contributor

treefrob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật