From c8b75d8c2923adc49f167227c2921281c20e1d70 Mon Sep 17 00:00:00 2001 From: Christoph Schaefer Date: Thu, 19 Nov 2026 06:24:41 +0100 Subject: [PATCH 1/2] fix(OdfFile): keep the ODF 1.3 sheet tab colour from ODS in XLSX Sheet tab colours from current LibreOffice files were lost when an ODS was opened: the editor showed every tab without its colour, and saving the file back to ODS dropped the colours for good. ODF 1.3 stores the tab colour as table:tab-color on the table style's style:table-properties. LibreOffice writes that attribute when it saves ODF 1.3 or later, its default format, and the older tableooo:tab-color only for ODF 1.2 extended. The reader only knew tableooo:tab-color. Read table:tab-color as well and write it to sheetPr/tabColor. When a file has both attributes, table:tab-color wins; tableooo:tab-color is still used on its own as before. Tested with four test_odf cases on one ODS with a sheet per case: table:tab-color, tableooo:tab-color, both with different colours, and no tab colour. The ODF 1.3 and the both-attributes cases fail without the fix. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Christoph Schaefer --- OdfFile/Reader/Converter/xlsx_table_state.cpp | 9 ++- .../Reader/Format/style_table_properties.cpp | 1 + .../Reader/Format/style_table_properties.h | 3 +- OdfFile/Test/test_odf/CMakeLists.txt | 1 + .../test_odf/ExampleFiles/sheet_tab_color.ods | Bin 0 -> 982 bytes OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp | 74 ++++++++++++++++++ REUSE.toml | 5 ++ 7 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color.ods create mode 100644 OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp diff --git a/OdfFile/Reader/Converter/xlsx_table_state.cpp b/OdfFile/Reader/Converter/xlsx_table_state.cpp index cc683d14eb..f7c0690156 100644 --- a/OdfFile/Reader/Converter/xlsx_table_state.cpp +++ b/OdfFile/Reader/Converter/xlsx_table_state.cpp @@ -722,11 +722,16 @@ void xlsx_table_state::serialize_table_format (std::wostream & strm) CP_XML_NODE(L"sheetPr") { //at filterMode="false"> - if (table_prop && table_prop->content().tableooo_tab_color_) + // ODF 1.3 table:tab-color wins over the older tableooo:tab-color. + _CP_OPT(odf_types::color) tab_color; + if (table_prop) + tab_color = table_prop->content().table_tab_color_ ? table_prop->content().table_tab_color_ + : table_prop->content().tableooo_tab_color_; + if (tab_color) { CP_XML_NODE(L"tabColor") { - CP_XML_ATTR(L"rgb", table_prop->content().tableooo_tab_color_->get_hex_value()); + CP_XML_ATTR(L"rgb", tab_color->get_hex_value()); } } if (fit_to_pages) diff --git a/OdfFile/Reader/Format/style_table_properties.cpp b/OdfFile/Reader/Format/style_table_properties.cpp index a2052ba9ba..ac127972cb 100644 --- a/OdfFile/Reader/Format/style_table_properties.cpp +++ b/OdfFile/Reader/Format/style_table_properties.cpp @@ -61,6 +61,7 @@ void table_format_properties::add_attributes( const xml::attributes_wc_ptr & Att CP_APPLY_ATTR(L"table:align", table_align_); CP_APPLY_ATTR(L"table:border-model", table_border_model_); CP_APPLY_ATTR(L"table:display", table_display_); + CP_APPLY_ATTR(L"table:tab-color", table_tab_color_); CP_APPLY_ATTR(L"tableooo:tab-color", tableooo_tab_color_); CP_APPLY_ATTR(L"style:use-optimal-column-width",style_use_optimal_column_width_); } diff --git a/OdfFile/Reader/Format/style_table_properties.h b/OdfFile/Reader/Format/style_table_properties.h index 4316fce4a5..b41fa3243a 100644 --- a/OdfFile/Reader/Format/style_table_properties.h +++ b/OdfFile/Reader/Format/style_table_properties.h @@ -73,7 +73,8 @@ class table_format_properties _CP_OPT(odf_types::border_model) table_border_model_; _CP_OPT(odf_types::table_align) table_align_; _CP_OPT(bool) table_display_; - _CP_OPT(odf_types::color) tableooo_tab_color_; + _CP_OPT(odf_types::color) table_tab_color_; // ODF 1.3 + _CP_OPT(odf_types::color) tableooo_tab_color_; // before ODF 1.3 _CP_OPT(odf_types::Bool) style_use_optimal_column_width_; //not specification office_element_ptr style_background_image_; diff --git a/OdfFile/Test/test_odf/CMakeLists.txt b/OdfFile/Test/test_odf/CMakeLists.txt index 1757ccfd7e..18e0de3e21 100644 --- a/OdfFile/Test/test_odf/CMakeLists.txt +++ b/OdfFile/Test/test_odf/CMakeLists.txt @@ -86,6 +86,7 @@ add_core_gtest( xlsx2ods_table_print_titles.cpp ods2xlsx_trailing_cell_style.cpp ods2xlsx_fit_to_page.cpp + ods2xlsx_tab_color.cpp ods2xlsx_page_margins.cpp ods2xlsx_hidden_header_footer.cpp ods2xlsx_font_size_column_width.cpp diff --git a/OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color.ods b/OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color.ods new file mode 100644 index 0000000000000000000000000000000000000000..840b8ade4c795c1f5605c0a84dec3428d5a206f8 GIT binary patch literal 982 zcmWIWW@Zs#fB?mq)*Q<&Js<~!Ie<7fGdH!QvLH3FpdcqRIk6-&KTp3bFGVjuu{g6> zFTWr)FC{;@G&eP`M6bA@C^a#qI3qQ+BminA$OOe0kCb~t%NQ9L%-ISvTv{RKEK3vXNapvlrS0Y}mENO1y~8x^w=~-c1^(rZ~AbTIrVt8%|#v29EqD!;y0-BUT^%Os-M;!Yyao+qxDzKCmrRoJI1KDDr^6Z$g5p9N_wYe zEq-Hubmd*K9HaLWAJk2I@c#S{u}^k~PZa)A-hCXuM+wFdB=1Q7Q_geO*Hw zbv^yu^m7yQGSgCv!4caVaF*+k0nfhonr`o+MfHQ;?D@&^i(%>6n1#NlXYtlnuU_Ei z@?lzkP5sHddd_|G3eV_GYAW5`Bz4l|^@}wRN^DG`Z2|CoW${cVq0|2RYE<@KSag7GS9#QX8#7nixchhNKJD|I#m<_PD0so@ z?g!I_OmiodyA^K~F~0fj;_BFInZ;6pw`~-RI{pj$s_Z*`+SGoNq`uC(*N?RJ-MbcQ z9^lQ$B*Kh4KLGs;1`R;ZBXS2q7xokn(Zj&dz!(B#BBg(Hjp$*E(5TFWBcuboS=m6! Nn1OH;kmh3s@c`Ora4G-* literal 0 HcmV?d00001 diff --git a/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp b/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp new file mode 100644 index 0000000000..4e403241b4 --- /dev/null +++ b/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: 2026 Euro-Office contributors +// SPDX-License-Identifier: AGPL-3.0-only + +// ODS -> XLSX conversion of the sheet tab colour: ODF 1.3 table:tab-color and the +// older tableooo:tab-color on style:table-properties become sheetPr/tabColor. + +#include "gtest/gtest.h" + +#include "common.h" + +#include + +namespace +{ + // The fixture has one sheet per case: + // 1 table:tab-color="#2b6cb0" + // 2 tableooo:tab-color="#ffff00" + // 3 table:tab-color="#00ff00" and tableooo:tab-color="#ff0000" + // 4 no tab colour + std::wstring sheet_xml(int sheet) + { + static const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/sheet_tab_color.ods"); + if (dir.empty()) + return std::wstring(); + return read_part(dir, L"xl/worksheets/sheet" + std::to_wstring(sheet) + L".xml"); + } + + // sheetPr/tabColor/@rgb in upper case, "(none)" if there is no tabColor. + std::wstring tab_color(const std::wstring& sheet) + { + const size_t end = sheet.find(L""); + const size_t pos = sheet.find(L" end) + return L"(none)"; + const std::wstring key = L" rgb=\""; + const size_t tag_end = sheet.find(L">", pos); + const size_t begin = sheet.find(key, pos); + if (begin == std::wstring::npos || begin > tag_end) + return L"(none)"; + const size_t value = begin + key.size(); + return sheet.substr(value, sheet.find(L"\"", value) - value); + } +} + +TEST(OdsToXlsx, TabColorOdf13) +{ + const std::wstring sheet = sheet_xml(1); + ASSERT_FALSE(sheet.empty()) << "ODS -> XLSX conversion failed"; + EXPECT_TRUE(is_well_formed(sheet)); + EXPECT_EQ(tab_color(sheet), L"2B6CB0"); +} + +TEST(OdsToXlsx, TabColorBeforeOdf13) +{ + const std::wstring sheet = sheet_xml(2); + ASSERT_FALSE(sheet.empty()) << "ODS -> XLSX conversion failed"; + EXPECT_EQ(tab_color(sheet), L"FFFF00"); +} + +// Both attributes: the ODF 1.3 one wins. +TEST(OdsToXlsx, TabColorOdf13WinsOverOld) +{ + const std::wstring sheet = sheet_xml(3); + ASSERT_FALSE(sheet.empty()) << "ODS -> XLSX conversion failed"; + EXPECT_EQ(tab_color(sheet), L"00FF00"); +} + +TEST(OdsToXlsx, NoTabColor) +{ + const std::wstring sheet = sheet_xml(4); + ASSERT_FALSE(sheet.empty()) << "ODS -> XLSX conversion failed"; + EXPECT_TRUE(is_well_formed(sheet)); + EXPECT_EQ(tab_color(sheet), L"(none)"); +} diff --git a/REUSE.toml b/REUSE.toml index d378433d43..f5741bc100 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -266,6 +266,11 @@ path = "OdfFile/Test/test_odf/ExampleFiles/sheet_fit_to_page.ods" SPDX-FileCopyrightText = "2026 Euro-Office contributors" SPDX-License-Identifier = "AGPL-3.0-only" +[[annotations]] +path = "OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color.ods" +SPDX-FileCopyrightText = "2026 Euro-Office contributors" +SPDX-License-Identifier = "AGPL-3.0-only" + [[annotations]] path = "OdfFile/Test/test_odf/ExampleFiles/sheet_page_margins.ods" SPDX-FileCopyrightText = "2026 Euro-Office contributors" From 1352a06be984da99be7054f4c15731900b8be3d4 Mon Sep 17 00:00:00 2001 From: Christoph Schaefer Date: Thu, 19 Nov 2026 21:53:27 +0100 Subject: [PATCH 2/2] fix(OdfFile): skip a sheet tab colour that is not a colour A table:tab-color value that is not a "#rrggbb" colour hid a valid tableooo:tab-color on the same table style, and the sheet got a black tab. Other invalid values gave a black tab as well, or an rgb attribute that is not a colour in XLSX ("12", "GG0000"): color::get_hex_value() falls back to black when it cannot read a value and passes short or non-hex values through. Take a tab colour only when its value is "#" and six hex digits, the ODF colour type, and try tableooo:tab-color when table:tab-color is missing or invalid. Without a valid value the sheet has no tab colour. LibreOffice also ignores a value that is not "#" and six characters. Tested with two new test_odf cases on a second ODS with a sheet per invalid value: an invalid table:tab-color next to a valid tableooo:tab-color, and four invalid values on their own. Both fail without the fix. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Christoph Schaefer --- OdfFile/Reader/Converter/xlsx_table_state.cpp | 37 +++++++++++++++--- .../sheet_tab_color_malformed.ods | Bin 0 -> 1029 bytes OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp | 34 ++++++++++++++++ REUSE.toml | 5 +++ 4 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color_malformed.ods diff --git a/OdfFile/Reader/Converter/xlsx_table_state.cpp b/OdfFile/Reader/Converter/xlsx_table_state.cpp index f7c0690156..3fffd4d3d0 100644 --- a/OdfFile/Reader/Converter/xlsx_table_state.cpp +++ b/OdfFile/Reader/Converter/xlsx_table_state.cpp @@ -41,6 +41,27 @@ namespace cpdoccore { namespace oox { +namespace +{ + // sheetPr/tabColor/@rgb for an ODF tab colour, or empty when the value is not "#rrggbb" + // (the ODF colour type), so the other tab colour attribute still applies. + std::wstring tab_color_rgb(const _CP_OPT(odf_types::color) & color) + { + if (!color) + return std::wstring(); + const std::wstring& value = color->get_color(); + if (value.size() != 7 || value[0] != L'#') + return std::wstring(); + for (size_t i = 1; i < value.size(); i++) + { + const wchar_t c = value[i]; + if (!((c >= L'0' && c <= L'9') || (c >= L'a' && c <= L'f') || (c >= L'A' && c <= L'F'))) + return std::wstring(); + } + return color->get_hex_value(); + } +} + void xlsx_data_range::serialize_autofilter(std::wostream& _Wostream) { if (!filter_button && filter_conditions.empty()) return; @@ -722,16 +743,20 @@ void xlsx_table_state::serialize_table_format (std::wostream & strm) CP_XML_NODE(L"sheetPr") { //at filterMode="false"> - // ODF 1.3 table:tab-color wins over the older tableooo:tab-color. - _CP_OPT(odf_types::color) tab_color; + // ODF 1.3 table:tab-color wins over the older tableooo:tab-color; a value + // that is not a colour is skipped. + std::wstring tab_color; if (table_prop) - tab_color = table_prop->content().table_tab_color_ ? table_prop->content().table_tab_color_ - : table_prop->content().tableooo_tab_color_; - if (tab_color) + { + tab_color = tab_color_rgb(table_prop->content().table_tab_color_); + if (tab_color.empty()) + tab_color = tab_color_rgb(table_prop->content().tableooo_tab_color_); + } + if (!tab_color.empty()) { CP_XML_NODE(L"tabColor") { - CP_XML_ATTR(L"rgb", tab_color->get_hex_value()); + CP_XML_ATTR(L"rgb", tab_color); } } if (fit_to_pages) diff --git a/OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color_malformed.ods b/OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color_malformed.ods new file mode 100644 index 0000000000000000000000000000000000000000..764016802ad5014f4d4e53bface9de8c86c53a0f GIT binary patch literal 1029 zcmWIWW@Zs#fB?mq)*Q<&Js<~!Ie<7fGdH!QvLH3FpdcqRIk6-&KTp3bFGVjuu{g6> zFTWr)FC{;@G&eP`M6bA@C^a#qI3qQ+BminA$OOe0J`q8wtBec`D}h{Yph$9lUI|D; zMQ+a4X|egU4Mf`B|JI&Rec5xVpNIBZv4BkR==Hn0*V-Robk>oSPnm7{^ZRG^lM3F; zT$(kM3TooceU_`&czJr8;6?pqChRj8K0VCj>LV6V6q)|w@AKRvF^zkV*dJZHN#j((vc(RuMR&PgZ3<1#bh;4t z)`u%VGOD}pwXc1GF7NTY)me%59M3i73jT5%&14O_`g-@%+M@lxN_bCa8BJTf&tt-@ zo9|5x`Zt?AE1Ma^{LzHpC3b$C+K)F{@7b>fyyN5F71}ec>hO${mz@P)E8bz}kI(pd z%x)j!l>4qb(?90-{(m!hqD|NbLp~9oj>FrQEK1&Zb8FbD-c8Hr2k$<-=&bwJpgZot zHGkGEp0M7$XHNNNw)I~&{Orq73BJkUn)YzJtd%wIYR=_}>;C<}(O|Uvqx|wi-%Zn^ zY(GuS^?z*dyS*xA;{Qj-mz!D0$uj;p^S|fMGjr6~k7hL9u>%-=`{os%(VNs%y1Pm0q|56UYaW!?m_*+wzp9r}WScha{6ameQ~E0sq&(f` z#!g$poOaUfSWC*{nM^{b{rA(T=Q)d=H7QZ>g4Nv* zrVE+oPAYdR-Y8;x^V`MMvDY$-r2=o;C>VA87xq=zclxxc{U%9$op-MvY3;jrEfnZg zMkWzv+}Q`{UodC@dLEIP5W28u5QrWIh6ctEAQLIOpld`ATZG0SCLAFh;LXYgQpOB~ Ln}D<}Gl&NO-R6w@ literal 0 HcmV?d00001 diff --git a/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp b/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp index 4e403241b4..1656cf7347 100644 --- a/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp +++ b/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp @@ -25,6 +25,20 @@ namespace return read_part(dir, L"xl/worksheets/sheet" + std::to_wstring(sheet) + L".xml"); } + // Values that are not "#rrggbb", one sheet per case: + // 1 table:tab-color="bluish" and tableooo:tab-color="#ff0000" + // 2 table:tab-color="rgb(1,2,3)" + // 3 tableooo:tab-color="nonsense" + // 4 table:tab-color="#12" + // 5 table:tab-color="#gg0000" + std::wstring malformed_sheet_xml(int sheet) + { + static const std::wstring dir = convert_odf_to_ooxml_dir(L"ExampleFiles/sheet_tab_color_malformed.ods"); + if (dir.empty()) + return std::wstring(); + return read_part(dir, L"xl/worksheets/sheet" + std::to_wstring(sheet) + L".xml"); + } + // sheetPr/tabColor/@rgb in upper case, "(none)" if there is no tabColor. std::wstring tab_color(const std::wstring& sheet) { @@ -72,3 +86,23 @@ TEST(OdsToXlsx, NoTabColor) EXPECT_TRUE(is_well_formed(sheet)); EXPECT_EQ(tab_color(sheet), L"(none)"); } + +// An invalid table:tab-color does not hide a valid tableooo:tab-color. +TEST(OdsToXlsx, InvalidTabColorFallsBackToOld) +{ + const std::wstring sheet = malformed_sheet_xml(1); + ASSERT_FALSE(sheet.empty()) << "ODS -> XLSX conversion failed"; + EXPECT_EQ(tab_color(sheet), L"FF0000"); +} + +// A value that is not a colour gives no tab colour, not a black tab and not an invalid rgb. +TEST(OdsToXlsx, InvalidTabColorIsSkipped) +{ + for (int i = 2; i <= 5; i++) + { + const std::wstring sheet = malformed_sheet_xml(i); + ASSERT_FALSE(sheet.empty()) << "ODS -> XLSX conversion failed"; + EXPECT_TRUE(is_well_formed(sheet)) << "sheet " << i; + EXPECT_EQ(tab_color(sheet), L"(none)") << "sheet " << i; + } +} diff --git a/REUSE.toml b/REUSE.toml index f5741bc100..a12af4a084 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -271,6 +271,11 @@ path = "OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color.ods" SPDX-FileCopyrightText = "2026 Euro-Office contributors" SPDX-License-Identifier = "AGPL-3.0-only" +[[annotations]] +path = "OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color_malformed.ods" +SPDX-FileCopyrightText = "2026 Euro-Office contributors" +SPDX-License-Identifier = "AGPL-3.0-only" + [[annotations]] path = "OdfFile/Test/test_odf/ExampleFiles/sheet_page_margins.ods" SPDX-FileCopyrightText = "2026 Euro-Office contributors"