2014-05-09 03:32:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
#include <fstream>
|
2014-05-09 03:32:12 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <detail/excel_serializer.hpp>
|
|
|
|
#include <detail/manifest_serializer.hpp>
|
|
|
|
#include <detail/relationship_serializer.hpp>
|
2016-07-20 08:36:12 +08:00
|
|
|
#include <detail/shared_strings_serializer.hpp>
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <detail/workbook_serializer.hpp>
|
2015-11-03 22:06:01 +08:00
|
|
|
#include <helpers/path_helper.hpp>
|
2016-05-15 23:51:32 +08:00
|
|
|
#include <xlnt/cell/text.hpp>
|
|
|
|
#include <xlnt/cell/text_run.hpp>
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <xlnt/packaging/manifest.hpp>
|
2016-05-15 23:51:32 +08:00
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
class test_read : public CxxTest::TestSuite
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-07-19 03:18:23 +08:00
|
|
|
|
|
|
|
xlnt::workbook standard_workbook()
|
|
|
|
{
|
2015-10-30 11:16:31 +08:00
|
|
|
xlnt::workbook wb;
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/empty.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
wb.load(path);
|
|
|
|
|
|
|
|
return wb;
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
2016-05-17 07:31:44 +08:00
|
|
|
|
2014-05-09 03:32:12 +08:00
|
|
|
void test_read_standard_workbook()
|
|
|
|
{
|
2016-07-22 10:14:00 +08:00
|
|
|
TS_ASSERT_THROWS_NOTHING(standard_workbook());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_standard_workbook_from_fileobj()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/empty.xlsx");
|
2015-11-11 07:58:54 +08:00
|
|
|
std::ifstream fo(path, std::ios::binary);
|
2016-07-18 02:59:11 +08:00
|
|
|
|
2015-10-30 11:16:31 +08:00
|
|
|
xlnt::workbook wb;
|
2016-07-18 02:59:11 +08:00
|
|
|
TS_ASSERT(wb.load(fo));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_worksheet()
|
|
|
|
{
|
2014-07-20 04:59:05 +08:00
|
|
|
auto wb = standard_workbook();
|
2014-05-19 09:29:19 +08:00
|
|
|
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
TS_ASSERT_DIFFERS(sheet2, nullptr);
|
2015-11-11 07:58:54 +08:00
|
|
|
TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5").get_value<std::string>());
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT_EQUALS(18, sheet2.get_cell("D18").get_value<int>());
|
|
|
|
TS_ASSERT_EQUALS(true, sheet2.get_cell("G9").get_value<bool>());
|
|
|
|
TS_ASSERT_EQUALS(false, sheet2.get_cell("G10").get_value<bool>());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_nostring_workbook()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/empty-no-string.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
2016-07-22 10:14:00 +08:00
|
|
|
TS_ASSERT_THROWS_NOTHING(wb.load(path));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_empty_file()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/null_file.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
2016-07-22 11:04:36 +08:00
|
|
|
TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_empty_archive()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/null_archive.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
2016-07-22 11:04:36 +08:00
|
|
|
TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
|
|
|
void test_read_workbook_with_no_properties()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/empty_with_no_properties.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
|
|
|
serializer.load_workbook(path);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
xlnt::workbook workbook_with_styles()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/empty-with-styles.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
2016-07-18 02:59:11 +08:00
|
|
|
wb.load(path);
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
return wb;
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 03:18:23 +08:00
|
|
|
void test_read_workbook_with_styles_general()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = workbook_with_styles();
|
|
|
|
auto ws = wb["Sheet1"];
|
2015-10-24 02:42:36 +08:00
|
|
|
auto format = ws.get_cell("A1").get_number_format();
|
|
|
|
auto expected = xlnt::number_format::general();
|
|
|
|
TS_ASSERT_EQUALS(format, expected);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
|
|
|
void test_read_workbook_with_styles_date()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = workbook_with_styles();
|
|
|
|
auto ws = wb["Sheet1"];
|
2015-10-24 02:42:36 +08:00
|
|
|
auto format = ws.get_cell("A2").get_number_format();
|
|
|
|
auto expected = xlnt::number_format::date_xlsx14();
|
|
|
|
TS_ASSERT_EQUALS(format, expected);
|
2014-05-13 01:42:28 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
|
|
|
void test_read_workbook_with_styles_number()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = workbook_with_styles();
|
|
|
|
auto ws = wb["Sheet1"];
|
2015-10-24 02:42:36 +08:00
|
|
|
auto code = ws.get_cell("A3").get_number_format();
|
|
|
|
auto expected = xlnt::number_format::number_00();
|
2014-07-24 08:51:28 +08:00
|
|
|
TS_ASSERT_EQUALS(code, expected);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
|
|
|
void test_read_workbook_with_styles_time()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = workbook_with_styles();
|
|
|
|
auto ws = wb["Sheet1"];
|
2015-10-24 02:42:36 +08:00
|
|
|
auto code = ws.get_cell("A4").get_number_format();
|
|
|
|
auto expected = xlnt::number_format::date_time3();
|
2014-07-24 08:51:28 +08:00
|
|
|
TS_ASSERT_EQUALS(code, expected);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
|
|
|
void test_read_workbook_with_styles_percentage()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = workbook_with_styles();
|
|
|
|
auto ws = wb["Sheet1"];
|
2015-10-24 02:42:36 +08:00
|
|
|
auto code = ws.get_cell("A5").get_number_format();
|
|
|
|
auto expected = xlnt::number_format::percentage_00();
|
2014-07-24 08:51:28 +08:00
|
|
|
TS_ASSERT_EQUALS(code, expected);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
2016-04-22 14:30:32 +08:00
|
|
|
void test_read_charset_excel()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/charset-excel.xlsx");
|
2016-04-22 14:30:32 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
|
|
|
serializer.load_workbook(path);
|
|
|
|
|
|
|
|
auto ws = wb["Sheet1"];
|
|
|
|
auto val = ws.get_cell("A1").get_value<std::string>();
|
|
|
|
TS_ASSERT_EQUALS(val, "Direnç");
|
|
|
|
}
|
|
|
|
|
2016-04-26 11:28:57 +08:00
|
|
|
void test_read_shared_strings_max_range()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/shared_strings-max_range.xlsx");
|
2016-04-26 11:28:57 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
|
|
|
serializer.load_workbook(path);
|
|
|
|
|
|
|
|
auto ws = wb["Sheet1"];
|
|
|
|
auto val = ws.get_cell("A1").get_value<std::string>();
|
|
|
|
TS_ASSERT_EQUALS(val, "Donald");
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:24:21 +08:00
|
|
|
void test_read_shared_strings_multiple_r_nodes()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/shared_strings-multiple_r_nodes.xlsx");
|
2016-05-14 16:24:21 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
|
|
|
serializer.load_workbook(path);
|
|
|
|
|
|
|
|
auto ws = wb["Sheet1"];
|
|
|
|
auto val = ws.get_cell("A1").get_value<std::string>();
|
|
|
|
TS_ASSERT_EQUALS(val, "abcdef");
|
|
|
|
}
|
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
xlnt::workbook date_mac_1904()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/date_1904.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
2015-11-03 03:22:13 +08:00
|
|
|
wb.load(path);
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
return wb;
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
xlnt::workbook date_std_1900()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/date_1900.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
2015-11-03 03:22:13 +08:00
|
|
|
wb.load(path);
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
return wb;
|
2014-05-13 01:42:28 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
2014-05-09 03:32:12 +08:00
|
|
|
void test_read_win_base_date()
|
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = date_std_1900();
|
2014-07-20 04:59:05 +08:00
|
|
|
TS_ASSERT_EQUALS(wb.get_properties().excel_base_date, xlnt::calendar::windows_1900);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
|
2014-05-09 03:32:12 +08:00
|
|
|
void test_read_mac_base_date()
|
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = date_mac_1904();
|
2014-07-20 04:59:05 +08:00
|
|
|
TS_ASSERT_EQUALS(wb.get_properties().excel_base_date, xlnt::calendar::mac_1904);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 03:18:23 +08:00
|
|
|
void test_read_date_style_win()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = date_std_1900();
|
|
|
|
auto ws = wb["Sheet1"];
|
2015-10-24 02:42:36 +08:00
|
|
|
TS_ASSERT_EQUALS(ws.get_cell("A1").get_number_format(), xlnt::number_format::date_xlsx14());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-19 03:18:23 +08:00
|
|
|
void test_read_date_style_mac()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-07-19 03:18:23 +08:00
|
|
|
auto wb = date_mac_1904();
|
|
|
|
auto ws = wb["Sheet1"];
|
2015-10-24 02:42:36 +08:00
|
|
|
TS_ASSERT_EQUALS(ws.get_cell("A1").get_number_format(), xlnt::number_format::date_xlsx14());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
void test_read_compare_mac_win_dates()
|
2014-07-19 03:18:23 +08:00
|
|
|
{
|
|
|
|
auto wb_mac = date_mac_1904();
|
|
|
|
auto ws_mac = wb_mac["Sheet1"];
|
2014-07-20 04:59:05 +08:00
|
|
|
auto wb_win = date_std_1900();
|
2014-07-19 03:18:23 +08:00
|
|
|
auto ws_win = wb_win["Sheet1"];
|
|
|
|
xlnt::datetime dt(2011, 10, 31);
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT_EQUALS(ws_mac.get_cell("A1").get_value<xlnt::datetime>(), dt);
|
|
|
|
TS_ASSERT_EQUALS(ws_win.get_cell("A1").get_value<xlnt::datetime>(), dt);
|
|
|
|
TS_ASSERT_EQUALS(ws_mac.get_cell("A1").get_value<xlnt::datetime>(), ws_win.get_cell("A1").get_value<xlnt::datetime>());
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_repair_central_directory()
|
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string data_a = "foobarbaz" + xlnt::excel_serializer::central_directory_signature();
|
|
|
|
std::string data_b = "bazbarfoo12345678901234567890";
|
2014-07-19 03:18:23 +08:00
|
|
|
|
2015-10-30 11:16:31 +08:00
|
|
|
auto f = xlnt::excel_serializer::repair_central_directory(data_a + data_b);
|
2014-07-19 03:18:23 +08:00
|
|
|
TS_ASSERT_EQUALS(f, data_a + data_b.substr(0, 18));
|
|
|
|
|
2015-10-30 11:16:31 +08:00
|
|
|
f = xlnt::excel_serializer::repair_central_directory(data_b);
|
2014-07-19 03:18:23 +08:00
|
|
|
TS_ASSERT_EQUALS(f, data_b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_no_theme()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/libreoffice_nrt.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
2016-07-22 10:14:00 +08:00
|
|
|
TS_ASSERT_THROWS_NOTHING(wb.load(path));
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
|
2014-07-26 04:39:25 +08:00
|
|
|
void _test_read_complex_formulae()
|
2014-07-19 03:18:23 +08:00
|
|
|
{
|
2014-07-25 05:31:46 +08:00
|
|
|
/*
|
|
|
|
auto path = PathHelper::GetDataDirectory("/reader/formulae.xlsx");
|
|
|
|
auto wb = xlnt::reader::load_workbook(path);
|
|
|
|
auto ws = wb.get_active_sheet();
|
|
|
|
|
|
|
|
// Test normal forumlae
|
|
|
|
TS_ASSERT(!ws.get_cell("A1").has_formula());
|
|
|
|
TS_ASSERT(!ws.get_cell("A2").has_formula());
|
|
|
|
TS_ASSERT(ws.get_cell("A3").has_formula());
|
|
|
|
TS_ASSERT(ws.get_formula_attributes().find("A3") == ws.get_formula_attributes().end());
|
|
|
|
TS_ASSERT(ws.get_cell("A3").get_formula() == "12345");
|
|
|
|
TS_ASSERT(ws.get_cell("A4").has_formula());
|
|
|
|
TS_ASSERT(ws.get_formula_attributes().find("A3") == ws.get_formula_attributes().end());
|
|
|
|
ws.get_cell("A4").set_formula("A2+A3");
|
|
|
|
TS_ASSERT(ws.get_cell("A5").has_formula());
|
|
|
|
TS_ASSERT(ws.get_formula_attributes().find("A5") == ws.get_formula_attributes().end());
|
|
|
|
ws.get_cell("A5").set_formula("SUM(A2:A4)");
|
|
|
|
|
|
|
|
// Test unicode
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string expected = "=IF(ISBLANK(B16), \"D\xFCsseldorf\", B16)";
|
2014-07-25 05:31:46 +08:00
|
|
|
TS_ASSERT(ws.get_cell("A16").get_formula() == expected);
|
|
|
|
|
|
|
|
// Test shared forumlae
|
|
|
|
TS_ASSERT(ws.get_cell("B7").get_data_type() == "f");
|
|
|
|
TS_ASSERT(ws.formula_attributes["B7"]["t"] == "shared");
|
|
|
|
TS_ASSERT(ws.formula_attributes["B7"]["si"] == "0");
|
|
|
|
TS_ASSERT(ws.formula_attributes["B7"]["ref"] == "B7:E7");
|
|
|
|
TS_ASSERT(ws.get_cell("B7").value == "=B4*2");
|
|
|
|
TS_ASSERT(ws.get_cell("C7").get_data_type() == "f");
|
|
|
|
TS_ASSERT(ws.formula_attributes["C7"]["t"] == "shared");
|
|
|
|
TS_ASSERT(ws.formula_attributes["C7"]["si"] == "0");
|
|
|
|
TS_ASSERT("ref" not in ws.formula_attributes["C7"]);
|
|
|
|
TS_ASSERT(ws.get_cell("C7").value == "=");
|
|
|
|
TS_ASSERT(ws.get_cell("D7").get_data_type() == "f");
|
|
|
|
TS_ASSERT(ws.formula_attributes["D7"]["t"] == "shared");
|
|
|
|
TS_ASSERT(ws.formula_attributes["D7"]["si"] == "0");
|
|
|
|
TS_ASSERT("ref" not in ws.formula_attributes["D7"]);
|
|
|
|
TS_ASSERT(ws.get_cell("D7").value == "=");
|
|
|
|
TS_ASSERT(ws.get_cell("E7").get_data_type() == "f");
|
|
|
|
TS_ASSERT(ws.formula_attributes["E7"]["t"] == "shared");
|
|
|
|
TS_ASSERT(ws.formula_attributes["E7"]["si"] == "0");
|
|
|
|
TS_ASSERT("ref" not in ws.formula_attributes["E7"]);
|
|
|
|
TS_ASSERT(ws.get_cell("E7").value == "=");
|
|
|
|
|
|
|
|
// Test array forumlae
|
|
|
|
TS_ASSERT(ws.get_cell("C10").get_data_type() == "f");
|
|
|
|
TS_ASSERT("ref" not in ws.formula_attributes["C10"]["ref"]);
|
|
|
|
TS_ASSERT(ws.formula_attributes["C10"]["t"] == "array");
|
|
|
|
TS_ASSERT("si" not in ws.formula_attributes["C10"]);
|
|
|
|
TS_ASSERT(ws.formula_attributes["C10"]["ref"] == "C10:C14");
|
|
|
|
TS_ASSERT(ws.get_cell("C10").value == "=SUM(A10:A14*B10:B14)");
|
|
|
|
TS_ASSERT(ws.get_cell("C11").get_data_type() != "f");
|
|
|
|
*/
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_data_only()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/formulae.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
2016-07-18 02:59:11 +08:00
|
|
|
xlnt::workbook wb;
|
2015-10-30 11:16:31 +08:00
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
|
|
|
serializer.load_workbook(path, false, true);
|
|
|
|
|
2014-07-25 05:31:46 +08:00
|
|
|
auto ws = wb.get_active_sheet();
|
|
|
|
|
|
|
|
TS_ASSERT(ws.get_formula_attributes().empty());
|
2016-05-12 07:24:53 +08:00
|
|
|
TS_ASSERT(ws.get_workbook().get_data_only());
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(ws.get_cell("A2").get_data_type() == xlnt::cell::type::numeric);
|
|
|
|
TS_ASSERT(ws.get_cell("A2").get_value<int>() == 12345);
|
2014-07-25 05:31:46 +08:00
|
|
|
TS_ASSERT(!ws.get_cell("A2").has_formula());
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(ws.get_cell("A3").get_data_type() == xlnt::cell::type::numeric);
|
|
|
|
TS_ASSERT(ws.get_cell("A3").get_value<int>() == 12345);
|
2014-07-25 05:31:46 +08:00
|
|
|
TS_ASSERT(!ws.get_cell("A3").has_formula());
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(ws.get_cell("A4").get_data_type() == xlnt::cell::type::numeric);
|
|
|
|
TS_ASSERT(ws.get_cell("A4").get_value<int>() == 24690);
|
2014-07-25 05:31:46 +08:00
|
|
|
TS_ASSERT(!ws.get_cell("A4").has_formula());
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(ws.get_cell("A5").get_data_type() == xlnt::cell::type::numeric);
|
|
|
|
TS_ASSERT(ws.get_cell("A5").get_value<int>() == 49380);
|
2014-07-25 05:31:46 +08:00
|
|
|
TS_ASSERT(!ws.get_cell("A5").has_formula());
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
2014-07-25 05:31:46 +08:00
|
|
|
|
2014-07-19 03:18:23 +08:00
|
|
|
void test_read_rels()
|
|
|
|
{
|
2014-07-25 05:31:46 +08:00
|
|
|
{
|
|
|
|
std::vector<xlnt::relationship> expected =
|
|
|
|
{
|
2015-11-02 12:52:19 +08:00
|
|
|
{xlnt::relationship::type::theme, "rId3", "theme/theme1.xml"},
|
|
|
|
{xlnt::relationship::type::worksheet, "rId2", "worksheets/sheet1.xml"},
|
|
|
|
{xlnt::relationship::type::chartsheet, "rId1", "chartsheets/sheet1.xml"},
|
|
|
|
{xlnt::relationship::type::shared_strings, "rId5", "sharedStrings.xml"},
|
|
|
|
{xlnt::relationship::type::styles, "rId4", "styles.xml"}
|
2014-07-25 05:31:46 +08:00
|
|
|
};
|
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/bug137.xlsx");
|
2014-08-01 21:44:21 +08:00
|
|
|
xlnt::zip_file archive(path);
|
2015-11-04 07:26:33 +08:00
|
|
|
xlnt::relationship_serializer serializer(archive);
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(serializer.read_relationships("xl/workbook.xml"), expected);
|
2014-07-25 05:31:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<xlnt::relationship> expected =
|
|
|
|
{
|
|
|
|
{xlnt::relationship::type::custom_xml, "rId8", "../customXml/item3.xml"},
|
2015-11-02 12:52:19 +08:00
|
|
|
{xlnt::relationship::type::worksheet, "rId3", "/xl/worksheets/sheet.xml"},
|
2014-07-25 05:31:46 +08:00
|
|
|
{xlnt::relationship::type::custom_xml, "rId7", "../customXml/item2.xml"},
|
2015-11-02 12:52:19 +08:00
|
|
|
{xlnt::relationship::type::worksheet, "rId2", "/xl/worksheets/sheet2.xml"},
|
|
|
|
{xlnt::relationship::type::worksheet, "rId1", "/xl/worksheets/sheet3.xml"},
|
2014-07-25 05:31:46 +08:00
|
|
|
{xlnt::relationship::type::custom_xml, "rId6", "../customXml/item1.xml"},
|
2015-11-02 12:52:19 +08:00
|
|
|
{xlnt::relationship::type::styles, "rId5", "/xl/styles.xml"},
|
|
|
|
{xlnt::relationship::type::theme, "rId4", "/xl/theme/theme.xml"}
|
2014-07-25 05:31:46 +08:00
|
|
|
};
|
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/bug304.xlsx");
|
2014-08-01 21:44:21 +08:00
|
|
|
xlnt::zip_file archive(path);
|
2015-11-04 07:26:33 +08:00
|
|
|
xlnt::relationship_serializer serializer(archive);
|
2014-07-25 05:31:46 +08:00
|
|
|
|
2015-11-04 07:26:33 +08:00
|
|
|
TS_ASSERT_EQUALS(serializer.read_relationships("xl/workbook.xml"), expected);
|
2014-07-25 05:31:46 +08:00
|
|
|
}
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_content_types()
|
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
std::vector<std::pair<std::string, std::string>> expected =
|
2014-07-19 03:18:23 +08:00
|
|
|
{
|
|
|
|
{"/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"}
|
|
|
|
};
|
2014-06-13 05:04:37 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/contains_chartsheets.xlsx");
|
2014-07-20 04:59:05 +08:00
|
|
|
|
2015-10-30 11:16:31 +08:00
|
|
|
xlnt::workbook wb;
|
2015-11-02 12:52:19 +08:00
|
|
|
wb.load(path);
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
auto &result = wb.get_manifest().get_override_types();
|
|
|
|
|
|
|
|
if(result.size() != expected.size())
|
|
|
|
{
|
|
|
|
TS_ASSERT_EQUALS(result.size(), expected.size());
|
|
|
|
return;
|
|
|
|
}
|
2014-07-24 04:00:09 +08:00
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
for(std::size_t i = 0; i < expected.size(); i++)
|
|
|
|
{
|
2015-11-02 12:52:19 +08:00
|
|
|
TS_ASSERT(wb.get_manifest().has_override_type(expected[i].first));
|
|
|
|
TS_ASSERT_EQUALS(wb.get_manifest().get_override_type(expected[i].first), expected[i].second);
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_guess_types()
|
|
|
|
{
|
|
|
|
bool guess;
|
2015-10-14 01:56:07 +08:00
|
|
|
xlnt::cell::type dtype;
|
|
|
|
std::vector<std::pair<bool, xlnt::cell::type>> test_cases = {{true, xlnt::cell::type::numeric}, {false, xlnt::cell::type::string}};
|
2014-07-19 03:18:23 +08:00
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
for(const auto &expected : test_cases)
|
2014-07-19 03:18:23 +08:00
|
|
|
{
|
|
|
|
std::tie(guess, dtype) = expected;
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/guess_types.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
|
|
|
serializer.load_workbook(path, guess);
|
|
|
|
|
2014-07-19 03:18:23 +08:00
|
|
|
auto ws = wb.get_active_sheet();
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(ws.get_cell("D2").get_data_type() == dtype);
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_autofilter()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/reader/bug275.xlsx");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
|
|
|
serializer.load_workbook(path);
|
|
|
|
|
2014-07-19 03:18:23 +08:00
|
|
|
auto ws = wb.get_active_sheet();
|
2014-07-20 04:59:05 +08:00
|
|
|
TS_ASSERT_EQUALS(ws.get_auto_filter().to_string(), "A1:B6");
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_bad_formats_xlsb()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/a.xlsb");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
2016-07-22 11:04:36 +08:00
|
|
|
TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error);
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_bad_formats_xls()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/a.xls");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
2016-07-22 11:04:36 +08:00
|
|
|
TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error);
|
2014-07-19 03:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_bad_formats_no()
|
|
|
|
{
|
2016-07-21 07:04:44 +08:00
|
|
|
auto path = path_helper::get_data_directory("/genuine/a.no-format");
|
2015-10-30 11:16:31 +08:00
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::excel_serializer serializer(wb);
|
|
|
|
|
2016-07-22 11:04:36 +08:00
|
|
|
TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error);
|
2014-06-13 05:04:37 +08:00
|
|
|
}
|
2016-07-20 08:36:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
void test_read_shared_strings_with_runs()
|
|
|
|
{
|
|
|
|
std::string source =
|
|
|
|
"<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>";
|
|
|
|
pugi::xml_document xml;
|
|
|
|
xml.load(source.c_str());
|
|
|
|
|
|
|
|
std::vector<xlnt::text> strings;
|
2016-07-20 09:23:11 +08:00
|
|
|
xlnt::shared_strings_serializer::read_shared_strings(xml, strings);
|
2016-07-20 08:36:12 +08:00
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(strings.size(), 1);
|
|
|
|
TS_ASSERT_EQUALS(strings.front().get_runs().size(), 1);
|
|
|
|
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_size(), 13);
|
|
|
|
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_color(), "color");
|
|
|
|
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_font(), "font");
|
|
|
|
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_family(), 12);
|
|
|
|
TS_ASSERT_EQUALS(strings.front().get_runs().front().get_scheme(), "scheme");
|
|
|
|
|
|
|
|
std::string source_bad =
|
|
|
|
"<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"2\" uniqueCount=\"2\">"
|
|
|
|
" <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>";
|
|
|
|
pugi::xml_document xml_bad;
|
|
|
|
xml_bad.load(source_bad.c_str());
|
|
|
|
|
2016-07-20 09:23:11 +08:00
|
|
|
TS_ASSERT_THROWS(xlnt::shared_strings_serializer::read_shared_strings(xml_bad, strings), std::runtime_error);
|
2016-07-20 08:36:12 +08:00
|
|
|
}
|
2016-07-22 08:44:00 +08:00
|
|
|
|
|
|
|
void test_read_inlinestr()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.load(path_helper::get_data_directory("/genuine/empty.xlsx"));
|
|
|
|
TS_ASSERT_EQUALS(wb.get_sheet_by_index(0).get_cell("A1").get_value<std::string>(), "This is cell A1 in Sheet 1");
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
};
|