diff --git a/include/xlnt/workbook/workbook.hpp b/include/xlnt/workbook/workbook.hpp
index 3afb95f8..f7aa4e3c 100644
--- a/include/xlnt/workbook/workbook.hpp
+++ b/include/xlnt/workbook/workbook.hpp
@@ -203,6 +203,12 @@ public:
///
worksheet active_sheet();
+ ///
+ /// Sets the worksheet that is determined to be active. An active
+ /// sheet is that which is initially shown by the spreadsheet editor.
+ ///
+ void active_sheet(std::size_t index);
+
///
/// Returns the worksheet with the given name. This may throw an exception
/// if the sheet isn't found. Use workbook::contains(const std::string &)
diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp
index e7dce8d1..dc50106c 100644
--- a/source/detail/serialization/xlsx_consumer.cpp
+++ b/source/detail/serialization/xlsx_consumer.cpp
@@ -2037,6 +2037,7 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_
if (parser().attribute_present("activeTab"))
{
view.active_tab = parser().attribute("activeTab");
+ target_.d_->active_sheet_index_.set(view.active_tab.get());
}
target_.view(view);
diff --git a/source/workbook/workbook.cpp b/source/workbook/workbook.cpp
index 5d49ddff..dce9d3bd 100644
--- a/source/workbook/workbook.cpp
+++ b/source/workbook/workbook.cpp
@@ -743,6 +743,11 @@ worksheet workbook::active_sheet()
{
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
{
diff --git a/tests/data/20_active_sheet.xlsx b/tests/data/20_active_sheet.xlsx
new file mode 100644
index 00000000..d441ddcc
Binary files /dev/null and b/tests/data/20_active_sheet.xlsx differ
diff --git a/tests/workbook/serialization_test_suite.cpp b/tests/workbook/serialization_test_suite.cpp
index 10dbb4fd..ca6383a1 100644
--- a/tests/workbook/serialization_test_suite.cpp
+++ b/tests/workbook/serialization_test_suite.cpp
@@ -72,6 +72,7 @@ public:
register_test(test_Issue492_stream_empty_row);
register_test(test_Issue503_external_link_load);
register_test(test_formatting);
+ register_test(test_active_sheet);
}
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()[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;
diff --git a/tests/workbook/workbook_test_suite.cpp b/tests/workbook/workbook_test_suite.cpp
index 59eb3a69..09e37660 100644
--- a/tests/workbook/workbook_test_suite.cpp
+++ b/tests/workbook/workbook_test_suite.cpp
@@ -77,6 +77,11 @@ public:
{
xlnt::workbook wb;
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()