Add the function of getting table hidden attributes.

This commit is contained in:
胡剑波 2020-08-25 13:32:21 +08:00
parent 3c7122a78c
commit dafdfa3ebb
6 changed files with 38 additions and 8 deletions

View File

@ -241,6 +241,13 @@ public:
/// </summary> /// </summary>
const worksheet sheet_by_id(std::size_t id) const; const worksheet sheet_by_id(std::size_t id) const;
/// <summary>
/// Returns the hidden identifier of the worksheet at the given index.
/// This will throw an exception if index is greater than or equal to the
/// number of sheets in this workbook.
/// </summary>
bool sheet_hidden_by_index(std::size_t index) const;
/// <summary> /// <summary>
/// Returns true if this workbook contains a sheet with the given title. /// Returns true if this workbook contains a sheet with the given title.
/// </summary> /// </summary>

View File

@ -79,6 +79,7 @@ struct workbook_impl
manifest_ = other.manifest_; manifest_ = other.manifest_;
sheet_title_rel_id_map_ = other.sheet_title_rel_id_map_; sheet_title_rel_id_map_ = other.sheet_title_rel_id_map_;
sheet_hidden_ = other.sheet_hidden_;
view_ = other.view_; view_ = other.view_;
code_name_ = other.code_name_; code_name_ = other.code_name_;
file_version_ = other.file_version_; file_version_ = other.file_version_;
@ -105,6 +106,7 @@ struct workbook_impl
&& extended_properties_ == other.extended_properties_ && extended_properties_ == other.extended_properties_
&& custom_properties_ == other.custom_properties_ && custom_properties_ == other.custom_properties_
&& sheet_title_rel_id_map_ == other.sheet_title_rel_id_map_ && sheet_title_rel_id_map_ == other.sheet_title_rel_id_map_
&& sheet_hidden_ == other.sheet_hidden_
&& view_ == other.view_ && view_ == other.view_
&& code_name_ == other.code_name_ && code_name_ == other.code_name_
&& file_version_ == other.file_version_ && file_version_ == other.file_version_
@ -134,16 +136,17 @@ struct workbook_impl
std::vector<std::pair<std::string, variant>> custom_properties_; std::vector<std::pair<std::string, variant>> custom_properties_;
std::unordered_map<std::string, std::string> sheet_title_rel_id_map_; std::unordered_map<std::string, std::string> sheet_title_rel_id_map_;
std::vector<bool> sheet_hidden_;
optional<workbook_view> view_; optional<workbook_view> view_;
optional<std::string> code_name_; optional<std::string> code_name_;
struct file_version_t struct file_version_t
{ {
std::string app_name; std::string app_name;
std::size_t last_edited; std::size_t last_edited;
std::size_t lowest_edited; std::size_t lowest_edited;
std::size_t rup_build; std::size_t rup_build;
bool operator==(const file_version_t& rhs) const bool operator==(const file_version_t& rhs) const
{ {
@ -152,7 +155,7 @@ struct workbook_impl
&& lowest_edited == rhs.lowest_edited && lowest_edited == rhs.lowest_edited
&& rup_build == rhs.rup_build; && rup_build == rhs.rup_build;
} }
}; };
optional<file_version_t> file_version_; optional<file_version_t> file_version_;
optional<calculation_properties> calculation_properties_; optional<calculation_properties> calculation_properties_;

View File

@ -1883,12 +1883,14 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_
expect_start_element(qn("spreadsheetml", "sheet"), xml::content::simple); expect_start_element(qn("spreadsheetml", "sheet"), xml::content::simple);
auto title = parser().attribute("name"); auto title = parser().attribute("name");
skip_attribute("state");
sheet_title_index_map_[title] = index++; sheet_title_index_map_[title] = index++;
sheet_title_id_map_[title] = parser().attribute<std::size_t>("sheetId"); sheet_title_id_map_[title] = parser().attribute<std::size_t>("sheetId");
target_.d_->sheet_title_rel_id_map_[title] = parser().attribute(qn("r", "id")); target_.d_->sheet_title_rel_id_map_[title] = parser().attribute(qn("r", "id"));
bool hidden = parser().attribute<std::string>("state", "") == "hidden";
target_.d_->sheet_hidden_.push_back(hidden);
expect_end_element(qn("spreadsheetml", "sheet")); expect_end_element(qn("spreadsheetml", "sheet"));
} }
} }

View File

@ -716,6 +716,16 @@ const worksheet workbook::sheet_by_id(std::size_t id) const
throw key_not_found(); throw key_not_found();
} }
bool workbook::sheet_hidden_by_index(std::size_t index) const
{
if (index >= d_->sheet_hidden_.size())
{
throw invalid_parameter();
}
return d_->sheet_hidden_.at(index);
}
worksheet workbook::active_sheet() 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);

Binary file not shown.

View File

@ -112,6 +112,7 @@ public:
register_test(test_delete_columns); register_test(test_delete_columns);
register_test(test_insert_too_many); register_test(test_insert_too_many);
register_test(test_insert_delete_moves_merges); register_test(test_insert_delete_moves_merges);
register_test(test_hidden_sheet);
} }
void test_new_worksheet() void test_new_worksheet()
@ -1582,5 +1583,12 @@ public:
xlnt_assert_equals(merged, expected); xlnt_assert_equals(merged, expected);
} }
} }
void test_hidden_sheet()
{
xlnt::workbook wb;
wb.load(path_helper::test_file("16_hidden_sheet.xlsx"));
xlnt_assert_equals(wb.sheet_hidden_by_index(1), true);
}
}; };
static worksheet_test_suite x; static worksheet_test_suite x;