Added the ability to set the active sheet

Closes #653
This commit is contained in:
snippet 2022-08-29 18:30:48 +10:00
parent fbae8cd6c1
commit c21014c0fd
6 changed files with 25 additions and 0 deletions

View File

@ -203,6 +203,12 @@ public:
/// </summary> /// </summary>
worksheet active_sheet(); worksheet active_sheet();
/// <summary>
/// Sets the worksheet that is determined to be active. An active
/// sheet is that which is initially shown by the spreadsheet editor.
/// </summary>
void active_sheet(std::size_t index);
/// <summary> /// <summary>
/// Returns the worksheet with the given name. This may throw an exception /// Returns the worksheet with the given name. This may throw an exception
/// if the sheet isn't found. Use workbook::contains(const std::string &) /// if the sheet isn't found. Use workbook::contains(const std::string &)

View File

@ -2037,6 +2037,7 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_
if (parser().attribute_present("activeTab")) if (parser().attribute_present("activeTab"))
{ {
view.active_tab = parser().attribute<std::size_t>("activeTab"); view.active_tab = parser().attribute<std::size_t>("activeTab");
target_.d_->active_sheet_index_.set(view.active_tab.get());
} }
target_.view(view); target_.view(view);

View File

@ -743,6 +743,11 @@ worksheet workbook::active_sheet()
{ {
return sheet_by_index(d_->active_sheet_index_.is_set() ? d_->active_sheet_index_.get() : 0); return sheet_by_index(d_->active_sheet_index_.is_set() ? d_->active_sheet_index_.get() : 0);
} }
void workbook::active_sheet(std::size_t index)
{
d_->active_sheet_index_.set(index);
d_->view_.get().active_tab = index;
}
bool workbook::has_named_range(const std::string &name) const bool workbook::has_named_range(const std::string &name) const
{ {

Binary file not shown.

View File

@ -72,6 +72,7 @@ public:
register_test(test_Issue492_stream_empty_row); register_test(test_Issue492_stream_empty_row);
register_test(test_Issue503_external_link_load); register_test(test_Issue503_external_link_load);
register_test(test_formatting); register_test(test_formatting);
register_test(test_active_sheet);
} }
bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file) bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file)
@ -800,6 +801,13 @@ public:
assert_run(rt.runs()[10], " first line ", "Calibri", xlnt::theme_color(1), 12, true, false, xlnt::font::underline_style::none); assert_run(rt.runs()[10], " first line ", "Calibri", xlnt::theme_color(1), 12, true, false, xlnt::font::underline_style::none);
assert_run(rt.runs()[11], "Bold And Underline", "Calibri (Body)", xlnt::theme_color(1), 12, true, false, xlnt::font::underline_style::single); assert_run(rt.runs()[11], "Bold And Underline", "Calibri (Body)", xlnt::theme_color(1), 12, true, false, xlnt::font::underline_style::single);
} }
void test_active_sheet()
{
xlnt::workbook wb;
wb.load(path_helper::test_file("20_active_sheet.xlsx"));
xlnt_assert_equals(wb.active_sheet(), wb[2]);
}
}; };
static serialization_test_suite x; static serialization_test_suite x;

View File

@ -77,6 +77,11 @@ public:
{ {
xlnt::workbook wb; xlnt::workbook wb;
xlnt_assert_equals(wb.active_sheet(), wb[0]); xlnt_assert_equals(wb.active_sheet(), wb[0]);
wb.create_sheet();
wb.create_sheet();
wb.active_sheet(2);
xlnt_assert_equals(wb.active_sheet(), wb[2]);
} }
void test_create_sheet() void test_create_sheet()