2014-05-09 03:32:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-05-22 07:17:56 +08:00
|
|
|
#include <algorithm>
|
2014-05-09 03:32:12 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
#include <xlnt/xlnt.hpp>
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
class test_workbook : public CxxTest::TestSuite
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void test_get_active_sheet()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
2014-05-13 01:42:28 +08:00
|
|
|
auto active_sheet = wb.get_active_sheet();
|
2014-05-19 09:29:19 +08:00
|
|
|
TS_ASSERT_EQUALS(active_sheet, wb[0]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_sheet()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet(0);
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, wb[0]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_sheet_with_name()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet(0, "LikeThisName");
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, wb[0]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_sheet_readonly()
|
|
|
|
{
|
2014-05-21 22:20:30 +08:00
|
|
|
xlnt::workbook wb(xlnt::optimization::read);
|
2014-05-19 09:29:19 +08:00
|
|
|
TS_ASSERT_THROWS_ANYTHING(wb.create_sheet());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_remove_sheet()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet(0);
|
2014-05-30 08:52:14 +08:00
|
|
|
auto sheet_name = new_sheet.get_title();
|
2014-05-13 01:42:28 +08:00
|
|
|
wb.remove_sheet(new_sheet);
|
2014-05-19 09:29:19 +08:00
|
|
|
for(auto worksheet : wb)
|
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
TS_ASSERT_DIFFERS(sheet_name, worksheet.get_title());
|
2014-05-19 09:29:19 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_sheet_by_name()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
|
|
|
std::string title = "my sheet";
|
|
|
|
new_sheet.set_title(title);
|
|
|
|
auto found_sheet = wb.get_sheet_by_name(title);
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, found_sheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_getitem()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
|
|
|
std::string title = "my sheet";
|
|
|
|
new_sheet.set_title(title);
|
|
|
|
auto found_sheet = wb.get_sheet_by_name(title);
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, found_sheet);
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
TS_ASSERT_EQUALS(wb.get_sheet_by_name("NotThere"), nullptr);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_index2()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet(0);
|
|
|
|
auto sheet_index = wb.get_index(new_sheet);
|
|
|
|
TS_ASSERT_EQUALS(sheet_index, 0);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_sheet_names()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
2014-05-20 08:47:15 +08:00
|
|
|
wb.clear();
|
|
|
|
std::vector<std::string> names = {"NewSheet", "NewSheet1", "NewSheet2", "NewSheet3", "NewSheet4", "NewSheet5"};
|
2014-05-19 09:29:19 +08:00
|
|
|
for(int count = 0; count < names.size(); count++)
|
|
|
|
{
|
|
|
|
wb.create_sheet(names[count]);
|
|
|
|
}
|
|
|
|
auto actual_names = wb.get_sheet_names();
|
|
|
|
std::sort(actual_names.begin(), actual_names.end());
|
|
|
|
std::sort(names.begin(), names.end());
|
|
|
|
for(int count = 0; count < names.size(); count++)
|
2014-05-13 01:42:28 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
TS_ASSERT_EQUALS(actual_names[count], names[count]);
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_active_sheet2()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto active_sheet = wb.get_active_sheet();
|
|
|
|
TS_ASSERT_EQUALS(active_sheet, wb[0]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_sheet2()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet(0);
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, wb[0]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_sheet_with_name2()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet(0, "LikeThisName");
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, wb[0]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_sheet_by_name2()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
|
|
|
std::string title = "my sheet";
|
|
|
|
new_sheet.set_title(title);
|
|
|
|
auto found_sheet = wb.get_sheet_by_name(title);
|
|
|
|
TS_ASSERT_EQUALS(new_sheet, found_sheet);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_index()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet(0);
|
|
|
|
auto sheet_index = wb.get_index(new_sheet);
|
|
|
|
TS_ASSERT_EQUALS(sheet_index, 0);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_add_named_range()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2014-05-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
|
|
|
TS_ASSERT(new_sheet.has_named_range("test_nr"));
|
|
|
|
TS_ASSERT(wb.has_named_range("test_nr"));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_named_range2()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2014-05-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
|
|
|
auto found_range = wb.get_named_range("test_nr");
|
|
|
|
auto expected_range = new_sheet.get_range("A1");
|
|
|
|
TS_ASSERT_EQUALS(expected_range, found_range);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
void test_remove_named_range()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2014-05-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
|
|
|
wb.remove_named_range("test_nr");
|
|
|
|
TS_ASSERT(!new_sheet.has_named_range("test_nr"));
|
|
|
|
TS_ASSERT(!wb.has_named_range("test_nr"));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
void test_add_local_named_range()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-20 08:47:15 +08:00
|
|
|
TemporaryFile temp_file;
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2014-05-21 22:20:30 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
2014-05-20 08:47:15 +08:00
|
|
|
auto dest_filename = temp_file.GetFilename();
|
2014-05-19 09:29:19 +08:00
|
|
|
wb.save(dest_filename);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
void test_write_regular_date()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
/*make_tmpdir();
|
|
|
|
auto today = datetime.datetime(2010, 1, 18, 14, 15, 20, 1600);
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto sheet = book.get_active_sheet();
|
|
|
|
sheet.cell("A1") = today;
|
|
|
|
dest_filename = osp.join(TMPDIR, "date_read_write_issue.xlsx");
|
|
|
|
book.save(dest_filename);
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
test_book = load_workbook(dest_filename);
|
|
|
|
test_sheet = test_book.get_active_sheet();
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
TS_ASSERT_EQUALS(test_sheet.cell("A1"), today);
|
|
|
|
clean_tmpdir();*/
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
void test_write_regular_float()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-20 08:47:15 +08:00
|
|
|
TemporaryFile temp_file;
|
2014-05-22 05:48:51 +08:00
|
|
|
double float_value = 1.0 / 3.0;
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook book;
|
|
|
|
auto sheet = book.get_active_sheet();
|
2014-05-21 22:20:30 +08:00
|
|
|
sheet.get_cell("A1") = float_value;
|
2014-05-20 08:47:15 +08:00
|
|
|
auto dest_filename = temp_file.GetFilename();
|
2014-05-19 09:29:19 +08:00
|
|
|
book.save(dest_filename);
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook test_book;
|
|
|
|
test_book.load(dest_filename);
|
|
|
|
auto test_sheet = test_book.get_active_sheet();
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
TS_ASSERT_LESS_THAN_EQUALS(std::stod(test_sheet.get_cell("A1").get_value()) - float_value, 0.001);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
};
|