diff --git a/OdfFile/Reader/Converter/xlsx_table_state.cpp b/OdfFile/Reader/Converter/xlsx_table_state.cpp index ae183e6a77..09118a7ebe 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; @@ -743,11 +764,20 @@ 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; a value + // that is not a colour is skipped. + std::wstring tab_color; + if (table_prop) + { + 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", table_prop->content().tableooo_tab_color_->get_hex_value()); + CP_XML_ATTR(L"rgb", tab_color); } } 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 8fe1848057..c40cafb7e5 100644 --- a/OdfFile/Test/test_odf/CMakeLists.txt +++ b/OdfFile/Test/test_odf/CMakeLists.txt @@ -113,6 +113,7 @@ add_core_gtest( ods2xlsx_trailing_cell_style.cpp ods2xlsx_header_footer_ampersand.cpp ods2xlsx_fit_to_page.cpp + ods2xlsx_tab_color.cpp ods2xlsx_page_margins.cpp odt2docx_heading_spaces.cpp odf2ooxml_white_space.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 0000000000..840b8ade4c Binary files /dev/null and b/OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color.ods differ 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 0000000000..764016802a Binary files /dev/null and b/OdfFile/Test/test_odf/ExampleFiles/sheet_tab_color_malformed.ods differ 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..1656cf7347 --- /dev/null +++ b/OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp @@ -0,0 +1,108 @@ +// 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"); + } + + // 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) + { + 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)"); +} + +// 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 37f3e4e6de..0929bb1815 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -454,6 +454,16 @@ path = [ 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_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"