Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions OdfFile/Reader/Converter/xlsx_table_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions OdfFile/Reader/Format/style_table_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}
Expand Down
3 changes: 2 additions & 1 deletion OdfFile/Reader/Format/style_table_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
1 change: 1 addition & 0 deletions OdfFile/Test/test_odf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file not shown.
Binary file not shown.
108 changes: 108 additions & 0 deletions OdfFile/Test/test_odf/ods2xlsx_tab_color.cpp
Original file line number Diff line number Diff line change
@@ -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 <string>

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"</sheetPr>");
const size_t pos = sheet.find(L"<tabColor ");
if (end == std::wstring::npos || pos == std::wstring::npos || pos > 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;
}
}
10 changes: 10 additions & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading