Merge branch 'master' of github.com:tfussell/xlnt

This commit is contained in:
Thomas Fussell 2014-07-19 14:43:56 -04:00
commit c7dbf12b51
2 changed files with 409 additions and 230 deletions

View File

@ -28,155 +28,296 @@ public:
} }
} }
xlnt::workbook standard_workbook()
{
auto path = PathHelper::GetDataDirectory("genuine/emtpy.xlsx");
return xlnt::load_workbook(path);
}
void test_read_standard_workbook() void test_read_standard_workbook()
{ {
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx"; TS_ASSERT_DIFFERS(standard_workbook(), nullptr);
xlnt::workbook wb;
wb.load(path);
} }
void test_read_standard_workbook_from_fileobj() void test_read_standard_workbook_from_fileobj()
{ {
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx"; auto path = PathHelper::GetDataDirectory("genuine/emtpy.xlsx");
std::ifstream fo(path, std::ios::binary); std::ifstream fo(path, std::ios::binary);
xlnt::workbook wb; auto wb = xlnt::load_workbook(path);
wb.load(fo); TS_ASSERT_DIFFERS(standard_workbook(), nullptr);
} }
void test_read_worksheet() void test_read_worksheet()
{ {
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx"; auto wb = standard_workbook()
xlnt::workbook wb;
wb.load(path);
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers"); auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
TS_ASSERT_DIFFERS(sheet2, nullptr); TS_ASSERT_DIFFERS(sheet2, nullptr);
TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5")); TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5"));
TS_ASSERT_EQUALS(18, sheet2.get_cell("D18")); TS_ASSERT_EQUALS(18, sheet2.get_cell("D18"));
TS_ASSERT_EQUALS(true, sheet2.get_cell("G9"));
TS_ASSERT_EQUALS(false, sheet2.get_cell("G10"));
} }
void test_read_nostring_workbook() void test_read_nostring_workbook()
{ {
auto genuine_wb = PathHelper::GetDataDirectory() + "/genuine/empty-no-string.xlsx"; auto path = PathHelper::GetDataDirectory("/genuine/empty-no-string.xlsx");
xlnt::workbook wb; auto wb = xlnt::load_workbook(path);
wb.load(genuine_wb); TS_ASSERT_DIFFERS(standard_workbook(), nullptr);
} }
void test_read_empty_file() void test_read_empty_file()
{ {
auto null_file = PathHelper::GetDataDirectory() + "/reader/null_file.xlsx"; auto path = PathHelper::GetDataDirectory("/reader/null_file.xlsx";
xlnt::workbook wb; TS_ASSERT_THROWS(xlnt::load_workbook(path), xlnt::invalid_file_exception);
TS_ASSERT_THROWS_ANYTHING(wb.load(null_file));
} }
//@raises(InvalidFileException)
void test_read_empty_archive() void test_read_empty_archive()
{ {
auto null_file = PathHelper::GetDataDirectory() + "/reader/null_archive.xlsx"; auto path = PathHelper::GetDataDirectory("/reader/null_archive.xlsx");
xlnt::workbook wb; TS_ASSERT_THROWS(xlnt::load_workbook(path), xlnt::invalid_file_exception);
TS_ASSERT_THROWS_ANYTHING(wb.load(null_file));
}
void test_read_dimension()
{
auto path = PathHelper::GetDataDirectory() + "/reader/sheet2.xml";
std::ifstream file(path);
std::stringstream ss;
ss << file.rdbuf();
auto dimension = xlnt::reader::read_dimension(ss.str());
TS_ASSERT_EQUALS("D1:AA30", dimension);
}
void test_calculate_dimension_iter()
{
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx";
xlnt::workbook wb;
wb.load(path);
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
auto dimensions = sheet2.calculate_dimension();
TS_ASSERT_EQUALS("D1:AA30", dimensions.to_string());
}
void test_get_highest_row_iter()
{
auto path = PathHelper::GetDataDirectory() + "/genuine/empty.xlsx";
xlnt::workbook wb;
wb.load(path);
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
auto max_row = sheet2.get_highest_row();
TS_ASSERT_EQUALS(30, max_row);
} }
void test_read_workbook_with_no_properties() void test_read_workbook_with_no_properties()
{ {
auto genuine_wb = PathHelper::GetDataDirectory() + "/genuine/empty_with_no_properties.xlsx"; auto path = PathHelper::GetDataDirectory("/reader/null_archive.xlsx");
xlnt::workbook wb; xlnt::load_workbook(path);
wb.load(genuine_wb);
} }
void test_read_general_style() void workbook_with_styles()
{ {
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A1").get_style().get_number_format().get_format_code(), xlnt::number_format::format::general); auto path = PathHelper::GetDataDirectory("/reader/empty-with-styles.xlsx");
return xlnt::load_workbook(path);
} }
void test_read_date_style() void test_read_workbook_with_styles_general()
{ {
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A2").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_xlsx14); auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
TS_ASSERT(ws.get_cell("A1").get_style().get_number_format().get_format_code() == xlnt::number_format::known_formats::general);
} }
void test_read_number_style() void test_read_workbook_with_styles_date()
{ {
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A3").get_style().get_number_format().get_format_code(), xlnt::number_format::format::number00); auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
TS_ASSERT(ws.get_cell("A2").get_style().get_number_format().get_format_code() == xlnt::number_format::known_formats::date_xlsx14);
} }
void test_read_time_style() void test_read_workbook_with_styles_number()
{ {
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A4").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_time3); auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
TS_ASSERT(ws.get_cell("A3").get_style().get_number_format().get_format_code() == xlnt::number_format::known_formats::number00);
} }
void test_read_percentage_style() void test_read_workbook_with_styles_time()
{ {
//TS_ASSERT_EQUALS(worksheet_with_styles.get_cell("A5").get_style().get_number_format().get_format_code(), xlnt::number_format::format::percentage00) auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
TS_ASSERT(ws.get_cell("A4").get_style().get_number_format().get_format_code() == xlnt::number_format::known_formats::date_time_3);
}
void test_read_workbook_with_styles_percentage()
{
auto wb = workbook_with_styles();
auto ws = wb["Sheet1"];
TS_ASSERT(ws.get_cell("A5").get_style().get_number_format().get_format_code() == xlnt::number_format::known_formats::percentage00);
}
void date_mac_1904()
{
auto path = PathHelper::GetDataDirectory("/reader/date_1904.xlsx");
return xlnt::load_workbook(path);
}
void date_std_1900()
{
auto path = PathHelper::GetDataDirectory("/reader/date_1900.xlsx");
return xlnt::load_workbook(path);
} }
void test_read_win_base_date() void test_read_win_base_date()
{ {
//TS_ASSERT_EQUALS(win_wb.get_properties().get_excel_base_date(), CALENDAR_WINDOWS_1900); auto wb = date_std_1900();
TS_ASSERT_EQUALS(wb.get_properties().get_excel_base_date(), xlnt::calendar::windows_1900);
} }
void test_read_mac_base_date() void test_read_mac_base_date()
{ {
//TS_ASSERT_EQUALS(mac_wb.get_properties().get_excel_base_date(), CALENDAR_MAC_1904); auto wb = date_mac_1904();
} TS_ASSERT_EQUALS(wb.get_properties().get_excel_base_date(), xlnt::calendar::mac_1904);
void test_read_date_style_mac()
{
//TS_ASSERT_EQUALS(mac_ws.get_cell("A1").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_xlsx14);
} }
void test_read_date_style_win() void test_read_date_style_win()
{ {
//TS_ASSERT_EQUALS(win_ws.get_cell("A1").get_style().get_number_format().get_format_code(), xlnt::number_format::format::date_xlsx14); auto wb = date_std_1900();
auto ws = wb["Sheet1"];
TS_ASSERT_EQUALS(ws.get_cell("A1").get_style().get_number_format().get_format_code() == xlnt::number_format::known_formats::date_xlsx14);
} }
void test_read_date_value() void test_read_date_style_mac()
{ {
//auto path = PathHelper::GetDataDirectory() + "/genuine/empty-with-styles.xlsx"; auto wb = date_mac_1904();
//wb_with_styles.load(path); auto ws = wb["Sheet1"];
//worksheet_with_styles = wb_with_styles.get_sheet_by_name("Sheet1"); TS_ASSERT_EQUALS(ws.get_cell("A1").get_style().get_number_format().get_format_code() == xlnt::number_format::known_formats::date_xlsx14);
}
auto mac_wb_path = PathHelper::GetDataDirectory() + "/reader/date_1904.xlsx";
xlnt::workbook mac_wb;
mac_wb.load(mac_wb_path);
auto mac_ws = mac_wb.get_sheet_by_name("Sheet1");
auto win_wb_path = PathHelper::GetDataDirectory() + "/reader/date_1900.xlsx";
xlnt::workbook win_wb;
win_wb.load(win_wb_path);
auto win_ws = win_wb.get_sheet_by_name("Sheet1");
void test_read_win_base_date()
{
auto wb_mac = date_mac_1904();
auto ws_mac = wb_mac["Sheet1"];
auto wb_win = date_win_1900();
auto ws_win = wb_win["Sheet1"];
xlnt::datetime dt(2011, 10, 31); xlnt::datetime dt(2011, 10, 31);
TS_ASSERT_EQUALS(mac_ws.get_cell("A1"), dt); TS_ASSERT_EQUALS(ws_mac.get_cell("A1"), dt);
TS_ASSERT_EQUALS(win_ws.get_cell("A1"), dt); TS_ASSERT_EQUALS(ws_win.get_cell("A1"), dt);
TS_ASSERT_EQUALS(mac_ws.get_cell("A1"), win_ws.get_cell("A1")); TS_ASSERT_EQUALS(ws_mac.get_cell("A1"), ws_win.get_cell("A1"));
}
void test_repair_central_directory()
{
TS_ASSERT(false);
/*
std::string data_a = "foobarbaz" + xlnt::CentralDirectorySignature;
std::string data_b = "bazbarfoo12345678901234567890";
auto f = xlnt::repair_central_directory(data_a + data_b, true);
TS_ASSERT_EQUALS(f, data_a + data_b.substr(0, 18));
f = xlnt::repair_central_directory(data_b, true);
TS_ASSERT_EQUALS(f, data_b);
*/
}
void test_read_no_theme()
{
auto path = PathHelper::GetDataDirectory("/genuine/libreoffice_nrt.xlsx");
auto wb = xlnt::load_workbook(path);
TS_ASSERT_DIFFERS(wb, nullptr);
}
void test_read_cell_formulae()
{
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
auto path = PathHelper::GetDataDirectory("/reader/worksheet_formula.xml");
std::ifstream ws_stream(path);
xlnt::fast_parse(ws, ws_stream, {"", ""}, {}, 0);
auto b1 = ws.get_cell("B1");
TS_ASSERT_EQUALS(b1.get_data_type(), xlnt::cell::type::formula);
TS_ASSERT_EQUALS(b1, "=CONCATENATE(A1, A2)");
auto a6 = ws.get_cell("A6");
TS_ASSERT_EQUALS(a6.get_data_type(), xlnt::cell::type::formula);
TS_ASSERT_EQUALS(a6, "=SUM(A4:A5)");
}
void test_read_complex_formulae()
{
TS_ASSERT(false);
}
void test_data_only()
{
TS_ASSERT(false);
}
void test_detect_worksheets()
{
TS_ASSERT(false);
}
void test_read_rels()
{
TS_ASSERT(false);
}
void test_read_content_types()
{
std::vector<std::pair<std::string, std::string>> expected =
{
{"/xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"},
{"/xl/worksheets/sheet1.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"},
{"/xl/chartsheets/sheet1.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml"},
{"/xl/worksheets/sheet2.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"},
{"/xl/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml"},
{"/xl/styles.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"},
{"/xl/sharedStrings.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"},
{"/xl/drawings/drawing1.xml", "application/vnd.openxmlformats-officedocument.drawing+xml"},
{"/xl/charts/chart1.xml", "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"},
{"/xl/drawings/drawing2.xml", "application/vnd.openxmlformats-officedocument.drawing+xml"},
{"/xl/charts/chart2.xml", "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"},
{"/xl/calcChain.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml"},
{"/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml"},
{"/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml"}
};
auto path = PathHelper::GetDataDirectory("/reader/bug137.xlsx");
xlnt::zip_file f(path);
auto result = xlnt::workbook_reader::read_conent_types(f);
TS_ASSERT_EQUALS(result, expected);
}
void test_read_sheets()
{
{
auto path = PathHelper::GetDataDirectory("/reader/bug137.xlsx");
xlnt::zip_file f(path);
auto sheets = xlnt::workbook_reader::read_sheets(f);
TS_ASSERT_EQUALS(sheets["rId1"], "Chart1");
TS_ASSERT_EQUALS(sheets["rId2"], "Sheet1");
}
{
auto path = PathHelper::GetDataDirectory("/reader/bug304.xlsx");
xlnt::zip_file f(path);
auto sheets = xlnt::workbook_reader::read_sheets(f);
TS_ASSERT_EQUALS(sheets["rId1"], "Sheet1");
TS_ASSERT_EQUALS(sheets["rId2"], "Sheet2");
TS_ASSERT_EQUALS(sheets["rId3"], "Sheet3");
}
}
void test_guess_types()
{
bool guess;
xlnt::cell:type dtype;
for(const auto &expected : {true, xlnt::cell::type::number}, {false, xlnt::cell::type::string})
{
std::tie(guess, dtype) = expected;
auto path = PathHelper::GetDataDirectory("/genuine/guess_types.xlsx");
auto wb = xlnt::load_workbook(path, guess);
auto ws = wb.get_active_sheet();
TS_ASSERT_EQUALS(ws.get_cell("D2").get_data_type(), dtype);
}
}
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.get_reference(), "A1:B6");
}
void test_bad_formats_xlsb()
{
auto path = PathHelper::GetDataDirectory("/genuine/a.xlsb");
TS_ASSERT_THROWS(xlnt::load_workbook(path), xlnt::invaid_file_exception);
}
void test_bad_formats_xls()
{
auto path = PathHelper::GetDataDirectory("/genuine/a.xls");
TS_ASSERT_THROWS(xlnt::load_workbook(path), xlnt::invaid_file_exception);
}
void test_bad_formats_no()
{
auto path = PathHelper::GetDataDirectory("/genuine/a.no-format");
TS_ASSERT_THROWS(xlnt::load_workbook(path), xlnt::invaid_file_exception);
} }
}; };

View File

@ -34,6 +34,14 @@ public:
TS_ASSERT_THROWS(wb_.create_sheet(title), xlnt::sheet_title_exception); TS_ASSERT_THROWS(wb_.create_sheet(title), xlnt::sheet_title_exception);
} }
void test_increment_title()
{
auto ws1 = wb_.create_sheet("Test");
TS_ASSERT_EQUALS(ws1.get_title(), "Test");
auto ws2 = wb_.create_sheet("Test");
TS_ASSERT_EQUALS(ws1.get_title(), "Test1");
}
void test_set_bad_title_character() void test_set_bad_title_character()
{ {
TS_ASSERT_THROWS(wb_.create_sheet("["), xlnt::sheet_title_exception); TS_ASSERT_THROWS(wb_.create_sheet("["), xlnt::sheet_title_exception);
@ -83,7 +91,7 @@ public:
xlnt::worksheet ws1(wb_); xlnt::worksheet ws1(wb_);
xlnt::worksheet ws2(wb_); xlnt::worksheet ws2(wb_);
wb_.create_named_range("wrong_sheet_range", ws1, "C5"); wb_.create_named_range("wrong_sheet_range", ws1, "C5");
TS_ASSERT_THROWS_ANYTHING(ws2.get_named_range("wrong_sheet_range")); TS_ASSERT_THROWS(ws2.get_named_range("wrong_sheet_range"), xlnt::named_range_exception);
} }
void test_cell_offset() void test_cell_offset()
@ -221,12 +229,27 @@ public:
auto rows = ws.rows(); auto rows = ws.rows();
TS_ASSERT_EQUALS(rows.num_rows(), 9); TS_ASSERT_EQUALS(rows.length(), 9);
TS_ASSERT_EQUALS(rows[0][0], "first"); TS_ASSERT_EQUALS(rows[0][0], "first");
TS_ASSERT_EQUALS(rows[8][2], "last"); TS_ASSERT_EQUALS(rows[8][2], "last");
} }
void test_cols()
{
xlnt::worksheet ws(wb_);
ws.get_cell("A1") = "first";
ws.get_cell("C9") = "last";
auto cols = ws.columns();
TS_ASSERT_EQUALS(cols.length(), 3);
TS_ASSERT_EQUALS(rows[0][0], "first");
TS_ASSERT_EQUALS(rows[2][8], "last");
}
void test_auto_filter() void test_auto_filter()
{ {
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
@ -241,121 +264,6 @@ public:
TS_ASSERT_EQUALS(ws.get_auto_filter(), "C1:G9"); TS_ASSERT_EQUALS(ws.get_auto_filter(), "C1:G9");
} }
void test_page_margins()
{
xlnt::worksheet ws(wb_);
ws.get_page_margins().set_left(2.0);
ws.get_page_margins().set_right(2.0);
ws.get_page_margins().set_top(2.0);
ws.get_page_margins().set_bottom(2.0);
ws.get_page_margins().set_header(1.5);
ws.get_page_margins().set_footer(1.5);
auto xml_string = xlnt::writer::write_worksheet(ws);
pugi::xml_document doc;
doc.load(xml_string.c_str());
auto page_margins_node = doc.child("worksheet").child("pageMargins");
TS_ASSERT_DIFFERS(page_margins_node.attribute("left"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("left").as_double(), 2.0);
TS_ASSERT_DIFFERS(page_margins_node.attribute("right"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("right").as_double(), 2.0);
TS_ASSERT_DIFFERS(page_margins_node.attribute("top"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("top").as_double(), 2.0);
TS_ASSERT_DIFFERS(page_margins_node.attribute("bottom"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("bottom").as_double(), 2.0);
TS_ASSERT_DIFFERS(page_margins_node.attribute("header"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("header").as_double(), 1.5);
TS_ASSERT_DIFFERS(page_margins_node.attribute("footer"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("footer").as_double(), 1.5);
xlnt::worksheet ws2(wb_);
xml_string = xlnt::writer::write_worksheet(ws2);
doc.load(xml_string.c_str());
TS_ASSERT_DIFFERS(page_margins_node.attribute("left"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("left").as_double(), 0.75);
TS_ASSERT_DIFFERS(page_margins_node.attribute("right"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("right").as_double(), 0.75);
TS_ASSERT_DIFFERS(page_margins_node.attribute("top"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("top").as_double(), 1);
TS_ASSERT_DIFFERS(page_margins_node.attribute("bottom"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("bottom").as_double(), 1);
TS_ASSERT_DIFFERS(page_margins_node.attribute("header"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("header").as_double(), 0.5);
TS_ASSERT_DIFFERS(page_margins_node.attribute("footer"), nullptr);
TS_ASSERT_EQUALS(page_margins_node.attribute("footer").as_double(), 0.5);
}
void test_merge()
{
xlnt::worksheet ws(wb_);
std::vector<std::string> string_table = {"Cell A1", "Cell B1"};
ws.get_cell("A1") = "Cell A1";
ws.get_cell("B1") = "Cell B1";
auto xml_string = xlnt::writer::write_worksheet(ws, string_table);
pugi::xml_document doc;
doc.load(xml_string.c_str());
auto sheet_data_node = doc.child("worksheet").child("sheetData");
auto row_node = sheet_data_node.find_child_by_attribute("row", "r", "1");
auto cell_node = row_node.find_child_by_attribute("c", "r", "A1");
TS_ASSERT_DIFFERS(cell_node, nullptr);
TS_ASSERT_DIFFERS(cell_node.attribute("r"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("r").as_string()), "A1");
TS_ASSERT_DIFFERS(cell_node.attribute("t"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("t").as_string()), "s");
TS_ASSERT_DIFFERS(cell_node.child("v"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.child("v").text().as_string()), "0");
cell_node = row_node.find_child_by_attribute("c", "r", "B1");
TS_ASSERT_DIFFERS(cell_node, nullptr);
TS_ASSERT_DIFFERS(cell_node.attribute("r"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("r").as_string()), "B1");
TS_ASSERT_DIFFERS(cell_node.attribute("t"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("t").as_string()), "s");
TS_ASSERT_DIFFERS(cell_node.child("v"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.child("v").text().as_string()), "1");
ws.merge_cells("A1:B1");
xml_string = xlnt::writer::write_worksheet(ws, string_table);
doc.load(xml_string.c_str());
sheet_data_node = doc.child("worksheet").child("sheetData");
row_node = sheet_data_node.find_child_by_attribute("row", "r", "1");
cell_node = row_node.find_child_by_attribute("c", "r", "A1");
TS_ASSERT_DIFFERS(cell_node, nullptr);
TS_ASSERT_DIFFERS(cell_node.attribute("r"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("r").as_string()), "A1");
TS_ASSERT_DIFFERS(cell_node.attribute("t"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("t").as_string()), "s");
TS_ASSERT_DIFFERS(cell_node.child("v"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.child("v").text().as_string()), "0");
cell_node = row_node.find_child_by_attribute("c", "r", "B1");
TS_ASSERT_DIFFERS(cell_node, nullptr);
TS_ASSERT_EQUALS(cell_node.child("v"), nullptr);
auto merge_node = doc.child("worksheet").child("mergeCells").child("mergeCell");
TS_ASSERT_DIFFERS(merge_node, nullptr);
TS_ASSERT_EQUALS(std::string(merge_node.attribute("ref").as_string()), "A1:B1");
ws.unmerge_cells("A1:B1");
xml_string = xlnt::writer::write_worksheet(ws, string_table);
doc.load(xml_string.c_str());
sheet_data_node = doc.child("worksheet").child("sheetData");
row_node = sheet_data_node.find_child_by_attribute("row", "r", "1");
cell_node = row_node.find_child_by_attribute("c", "r", "A1");
TS_ASSERT_DIFFERS(cell_node, nullptr);
TS_ASSERT_DIFFERS(cell_node.attribute("r"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("r").as_string()), "A1");
TS_ASSERT_DIFFERS(cell_node.attribute("t"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.attribute("t").as_string()), "s");
TS_ASSERT_DIFFERS(cell_node.child("v"), nullptr);
TS_ASSERT_EQUALS(std::string(cell_node.child("v").text().as_string()), "0");
cell_node = row_node.find_child_by_attribute("c", "r", "B1");
TS_ASSERT_EQUALS(cell_node, nullptr);
merge_node = doc.child("worksheet").child("mergeCells").child("mergeCell");
TS_ASSERT_EQUALS(merge_node, nullptr);
}
void test_freeze() void test_freeze()
{ {
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
@ -373,8 +281,46 @@ public:
TS_ASSERT(!ws.has_frozen_panes()); TS_ASSERT(!ws.has_frozen_panes());
} }
void test_write_empty()
{
TS_ASSERT(false);
xlnt::worksheet ws(wb_);
auto xml_string = xlnt::writer::write_worksheet(ws);
pugi::xml_document doc;
doc.load(xml_string.c_str());
}
void test_page_margins()
{
TS_ASSERT(false);
xlnt::worksheet ws(wb_);
auto xml_string = xlnt::writer::write_worksheet(ws);
pugi::xml_document doc;
doc.load(xml_string.c_str());
}
void test_merge()
{
TS_ASSERT(false);
xlnt::worksheet ws(wb_);
auto xml_string = xlnt::writer::write_worksheet(ws);
pugi::xml_document doc;
doc.load(xml_string.c_str());
}
void test_printer_settings() void test_printer_settings()
{ {
TS_ASSERT(false);
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
ws.get_page_setup().set_orientation(xlnt::page_setup::orientation::landscape); ws.get_page_setup().set_orientation(xlnt::page_setup::orientation::landscape);
@ -388,24 +334,116 @@ public:
pugi::xml_document doc; pugi::xml_document doc;
doc.load(xml_string.c_str()); doc.load(xml_string.c_str());
auto page_setup_node = doc.child("worksheet").child("pageSetup");
TS_ASSERT_DIFFERS(page_setup_node, nullptr);
TS_ASSERT_DIFFERS(page_setup_node.attribute("orientation"), nullptr);
TS_ASSERT_EQUALS(std::string(page_setup_node.attribute("orientation").as_string()), "landscape");
TS_ASSERT_DIFFERS(page_setup_node.attribute("paperSize"), nullptr);
TS_ASSERT_EQUALS(page_setup_node.attribute("paperSize").as_int(), 3);
TS_ASSERT_DIFFERS(page_setup_node.attribute("fitToHeight"), nullptr);
TS_ASSERT_EQUALS(page_setup_node.attribute("fitToHeight").as_int(), 0);
TS_ASSERT_DIFFERS(page_setup_node.attribute("fitToWidth"), nullptr);
TS_ASSERT_EQUALS(page_setup_node.attribute("fitToWidth").as_int(), 1);
TS_ASSERT_DIFFERS(doc.child("worksheet").child("pageSetUpPr").attribute("fitToPage"), nullptr);
TS_ASSERT_EQUALS(doc.child("worksheet").child("pageSetUpPr").attribute("fitToPage").as_int(), 1);
xlnt::worksheet ws2(wb_); xlnt::worksheet ws2(wb_);
xml_string = xlnt::writer::write_worksheet(ws2); xml_string = xlnt::writer::write_worksheet(ws2);
doc.load(xml_string.c_str()); doc.load(xml_string.c_str());
TS_ASSERT_EQUALS(doc.child("worksheet").child("pageSetup"), nullptr); }
TS_ASSERT_EQUALS(doc.child("worksheet").child("pageSetUpPr"), nullptr);
void test_header_footer()
{
auto ws = wb_.create_sheet();
ws.get_header_footer().get_left_header().set_text("Left Header Text");
ws.get_header_footer().get_center_header().set_text("Center Header Text");
ws.get_header_footer().get_center_header().set_font_name("Arial,Regular");
ws.get_header_footer().get_center_header().set_font_size(6);
ws.get_header_footer().get_center_header().set_font_color("445566");
ws.get_header_footer().get_right_header().set_text("Right Header Text");
ws.get_header_footer().get_right_header().set_font_name("Arial,Bold");
ws.get_header_footer().get_right_header().set_font_size(8);
ws.get_header_footer().get_right_header().set_font_color("112233");
ws.get_header_footer().get_left_footer().set_text("Left Footer Text\nAnd &[Date] and &[Time]");
ws.get_header_footer().get_left_footer().set_font_name("Times New Roman,Regular");
ws.get_header_footer().get_left_footer().set_font_size(10);
ws.get_header_footer().get_left_footer().set_font_color("445566");
ws.get_header_footer().get_center_footer().set_text("Center Footer Text &[Path]&[File] on &[Tab]");
ws.get_header_footer().get_center_footer().set_font_name("Times New Roman,Bold");
ws.get_header_footer().get_center_footer().set_font_size(12);
ws.get_header_footer().get_center_footer().set_font_color("778899");
ws.get_header_footer().get_right_footer().set_text("Right Footer Text &[Page] of &[Pages]");
ws.get_header_footer().get_right_footer().set_font_name("Times New Roman,Italic");
ws.get_header_footer().get_right_footer().set_font_size(14);
ws.get_header_footer().get_right_footer().set_font_color("AABBCC");
auto expected_xml_string =
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <sheetPr>"
" <outlinePr summaryRight=\"1\" summaryBelow=\"1\"/>"
" </sheetPr>"
" <dimension ref=\"A1:A1\"/>"
" <sheetViews>"
" <sheetView workbookViewId=\"0\">"
" <selection sqref=\"A1\" activeCell=\"A1\"/>"
" </sheetView>"
" </sheetViews>"
" <sheetFormatPr baseColWidth=\"10\" defaultRowHeight=\"15\"/>"
" <sheetData/>"
" <pageMargins left=\"0.75\" right=\"0.75\" top=\"1\" bottom=\"1\" header=\"0.5\" footer=\"0.5\"/>"
" <headerFooter>"
" <oddHeader>&amp;L&amp;\"Calibri,Regular\"&amp;K000000Left Header Text&amp;C&amp;\"Arial,Regular\"&amp;6&amp;K445566Center Header Text&amp;R&amp;\"Arial,Bold\"&amp;8&amp;K112233Right Header Text</oddHeader>"
" <oddFooter>&amp;L&amp;\"Times New Roman,Regular\"&amp;10&amp;K445566Left Footer Text_x000D_And &amp;D and &amp;T&amp;C&amp;\"Times New Roman,Bold\"&amp;12&amp;K778899Center Footer Text &amp;Z&amp;F on &amp;A&amp;R&amp;\"Times New Roman,Italic\"&amp;14&amp;KAABBCCRight Footer Text &amp;P of &amp;N</oddFooter>"
" </headerFooter>"
"</worksheet>";
pugi::xml_document doc;
doc.load(expected_xml_string.c_str());
TS_ASSERT(Helper::compare_xml(doc, xlnt::worksheet_writer::write_worksheet(ws, {}, {})));
auto ws = wb_.create_sheet();
expected_xml_string =
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <sheetPr>"
" <outlinePr summaryRight="1" summaryBelow="1"/>"
" </sheetPr>"
" <dimension ref="A1:A1"/>"
" <sheetViews>"
" <sheetView workbookViewId="0">"
" <selection sqref="A1" activeCell="A1"/>"
" </sheetView>"
" </sheetViews>"
" <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>"
" <sheetData/>"
" <pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>"
"</worksheet>";
pugi::xml_document doc;
doc.load(expected_xml_string.c_str());
TS_ASSERT(Helper::compare_xml(doc, xlnt::worksheet_writer::write_worksheet(ws, {}, {})));
}
void test_positioning_point()
{
auto ws = wb_.create_sheet();
TS_ASSERT(false);
}
void test_positioning_roundtrip()
{
auto ws = wb_.create_sheet();
TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("A1").get_anchor()), xlnt::cell_reference("A1"));
TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("D52").get_anchor()), xlnt::cell_reference("D52"));
TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("X11").get_anchor()), xlnt::cell_reference("X11"));
}
void test_page_setup()
{
xlnt::page_setup p;
TS_ASSERT(p.get_setup().empty());
p.set_scale(1);
TS_ASSERT_EQUALS(p.get_setup().at("scale"), 1);
}
void test_page_options()
{
xlnt::page_setup p;
TS_ASSERT(p.get_options().empty());
p.set_horizontal_centered(true);
p.set_vertical_centered(true);
TS_ASSERT_EQUALS(p.get_options().at("verticalCentered"), "1");
TS_ASSERT_EQUALS(p.get_options().at("horizontalCentered"), "1");
} }
private: private: