2015-10-14 12:03:48 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2016-07-20 08:27:14 +08:00
|
|
|
#include <detail/shared_strings_serializer.hpp>
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <detail/workbook_serializer.hpp>
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <helpers/path_helper.hpp>
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2015-10-14 12:03:48 +08:00
|
|
|
|
|
|
|
class test_write_workbook : public CxxTest::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void test_write_auto_filter()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-07-30 05:50:33 +08:00
|
|
|
auto ws = wb.get_active_sheet();
|
2015-10-14 12:03:48 +08:00
|
|
|
ws.get_cell("F42").set_value("hello");
|
2015-11-11 08:47:31 +08:00
|
|
|
ws.auto_filter("A1:F1");
|
2015-10-14 12:03:48 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::workbook_serializer serializer(wb);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_workbook(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory("/writer/expected/workbook_auto_filter.xml"), xml));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hidden_worksheet()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-07-30 05:50:33 +08:00
|
|
|
auto ws = wb.get_active_sheet();
|
2015-11-20 11:54:54 +08:00
|
|
|
ws.set_sheet_state(xlnt::sheet_state::hidden);
|
2015-10-14 12:03:48 +08:00
|
|
|
wb.create_sheet();
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::workbook_serializer serializer(wb);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_workbook(xml);
|
2016-07-30 05:50:33 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string expected_string =
|
2015-10-14 12:03:48 +08:00
|
|
|
"<workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
|
|
|
|
" <workbookPr/>"
|
|
|
|
" <bookViews>"
|
|
|
|
" <workbookView activeTab=\"0\"/>"
|
|
|
|
" </bookViews>"
|
|
|
|
" <sheets>"
|
|
|
|
" <sheet name=\"Sheet\" sheetId=\"1\" state=\"hidden\" r:id=\"rId1\"/>"
|
|
|
|
" <sheet name=\"Sheet1\" sheetId=\"2\" r:id=\"rId2\"/>"
|
|
|
|
" </sheets>"
|
|
|
|
" <definedNames/>"
|
|
|
|
" <calcPr calcId=\"124519\" fullCalcOnLoad=\"1\"/>"
|
|
|
|
"</workbook>";
|
2016-07-30 05:50:33 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document expected;
|
|
|
|
expected.load(expected_string.c_str());
|
2016-07-21 07:04:44 +08:00
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(expected.child("workbook").child("sheets"),
|
|
|
|
xml.child("workbook").child("sheets")));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hidden_single_worksheet()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.get_active_sheet();
|
2015-11-20 11:54:54 +08:00
|
|
|
ws.set_sheet_state(xlnt::sheet_state::hidden);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::workbook_serializer serializer(wb);
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
2016-07-30 06:55:49 +08:00
|
|
|
TS_ASSERT_THROWS(serializer.write_workbook(xml), xlnt::no_visible_worksheets);
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_empty_workbook()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-07-21 07:54:24 +08:00
|
|
|
temporary_file file;
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
2016-07-21 07:54:24 +08:00
|
|
|
serializer.save_workbook(file.get_filename());
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:54:24 +08:00
|
|
|
TS_ASSERT(path_helper::file_exists(file.get_filename()));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_virtual_workbook()
|
|
|
|
{
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::workbook old_wb, new_wb;
|
|
|
|
|
|
|
|
xlnt::excel_serializer serializer(old_wb);
|
|
|
|
std::vector<std::uint8_t> wb_bytes;
|
|
|
|
serializer.save_virtual_workbook(wb_bytes);
|
|
|
|
|
|
|
|
xlnt::excel_serializer deserializer(new_wb);
|
|
|
|
deserializer.load_virtual_workbook(wb_bytes);
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_workbook_rels()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-07-30 05:50:33 +08:00
|
|
|
auto ws = wb.get_active_sheet();
|
|
|
|
ws.get_cell("A1").set_value("string");
|
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::zip_file archive;
|
2015-11-04 07:26:33 +08:00
|
|
|
xlnt::relationship_serializer serializer(archive);
|
|
|
|
serializer.write_relationships(wb.get_relationships(), "xl/workbook.xml");
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document observed;
|
|
|
|
observed.load(archive.read("xl/_rels/workbook.xml.rels").c_str());
|
2016-07-21 07:04:44 +08:00
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory("/writer/expected/workbook.xml.rels"), observed));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
void test_write_workbook_part()
|
2015-10-14 12:03:48 +08:00
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::workbook_serializer serializer(wb);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_workbook(xml);
|
2016-07-30 05:50:33 +08:00
|
|
|
auto filename = path_helper::get_data_directory("/writer/expected/workbook.xml");
|
2016-07-21 07:04:44 +08:00
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(filename, xml));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_named_range()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-07-30 05:50:33 +08:00
|
|
|
auto ws = wb.get_active_sheet();
|
|
|
|
wb.create_named_range("test_range", ws, "$A$1:$B$5");
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::workbook_serializer serializer(wb);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
2016-07-30 05:50:33 +08:00
|
|
|
auto root = xml.root().append_child("root");
|
|
|
|
serializer.write_named_ranges(root);
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string expected =
|
2015-10-14 12:03:48 +08:00
|
|
|
"<root>"
|
|
|
|
"<s:definedName xmlns:s=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" name=\"test_range\">'Sheet'!$A$1:$B$5</s:definedName>"
|
|
|
|
"</root>";
|
2016-07-21 07:04:44 +08:00
|
|
|
|
|
|
|
TS_ASSERT(xml_helper::compare_xml(expected, xml));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_workbook_code_name()
|
|
|
|
{
|
|
|
|
// with open(tmpl, "rb") as expected:
|
|
|
|
// TS_ASSERT(read_workbook_code_name(expected.read()) == code_name
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_workbook_code_name()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.set_code_name("MyWB");
|
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::workbook_serializer serializer(wb);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_workbook(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string expected =
|
2015-10-14 12:03:48 +08:00
|
|
|
"<workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
|
|
|
|
" <workbookPr codeName=\"MyWB\"/>"
|
|
|
|
" <bookViews>"
|
|
|
|
" <workbookView activeTab=\"0\"/>"
|
|
|
|
" </bookViews>"
|
|
|
|
" <sheets>"
|
|
|
|
" <sheet name=\"Sheet\" sheetId=\"1\" r:id=\"rId1\"/>"
|
|
|
|
" </sheets>"
|
|
|
|
" <definedNames/>"
|
|
|
|
" <calcPr calcId=\"124519\" fullCalcOnLoad=\"1\"/>"
|
|
|
|
"</workbook>";
|
2016-07-30 05:50:33 +08:00
|
|
|
pugi::xml_document expected_xml;
|
|
|
|
expected_xml.load(expected.c_str());
|
2016-07-21 07:04:44 +08:00
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(expected_xml.child("workbook").child("workbookPr"),
|
|
|
|
xml.child("workbook").child("workbookPr")));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_root_rels()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::zip_file archive;
|
2015-11-04 07:26:33 +08:00
|
|
|
xlnt::relationship_serializer serializer(archive);
|
|
|
|
serializer.write_relationships(wb.get_root_relationships(), "");
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document observed;
|
|
|
|
observed.load(archive.read("_rels/.rels").c_str());
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string expected =
|
2015-10-14 12:03:48 +08:00
|
|
|
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"
|
|
|
|
" <Relationship Id=\"rId1\" Target=\"xl/workbook.xml\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\"/>"
|
|
|
|
" <Relationship Id=\"rId2\" Target=\"docProps/core.xml\" Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\"/>"
|
|
|
|
" <Relationship Id=\"rId3\" Target=\"docProps/app.xml\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\"/>"
|
|
|
|
"</Relationships>";
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(expected, observed));
|
2015-10-14 12:03:48 +08:00
|
|
|
}
|
2016-07-20 08:27:14 +08:00
|
|
|
|
|
|
|
void test_write_shared_strings_with_runs()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.get_active_sheet();
|
|
|
|
auto cell = ws.get_cell("A1");
|
|
|
|
xlnt::text_run run;
|
|
|
|
run.set_color("color");
|
|
|
|
run.set_family(12);
|
|
|
|
run.set_font("font");
|
|
|
|
run.set_scheme("scheme");
|
|
|
|
run.set_size(13);
|
|
|
|
run.set_string("string");
|
|
|
|
xlnt::text text;
|
|
|
|
text.add_run(run);
|
|
|
|
cell.set_value(text);
|
2016-07-20 09:23:11 +08:00
|
|
|
|
2016-07-20 08:27:14 +08:00
|
|
|
pugi::xml_document xml;
|
2016-07-20 09:23:11 +08:00
|
|
|
xlnt::shared_strings_serializer::write_shared_strings(wb.get_shared_strings(), xml);
|
2016-07-20 08:27:14 +08:00
|
|
|
|
|
|
|
std::string expected =
|
|
|
|
"<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"1\" uniqueCount=\"1\">"
|
|
|
|
" <si>"
|
|
|
|
" <r>"
|
|
|
|
" <rPr>"
|
|
|
|
" <sz val=\"13\"/>"
|
|
|
|
" <color rgb=\"color\"/>"
|
|
|
|
" <rFont val=\"font\"/>"
|
|
|
|
" <family val=\"12\"/>"
|
|
|
|
" <scheme val=\"scheme\"/>"
|
|
|
|
" </rPr>"
|
|
|
|
" <t>string</t>"
|
|
|
|
" </r>"
|
|
|
|
" </si>"
|
|
|
|
"</sst>";
|
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(expected, xml));
|
2016-07-20 08:27:14 +08:00
|
|
|
}
|
2016-07-20 08:36:12 +08:00
|
|
|
|
2016-07-25 09:36:40 +08:00
|
|
|
void test_write_worksheet_order()
|
|
|
|
{
|
|
|
|
auto path = path_helper::get_data_directory("/genuine/tab_order.xlsx");
|
|
|
|
|
|
|
|
// Load an original workbook produced by Excel
|
|
|
|
xlnt::workbook wb_src;
|
|
|
|
{
|
|
|
|
xlnt::excel_serializer serializer(wb_src);
|
|
|
|
serializer.load_workbook(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save it to a new file, unmodified
|
|
|
|
temporary_file file;
|
|
|
|
{
|
|
|
|
xlnt::excel_serializer serializer(wb_src);
|
|
|
|
serializer.save_workbook(file.get_filename());
|
|
|
|
TS_ASSERT(path_helper::file_exists(file.get_filename()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load it again
|
|
|
|
xlnt::workbook wb_dst;
|
|
|
|
{
|
|
|
|
xlnt::excel_serializer serializer(wb_dst);
|
|
|
|
serializer.load_workbook(file.get_filename());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the number of worksheets is the same
|
|
|
|
auto count_src = std::distance(wb_src.begin(), wb_src.end());
|
|
|
|
auto count_dst = std::distance(wb_dst.begin(), wb_dst.end());
|
|
|
|
TS_ASSERT(count_src == count_dst);
|
|
|
|
|
|
|
|
// Make sure the title of the first sheet matches
|
|
|
|
auto ws1title_src = wb_src[0].get_title();
|
|
|
|
auto ws1title_dst = wb_dst[0].get_title();
|
|
|
|
TS_ASSERT(ws1title_src.compare(ws1title_dst) == 0);
|
|
|
|
}
|
|
|
|
|
2015-10-14 12:03:48 +08:00
|
|
|
private:
|
|
|
|
xlnt::workbook wb_;
|
|
|
|
};
|