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_null() { xlnt::workbook wb; const auto datatypes = { xlnt::cell::type::empty, xlnt::cell::type::boolean, xlnt::cell::type::error, xlnt::cell::type::formula_string, xlnt::cell::type::number, xlnt::cell::type::shared_string }; for (const auto &datatype : datatypes) { auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.data_type(datatype); xlnt_assert(cell.data_type() == datatype); cell.clear_value(); xlnt_assert(cell.data_type() == xlnt::cell::type::empty); } }
void test_formula2() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value("=if(A1<4;-1;1)", true); xlnt_assert(cell.data_type() == xlnt::cell::type::number); xlnt_assert(cell.has_formula()); }
void test_cell_formatted_as_date3() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value(xlnt::datetime::today()); cell.value(true); xlnt_assert(!cell.is_date()); xlnt_assert(cell.value<bool>() == true); }
void test_not_formula() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value("=", true); xlnt_assert(cell.data_type() == xlnt::cell::type::shared_string); xlnt_assert(cell.value<std::string>() == "="); xlnt_assert(!cell.has_formula()); }
void test_cell_formatted_as_date1() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value(xlnt::datetime::today()); cell.clear_value(); xlnt_assert(!cell.is_date()); // disagree with openpyxl xlnt_assert(!cell.has_value()); }
void test_cell_formatted_as_date2() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value(xlnt::datetime::today()); cell.value("testme"); xlnt_assert(!cell.is_date()); xlnt_assert(cell.value<std::string>() == "testme"); }
void test_insert_time() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value(xlnt::time(1, 3)); xlnt_assert(cell.data_type() == xlnt::cell::type::number); xlnt_assert_delta(cell.value<long double>(), 0.04375L, 1E-9); xlnt_assert(cell.is_date()); xlnt_assert(cell.number_format().format_string() == "h:mm:ss"); }
void test_insert_date() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value(xlnt::date(2010, 7, 13)); xlnt_assert(cell.data_type() == xlnt::cell::type::number); xlnt_assert(cell.value<long double>() == 40372.L); xlnt_assert(cell.is_date()); xlnt_assert(cell.number_format().format_string() == "yyyy-mm-dd"); }
void test_constructor() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference("A", 1)); xlnt_assert(cell.data_type() == xlnt::cell::type::empty); xlnt_assert(cell.column() == "A"); xlnt_assert(cell.row() == 1); xlnt_assert(cell.reference() == "A1"); xlnt_assert(!cell.has_value()); }
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_insert_datetime() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value(xlnt::datetime(2010, 7, 13, 6, 37, 41)); xlnt_assert(cell.data_type() == xlnt::cell::type::number); xlnt_assert_delta(cell.value<long double>(), 40372.27616898148L, 1E-9); xlnt_assert(cell.is_date()); xlnt_assert(cell.number_format().format_string() == "yyyy-mm-dd h:mm:ss"); }
void test_timedelta() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value(xlnt::timedelta(1, 3, 0, 0, 0)); xlnt_assert(cell.value<long double>() == 1.125); xlnt_assert(cell.data_type() == xlnt::cell::type::number); xlnt_assert(!cell.is_date()); xlnt_assert(cell.number_format().format_string() == "[hh]:mm:ss"); }
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_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_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_string() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); cell.value("hello"); xlnt_assert(cell.data_type() == xlnt::cell::type::shared_string); cell.value("."); xlnt_assert(cell.data_type() == xlnt::cell::type::shared_string); cell.value("0800"); xlnt_assert(cell.data_type() == xlnt::cell::type::shared_string); }
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_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_cell_offset() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); xlnt_assert(cell.offset(1, 2).reference() == "B3"); }
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_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_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_boolean() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); for (auto value : { true, false }) { cell.value(value); xlnt_assert(cell.data_type() == xlnt::cell::type::boolean); } }
void test_error_codes() { xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); for (auto error_code : xlnt::cell::error_codes()) { cell.value(error_code.first, true); xlnt_assert(cell.data_type() == xlnt::cell::type::error); } }
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_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_all() { auto prot = xlnt::protection::unlocked_and_visible(); xlnt_assert(!prot.hidden()); xlnt_assert(!prot.locked()); prot = xlnt::protection::locked_and_visible(); xlnt_assert(!prot.hidden()); xlnt_assert(prot.locked()); prot = xlnt::protection::unlocked_and_hidden(); xlnt_assert(prot.hidden()); xlnt_assert(!prot.locked()); prot = xlnt::protection::locked_and_hidden(); xlnt_assert(prot.hidden()); xlnt_assert(prot.locked()); }