void test_batch_formatting() { xlnt::workbook wb; auto ws = wb.active_sheet(); for (auto row = 1; row <= 10; ++row) { for (auto column = 1; column <= 10; ++column) { auto ref = xlnt::cell_reference(column, row); ws[ref].value(ref.to_string()); } } ws.range("A1:A10").font(xlnt::font().name("Arial")); ws.range("A1:J1").font(xlnt::font().bold(true)); xlnt_assert_equals(ws.cell("A1").font().name(), "Calibri"); xlnt_assert(ws.cell("A1").font().bold()); xlnt_assert_equals(ws.cell("A2").font().name(), "Arial"); xlnt_assert(!ws.cell("A2").font().bold()); xlnt_assert_equals(ws.cell("B1").font().name(), "Calibri"); xlnt_assert(ws.cell("B1").font().bold()); xlnt_assert(!ws.cell("B2").has_format()); }
void test_from_string() { xlnt::time t("10:35:45"); xlnt_assert_equals(t.hour, 10); xlnt_assert_equals(t.minute, 35); xlnt_assert_equals(t.second, 45); }
void test_anchor() { xlnt::workbook wb; auto cell = wb.active_sheet().cell("A1"); auto anchor = cell.anchor(); xlnt_assert_equals(anchor.first, 0); xlnt_assert_equals(anchor.second, 0); }
void test_leap_year_bug() { xlnt::datetime dt(1900, 2, 29, 0, 0, 0, 0); auto number = dt.to_number(xlnt::calendar::windows_1900); xlnt_assert_equals(static_cast<int>(number), 60); auto converted = xlnt::datetime::from_number(number, xlnt::calendar::windows_1900); xlnt_assert_equals(dt, converted); }
void test_early_date() { xlnt::date d(1900, 1, 29); auto number = d.to_number(xlnt::calendar::windows_1900); xlnt_assert_equals(number, 29); auto converted = xlnt::date::from_number(number, xlnt::calendar::windows_1900); xlnt_assert_equals(d, converted); }
void test_mac_calendar() { xlnt::date d(2016, 7, 16); auto number_1900 = d.to_number(xlnt::calendar::windows_1900); auto number_1904 = d.to_number(xlnt::calendar::mac_1904); xlnt_assert_equals(number_1900, 42567); xlnt_assert_equals(number_1904, 41105); auto converted_1900 = xlnt::date::from_number(number_1900, xlnt::calendar::windows_1900); auto converted_1904 = xlnt::date::from_number(number_1904, xlnt::calendar::mac_1904); xlnt_assert_equals(converted_1900, d); xlnt_assert_equals(converted_1904, d); }
void test_runs() { xlnt::rich_text rt; xlnt_assert(rt.runs().empty()); std::vector<xlnt::rich_text_run> test_runs{xlnt::rich_text_run{"1_abc_test_123"}, xlnt::rich_text_run{"2_abc_test_123"}, xlnt::rich_text_run{"3_abc_test_123"}}; // just one rt.add_run(test_runs[0]); xlnt_assert_equals(1, rt.runs().size()); xlnt_assert_equals(test_runs[0], rt.runs()[0]); // whole set rt.runs(test_runs); xlnt_assert_equals(test_runs, rt.runs()); }
void test_operators() { xlnt::date d1(2016, 7, 16); xlnt::date d2(2016, 7, 16); xlnt::date d3(2016, 7, 15); xlnt_assert_equals(d1, d2); xlnt_assert_differs(d1, d3); }
void test_carry() { // We want a time that rolls over to the next second, minute, and hour // Start off with a time 1 microsecond before the next hour xlnt::datetime dt(2016, 7, 9, 10, 59, 59, 999999); auto number = dt.to_number(xlnt::calendar::windows_1900); // Add 600 nanoseconds to the raw number which represents time as a fraction of a day // In other words, 6 tenths of a millionth of a sixtieth of a sixtieth of a twenty-fourth of a day number += (0.6 / 1000000) / 60 / 60 / 24; auto rollover = xlnt::datetime::from_number(number, xlnt::calendar::windows_1900); xlnt_assert_equals(rollover.hour, 11); xlnt_assert_equals(rollover.minute, 00); xlnt_assert_equals(rollover.second, 00); xlnt_assert_equals(rollover.microsecond, 00); }
void test_values() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); cell.value(static_cast<int>(4)); xlnt_assert_equals(cell.value<int>(), 4); cell.value(static_cast<unsigned int>(3)); xlnt_assert_equals(cell.value<unsigned>(), 3); cell.value(static_cast<unsigned long long>(4)); xlnt_assert_equals(cell.value<unsigned long long>(), 4); cell.value(static_cast<long long int>(3)); xlnt_assert_equals(cell.value<long long int>(), 3); cell.value(static_cast<float>(3.14)); xlnt_assert_delta(cell.value<float>(), 3.14, 0.001); cell.value(static_cast<double>(4.1415)); xlnt_assert_equals(cell.value<double>(), 4.1415); cell.value(static_cast<long double>(3.141592)); xlnt_assert_equals(cell.value<long double>(), 3.141592); auto cell2 = ws.cell("A2"); cell2.value(std::string(100000, 'a')); cell.value(cell2); xlnt_assert_equals(cell.value<std::string>(), std::string(32767, 'a')); }
void test_reference() { xlnt::cell_reference_hash hash; xlnt_assert_differs(hash(xlnt::cell_reference("A2")), hash(xlnt::cell_reference(1, 1))); xlnt_assert_equals(hash(xlnt::cell_reference("A2")), hash(xlnt::cell_reference(1, 2))); xlnt_assert_equals((xlnt::cell_reference("A1"), xlnt::cell_reference("B2")), xlnt::range_reference("A1:B2")); xlnt_assert_throws(xlnt::cell_reference("A1&"), xlnt::invalid_cell_reference); xlnt_assert_throws(xlnt::cell_reference("A"), xlnt::invalid_cell_reference); auto ref = xlnt::cell_reference("$B$7"); xlnt_assert(ref.row_absolute()); xlnt_assert(ref.column_absolute()); xlnt_assert(xlnt::cell_reference("A1") == "A1"); xlnt_assert(xlnt::cell_reference("A1") != "A2"); }
void test_hyperlink() { xlnt::workbook wb; auto cell = wb.active_sheet().cell("A1"); xlnt_assert(!cell.has_hyperlink()); xlnt_assert_throws(cell.hyperlink(), xlnt::invalid_attribute); xlnt_assert_throws(cell.hyperlink("notaurl"), xlnt::invalid_parameter); xlnt_assert_throws(cell.hyperlink(""), xlnt::invalid_parameter); cell.hyperlink("http://example.com"); xlnt_assert(cell.has_hyperlink()); xlnt_assert_equals(cell.hyperlink(), "http://example.com"); }
void test_number_format() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); xlnt::number_format format("dd--hh--mm"); cell.number_format(format); xlnt_assert(cell.has_format()); xlnt_assert(cell.format().number_format_applied()); xlnt_assert_equals(cell.number_format().format_string(), "dd--hh--mm"); }
void test_border() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); xlnt::border border; cell.border(border); xlnt_assert(cell.has_format()); xlnt_assert(cell.format().border_applied()); xlnt_assert_equals(cell.border(), border); }
void test_column_operators() { auto c1 = xlnt::column_t(); c1 = "B"; auto c2 = xlnt::column_t(); const auto d = std::string("D"); c2 = d; xlnt_assert(c1 != c2); xlnt_assert(c1 == static_cast<xlnt::column_t::index_t>(2)); xlnt_assert(c2 == d); xlnt_assert(c1 != 3); xlnt_assert(c1 != static_cast<xlnt::column_t::index_t>(5)); xlnt_assert(c1 != "D"); xlnt_assert(c1 != d); xlnt_assert(c2 >= c1); xlnt_assert(c2 > c1); xlnt_assert(c1 < c2); xlnt_assert(c1 <= c2); xlnt_assert_equals(--c2, 3); xlnt_assert_equals(c2--, 3); xlnt_assert_equals(c2, 2); c2 = 4; c1 = 3; xlnt_assert(c2 <= 4); xlnt_assert(!(c2 < 3)); xlnt_assert(c1 >= 3); xlnt_assert(!(c1 > 4)); xlnt_assert(4 >= c2); xlnt_assert(!(3 >= c2)); xlnt_assert(3 <= c1); xlnt_assert(!(4 <= c1)); }
void test_font() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); auto font = xlnt::font().bold(true); cell.font(font); xlnt_assert(cell.has_format()); xlnt_assert(cell.format().font_applied()); xlnt_assert_equals(cell.font(), font); }
void test_comment() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); xlnt_assert(!cell.has_comment()); xlnt_assert_throws(cell.comment(), xlnt::exception); cell.comment(xlnt::comment("comment", "author")); xlnt_assert(cell.has_comment()); xlnt_assert_equals(cell.comment(), xlnt::comment("comment", "author")); cell.clear_comment(); xlnt_assert(!cell.has_comment()); xlnt_assert_throws(cell.comment(), xlnt::exception); }
void test_fill() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); xlnt::fill fill(xlnt::pattern_fill() .type(xlnt::pattern_fill_type::solid) .foreground(xlnt::color::red())); cell.fill(fill); xlnt_assert(cell.has_format()); xlnt_assert(cell.format().fill_applied()); xlnt_assert_equals(cell.fill(), fill); }
void test_formula3() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); xlnt_assert(!cell.has_formula()); xlnt_assert_throws_nothing(cell.formula("")); xlnt_assert(!cell.has_formula()); cell.formula("=42"); xlnt_assert(cell.has_formula()); xlnt_assert_equals(cell.formula(), "42"); cell.clear_formula(); xlnt_assert(!cell.has_formula()); }
void test_alignment() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); xlnt::alignment align; align.wrap(true); cell.alignment(align); xlnt_assert(cell.has_format()); xlnt_assert(cell.format().alignment_applied()); xlnt_assert_equals(cell.alignment(), align); }
void test_infer_numeric() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); cell.value("4.2", true); xlnt_assert_delta(cell.value<long double>(), 4.2L, 1E-9); cell.value("-42.000", true); xlnt_assert(cell.value<int>() == -42); cell.value("0", true); xlnt_assert(cell.value<int>() == 0); cell.value("0.9999", true); xlnt_assert(cell.value<long double>() == 0.9999L); cell.value("99E-02", true); xlnt_assert(cell.value<long double>() == 0.99L); cell.value("4", true); xlnt_assert(cell.value<int>() == 4); cell.value("-1E3", true); xlnt_assert(cell.value<int>() == -1000); cell.value("2e+2", true); xlnt_assert(cell.value<int>() == 200); cell.value("3.1%", true); xlnt_assert_delta(cell.value<long double>(), 0.031L, 1E-9); cell.value("03:40:16", true); xlnt_assert(cell.value<xlnt::time>() == xlnt::time(3, 40, 16)); cell.value("03:", true); xlnt_assert_equals(cell.value<std::string>(), "03:"); cell.value("03:40", true); xlnt_assert(cell.value<xlnt::time>() == xlnt::time(3, 40)); cell.value("30:33.865633336", true); xlnt_assert(cell.value<xlnt::time>() == xlnt::time(0, 30, 33, 865633)); }
void test_protection() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); xlnt_assert(!cell.has_format()); auto protection = xlnt::protection().locked(false).hidden(true); cell.protection(protection); xlnt_assert(cell.has_format()); xlnt_assert(cell.format().protection_applied()); xlnt_assert_equals(cell.protection(), protection); xlnt_assert(cell.has_format()); cell.clear_format(); xlnt_assert(!cell.has_format()); }
void test_style() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell("A1"); xlnt_assert(!cell.has_style()); auto test_style = wb.create_style("test_style"); test_style.number_format(xlnt::number_format::date_ddmmyyyy(), true); cell.style(test_style); xlnt_assert(cell.has_style()); xlnt_assert_equals(cell.style().number_format(), xlnt::number_format::date_ddmmyyyy()); xlnt_assert_equals(cell.style(), test_style); auto other_style = wb.create_style("other_style"); other_style.number_format(xlnt::number_format::date_time2(), true); cell.style("other_style"); xlnt_assert_equals(cell.style().number_format(), xlnt::number_format::date_time2()); xlnt_assert_equals(cell.style(), other_style); auto last_style = wb.create_style("last_style"); last_style.number_format(xlnt::number_format::percentage(), true); cell.style(last_style); xlnt_assert_equals(cell.style().number_format(), xlnt::number_format::percentage()); xlnt_assert_equals(cell.style(), last_style); xlnt_assert_throws(cell.style("doesn't exist"), xlnt::key_not_found); cell.clear_style(); xlnt_assert(!cell.has_style()); xlnt_assert_throws(cell.style(), xlnt::invalid_attribute); }
void test_to_string() { xlnt::datetime dt(2016, 7, 16, 9, 11, 32, 999999); xlnt_assert_equals(dt.to_string(), "2016/7/16 9:11:32:999999"); }
void test_print() { xlnt::workbook wb; auto ws = wb.active_sheet(); { auto cell = ws.cell("A1"); std::stringstream ss; ss << cell; auto stream_string = ss.str(); xlnt_assert_equals(cell.to_string(), stream_string); xlnt_assert_equals(stream_string, ""); } { auto cell = ws.cell("A2"); cell.value(false); std::stringstream ss; ss << cell; auto stream_string = ss.str(); xlnt_assert_equals(cell.to_string(), stream_string); xlnt_assert_equals(stream_string, "FALSE"); } { auto cell = ws.cell("A3"); cell.value(true); std::stringstream ss; ss << cell; auto stream_string = ss.str(); xlnt_assert_equals(cell.to_string(), stream_string); xlnt_assert_equals(stream_string, "TRUE"); } { auto cell = ws.cell("A4"); cell.value(1.234); std::stringstream ss; ss << cell; auto stream_string = ss.str(); xlnt_assert_equals(cell.to_string(), stream_string); xlnt_assert_equals(stream_string, "1.234"); } { auto cell = ws.cell("A5"); cell.error("#REF"); std::stringstream ss; ss << cell; auto stream_string = ss.str(); xlnt_assert_equals(cell.to_string(), stream_string); xlnt_assert_equals(stream_string, "#REF"); } { auto cell = ws.cell("A6"); cell.value("test"); std::stringstream ss; ss << cell; auto stream_string = ss.str(); xlnt_assert_equals(cell.to_string(), stream_string); xlnt_assert_equals(stream_string, "test"); } }
void test_operators() { xlnt::rich_text text1; xlnt::rich_text text2; xlnt_assert_equals(text1, text2); xlnt::rich_text_run run_default; text1.add_run(run_default); xlnt_assert_differs(text1, text2); text2.add_run(run_default); xlnt_assert_equals(text1, text2); xlnt::rich_text_run run_formatted; xlnt::font run_font; run_font.color(xlnt::color::green()); run_font.name("Cambria"); run_font.scheme("ascheme"); run_font.size(40); run_font.family(17); run_formatted.second = run_font; xlnt::rich_text text_formatted; text_formatted.add_run(run_formatted); xlnt::rich_text_run run_color_differs = run_formatted; run_font = xlnt::font(); run_font.color(xlnt::color::red()); run_color_differs.second = run_font; xlnt::rich_text text_color_differs; text_color_differs.add_run(run_color_differs); xlnt_assert_differs(text_formatted, text_color_differs); xlnt::rich_text_run run_font_differs = run_formatted; run_font = xlnt::font(); run_font.name("Calibri"); run_font_differs.second = run_font; xlnt::rich_text text_font_differs; text_font_differs.add_run(run_font_differs); xlnt_assert_differs(text_formatted, text_font_differs); xlnt::rich_text_run run_scheme_differs = run_formatted; run_font = xlnt::font(); run_font.scheme("bscheme"); run_scheme_differs.second = run_font; xlnt::rich_text text_scheme_differs; text_scheme_differs.add_run(run_scheme_differs); xlnt_assert_differs(text_formatted, text_scheme_differs); xlnt::rich_text_run run_size_differs = run_formatted; run_font = xlnt::font(); run_font.size(41); run_size_differs.second = run_font; xlnt::rich_text text_size_differs; text_size_differs.add_run(run_size_differs); xlnt_assert_differs(text_formatted, text_size_differs); xlnt::rich_text_run run_family_differs = run_formatted; run_font = xlnt::font(); run_font.family(18); run_family_differs.second = run_font; xlnt::rich_text text_family_differs; text_family_differs.add_run(run_family_differs); xlnt_assert_differs(text_formatted, text_family_differs); }