void test_read_autofilter() { auto path = PathHelper::GetDataDirectory("/reader/bug275.xlsx"); auto wb = xlnt::load_workbook(path); auto ws = wb.get_active_sheet(); TS_ASSERT_EQUALS(ws.get_auto_filter().to_string(), "A1:B6"); }
void test_write_auto_filter() { xlnt::workbook wb; auto ws = wb.create_sheet(); ws.get_cell("F42").set_value("hello"); ws.get_auto_filter() = "A1:F1"; auto content = xlnt::write_workbook(wb); auto diff = Helper::compare_xml(PathHelper::read_file("workbook_auto_filter.xml"), content); TS_ASSERT(!diff); }
xml_document workbook_serializer::write_workbook() const { std::size_t num_visible = 0; for (auto ws : workbook_) { if (ws.get_page_setup().get_sheet_state() == sheet_state::visible) { num_visible++; } } if (num_visible == 0) { throw xlnt::value_error(); } xml_document xml; auto root_node = xml.add_child("workbook"); xml.add_namespace("", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"); xml.add_namespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); auto file_version_node = root_node.add_child("fileVersion"); file_version_node.add_attribute("appName", "xl"); file_version_node.add_attribute("lastEdited", "4"); file_version_node.add_attribute("lowestEdited", "4"); file_version_node.add_attribute("rupBuild", "4505"); auto workbook_pr_node = root_node.add_child("workbookPr"); workbook_pr_node.add_attribute("codeName", "ThisWorkbook"); workbook_pr_node.add_attribute("defaultThemeVersion", "124226"); workbook_pr_node.add_attribute("date1904", workbook_.get_properties().excel_base_date == calendar::mac_1904 ? "1" : "0"); auto book_views_node = root_node.add_child("bookViews"); auto workbook_view_node = book_views_node.add_child("workbookView"); workbook_view_node.add_attribute("activeTab", "0"); workbook_view_node.add_attribute("autoFilterDateGrouping", "1"); workbook_view_node.add_attribute("firstSheet", "0"); workbook_view_node.add_attribute("minimized", "0"); workbook_view_node.add_attribute("showHorizontalScroll", "1"); workbook_view_node.add_attribute("showSheetTabs", "1"); workbook_view_node.add_attribute("showVerticalScroll", "1"); workbook_view_node.add_attribute("tabRatio", "600"); workbook_view_node.add_attribute("visibility", "visible"); auto sheets_node = root_node.add_child("sheets"); auto defined_names_node = root_node.add_child("definedNames"); for (const auto &relationship : workbook_.get_relationships()) { if (relationship.get_type() == relationship::type::worksheet) { // TODO: this is ugly std::string sheet_index_string = relationship.get_target_uri(); sheet_index_string = sheet_index_string.substr(0, sheet_index_string.find('.')); sheet_index_string = sheet_index_string.substr(sheet_index_string.find_last_of('/')); auto iter = sheet_index_string.end(); iter--; while (isdigit(*iter)) iter--; auto first_digit = iter - sheet_index_string.begin(); sheet_index_string = sheet_index_string.substr(static_cast<std::string::size_type>(first_digit + 1)); std::size_t sheet_index = static_cast<std::size_t>(std::stoll(sheet_index_string) - 1); auto ws = workbook_.get_sheet_by_index(sheet_index); auto sheet_node = sheets_node.add_child("sheet"); sheet_node.add_attribute("name", ws.get_title()); sheet_node.add_attribute("sheetId", std::to_string(sheet_index + 1)); sheet_node.add_attribute("r:id", relationship.get_id()); if (ws.has_auto_filter()) { auto defined_name_node = defined_names_node.add_child("definedName"); defined_name_node.add_attribute("name", "_xlnm._FilterDatabase"); defined_name_node.add_attribute("hidden", "1"); defined_name_node.add_attribute("localSheetId", "0"); std::string name = "'" + ws.get_title() + "'!" + range_reference::make_absolute(ws.get_auto_filter()).to_string(); defined_name_node.set_text(name); } } } auto calc_pr_node = root_node.add_child("calcPr"); calc_pr_node.add_attribute("calcId", "124519"); calc_pr_node.add_attribute("calcMode", "auto"); calc_pr_node.add_attribute("fullCalcOnLoad", "1"); return xml; }