2016-08-06 22:40:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
#include <detail/vector_streambuf.hpp>
|
2016-08-06 22:40:17 +08:00
|
|
|
#include <helpers/temporary_file.hpp>
|
|
|
|
#include <helpers/path_helper.hpp>
|
|
|
|
#include <helpers/xml_helper.hpp>
|
|
|
|
#include <xlnt/workbook/workbook.hpp>
|
|
|
|
|
|
|
|
class test_produce_xlsx : public CxxTest::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file)
|
|
|
|
{
|
2016-10-31 03:48:40 +08:00
|
|
|
std::vector<std::uint8_t> wb_data;
|
|
|
|
wb.save(wb_data);
|
2016-08-06 22:40:17 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
std::ifstream file_stream(file.string(), std::ios::binary);
|
|
|
|
std::vector<std::uint8_t> file_data;
|
2016-08-06 22:40:17 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
{
|
|
|
|
xlnt::detail::vector_ostreambuf file_data_buffer(file_data);
|
|
|
|
std::ostream file_data_stream(&file_data_buffer);
|
|
|
|
file_data_stream << file_stream.rdbuf();
|
|
|
|
}
|
|
|
|
|
|
|
|
return xml_helper::xlsx_archives_match(wb_data, file_data);
|
2016-08-06 22:40:17 +08:00
|
|
|
}
|
|
|
|
|
2016-12-30 07:36:29 +08:00
|
|
|
void test_produce_empty()
|
2016-08-14 02:45:26 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
xlnt::workbook wb = xlnt::workbook::empty();
|
2017-01-21 23:12:08 +08:00
|
|
|
TS_ASSERT(workbook_matches_file(wb, path_helper::data_directory("9_default-excel.xlsx")));
|
2016-08-16 12:23:49 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 19:34:18 +08:00
|
|
|
void test_produce_simple_excel()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
xlnt::workbook wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
auto ws = wb.active_sheet();
|
2016-08-18 19:34:18 +08:00
|
|
|
|
|
|
|
auto bold_font = xlnt::font().bold(true);
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A1").value("Type");
|
|
|
|
ws.cell("A1").font(bold_font);
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("B1").value("Value");
|
|
|
|
ws.cell("B1").font(bold_font);
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A2").value("null");
|
|
|
|
ws.cell("B2").value(nullptr);
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A3").value("bool (true)");
|
|
|
|
ws.cell("B3").value(true);
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A4").value("bool (false)");
|
|
|
|
ws.cell("B4").value(false);
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A5").value("number (std::int8_t)");
|
|
|
|
ws.cell("B5").value(std::numeric_limits<std::int8_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A6").value("number (std::uint8_t)");
|
|
|
|
ws.cell("B6").value(std::numeric_limits<std::uint8_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A7").value("number (std::uint16_t)");
|
|
|
|
ws.cell("B7").value(std::numeric_limits<std::int16_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A8").value("number (std::uint16_t)");
|
|
|
|
ws.cell("B8").value(std::numeric_limits<std::uint16_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A9").value("number (std::uint32_t)");
|
|
|
|
ws.cell("B9").value(std::numeric_limits<std::int32_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A10").value("number (std::uint32_t)");
|
|
|
|
ws.cell("B10").value(std::numeric_limits<std::uint32_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A11").value("number (std::uint64_t)");
|
|
|
|
ws.cell("B11").value(std::numeric_limits<std::int64_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A12").value("number (std::uint64_t)");
|
|
|
|
ws.cell("B12").value(std::numeric_limits<std::uint64_t>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A13").value("number (float)");
|
|
|
|
ws.cell("B13").value(std::numeric_limits<float>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A14").value("number (double)");
|
|
|
|
ws.cell("B14").value(std::numeric_limits<double>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A15").value("number (long double)");
|
|
|
|
ws.cell("B15").value(std::numeric_limits<long double>::max());
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A16").value("text (char *)");
|
|
|
|
ws.cell("B16").value("string");
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A17").value("text (std::string)");
|
|
|
|
ws.cell("B17").value(std::string("string"));
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A18").value("date");
|
|
|
|
ws.cell("B18").value(xlnt::date(2016, 2, 3));
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A19").value("time");
|
|
|
|
ws.cell("B19").value(xlnt::time(1, 2, 3, 4));
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A20").value("datetime");
|
|
|
|
ws.cell("B20").value(xlnt::datetime(2016, 2, 3, 1, 2, 3, 4));
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
ws.cell("A21").value("timedelta");
|
|
|
|
ws.cell("B21").value(xlnt::timedelta(1, 2, 3, 4, 5));
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-09-08 06:48:08 +08:00
|
|
|
ws.freeze_panes("B2");
|
2016-08-18 19:34:18 +08:00
|
|
|
|
2016-10-26 08:21:58 +08:00
|
|
|
std::vector<std::uint8_t> temp_buffer;
|
|
|
|
wb.save(temp_buffer);
|
|
|
|
TS_ASSERT(!temp_buffer.empty());
|
2016-08-18 19:34:18 +08:00
|
|
|
}
|
2016-10-23 01:53:44 +08:00
|
|
|
|
|
|
|
void test_save_after_sheet_deletion()
|
|
|
|
{
|
|
|
|
xlnt::workbook workbook;
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(workbook.sheet_titles().size(), 1);
|
2016-10-23 01:53:44 +08:00
|
|
|
|
|
|
|
auto sheet = workbook.create_sheet();
|
2016-12-02 21:37:50 +08:00
|
|
|
sheet.title("XXX1");
|
|
|
|
TS_ASSERT_EQUALS(workbook.sheet_titles().size(), 2);
|
2016-10-23 01:53:44 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
workbook.remove_sheet(workbook.sheet_by_title("XXX1"));
|
|
|
|
TS_ASSERT_EQUALS(workbook.sheet_titles().size(), 1);
|
2016-10-23 01:53:44 +08:00
|
|
|
|
2016-10-26 08:21:58 +08:00
|
|
|
std::vector<std::uint8_t> temp_buffer;
|
|
|
|
TS_ASSERT_THROWS_NOTHING(workbook.save(temp_buffer));
|
|
|
|
TS_ASSERT(!temp_buffer.empty());
|
2016-10-23 01:53:44 +08:00
|
|
|
}
|
2016-10-30 04:31:30 +08:00
|
|
|
|
|
|
|
void test_write_comments()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
auto sheet1 = wb.active_sheet();
|
2016-12-23 19:51:30 +08:00
|
|
|
auto comment_font = xlnt::font().bold(true).size(10).color(xlnt::indexed_color(81)).name("Calibri");
|
2016-10-30 04:31:30 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
sheet1.cell("A1").value("Sheet1!A1");
|
2016-12-23 19:51:30 +08:00
|
|
|
sheet1.cell("A1").comment("Sheet1 comment", comment_font, "Microsoft Office User");
|
2016-11-28 02:12:38 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
sheet1.cell("A2").value("Sheet1!A2");
|
2016-12-23 19:51:30 +08:00
|
|
|
sheet1.cell("A2").comment("Sheet1 comment2", comment_font, "Microsoft Office User");
|
2016-11-28 02:12:38 +08:00
|
|
|
|
2016-10-30 04:31:30 +08:00
|
|
|
auto sheet2 = wb.create_sheet();
|
2016-12-02 21:37:50 +08:00
|
|
|
sheet2.cell("A1").value("Sheet2!A1");
|
2016-12-23 19:51:30 +08:00
|
|
|
sheet2.cell("A2").comment("Sheet2 comment", comment_font, "Microsoft Office User");
|
2016-10-30 04:31:30 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
sheet2.cell("A2").value("Sheet2!A2");
|
2016-12-23 19:51:30 +08:00
|
|
|
sheet2.cell("A2").comment("Sheet2 comment2", comment_font, "Microsoft Office User");
|
2016-10-30 04:31:30 +08:00
|
|
|
|
2017-01-21 23:12:08 +08:00
|
|
|
TS_ASSERT(workbook_matches_file(wb, path_helper::data_directory("15_basic_comments.xlsx")));
|
2016-10-30 04:31:30 +08:00
|
|
|
}
|
2017-01-03 08:12:23 +08:00
|
|
|
|
|
|
|
void test_save_after_clear_all_formulae()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2017-01-21 23:12:08 +08:00
|
|
|
wb.load(path_helper::data_directory("22_formulae.xlsx"));
|
2017-01-03 08:12:23 +08:00
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
TS_ASSERT(ws.cell("A1").has_formula());
|
|
|
|
TS_ASSERT_EQUALS(ws.cell("A1").formula(), "A2*A3");
|
|
|
|
ws.cell("A1").clear_formula();
|
|
|
|
wb.save("clear_formulae.xlsx");
|
|
|
|
}
|
2016-08-06 22:40:17 +08:00
|
|
|
};
|