implement remaining tests

This commit is contained in:
Thomas Fussell 2014-07-25 16:39:25 -04:00
parent 24fbb75a9e
commit c5967686ff
22 changed files with 1307 additions and 835 deletions

View File

@ -33,7 +33,9 @@
namespace xlnt { namespace xlnt {
class cell_reference; class cell_reference;
class comment;
class relationship; class relationship;
class value;
class worksheet; class worksheet;
struct date; struct date;
@ -43,23 +45,8 @@ struct timedelta;
namespace detail { namespace detail {
struct cell_impl; struct cell_impl;
struct comment_impl;
} // namespace detail } // namespace detail
class comment
{
public:
comment(const std::string &text, const std::string &author);
~comment();
std::string get_text() const;
std::string get_author() const;
private:
friend class cell;
comment(detail::comment_impl *d);
detail::comment_impl *d_;
};
/// <summary> /// <summary>
/// Describes cell associated properties. /// Describes cell associated properties.
/// </summary> /// </summary>
@ -72,15 +59,6 @@ private:
class cell class cell
{ {
public: public:
enum class type
{
null,
numeric,
string,
boolean,
error
};
static const std::unordered_map<std::string, int> ErrorCodes; static const std::unordered_map<std::string, int> ErrorCodes;
static std::string check_string(const std::string &value); static std::string check_string(const std::string &value);
@ -88,24 +66,14 @@ public:
static std::string check_error(const std::string &value); static std::string check_error(const std::string &value);
cell(); cell();
cell(worksheet ws, const cell_reference &reference, const std::string &initial_value = std::string()); cell(worksheet ws, const cell_reference &reference);
cell(worksheet ws, const cell_reference &reference, const value &initial_value);
std::string get_internal_value_string() const;
long double get_internal_value_numeric() const;
std::string get_column() const; std::string get_column() const;
row_t get_row() const; row_t get_row() const;
std::string to_string() const; std::string to_string() const;
void set_explicit_value(const std::string &value, type data_type);
void set_explicit_value(int value, type data_type);
void set_explicit_value(double value, type data_type);
void set_explicit_value(const date &value, type data_type);
void set_explicit_value(const time &value, type data_type);
void set_explicit_value(const datetime &value, type data_type);
type data_type_for_value(const std::string &value);
bool is_merged() const; bool is_merged() const;
void set_merged(bool merged); void set_merged(bool merged);
@ -116,11 +84,12 @@ public:
void set_number_format(const std::string &format_code); void set_number_format(const std::string &format_code);
bool has_style() const; bool has_style() const;
style &get_style(); style &get_style();
const style &get_style() const; const style &get_style() const;
type get_data_type() const; std::pair<int, int> get_anchor() const;
bool garbage_collectible() const;
cell_reference get_reference() const; cell_reference get_reference() const;
@ -140,48 +109,32 @@ public:
std::string get_error() const; std::string get_error() const;
void set_error(const std::string &error); void set_error(const std::string &error);
void set_null();
cell offset(row_t row, column_t column); cell offset(row_t row, column_t column);
worksheet get_parent(); worksheet get_parent();
const worksheet get_parent() const;
value &get_value();
const value &get_value() const;
void set_value(bool b);
void set_value(int i);
void set_value(double d);
void set_value(long double d);
void set_value(long long int i);
void set_value(const date &d);
void set_value(const datetime &d);
void set_value(const time &t);
void set_value(const timedelta &t);
void set_value(const char *s);
void set_value(const std::string &s);
void set_value(const value &v);
cell &operator=(const cell &rhs); cell &operator=(const cell &rhs);
cell &operator=(bool value);
cell &operator=(int value);
cell &operator=(double value);
cell &operator=(long int value);
cell &operator=(long long value);
cell &operator=(long double value);
cell &operator=(const std::string &value);
cell &operator=(const char *value);
cell &operator=(const date &value);
cell &operator=(const time &value);
cell &operator=(const datetime &value);
cell &operator=(const timedelta &value);
bool operator==(const cell &comparand) const; bool operator==(const cell &comparand) const;
bool operator==(std::nullptr_t) const; bool operator==(std::nullptr_t) const;
bool operator==(bool comparand) const;
bool operator==(int comparand) const;
bool operator==(double comparand) const;
bool operator==(const std::string &comparand) const;
bool operator==(const char *comparand) const;
bool operator==(const date &comparand) const;
bool operator==(const time &comparand) const;
bool operator==(const datetime &comparand) const;
bool operator==(const timedelta &comparand) const;
friend bool operator==(std::nullptr_t, const cell &cell); friend bool operator==(std::nullptr_t, const cell &cell);
friend bool operator==(bool comparand, const cell &cell);
friend bool operator==(int comparand, const cell &cell);
friend bool operator==(double comparand, const cell &cell);
friend bool operator==(const std::string &comparand, const cell &cell);
friend bool operator==(const char *comparand, const cell &cell);
friend bool operator==(const date &comparand, const cell &cell);
friend bool operator==(const time &comparand, const cell &cell);
friend bool operator==(const datetime &comparand, const cell &cell);
friend bool operator<(cell left, cell right); friend bool operator<(cell left, cell right);
private: private:

View File

@ -0,0 +1,25 @@
#pragma once
#include <string>
namespace xlnt {
namespace detail {
struct cell_impl;
struct comment_impl;
} // namespace detail
class comment
{
public:
comment(const std::string &text, const std::string &author);
~comment();
std::string get_text() const;
std::string get_author() const;
private:
friend class cell;
comment(detail::comment_impl *d);
detail::comment_impl *d_;
};
} // namespace xlnt

108
include/xlnt/cell/value.hpp Normal file
View File

@ -0,0 +1,108 @@
#pragma once
#include <cstdint>
#include <string>
namespace xlnt {
struct date;
struct datetime;
struct time;
struct timedelta;
class value
{
public:
enum class type
{
null,
numeric,
string,
boolean,
error
};
static value null();
static value error(const std::string &error_string);
value();
value(value &&v);
value(bool b);
value(int8_t i);
value(int16_t i);
value(int32_t i);
value(int64_t i);
value(uint8_t i);
value(uint16_t i);
value(uint32_t i);
value(uint64_t i);
value(float f);
value(double d);
value(long double d);
value(const char *s);
value(const std::string &s);
value(const date &d);
value(const datetime &d);
value(const time &t);
value(const timedelta &t);
template<typename T>
T get() const;
template<typename T>
T as() const;
type get_type() const;
bool is(type t) const;
bool is_integral() const;
std::string to_string() const;
value &operator=(value other);
value &operator=(bool b);
value &operator=(int8_t i);
value &operator=(int16_t i);
value &operator=(int32_t i);
value &operator=(int64_t i);
value &operator=(uint8_t i);
value &operator=(uint16_t i);
value &operator=(uint32_t i);
value &operator=(uint64_t i);
value &operator=(const char *s);
value &operator=(const std::string &s);
value &operator=(const date &d);
value &operator=(const datetime &d);
value &operator=(const time &t);
value &operator=(const timedelta &t);
bool operator==(const value &comparand) const;
bool operator==(bool comparand) const;
bool operator==(int comparand) const;
bool operator==(double comparand) const;
bool operator==(const std::string &comparand) const;
bool operator==(const char *comparand) const;
bool operator==(const date &comparand) const;
bool operator==(const time &comparand) const;
bool operator==(const datetime &comparand) const;
bool operator==(const timedelta &comparand) const;
friend bool operator==(bool comparand, const value &v);
friend bool operator==(int comparand, const value &v);
friend bool operator==(double comparand, const value &v);
friend bool operator==(const std::string &comparand, const value &v);
friend bool operator==(const char *comparand, const value &v);
friend bool operator==(const date &comparand, const value &v);
friend bool operator==(const time &comparand, const value &v);
friend bool operator==(const datetime &comparand, const value &v);
friend void swap(value &left, value &right);
private:
type type_;
std::string string_value_;
long double numeric_value_;
};
} // namespace xlnt

View File

@ -55,7 +55,7 @@ struct time
static time now(); static time now();
static time from_number(long double number); static time from_number(long double number);
time(int hour = 0, int minute = 0, int second = 0, int microsecond = 0) explicit time(int hour = 0, int minute = 0, int second = 0, int microsecond = 0)
: hour(hour), minute(minute), second(second), microsecond(microsecond) : hour(hour), minute(minute), second(second), microsecond(microsecond)
{ {
} }

View File

@ -156,7 +156,7 @@ struct page_setup
public: public:
page_setup() : default_(true), break_(page_break::none), sheet_state_(sheet_state::visible), paper_size_(paper_size::letter), page_setup() : default_(true), break_(page_break::none), sheet_state_(sheet_state::visible), paper_size_(paper_size::letter),
orientation_(orientation::portrait), fit_to_page_(false), fit_to_height_(false), fit_to_width_(false) {} orientation_(orientation::portrait), fit_to_page_(false), fit_to_height_(false), fit_to_width_(false), horizontal_centered_(false), vertical_centered_(false), scale_(1) {}
bool is_default() const { return default_; } bool is_default() const { return default_; }
page_break get_break() const { return break_; } page_break get_break() const { return break_; }
void set_break(page_break b) { default_ = false; break_ = b; } void set_break(page_break b) { default_ = false; break_ = b; }
@ -172,6 +172,12 @@ public:
void set_fit_to_height(bool fit_to_height) { default_ = false; fit_to_height_ = fit_to_height; } void set_fit_to_height(bool fit_to_height) { default_ = false; fit_to_height_ = fit_to_height; }
bool fit_to_width() const { return fit_to_width_; } bool fit_to_width() const { return fit_to_width_; }
void set_fit_to_width(bool fit_to_width) { default_ = false; fit_to_width_ = fit_to_width; } void set_fit_to_width(bool fit_to_width) { default_ = false; fit_to_width_ = fit_to_width; }
void set_horizontal_centered(bool horizontal_centered) { default_ = false; horizontal_centered_ = horizontal_centered; }
bool get_horizontal_centered() const { return horizontal_centered_; }
void set_vertical_centered(bool vertical_centered) { default_ = false; vertical_centered_ = vertical_centered; }
bool get_vertical_centered() const { return vertical_centered_; }
void set_scale(double scale) { default_ = false; scale_ = scale; }
double get_scale() const { return scale_; }
private: private:
bool default_; bool default_;
@ -182,6 +188,9 @@ private:
bool fit_to_page_; bool fit_to_page_;
bool fit_to_height_; bool fit_to_height_;
bool fit_to_width_; bool fit_to_width_;
bool horizontal_centered_;
bool vertical_centered_;
double scale_;
}; };
struct margins struct margins
@ -248,6 +257,12 @@ public:
range columns() const; range columns() const;
std::list<cell> get_cell_collection(); std::list<cell> get_cell_collection();
cell_reference get_point_pos(int left, int top) const;
cell_reference get_point_pos(const std::pair<int, int> &point) const;
const std::unordered_map<column_t, double> &get_column_dimensions() const;
const std::unordered_map<row_t, double> &get_row_dimensions() const;
// named range // named range
void create_named_range(const std::string &name, const range_reference &reference); void create_named_range(const std::string &name, const range_reference &reference);
bool has_named_range(const std::string &name); bool has_named_range(const std::string &name);

View File

@ -48,3 +48,5 @@ const std::string download_url = "https://github.com/tfussell/xlnt/archive/maste
#include "common/string_table.hpp" #include "common/string_table.hpp"
#include "common/zip_file.hpp" #include "common/zip_file.hpp"
#include "workbook/document_properties.hpp" #include "workbook/document_properties.hpp"
#include "cell/value.hpp"
#include "cell/comment.hpp"

View File

@ -4,6 +4,8 @@
#include "cell/cell.hpp" #include "cell/cell.hpp"
#include "cell/cell_reference.hpp" #include "cell/cell_reference.hpp"
#include "cell/comment.hpp"
#include "cell/value.hpp"
#include "common/datetime.hpp" #include "common/datetime.hpp"
#include "common/relationship.hpp" #include "common/relationship.hpp"
#include "worksheet/worksheet.hpp" #include "worksheet/worksheet.hpp"
@ -12,49 +14,102 @@
#include "workbook/workbook.hpp" #include "workbook/workbook.hpp"
#include "workbook/document_properties.hpp" #include "workbook/document_properties.hpp"
namespace {
std::vector<std::string> split_string(const std::string &string, char delim = ' ')
{
std::stringstream ss(string);
std::string part;
std::vector<std::string> parts;
while(std::getline(ss, part, delim))
{
parts.push_back(part);
}
return parts;
}
xlnt::value::type data_type_for_value(const std::string &value)
{
if(value.empty())
{
return xlnt::value::type::null;
}
if(value[0] == '0')
{
if(value.length() > 1)
{
if(value[1] == '.' || (value.length() > 2 && (value[1] == 'e' || value[1] == 'E')))
{
auto first_non_number = std::find_if(value.begin() + 2, value.end(),
[](char c) { return !std::isdigit(c, std::locale::classic()); });
if(first_non_number == value.end())
{
return xlnt::value::type::numeric;
}
}
auto split = split_string(value, ':');
if(split.size() == 2 || split.size() == 3)
{
for(auto part : split)
{
try
{
std::stoi(part);
}
catch(std::invalid_argument)
{
return xlnt::value::type::string;
}
}
return xlnt::value::type::numeric;
}
else
{
return xlnt::value::type::string;
}
}
return xlnt::value::type::numeric;
}
else if(value[0] == '#')
{
return xlnt::value::type::error;
}
else
{
char *p;
strtod(value.c_str(), &p);
if(*p != 0)
{
static const std::vector<std::string> possible_booleans = {"TRUE", "true", "FALSE", "false"};
if(std::find(possible_booleans.begin(), possible_booleans.end(), value) != possible_booleans.end())
{
return xlnt::value::type::boolean;
}
if(value.back() == '%')
{
strtod(value.substr(0, value.length() - 1).c_str(), &p);
if(*p == 0)
{
return xlnt::value::type::numeric;
}
}
return xlnt::value::type::string;
}
else
{
return xlnt::value::type::numeric;
}
}
}
} // namespace
namespace xlnt { namespace xlnt {
const xlnt::color xlnt::color::black(0); const xlnt::color xlnt::color::black(0);
const xlnt::color xlnt::color::white(1); const xlnt::color xlnt::color::white(1);
detail::comment_impl::comment_impl() : parent_worksheet_(nullptr)
{
}
detail::comment_impl::comment_impl(detail::worksheet_impl *ws, const cell_reference &ref, const std::string &text, const std::string &author) : parent_worksheet_(ws), parent_cell_(ref), text_(text), author_(author)
{
}
comment::comment(const std::string &text, const std::string &author) : d_(new detail::comment_impl())
{
d_->text_ = text;
d_->author_ = author;
}
comment::comment(detail::comment_impl *d) : d_(d)
{
}
comment::~comment()
{
if(d_->parent_worksheet_ == nullptr)
{
delete d_;
d_ = nullptr;
}
}
std::string comment::get_author() const
{
return d_->author_;
}
std::string comment::get_text() const
{
return d_->text_;
}
const std::unordered_map<std::string, int> cell::ErrorCodes = const std::unordered_map<std::string, int> cell::ErrorCodes =
{ {
{"#NULL!", 0}, {"#NULL!", 0},
@ -74,28 +129,132 @@ cell::cell(detail::cell_impl *d) : d_(d)
{ {
} }
cell::cell(worksheet worksheet, const cell_reference &reference, const std::string &initial_value) : d_(nullptr) cell::cell(worksheet worksheet, const cell_reference &reference) : d_(nullptr)
{ {
cell self = worksheet.get_cell(reference); cell self = worksheet.get_cell(reference);
d_ = self.d_; d_ = self.d_;
}
if(initial_value != "") cell::cell(worksheet worksheet, const cell_reference &reference, const value &initial_value) : cell(worksheet, reference)
{
set_value(initial_value);
}
bool cell::garbage_collectible() const
{
return !(get_value().get_type() != value::type::null || is_merged() || has_comment() || has_formula());
}
value &cell::get_value()
{
return d_->value_;
}
const value &cell::get_value() const
{
return d_->value_;
}
void cell::set_value(const value &v)
{
d_->value_ = v;
}
void cell::set_value(const std::string &s)
{
if(!get_parent().get_parent().get_guess_types())
{ {
*this = initial_value; d_->is_date_ = false;
d_->value_ = value(s);
}
else
{
d_->is_date_ = false;
switch(data_type_for_value(s))
{
case value::type::numeric:
if(s.find(':') != std::string::npos)
{
d_->is_date_ = true;
d_->value_ = value(time(s).to_number());
}
else if(s.back() == '%')
{
d_->value_ = value(std::stod(s.substr(0, s.length() - 1)) / 100);
get_style().get_number_format().set_format_code(xlnt::number_format::format::percentage);
}
else
{
d_->value_ = value(std::stod(s));
}
break;
case value::type::boolean:
d_->value_ = value(s == "TRUE" || s == "true");
break;
case value::type::error:
d_->value_ = value(s);
break;
case value::type::string:
d_->value_ = value(s);
break;
case value::type::null:
d_->value_ = value::null();
break;
default: throw data_type_exception();
}
} }
} }
std::string cell::get_internal_value_string() const void cell::set_value(const char *s)
{ {
switch(d_->type_) set_value(std::string(s));
{ }
case type::string:
return d_->string_value; void cell::set_value(bool b)
case type::error: {
return d_->string_value; d_->value_ = value(b);
default: }
throw std::runtime_error("bad enum");
} void cell::set_value(int i)
{
d_->value_ = value(i);
}
void cell::set_value(long long int i)
{
d_->value_ = value(i);
}
void cell::set_value(double d)
{
d_->value_ = value(d);
}
void cell::set_value(const date &d)
{
d_->is_date_ = true;
auto base_date = get_parent().get_parent().get_properties().excel_base_date;
set_value(d.to_number(base_date));
}
void cell::set_value(const datetime &d)
{
d_->is_date_ = true;
auto base_date = get_parent().get_parent().get_properties().excel_base_date;
set_value(d.to_number(base_date));
}
void cell::set_value(const time &t)
{
d_->is_date_ = true;
set_value(t.to_number());
}
void cell::set_value(const timedelta &t)
{
d_->is_date_ = true;
set_value(t.to_number());
} }
bool cell::has_style() const bool cell::has_style() const
@ -103,173 +262,14 @@ bool cell::has_style() const
return d_->style_ != nullptr; return d_->style_ != nullptr;
} }
long double cell::get_internal_value_numeric() const
{
switch(d_->type_)
{
case type::numeric:
return d_->numeric_value;
case type::boolean:
return d_->numeric_value == 0 ? 0 : 1;
default:
throw std::runtime_error("bad enum");
}
}
row_t cell::get_row() const row_t cell::get_row() const
{ {
return d_->row + 1; return d_->row_ + 1;
} }
std::string cell::get_column() const std::string cell::get_column() const
{ {
return cell_reference::column_string_from_index(d_->column + 1); return cell_reference::column_string_from_index(d_->column_ + 1);
}
std::vector<std::string> split_string(const std::string &string, char delim = ' ')
{
std::stringstream ss(string);
std::string part;
std::vector<std::string> parts;
while(std::getline(ss, part, delim))
{
parts.push_back(part);
}
return parts;
}
cell::type cell::data_type_for_value(const std::string &value)
{
if(value.empty())
{
return type::null;
}
if(value[0] == '0')
{
if(value.length() > 1)
{
if(value[1] == '.' || (value.length() > 2 && (value[1] == 'e' || value[1] == 'E')))
{
auto first_non_number = std::find_if(value.begin() + 2, value.end(),
[](char c) { return !std::isdigit(c, std::locale::classic()); });
if(first_non_number == value.end())
{
return type::numeric;
}
}
auto split = split_string(value, ':');
if(split.size() == 2 || split.size() == 3)
{
for(auto part : split)
{
try
{
std::stoi(part);
}
catch(std::invalid_argument)
{
return type::string;
}
}
return type::numeric;
}
else
{
return type::string;
}
}
return type::numeric;
}
else if(value[0] == '#')
{
return type::error;
}
else
{
char *p;
strtod(value.c_str(), &p);
if(*p != 0)
{
static const std::vector<std::string> possible_booleans = {"TRUE", "true", "FALSE", "false"};
if(std::find(possible_booleans.begin(), possible_booleans.end(), value) != possible_booleans.end())
{
return type::boolean;
}
if(value.back() == '%')
{
strtod(value.substr(0, value.length() - 1).c_str(), &p);
if(*p == 0)
{
return type::numeric;
}
}
return type::string;
}
else
{
return type::numeric;
}
}
}
void cell::set_explicit_value(const std::string &value, type data_type)
{
d_->type_ = data_type;
switch(data_type)
{
/*
case type::null:
if(value != "")
{
throw data_type_exception();
}
return;
case type::error: d_->string_value = value; return;
case type::boolean:
d_->numeric_value = value == "true";
return;
case type::numeric:
try
{
d_->numeric_value = std::stod(value);
}
catch(std::invalid_argument)
{
throw data_type_exception();
}
return;
*/
case type::string:
d_->string_value = value;
return;
default: throw data_type_exception();
}
}
void cell::set_explicit_value(int value, type data_type)
{
d_->type_ = data_type;
switch(data_type)
{
case type::numeric: d_->numeric_value = value; return;
//case type::string: d_->string_value = std::to_string(value); return;
default: throw data_type_exception();
}
}
void cell::set_explicit_value(double value, type data_type)
{
d_->type_ = data_type;
switch(data_type)
{
case type::numeric: d_->numeric_value = value; return;
//case type::string: d_->string_value = std::to_string(value); return;
default: throw data_type_exception();
}
} }
void cell::set_merged(bool merged) void cell::set_merged(bool merged)
@ -289,7 +289,7 @@ bool cell::is_date() const
cell_reference cell::get_reference() const cell_reference cell::get_reference() const
{ {
return {d_->column, d_->row}; return {d_->column_, d_->row_};
} }
bool cell::operator==(std::nullptr_t) const bool cell::operator==(std::nullptr_t) const
@ -297,68 +297,6 @@ bool cell::operator==(std::nullptr_t) const
return d_ == nullptr; return d_ == nullptr;
} }
bool cell::operator==(bool value) const
{
return d_->type_ == type::boolean && (d_->numeric_value != 0) == value;
}
bool cell::operator==(int comparand) const
{
return d_->type_ == type::numeric && d_->numeric_value == comparand;
}
bool cell::operator==(double comparand) const
{
return d_->type_ == type::numeric && d_->numeric_value == comparand;
}
bool cell::operator==(const std::string &comparand) const
{
if(d_->type_ == type::string)
{
return d_->string_value == comparand;
}
return false;
}
bool cell::operator==(const char *comparand) const
{
return *this == std::string(comparand);
}
bool cell::operator==(const time &comparand) const
{
if(d_->type_ != type::numeric)
{
return false;
}
return time::from_number(d_->numeric_value) == comparand;
}
bool cell::operator==(const date &comparand) const
{
if(d_->type_ != type::numeric)
{
return false;
}
auto base_year = worksheet(d_->parent_).get_parent().get_properties().excel_base_date;
return date::from_number((int)d_->numeric_value, base_year) == comparand;
}
bool cell::operator==(const datetime &comparand) const
{
if(d_->type_ != type::numeric)
{
return false;
}
auto base_year = worksheet(d_->parent_).get_parent().get_properties().excel_base_date;
return datetime::from_number(d_->numeric_value, base_year) == comparand;
}
bool cell::operator==(const cell &comparand) const bool cell::operator==(const cell &comparand) const
{ {
if(comparand == nullptr) if(comparand == nullptr)
@ -366,67 +304,7 @@ bool cell::operator==(const cell &comparand) const
return d_ == nullptr; return d_ == nullptr;
} }
if(comparand.get_data_type() != get_data_type()) return d_->value_ == comparand.d_->value_;
{
return false;
}
switch(get_data_type())
{
case type::boolean:
return d_->numeric_value == comparand.d_->numeric_value;
case type::error:
return d_->string_value == comparand.d_->string_value;
case type::string:
return d_->string_value == comparand.d_->string_value;
case type::null:
return true;
case type::numeric:
if(is_date() && comparand.is_date())
{
auto base_year = worksheet(d_->parent_).get_parent().get_properties().excel_base_date;
auto other_base_year = worksheet(comparand.d_->parent_).get_parent().get_properties().excel_base_date;
return date::from_number((int)d_->numeric_value, base_year) == date::from_number((int)comparand.d_->numeric_value, other_base_year);
}
return d_->numeric_value == comparand.d_->numeric_value;
}
return false;
}
bool operator==(bool comparand, const xlnt::cell &cell)
{
return cell == comparand;
}
bool operator==(int comparand, const xlnt::cell &cell)
{
return cell == comparand;
}
bool operator==(const char *comparand, const xlnt::cell &cell)
{
return cell == comparand;
}
bool operator==(const std::string &comparand, const xlnt::cell &cell)
{
return cell == comparand;
}
bool operator==(const time &comparand, const xlnt::cell &cell)
{
return cell == comparand;
}
bool operator==(const date &comparand, const xlnt::cell &cell)
{
return cell == comparand;
}
bool operator==(const datetime &comparand, const xlnt::cell &cell)
{
return cell == comparand;
} }
style &cell::get_style() style &cell::get_style()
@ -447,158 +325,17 @@ const style &cell::get_style() const
return *d_->style_; return *d_->style_;
} }
xlnt::cell::type cell::get_data_type() const
{
return (type)d_->type_;
}
cell &cell::operator=(const cell &rhs) cell &cell::operator=(const cell &rhs)
{ {
d_ = rhs.d_; d_ = rhs.d_;
return *this; return *this;
} }
cell &cell::operator=(int value)
{
d_->is_date_ = false;
d_->type_ = type::numeric;
d_->numeric_value = value;
return *this;
}
cell &cell::operator=(double value)
{
d_->is_date_ = false;
d_->type_ = type::numeric;
d_->numeric_value = value;
return *this;
}
cell &cell::operator=(long int value)
{
d_->is_date_ = false;
d_->type_ = type::numeric;
d_->numeric_value = value;
return *this;
}
cell &cell::operator=(long long value)
{
d_->is_date_ = false;
d_->type_ = type::numeric;
d_->numeric_value = (long double)value;
return *this;
}
cell &cell::operator=(long double value)
{
d_->is_date_ = false;
d_->type_ = type::numeric;
d_->numeric_value = value;
return *this;
}
cell &cell::operator=(bool value)
{
d_->is_date_ = false;
d_->type_ = type::boolean;
d_->numeric_value = value ? 1 : 0;
return *this;
}
bool operator<(cell left, cell right) bool operator<(cell left, cell right)
{ {
return left.get_reference() < right.get_reference(); return left.get_reference() < right.get_reference();
} }
cell &cell::operator=(const std::string &value)
{
if(!get_parent().get_parent().get_guess_types())
{
d_->is_date_ = false;
d_->type_ = type::string;
d_->string_value = value;
}
else
{
d_->is_date_ = false;
d_->type_ = data_type_for_value(value);
switch(d_->type_)
{
case type::numeric:
if(value.find(':') != std::string::npos)
{
d_->is_date_ = true;
d_->numeric_value = time(value).to_number();
}
else if(value.back() == '%')
{
d_->numeric_value = std::stod(value.substr(0, value.length() - 1)) / 100;
get_style().get_number_format().set_format_code(xlnt::number_format::format::percentage);
}
else
{
d_->numeric_value = std::stod(value);
}
break;
case type::boolean:
d_->numeric_value = value == "TRUE" || value == "true";
break;
case type::error:
d_->string_value = value;
break;
case type::string:
d_->string_value = value;
break;
case type::null:
break;
default: throw data_type_exception();
}
}
return *this;
}
cell &cell::operator=(const char *value)
{
return *this = std::string(value);
}
cell &cell::operator=(const time &value)
{
d_->type_ = type::numeric;
d_->numeric_value = value.to_number();
d_->is_date_ = true;
return *this;
}
cell &cell::operator=(const date &value)
{
d_->type_ = type::numeric;
auto base_year = worksheet(d_->parent_).get_parent().get_properties().excel_base_date;
d_->numeric_value = value.to_number(base_year);
d_->is_date_ = true;
return *this;
}
cell &cell::operator=(const datetime &value)
{
d_->type_ = type::numeric;
auto base_year = worksheet(d_->parent_).get_parent().get_properties().excel_base_date;
d_->numeric_value = value.to_number(base_year);
d_->is_date_ = true;
return *this;
}
cell &cell::operator=(const timedelta &value)
{
d_->type_ = type::numeric;
d_->numeric_value = value.to_number();
d_->is_date_ = true;
return *this;
}
std::string cell::to_string() const std::string cell::to_string() const
{ {
return "<Cell " + worksheet(d_->parent_).get_title() + "." + get_reference().to_string() + ">"; return "<Cell " + worksheet(d_->parent_).get_title() + "." + get_reference().to_string() + ">";
@ -629,17 +366,12 @@ void cell::set_hyperlink(const std::string &hyperlink)
d_->has_hyperlink_ = true; d_->has_hyperlink_ = true;
d_->hyperlink_ = worksheet(d_->parent_).create_relationship(relationship::type::hyperlink, hyperlink); d_->hyperlink_ = worksheet(d_->parent_).create_relationship(relationship::type::hyperlink, hyperlink);
if(d_->type_ == type::null) if(get_value().is(value::type::null))
{ {
*this = hyperlink; set_value(hyperlink);
} }
} }
void cell::set_null()
{
d_->type_ = type::null;
}
void cell::set_formula(const std::string &formula) void cell::set_formula(const std::string &formula)
{ {
if(formula.length() == 0) if(formula.length() == 0)
@ -647,27 +379,27 @@ void cell::set_formula(const std::string &formula)
throw data_type_exception(); throw data_type_exception();
} }
d_->formula_value = formula; d_->formula_ = formula;
} }
bool cell::has_formula() const bool cell::has_formula() const
{ {
return !d_->formula_value.empty(); return !d_->formula_.empty();
} }
std::string cell::get_formula() const std::string cell::get_formula() const
{ {
if(d_->formula_value.empty()) if(d_->formula_.empty())
{ {
throw data_type_exception(); throw data_type_exception();
} }
return d_->formula_value; return d_->formula_;
} }
void cell::clear_formula() void cell::clear_formula()
{ {
d_->formula_value.clear(); d_->formula_.clear();
} }
void cell::set_comment(xlnt::comment &c) void cell::set_comment(xlnt::comment &c)
@ -737,13 +469,12 @@ void cell::set_error(const std::string &error)
throw data_type_exception(); throw data_type_exception();
} }
d_->type_ = type::error; set_value(value::error(error));
d_->string_value = error;
} }
cell cell::offset(column_t column, row_t row) cell cell::offset(column_t column, row_t row)
{ {
return get_parent().get_cell(cell_reference(d_->column + column, d_->row + row)); return get_parent().get_cell(cell_reference(d_->column_ + column, d_->row_ + row));
} }
worksheet cell::get_parent() worksheet cell::get_parent()
@ -751,9 +482,66 @@ worksheet cell::get_parent()
return worksheet(d_->parent_); return worksheet(d_->parent_);
} }
const worksheet cell::get_parent() const
{
return worksheet(d_->parent_);
}
comment cell::get_comment() const comment cell::get_comment() const
{ {
return comment(&d_->comment_); return comment(&d_->comment_);
} }
std::pair<int, int> cell::get_anchor() const
{
static const double DefaultColumnWidth = 51.85;
static const double DefaultRowHeight = 15.0;
auto points_to_pixels = [](double value, double dpi) { return (int)std::ceil(value * dpi / 72); };
auto left_columns = d_->column_;
auto &column_dimensions = get_parent().get_column_dimensions();
int left_anchor = 0;
auto default_width = points_to_pixels(DefaultColumnWidth, 96.0);
for(int column_index = 1; column_index <= (int)left_columns; column_index++)
{
if(column_dimensions.find(column_index) != column_dimensions.end())
{
auto cdw = column_dimensions.at(column_index);
if(cdw > 0)
{
left_anchor += points_to_pixels(cdw, 96.0);
continue;
}
}
left_anchor += default_width;
}
auto top_rows = d_->row_;
auto &row_dimensions = get_parent().get_row_dimensions();
int top_anchor = 0;
auto default_height = points_to_pixels(DefaultRowHeight, 96.0);
for(int row_index = 1; row_index <= (int)top_rows; row_index++)
{
if(row_dimensions.find(row_index) != row_dimensions.end())
{
auto rdh = row_dimensions.at(row_index);
if(rdh > 0)
{
top_anchor += points_to_pixels(rdh, 96.0);
continue;
}
}
top_anchor += default_height;
}
return {left_anchor, top_anchor};
}
} // namespace xlnt } // namespace xlnt

47
source/comment.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "cell/comment.hpp"
#include "detail/comment_impl.hpp"
namespace xlnt {
namespace detail {
detail::comment_impl::comment_impl() : parent_worksheet_(nullptr)
{
}
comment_impl::comment_impl(worksheet_impl *ws, const cell_reference &ref, const std::string &text, const std::string &author) : parent_worksheet_(ws), parent_cell_(ref), text_(text), author_(author)
{
}
} // namespace detail
comment::comment(const std::string &text, const std::string &author) : d_(new detail::comment_impl())
{
d_->text_ = text;
d_->author_ = author;
}
comment::comment(detail::comment_impl *d) : d_(d)
{
}
comment::~comment()
{
if(d_->parent_worksheet_ == nullptr)
{
delete d_;
d_ = nullptr;
}
}
std::string comment::get_author() const
{
return d_->author_;
}
std::string comment::get_text() const
{
return d_->text_;
}
} // namespace xlnt

View File

@ -4,15 +4,15 @@
namespace xlnt { namespace xlnt {
namespace detail { namespace detail {
cell_impl::cell_impl() : parent_(nullptr), type_(cell::type::null), column(0), row(0), style_(nullptr), merged(false), has_hyperlink_(false), comment_(nullptr, "A1", "", "") cell_impl::cell_impl() : parent_(nullptr), column_(0), row_(0), style_(nullptr), merged(false), has_hyperlink_(false), comment_(nullptr, "A1", "", ""), is_date_(false)
{ {
} }
cell_impl::cell_impl(worksheet_impl *parent, int column_index, int row_index) : parent_(parent), type_(cell::type::null), column(column_index), row(row_index), style_(nullptr), merged(false), has_hyperlink_(false), comment_(nullptr, "A1", "", "") cell_impl::cell_impl(worksheet_impl *parent, int column_index, int row_index) : parent_(parent), column_(column_index), row_(row_index), style_(nullptr), merged(false), has_hyperlink_(false), comment_(nullptr, "A1", "", ""), is_date_(false)
{ {
} }
cell_impl::cell_impl(const cell_impl &rhs) : comment_(nullptr, "A1", "", "") cell_impl::cell_impl(const cell_impl &rhs) : comment_(nullptr, "A1", "", ""), is_date_(false)
{ {
*this = rhs; *this = rhs;
} }
@ -20,12 +20,11 @@ cell_impl::cell_impl(const cell_impl &rhs) : comment_(nullptr, "A1", "", "")
cell_impl &cell_impl::operator=(const cell_impl &rhs) cell_impl &cell_impl::operator=(const cell_impl &rhs)
{ {
parent_ = rhs.parent_; parent_ = rhs.parent_;
type_ = rhs.type_; value_ = rhs.value_;
numeric_value = rhs.numeric_value;
string_value = rhs.string_value;
hyperlink_ = rhs.hyperlink_; hyperlink_ = rhs.hyperlink_;
column = rhs.column; formula_ = rhs.formula_;
row = rhs.row; column_ = rhs.column_;
row_ = rhs.row_;
style_ = rhs.style_; style_ = rhs.style_;
merged = rhs.merged; merged = rhs.merged;
is_date_ = rhs.is_date_; is_date_ = rhs.is_date_;

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "cell/cell.hpp" #include "cell/cell.hpp"
#include "cell/value.hpp"
#include "common/types.hpp" #include "common/types.hpp"
#include "common/relationship.hpp" #include "common/relationship.hpp"
#include "comment_impl.hpp" #include "comment_impl.hpp"
@ -21,13 +22,11 @@ struct cell_impl
cell_impl &operator=(const cell_impl &rhs); cell_impl &operator=(const cell_impl &rhs);
worksheet_impl *parent_; worksheet_impl *parent_;
cell::type type_; value value_;
long double numeric_value; std::string formula_;
std::string string_value;
std::string formula_value;
relationship hyperlink_; relationship hyperlink_;
column_t column; column_t column_;
row_t row; row_t row_;
style *style_; style *style_;
bool merged; bool merged;
bool is_date_; bool is_date_;

View File

@ -65,6 +65,8 @@ struct worksheet_impl
std::unordered_map<std::string, range_reference> named_ranges_; std::unordered_map<std::string, range_reference> named_ranges_;
std::size_t comment_count_; std::size_t comment_count_;
header_footer header_footer_; header_footer header_footer_;
std::unordered_map<column_t, double> column_dimensions_;
std::unordered_map<row_t, double> row_dimensions_;
}; };
} // namespace detail } // namespace detail

View File

@ -2,7 +2,10 @@
namespace xlnt { namespace xlnt {
document_properties::document_properties() : created(1900, 1, 1), modified(1900, 1, 1) document_properties::document_properties()
: created(1900, 1, 1),
modified(1900, 1, 1),
excel_base_date(calendar::windows_1900)
{ {
} }

View File

@ -3,6 +3,7 @@
#include "reader/reader.hpp" #include "reader/reader.hpp"
#include "cell/cell.hpp" #include "cell/cell.hpp"
#include "cell/value.hpp"
#include "common/datetime.hpp" #include "common/datetime.hpp"
#include "worksheet/range_reference.hpp" #include "worksheet/range_reference.hpp"
#include "workbook/workbook.hpp" #include "workbook/workbook.hpp"
@ -279,7 +280,7 @@ void read_worksheet_common(worksheet ws, const pugi::xml_node &root_node, const
if(cell_node != nullptr) if(cell_node != nullptr)
{ {
bool has_value = cell_node.child("v") != nullptr; bool has_value = cell_node.child("v") != nullptr;
std::string value = cell_node.child("v").text().as_string(); std::string value_string = cell_node.child("v").text().as_string();
bool has_type = cell_node.attribute("t") != nullptr; bool has_type = cell_node.attribute("t") != nullptr;
std::string type = cell_node.attribute("t").as_string(); std::string type = cell_node.attribute("t").as_string();
@ -299,36 +300,47 @@ void read_worksheet_common(worksheet ws, const pugi::xml_node &root_node, const
if(has_type && type == "inlineStr") // inline string if(has_type && type == "inlineStr") // inline string
{ {
std::string inline_string = cell_node.child("is").child("t").text().as_string(); std::string inline_string = cell_node.child("is").child("t").text().as_string();
ws.get_cell(address) = inline_string; ws.get_cell(address).set_value(inline_string);
} }
else if(has_type && type == "s") // shared string else if(has_type && type == "s") // shared string
{ {
ws.get_cell(address) = string_table.at(cell_node.child("v").text().as_int()); auto shared_string_index = std::stoi(value_string);
auto shared_string = string_table.at(shared_string_index);
ws.get_cell(address).set_value(shared_string);
} }
else if(has_type && type == "b") // boolean else if(has_type && type == "b") // boolean
{ {
ws.get_cell(address) = value != "0"; ws.get_cell(address).set_value(value(value_string != "0"));
} }
else if(has_type && type == "str") else if(has_type && type == "str")
{ {
ws.get_cell(address) = value; ws.get_cell(address).set_value(value_string);
} }
else if(has_style) else if(has_style)
{ {
auto number_format_id = number_format_ids.at(std::stoi(style)); auto number_format_id = number_format_ids.at(std::stoi(style));
auto format = number_format::lookup_format(number_format_id); auto format = number_format::lookup_format(number_format_id);
ws.get_cell(address).get_style().get_number_format().set_format_code(format); ws.get_cell(address).get_style().get_number_format().set_format_code(format);
ws.get_cell(address) = std::stod(value); if(format == number_format::format::date_xlsx14)
{
auto base_date = ws.get_parent().get_properties().excel_base_date;
auto converted = date::from_number(std::stoi(value_string), base_date);
ws.get_cell(address).set_value(converted.to_number(calendar::windows_1900));
}
else
{
ws.get_cell(address).set_value(value(std::stod(value_string)));
}
} }
else if(has_value) else if(has_value)
{ {
try try
{ {
ws.get_cell(address) = std::stod(value); ws.get_cell(address).set_value(value(std::stod(value_string)));
} }
catch(std::invalid_argument) catch(std::invalid_argument)
{ {
ws.get_cell(address) = value; ws.get_cell(address).set_value(value_string);
} }
} }
} }

314
source/value.cpp Normal file
View File

@ -0,0 +1,314 @@
#include "cell/value.hpp"
#include "common/datetime.hpp"
namespace xlnt {
value value::error(const std::string &error_string)
{
value v(error_string);
v.type_ = type::error;
return v;
}
value::value() : type_(type::null), numeric_value_(0)
{
}
value::value(value &&v)
{
swap(*this, v);
}
value::value(bool b) : type_(type::boolean), numeric_value_(b ? 1 : 0)
{
}
value::value(int i) : type_(type::numeric), numeric_value_(i)
{
}
value::value(double d) : type_(type::numeric), numeric_value_(d)
{
}
value::value(int64_t i) : type_(type::numeric), numeric_value_((long double)i)
{
}
value::value(const char *s) : value(std::string(s))
{
}
value::value(const std::string &s) : type_(type::string), string_value_(s)
{
}
value &value::operator=(value other)
{
swap(*this, other);
return *this;
}
bool value::is(type t) const
{
return type_ == t;
}
template<>
double value::as() const
{
switch(type_)
{
case type::boolean:
case type::numeric:
return (double)numeric_value_;
case type::string:
return std::stod(string_value_);
case type::error:
throw std::runtime_error("invalid");
case type::null:
return 0;
}
return 0;
}
template<>
int value::as() const
{
switch(type_)
{
case type::boolean:
case type::numeric:
return (int)numeric_value_;
case type::string:
return std::stoi(string_value_);
case type::error:
throw std::runtime_error("invalid");
case type::null:
return 0;
}
return 0;
}
template<>
int64_t value::as() const
{
switch(type_)
{
case type::boolean:
case type::numeric:
return (int64_t)numeric_value_;
case type::string:
return std::stoi(string_value_);
case type::error:
throw std::runtime_error("invalid");
case type::null:
return 0;
}
return 0;
}
template<>
bool value::as() const
{
switch(type_)
{
case type::boolean:
case type::numeric:
return numeric_value_ != 0;
case type::string:
return !string_value_.empty();
case type::error:
throw std::runtime_error("invalid");
case type::null:
return false;
}
return false;
}
bool value::is_integral() const
{
return type_ == type::numeric && (int64_t)numeric_value_ == numeric_value_;
}
template<>
std::string value::as() const
{
return to_string();
}
value value::null()
{
return value();
}
value::type value::get_type() const
{
return type_;
}
std::string value::to_string() const
{
switch(type_)
{
case type::boolean:
return numeric_value_ != 0 ? "1" : "0";
case type::numeric:
return std::to_string(numeric_value_);
case type::string:
case type::error:
return string_value_;
case type::null:
return "";
}
return "";
}
bool value::operator==(bool value) const
{
return type_ == type::boolean && (numeric_value_ != 0) == value;
}
bool value::operator==(int comparand) const
{
return type_ == type::numeric && numeric_value_ == comparand;
}
bool value::operator==(double comparand) const
{
return type_ == type::numeric && numeric_value_ == comparand;
}
bool value::operator==(const std::string &comparand) const
{
if(type_ == type::string)
{
return string_value_ == comparand;
}
return false;
}
bool value::operator==(const char *comparand) const
{
return *this == std::string(comparand);
}
bool value::operator==(const time &comparand) const
{
if(type_ != type::numeric)
{
return false;
}
return time::from_number(numeric_value_) == comparand;
}
bool value::operator==(const date &comparand) const
{
return type_ == type::numeric && comparand.to_number(calendar::windows_1900) == numeric_value_;
}
bool value::operator==(const datetime &comparand) const
{
return type_ == type::numeric && comparand.to_number(calendar::windows_1900) == numeric_value_;
}
bool value::operator==(const timedelta &comparand) const
{
return type_ == type::numeric && comparand.to_number() == numeric_value_;
}
bool value::operator==(const value &v) const
{
if(type_ != v.type_) return false;
if(type_ == type::string || type_ == type::error) return string_value_ == v.string_value_;
if(type_ == type::numeric || type_ == type::boolean) return numeric_value_ == v.numeric_value_;
return true;
}
/*
value &value::operator=(const time &value)
{
d_->type_ = type::numeric;
d_->numeric_value = value.to_number();
d_->is_date_ = true;
return *this;
}
value &value::operator=(const date &value)
{
d_->type_ = type::numeric;
auto base_year = worksheet(d_->parent_).get_parent().get_properties().excel_base_date;
d_->numeric_value = value.to_number(base_year);
d_->is_date_ = true;
return *this;
}
value &value::operator=(const datetime &value)
{
d_->type_ = type::numeric;
auto base_year = worksheet(d_->parent_).get_parent().get_properties().excel_base_date;
d_->numeric_value = value.to_number(base_year);
d_->is_date_ = true;
return *this;
}
value &value::operator=(const timedelta &value)
{
d_->type_ = type::numeric;
d_->numeric_value = value.to_number();
d_->is_date_ = true;
return *this;
}
*/
bool operator==(bool comparand, const xlnt::value &value)
{
return value == comparand;
}
bool operator==(int comparand, const xlnt::value &value)
{
return value == comparand;
}
bool operator==(const char *comparand, const xlnt::value &value)
{
return value == comparand;
}
bool operator==(const std::string &comparand, const xlnt::value &value)
{
return value == comparand;
}
bool operator==(const time &comparand, const xlnt::value &value)
{
return value == comparand;
}
bool operator==(const date &comparand, const xlnt::value &value)
{
return value == comparand;
}
bool operator==(const datetime &comparand, const xlnt::value &value)
{
return value == comparand;
}
void swap(value &left, value &right)
{
using std::swap;
swap(left.type_, right.type_);
swap(left.string_value_, right.string_value_);
swap(left.numeric_value_, right.numeric_value_);
}
} // namespace xlnt

View File

@ -2,6 +2,7 @@
#include "worksheet/worksheet.hpp" #include "worksheet/worksheet.hpp"
#include "cell/cell.hpp" #include "cell/cell.hpp"
#include "cell/value.hpp"
#include "common/datetime.hpp" #include "common/datetime.hpp"
#include "worksheet/range.hpp" #include "worksheet/range.hpp"
#include "worksheet/range_reference.hpp" #include "worksheet/range_reference.hpp"
@ -108,7 +109,7 @@ void worksheet::garbage_collect()
{ {
cell current_cell(&cell_iter->second); cell current_cell(&cell_iter->second);
if(current_cell.get_data_type() == cell::type::null && !current_cell.has_comment()) if(current_cell.garbage_collectible())
{ {
cell_iter = cell_map_iter->second.erase(cell_iter); cell_iter = cell_map_iter->second.erase(cell_iter);
continue; continue;
@ -174,6 +175,16 @@ void worksheet::unfreeze_panes()
d_->freeze_panes_ = cell_reference("A1"); d_->freeze_panes_ = cell_reference("A1");
} }
const std::unordered_map<column_t, double> &worksheet::get_column_dimensions() const
{
return d_->column_dimensions_;
}
const std::unordered_map<row_t, double> &worksheet::get_row_dimensions() const
{
return d_->row_dimensions_;
}
cell worksheet::get_cell(const cell_reference &reference) cell worksheet::get_cell(const cell_reference &reference)
{ {
if(d_->cell_map_.find(reference.get_row_index()) == d_->cell_map_.end()) if(d_->cell_map_.find(reference.get_row_index()) == d_->cell_map_.end())
@ -323,10 +334,19 @@ void worksheet::merge_cells(const range_reference &reference)
for(auto cell : row) for(auto cell : row)
{ {
cell.set_merged(true); cell.set_merged(true);
if(!first) if(!first)
{ {
cell.set_null(); if(cell.get_value().is(value::type::string))
{
cell.set_value(value(""));
} }
else
{
cell.set_value(value::null());
}
}
first = false; first = false;
} }
} }
@ -365,7 +385,7 @@ void worksheet::append(const std::vector<std::string> &cells)
for(auto cell : cells) for(auto cell : cells)
{ {
this->get_cell(cell_reference(column++, row)) = cell; get_cell(cell_reference(column++, row)).set_value(cell);
} }
} }
@ -382,7 +402,7 @@ void worksheet::append(const std::vector<int> &cells)
for(auto cell : cells) for(auto cell : cells)
{ {
this->get_cell(cell_reference(column++, row)) = cell; get_cell(cell_reference(column++, row)).set_value(value(cell));
} }
} }
@ -399,7 +419,7 @@ void worksheet::append(const std::vector<date> &cells)
for(auto cell : cells) for(auto cell : cells)
{ {
this->get_cell(cell_reference(column++, row)) = cell; get_cell(cell_reference(column++, row)).set_value(cell);
} }
} }
@ -414,7 +434,7 @@ void worksheet::append(const std::unordered_map<std::string, std::string> &cells
for(auto cell : cells) for(auto cell : cells)
{ {
this->get_cell(cell_reference(cell.first, row + 1)) = cell.second; get_cell(cell_reference(cell.first, row + 1)).set_value(cell.second);
} }
} }
@ -429,7 +449,7 @@ void worksheet::append(const std::unordered_map<int, std::string> &cells)
for(auto cell : cells) for(auto cell : cells)
{ {
this->get_cell(cell_reference(cell.first, row)) = cell.second; get_cell(cell_reference(cell.first, row)).set_value(cell.second);
} }
} }
@ -558,4 +578,64 @@ std::vector<std::string> worksheet::get_formula_attributes() const
return {}; return {};
} }
cell_reference worksheet::get_point_pos(int left, int top) const
{
static const double DefaultColumnWidth = 51.85;
static const double DefaultRowHeight = 15.0;
auto points_to_pixels = [](double value, double dpi) { return (int)std::ceil(value * dpi / 72); };
auto default_height = points_to_pixels(DefaultRowHeight, 96.0);
auto default_width = points_to_pixels(DefaultColumnWidth, 96.0);
column_t current_column = 0;
row_t current_row = 0;
int left_pos = 0;
int top_pos = 0;
while(left_pos <= left)
{
current_column++;
if(d_->column_dimensions_.find(current_column) != d_->column_dimensions_.end())
{
auto cdw = d_->column_dimensions_.at(current_column);
if(cdw >= 0)
{
left_pos += points_to_pixels(cdw, 96.0);
continue;
}
}
left_pos += default_width;
}
while(top_pos <= top)
{
current_row++;
if(d_->row_dimensions_.find(current_row) != d_->row_dimensions_.end())
{
auto cdh = d_->column_dimensions_.at(current_row);
if(cdh >= 0)
{
top_pos += points_to_pixels(cdh, 96.0);
continue;
}
}
top_pos += default_height;
}
return {current_column - 1, current_row - 1};
}
cell_reference worksheet::get_point_pos(const std::pair<int, int> &point) const
{
return get_point_pos(point.first, point.second);
}
} // namespace xlnt } // namespace xlnt

View File

@ -9,6 +9,7 @@
#include "writer/writer.hpp" #include "writer/writer.hpp"
#include "cell/cell.hpp" #include "cell/cell.hpp"
#include "cell/value.hpp"
#include "constants.hpp" #include "constants.hpp"
#include "worksheet/range.hpp" #include "worksheet/range.hpp"
#include "worksheet/range_reference.hpp" #include "worksheet/range_reference.hpp"
@ -313,7 +314,7 @@ std::string writer::write_worksheet(worksheet ws, const std::vector<std::string>
min = std::min(min, cell_reference::column_index_from_string(cell.get_column())); min = std::min(min, cell_reference::column_index_from_string(cell.get_column()));
max = std::max(max, cell_reference::column_index_from_string(cell.get_column())); max = std::max(max, cell_reference::column_index_from_string(cell.get_column()));
if(cell.get_data_type() != cell::type::null || cell.is_merged() || cell.has_comment() || cell.has_formula()) if(!cell.garbage_collectible())
{ {
any_non_null = true; any_non_null = true;
} }
@ -345,7 +346,7 @@ std::string writer::write_worksheet(worksheet ws, const std::vector<std::string>
for(auto cell : row) for(auto cell : row)
{ {
if(cell.get_data_type() != cell::type::null || cell.is_merged() || cell.has_comment() || cell.has_formula()) if(!cell.garbage_collectible())
{ {
if(cell.has_hyperlink()) if(cell.has_hyperlink())
{ {
@ -355,20 +356,20 @@ std::string writer::write_worksheet(worksheet ws, const std::vector<std::string>
auto cell_node = row_node.append_child("c"); auto cell_node = row_node.append_child("c");
cell_node.append_attribute("r").set_value(cell.get_reference().to_string().c_str()); cell_node.append_attribute("r").set_value(cell.get_reference().to_string().c_str());
if(cell.get_data_type() == cell::type::string) if(cell.get_value().is(value::type::string))
{ {
if(cell.has_formula()) if(cell.has_formula())
{ {
cell_node.append_attribute("t").set_value("str"); cell_node.append_attribute("t").set_value("str");
cell_node.append_child("f").text().set(cell.get_formula().c_str()); cell_node.append_child("f").text().set(cell.get_formula().c_str());
cell_node.append_child("v").text().set(cell.get_internal_value_string().c_str()); cell_node.append_child("v").text().set(cell.get_value().to_string().c_str());
continue; continue;
} }
int match_index = -1; int match_index = -1;
for(int i = 0; i < (int)string_table.size(); i++) for(int i = 0; i < (int)string_table.size(); i++)
{ {
if(string_table[i] == cell.get_internal_value_string()) if(string_table[i] == cell.get_value().as<std::string>())
{ {
match_index = i; match_index = i;
break; break;
@ -376,10 +377,17 @@ std::string writer::write_worksheet(worksheet ws, const std::vector<std::string>
} }
if(match_index == -1) if(match_index == -1)
{
if(cell.get_value().as<std::string>().empty())
{
cell_node.append_attribute("t").set_value("s");
}
else
{ {
cell_node.append_attribute("t").set_value("inlineStr"); cell_node.append_attribute("t").set_value("inlineStr");
auto inline_string_node = cell_node.append_child("is"); auto inline_string_node = cell_node.append_child("is");
inline_string_node.append_child("t").text().set(cell.get_internal_value_string().c_str()); inline_string_node.append_child("t").text().set(cell.get_value().as<std::string>().c_str());
}
} }
else else
{ {
@ -390,32 +398,32 @@ std::string writer::write_worksheet(worksheet ws, const std::vector<std::string>
} }
else else
{ {
if(cell.get_data_type() != cell::type::null) if(!cell.get_value().is(value::type::null))
{ {
if(cell.get_data_type() == cell::type::boolean) if(cell.get_value().is(value::type::boolean))
{ {
cell_node.append_attribute("t").set_value("b"); cell_node.append_attribute("t").set_value("b");
auto value_node = cell_node.append_child("v"); auto value_node = cell_node.append_child("v");
value_node.text().set(cell.get_internal_value_numeric() == 0 ? 0 : 1); value_node.text().set(cell.get_value().as<bool>() ? 1 : 0);
} }
else if(cell.get_data_type() == cell::type::numeric) else if(cell.get_value().is(value::type::numeric))
{ {
if(cell.has_formula()) if(cell.has_formula())
{ {
cell_node.append_child("f").text().set(cell.get_formula().c_str()); cell_node.append_child("f").text().set(cell.get_formula().c_str());
cell_node.append_child("v").text().set(std::to_string(cell.get_internal_value_numeric()).c_str()); cell_node.append_child("v").text().set(cell.get_value().to_string().c_str());
continue; continue;
} }
cell_node.append_attribute("t").set_value("n"); cell_node.append_attribute("t").set_value("n");
auto value_node = cell_node.append_child("v"); auto value_node = cell_node.append_child("v");
if(std::floor(cell.get_internal_value_numeric()) == cell.get_internal_value_numeric()) if(cell.get_value().is_integral())
{ {
value_node.text().set((long long)cell.get_internal_value_numeric()); value_node.text().set(cell.get_value().as<long long>());
} }
else else
{ {
value_node.text().set((double)cell.get_internal_value_numeric()); value_node.text().set(cell.get_value().as<double>());
} }
} }
} }

View File

@ -204,61 +204,61 @@ public:
static class TestDescription_suite_test_cell_test_time : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_time : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_time() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 326, "test_time" ) {} TestDescription_suite_test_cell_test_time() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 321, "test_time" ) {}
void runTest() { suite_test_cell.test_time(); } void runTest() { suite_test_cell.test_time(); }
} testDescription_suite_test_cell_test_time; } testDescription_suite_test_cell_test_time;
static class TestDescription_suite_test_cell_test_timedelta : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_timedelta : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_timedelta() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 340, "test_timedelta" ) {} TestDescription_suite_test_cell_test_timedelta() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 335, "test_timedelta" ) {}
void runTest() { suite_test_cell.test_timedelta(); } void runTest() { suite_test_cell.test_timedelta(); }
} testDescription_suite_test_cell_test_timedelta; } testDescription_suite_test_cell_test_timedelta;
static class TestDescription_suite_test_cell_test_date_format_on_non_date : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_date_format_on_non_date : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_date_format_on_non_date() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 350, "test_date_format_on_non_date" ) {} TestDescription_suite_test_cell_test_date_format_on_non_date() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 345, "test_date_format_on_non_date" ) {}
void runTest() { suite_test_cell.test_date_format_on_non_date(); } void runTest() { suite_test_cell.test_date_format_on_non_date(); }
} testDescription_suite_test_cell_test_date_format_on_non_date; } testDescription_suite_test_cell_test_date_format_on_non_date;
static class TestDescription_suite_test_cell_test_set_get_date : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_set_get_date : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_set_get_date() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 360, "test_set_get_date" ) {} TestDescription_suite_test_cell_test_set_get_date() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 355, "test_set_get_date" ) {}
void runTest() { suite_test_cell.test_set_get_date(); } void runTest() { suite_test_cell.test_set_get_date(); }
} testDescription_suite_test_cell_test_set_get_date; } testDescription_suite_test_cell_test_set_get_date;
static class TestDescription_suite_test_cell_test_repr : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_repr : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_repr() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 371, "test_repr" ) {} TestDescription_suite_test_cell_test_repr() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 366, "test_repr" ) {}
void runTest() { suite_test_cell.test_repr(); } void runTest() { suite_test_cell.test_repr(); }
} testDescription_suite_test_cell_test_repr; } testDescription_suite_test_cell_test_repr;
static class TestDescription_suite_test_cell_test_is_date : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_is_date : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_is_date() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 379, "test_is_date" ) {} TestDescription_suite_test_cell_test_is_date() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 374, "test_is_date" ) {}
void runTest() { suite_test_cell.test_is_date(); } void runTest() { suite_test_cell.test_is_date(); }
} testDescription_suite_test_cell_test_is_date; } testDescription_suite_test_cell_test_is_date;
static class TestDescription_suite_test_cell_test_is_not_date_color_format : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_is_not_date_color_format : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_is_not_date_color_format() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 393, "test_is_not_date_color_format" ) {} TestDescription_suite_test_cell_test_is_not_date_color_format() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 388, "test_is_not_date_color_format" ) {}
void runTest() { suite_test_cell.test_is_not_date_color_format(); } void runTest() { suite_test_cell.test_is_not_date_color_format(); }
} testDescription_suite_test_cell_test_is_not_date_color_format; } testDescription_suite_test_cell_test_is_not_date_color_format;
static class TestDescription_suite_test_cell_test_comment_count : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_comment_count : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_comment_count() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 404, "test_comment_count" ) {} TestDescription_suite_test_cell_test_comment_count() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 399, "test_comment_count" ) {}
void runTest() { suite_test_cell.test_comment_count(); } void runTest() { suite_test_cell.test_comment_count(); }
} testDescription_suite_test_cell_test_comment_count; } testDescription_suite_test_cell_test_comment_count;
static class TestDescription_suite_test_cell_test_comment_assignment : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_comment_assignment : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_comment_assignment() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 420, "test_comment_assignment" ) {} TestDescription_suite_test_cell_test_comment_assignment() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 415, "test_comment_assignment" ) {}
void runTest() { suite_test_cell.test_comment_assignment(); } void runTest() { suite_test_cell.test_comment_assignment(); }
} testDescription_suite_test_cell_test_comment_assignment; } testDescription_suite_test_cell_test_comment_assignment;
static class TestDescription_suite_test_cell_test_cell_offset : public CxxTest::RealTestDescription { static class TestDescription_suite_test_cell_test_cell_offset : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_cell_test_cell_offset() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 434, "test_cell_offset" ) {} TestDescription_suite_test_cell_test_cell_offset() : CxxTest::RealTestDescription( Tests_test_cell, suiteDescription_test_cell, 429, "test_cell_offset" ) {}
void runTest() { suite_test_cell.test_cell_offset(); } void runTest() { suite_test_cell.test_cell_offset(); }
} testDescription_suite_test_cell_test_cell_offset; } testDescription_suite_test_cell_test_cell_offset;
@ -723,69 +723,63 @@ public:
void runTest() { suite_test_read.test_read_cell_formulae(); } void runTest() { suite_test_read.test_read_cell_formulae(); }
} testDescription_suite_test_read_test_read_cell_formulae; } testDescription_suite_test_read_test_read_cell_formulae;
static class TestDescription_suite_test_read_test_read_complex_formulae : public CxxTest::RealTestDescription {
public:
TestDescription_suite_test_read_test_read_complex_formulae() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 223, "test_read_complex_formulae" ) {}
void runTest() { suite_test_read.test_read_complex_formulae(); }
} testDescription_suite_test_read_test_read_complex_formulae;
static class TestDescription_suite_test_read_test_data_only : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_data_only : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_data_only() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 282, "test_data_only" ) {} TestDescription_suite_test_read_test_data_only() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 280, "test_data_only" ) {}
void runTest() { suite_test_read.test_data_only(); } void runTest() { suite_test_read.test_data_only(); }
} testDescription_suite_test_read_test_data_only; } testDescription_suite_test_read_test_data_only;
static class TestDescription_suite_test_read_test_detect_worksheets : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_detect_worksheets : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_detect_worksheets() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 304, "test_detect_worksheets" ) {} TestDescription_suite_test_read_test_detect_worksheets() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 302, "test_detect_worksheets" ) {}
void runTest() { suite_test_read.test_detect_worksheets(); } void runTest() { suite_test_read.test_detect_worksheets(); }
} testDescription_suite_test_read_test_detect_worksheets; } testDescription_suite_test_read_test_detect_worksheets;
static class TestDescription_suite_test_read_test_read_rels : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_read_rels : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_read_rels() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 344, "test_read_rels" ) {} TestDescription_suite_test_read_test_read_rels() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 342, "test_read_rels" ) {}
void runTest() { suite_test_read.test_read_rels(); } void runTest() { suite_test_read.test_read_rels(); }
} testDescription_suite_test_read_test_read_rels; } testDescription_suite_test_read_test_read_rels;
static class TestDescription_suite_test_read_test_read_content_types : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_read_content_types : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_read_content_types() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 382, "test_read_content_types" ) {} TestDescription_suite_test_read_test_read_content_types() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 380, "test_read_content_types" ) {}
void runTest() { suite_test_read.test_read_content_types(); } void runTest() { suite_test_read.test_read_content_types(); }
} testDescription_suite_test_read_test_read_content_types; } testDescription_suite_test_read_test_read_content_types;
static class TestDescription_suite_test_read_test_read_sheets : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_read_sheets : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_read_sheets() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 418, "test_read_sheets" ) {} TestDescription_suite_test_read_test_read_sheets() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 416, "test_read_sheets" ) {}
void runTest() { suite_test_read.test_read_sheets(); } void runTest() { suite_test_read.test_read_sheets(); }
} testDescription_suite_test_read_test_read_sheets; } testDescription_suite_test_read_test_read_sheets;
static class TestDescription_suite_test_read_test_guess_types : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_guess_types : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_guess_types() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 439, "test_guess_types" ) {} TestDescription_suite_test_read_test_guess_types() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 437, "test_guess_types" ) {}
void runTest() { suite_test_read.test_guess_types(); } void runTest() { suite_test_read.test_guess_types(); }
} testDescription_suite_test_read_test_guess_types; } testDescription_suite_test_read_test_guess_types;
static class TestDescription_suite_test_read_test_read_autofilter : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_read_autofilter : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_read_autofilter() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 455, "test_read_autofilter" ) {} TestDescription_suite_test_read_test_read_autofilter() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 453, "test_read_autofilter" ) {}
void runTest() { suite_test_read.test_read_autofilter(); } void runTest() { suite_test_read.test_read_autofilter(); }
} testDescription_suite_test_read_test_read_autofilter; } testDescription_suite_test_read_test_read_autofilter;
static class TestDescription_suite_test_read_test_bad_formats_xlsb : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_bad_formats_xlsb : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_bad_formats_xlsb() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 463, "test_bad_formats_xlsb" ) {} TestDescription_suite_test_read_test_bad_formats_xlsb() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 461, "test_bad_formats_xlsb" ) {}
void runTest() { suite_test_read.test_bad_formats_xlsb(); } void runTest() { suite_test_read.test_bad_formats_xlsb(); }
} testDescription_suite_test_read_test_bad_formats_xlsb; } testDescription_suite_test_read_test_bad_formats_xlsb;
static class TestDescription_suite_test_read_test_bad_formats_xls : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_bad_formats_xls : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_bad_formats_xls() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 469, "test_bad_formats_xls" ) {} TestDescription_suite_test_read_test_bad_formats_xls() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 467, "test_bad_formats_xls" ) {}
void runTest() { suite_test_read.test_bad_formats_xls(); } void runTest() { suite_test_read.test_bad_formats_xls(); }
} testDescription_suite_test_read_test_bad_formats_xls; } testDescription_suite_test_read_test_bad_formats_xls;
static class TestDescription_suite_test_read_test_bad_formats_no : public CxxTest::RealTestDescription { static class TestDescription_suite_test_read_test_bad_formats_no : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_read_test_bad_formats_no() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 475, "test_bad_formats_no" ) {} TestDescription_suite_test_read_test_bad_formats_no() : CxxTest::RealTestDescription( Tests_test_read, suiteDescription_test_read, 473, "test_bad_formats_no" ) {}
void runTest() { suite_test_read.test_bad_formats_no(); } void runTest() { suite_test_read.test_bad_formats_no(); }
} testDescription_suite_test_read_test_bad_formats_no; } testDescription_suite_test_read_test_bad_formats_no;
@ -1222,49 +1216,49 @@ public:
static class TestDescription_suite_test_worksheet_test_page_margins : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_page_margins : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_page_margins() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 311, "test_page_margins" ) {} TestDescription_suite_test_worksheet_test_page_margins() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 330, "test_page_margins" ) {}
void runTest() { suite_test_worksheet.test_page_margins(); } void runTest() { suite_test_worksheet.test_page_margins(); }
} testDescription_suite_test_worksheet_test_page_margins; } testDescription_suite_test_worksheet_test_page_margins;
static class TestDescription_suite_test_worksheet_test_merge : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_merge : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_merge() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 323, "test_merge" ) {} TestDescription_suite_test_worksheet_test_merge() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 368, "test_merge" ) {}
void runTest() { suite_test_worksheet.test_merge(); } void runTest() { suite_test_worksheet.test_merge(); }
} testDescription_suite_test_worksheet_test_merge; } testDescription_suite_test_worksheet_test_merge;
static class TestDescription_suite_test_worksheet_test_printer_settings : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_printer_settings : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_printer_settings() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 335, "test_printer_settings" ) {} TestDescription_suite_test_worksheet_test_printer_settings() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 477, "test_printer_settings" ) {}
void runTest() { suite_test_worksheet.test_printer_settings(); } void runTest() { suite_test_worksheet.test_printer_settings(); }
} testDescription_suite_test_worksheet_test_printer_settings; } testDescription_suite_test_worksheet_test_printer_settings;
static class TestDescription_suite_test_worksheet_test_header_footer : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_header_footer : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_header_footer() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 357, "test_header_footer" ) {} TestDescription_suite_test_worksheet_test_header_footer() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 497, "test_header_footer" ) {}
void runTest() { suite_test_worksheet.test_header_footer(); } void runTest() { suite_test_worksheet.test_header_footer(); }
} testDescription_suite_test_worksheet_test_header_footer; } testDescription_suite_test_worksheet_test_header_footer;
static class TestDescription_suite_test_worksheet_test_positioning_point : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_positioning_point : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_positioning_point() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 434, "test_positioning_point" ) {} TestDescription_suite_test_worksheet_test_positioning_point() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 574, "test_positioning_point" ) {}
void runTest() { suite_test_worksheet.test_positioning_point(); } void runTest() { suite_test_worksheet.test_positioning_point(); }
} testDescription_suite_test_worksheet_test_positioning_point; } testDescription_suite_test_worksheet_test_positioning_point;
static class TestDescription_suite_test_worksheet_test_positioning_roundtrip : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_positioning_roundtrip : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_positioning_roundtrip() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 442, "test_positioning_roundtrip" ) {} TestDescription_suite_test_worksheet_test_positioning_roundtrip() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 580, "test_positioning_roundtrip" ) {}
void runTest() { suite_test_worksheet.test_positioning_roundtrip(); } void runTest() { suite_test_worksheet.test_positioning_roundtrip(); }
} testDescription_suite_test_worksheet_test_positioning_roundtrip; } testDescription_suite_test_worksheet_test_positioning_roundtrip;
static class TestDescription_suite_test_worksheet_test_page_setup : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_page_setup : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_page_setup() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 453, "test_page_setup" ) {} TestDescription_suite_test_worksheet_test_page_setup() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 588, "test_page_setup" ) {}
void runTest() { suite_test_worksheet.test_page_setup(); } void runTest() { suite_test_worksheet.test_page_setup(); }
} testDescription_suite_test_worksheet_test_page_setup; } testDescription_suite_test_worksheet_test_page_setup;
static class TestDescription_suite_test_worksheet_test_page_options : public CxxTest::RealTestDescription { static class TestDescription_suite_test_worksheet_test_page_options : public CxxTest::RealTestDescription {
public: public:
TestDescription_suite_test_worksheet_test_page_options() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 464, "test_page_options" ) {} TestDescription_suite_test_worksheet_test_page_options() : CxxTest::RealTestDescription( Tests_test_worksheet, suiteDescription_test_worksheet, 596, "test_page_options" ) {}
void runTest() { suite_test_worksheet.test_page_options(); } void runTest() { suite_test_worksheet.test_page_options(); }
} testDescription_suite_test_worksheet_test_page_options; } testDescription_suite_test_worksheet_test_page_options;

View File

@ -100,7 +100,7 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1", "17.5"); xlnt::cell cell(ws, "A1", "17.5");
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
} }
void test_1st() void test_1st()
@ -108,16 +108,16 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
TS_ASSERT_EQUALS(xlnt::cell::type::null, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::null));
} }
void test_null() void test_null()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1", "17.5"); xlnt::cell cell(ws, "A1", "17.5");
cell.set_null(); cell.set_value(xlnt::value::null());
TS_ASSERT_EQUALS(xlnt::cell::type::null, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::null));
} }
void test_numeric() void test_numeric()
@ -127,41 +127,41 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = 42; cell.set_value(42);
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "4.2"; cell.set_value("4.2");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "-42.000"; cell.set_value("-42.000");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "0"; cell.set_value("0");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = 0; cell.set_value(0);
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = 0.0001; cell.set_value(0.0001);
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "0.9999"; cell.set_value("0.9999");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "99E-02"; cell.set_value("99E-02");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = 1e1; cell.set_value(1e1);
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "4"; cell.set_value("4");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "-1E3"; cell.set_value("-1E3");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = 4; cell.set_value(4);
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
} }
void test_string() void test_string()
@ -169,16 +169,16 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = "hello"; cell.set_value("hello");
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
} }
void test_single_dot() void test_single_dot()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = "."; cell.set_value(".");
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
} }
void test_formula() void test_formula()
@ -197,18 +197,18 @@ public:
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = true; cell.set_value(true);
TS_ASSERT_EQUALS(xlnt::cell::type::boolean, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::boolean));
cell = false; cell.set_value(false);
TS_ASSERT_EQUALS(xlnt::cell::type::boolean, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::boolean));
} }
void test_leading_zero() void test_leading_zero()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = "0800"; cell.set_value("0800");
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
} }
void test_error_codes() void test_error_codes()
@ -218,8 +218,8 @@ public:
for(auto error : xlnt::cell::ErrorCodes) for(auto error : xlnt::cell::ErrorCodes)
{ {
cell = error.first; cell.set_error(error.first);
TS_ASSERT_EQUALS(xlnt::cell::type::error, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::error));
} }
} }
@ -227,32 +227,32 @@ public:
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = 3.14; cell.set_value(3.14);
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
} }
void test_insert_percentage() void test_insert_percentage()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = "3.14%"; cell.set_value("3.14%");
TS_ASSERT_DELTA(0.0314, cell.get_internal_value_numeric(), 1e-7); TS_ASSERT_DELTA(0.0314, cell.get_value().as<double>(), 1e-7);
} }
void test_insert_datetime() void test_insert_datetime()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = xlnt::date::today(); cell.set_value(xlnt::date::today());
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
} }
void test_insert_date() void test_insert_date()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = xlnt::datetime::now(); cell.set_value(xlnt::datetime::now());
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
} }
void test_internal_date() void test_internal_date()
@ -260,8 +260,8 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
xlnt::datetime dt(2010, 7, 13, 6, 37, 41); xlnt::datetime dt(2010, 7, 13, 6, 37, 41);
cell = dt; cell.set_value(dt);
TS_ASSERT_EQUALS(40372.27616898148, cell.get_internal_value_numeric()); TS_ASSERT_EQUALS(40372.27616898148, cell.get_value().as<double>());
} }
void test_datetime_interpretation() void test_datetime_interpretation()
@ -269,9 +269,9 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
xlnt::datetime dt(2010, 7, 13, 6, 37, 41); xlnt::datetime dt(2010, 7, 13, 6, 37, 41);
cell = dt; cell.set_value(dt);
TS_ASSERT_EQUALS(cell, dt); TS_ASSERT_EQUALS(cell.get_value(), dt);
TS_ASSERT_DELTA(cell.get_internal_value_numeric(), 40372.27616898148, 1e-7); TS_ASSERT_DELTA(cell.get_value().as<double>(), 40372.27616898148, 1e-7);
} }
void test_date_interpretation() void test_date_interpretation()
@ -279,16 +279,16 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
xlnt::date dt(2010, 7, 13); xlnt::date dt(2010, 7, 13);
cell = dt; cell.set_value(dt);
TS_ASSERT_EQUALS(cell, dt); TS_ASSERT_EQUALS(cell.get_value(), dt);
TS_ASSERT_EQUALS(cell.get_internal_value_numeric(), 40372); TS_ASSERT_EQUALS(cell.get_value().as<int>(), 40372);
} }
void test_number_format_style() void test_number_format_style()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = "12.6%"; cell.set_value("12.6%");
TS_ASSERT_EQUALS(xlnt::number_format::format::percentage, cell.get_style().get_number_format().get_format_code()); TS_ASSERT_EQUALS(xlnt::number_format::format::percentage, cell.get_style().get_number_format().get_format_code());
} }
@ -297,27 +297,22 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
TS_ASSERT_EQUALS(xlnt::cell::type::null, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::null));
cell = ".0e000"; cell.set_value(".0e000");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "-0.e-0"; cell.set_value("-0.e-0");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
cell = "1E"; cell.set_value("1E");
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
} }
void test_set_bad_type() void test_set_bad_type()
{ {
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
TS_ASSERT_THROWS(cell.set_explicit_value("ABC", xlnt::cell::type::numeric), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_explicit_value(1, xlnt::cell::type::string), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_explicit_value(1.0, xlnt::cell::type::error), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_explicit_value("3", xlnt::cell::type::boolean), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_error("1"), xlnt::data_type_exception); TS_ASSERT_THROWS(cell.set_error("1"), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_hyperlink("1"), xlnt::data_type_exception); TS_ASSERT_THROWS(cell.set_hyperlink("1"), xlnt::data_type_exception);
} }
@ -328,13 +323,13 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = "03:40:16"; cell.set_value("03:40:16");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
TS_ASSERT_EQUALS(cell, xlnt::time(3, 40, 16)); TS_ASSERT_EQUALS(cell.get_value(), xlnt::time(3, 40, 16));
cell = "03:40"; cell.set_value("03:40");
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type()); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
TS_ASSERT_EQUALS(cell, xlnt::time(3, 40)); TS_ASSERT_EQUALS(cell.get_value(), xlnt::time(3, 40));
} }
void test_timedelta() void test_timedelta()
@ -342,9 +337,9 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = xlnt::timedelta(1, 3, 0, 0, 0); cell.set_value(xlnt::timedelta(1, 3, 0, 0, 0));
TS_ASSERT_EQUALS(cell, 1.125); TS_ASSERT_EQUALS(cell.get_value(), 1.125);
TS_ASSERT_EQUALS(cell.get_data_type(), xlnt::cell::type::numeric); TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
} }
void test_date_format_on_non_date() void test_date_format_on_non_date()
@ -352,9 +347,9 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = xlnt::datetime::now(); cell.set_value(xlnt::datetime::now());
cell = "testme"; cell.set_value("testme");
TS_ASSERT("testme" == cell); TS_ASSERT("testme" == cell.get_value());
} }
void test_set_get_date() void test_set_get_date()
@ -364,8 +359,8 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = today; cell.set_value(today);
TS_ASSERT(today == cell); TS_ASSERT(today == cell.get_value());
} }
void test_repr() void test_repr()
@ -381,11 +376,11 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = xlnt::datetime::now(); cell.set_value(xlnt::datetime::now());
TS_ASSERT(cell.is_date()); TS_ASSERT(cell.is_date());
cell = "testme"; cell.set_value("testme");
TS_ASSERT_EQUALS("testme", cell); TS_ASSERT_EQUALS("testme", cell.get_value());
TS_ASSERT(!cell.is_date()); TS_ASSERT(!cell.is_date());
} }
@ -395,7 +390,7 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = -13.5; cell.set_value(-13.5);
cell.get_style().get_number_format().set_format_code("0.00_);[Red]\\(0.00\\)"); cell.get_style().get_number_format().set_format_code("0.00_);[Red]\\(0.00\\)");
TS_ASSERT(!cell.is_date()); TS_ASSERT(!cell.is_date());

View File

@ -22,9 +22,9 @@ public:
TS_ASSERT_DIFFERS(ws, nullptr); TS_ASSERT_DIFFERS(ws, nullptr);
if(!(ws == nullptr)) if(!(ws == nullptr))
{ {
TS_ASSERT_EQUALS(ws.get_cell("G5"), "hello"); TS_ASSERT_EQUALS(ws.get_cell("G5").get_value(), "hello");
TS_ASSERT_EQUALS(ws.get_cell("D30"), 30); TS_ASSERT_EQUALS(ws.get_cell("D30").get_value(), 30);
TS_ASSERT_EQUALS(ws.get_cell("K9"), 0.09); TS_ASSERT_EQUALS(ws.get_cell("K9").get_value(), 0.09);
} }
} }
@ -52,10 +52,10 @@ public:
auto wb = standard_workbook(); auto wb = standard_workbook();
auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers"); auto sheet2 = wb.get_sheet_by_name("Sheet2 - Numbers");
TS_ASSERT_DIFFERS(sheet2, nullptr); TS_ASSERT_DIFFERS(sheet2, nullptr);
TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5")); TS_ASSERT_EQUALS("This is cell G5", sheet2.get_cell("G5").get_value());
TS_ASSERT_EQUALS(18, sheet2.get_cell("D18")); TS_ASSERT_EQUALS(18, sheet2.get_cell("D18").get_value());
TS_ASSERT_EQUALS(true, sheet2.get_cell("G9")); TS_ASSERT_EQUALS(true, sheet2.get_cell("G9").get_value());
TS_ASSERT_EQUALS(false, sheet2.get_cell("G10")); TS_ASSERT_EQUALS(false, sheet2.get_cell("G10").get_value());
} }
void test_read_nostring_workbook() void test_read_nostring_workbook()
@ -179,9 +179,9 @@ public:
auto wb_win = date_std_1900(); auto wb_win = date_std_1900();
auto ws_win = wb_win["Sheet1"]; auto ws_win = wb_win["Sheet1"];
xlnt::datetime dt(2011, 10, 31); xlnt::datetime dt(2011, 10, 31);
TS_ASSERT_EQUALS(ws_mac.get_cell("A1"), dt); TS_ASSERT_EQUALS(ws_mac.get_cell("A1").get_value(), dt);
TS_ASSERT_EQUALS(ws_win.get_cell("A1"), dt); TS_ASSERT_EQUALS(ws_win.get_cell("A1").get_value(), dt);
TS_ASSERT_EQUALS(ws_mac.get_cell("A1"), ws_win.get_cell("A1")); TS_ASSERT_EQUALS(ws_mac.get_cell("A1").get_value(), ws_win.get_cell("A1").get_value());
} }
void test_repair_central_directory() void test_repair_central_directory()
@ -220,10 +220,8 @@ public:
TS_ASSERT_EQUALS(a6.get_formula(), "SUM(A4:A5)"); TS_ASSERT_EQUALS(a6.get_formula(), "SUM(A4:A5)");
} }
void test_read_complex_formulae() void _test_read_complex_formulae()
{ {
TS_SKIP("not yet implemented");
/* /*
auto path = PathHelper::GetDataDirectory("/reader/formulae.xlsx"); auto path = PathHelper::GetDataDirectory("/reader/formulae.xlsx");
auto wb = xlnt::reader::load_workbook(path); auto wb = xlnt::reader::load_workbook(path);
@ -287,17 +285,17 @@ public:
TS_ASSERT(ws.get_formula_attributes().empty()); TS_ASSERT(ws.get_formula_attributes().empty());
TS_ASSERT(ws.get_parent().get_data_only()); TS_ASSERT(ws.get_parent().get_data_only());
TS_ASSERT(ws.get_cell("A2").get_data_type() == xlnt::cell::type::numeric); TS_ASSERT(ws.get_cell("A2").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A2") == 12345); TS_ASSERT(ws.get_cell("A2").get_value() == 12345);
TS_ASSERT(!ws.get_cell("A2").has_formula()); TS_ASSERT(!ws.get_cell("A2").has_formula());
TS_ASSERT(ws.get_cell("A3").get_data_type() == xlnt::cell::type::numeric); TS_ASSERT(ws.get_cell("A3").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A3") == 12345); TS_ASSERT(ws.get_cell("A3").get_value() == 12345);
TS_ASSERT(!ws.get_cell("A3").has_formula()); TS_ASSERT(!ws.get_cell("A3").has_formula());
TS_ASSERT(ws.get_cell("A4").get_data_type() == xlnt::cell::type::numeric); TS_ASSERT(ws.get_cell("A4").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A4") == 24690); TS_ASSERT(ws.get_cell("A4").get_value() == 24690);
TS_ASSERT(!ws.get_cell("A4").has_formula()); TS_ASSERT(!ws.get_cell("A4").has_formula());
TS_ASSERT(ws.get_cell("A5").get_data_type() == xlnt::cell::type::numeric); TS_ASSERT(ws.get_cell("A5").get_value().is(xlnt::value::type::numeric));
TS_ASSERT(ws.get_cell("A5") == 49380); TS_ASSERT(ws.get_cell("A5").get_value() == 49380);
TS_ASSERT(!ws.get_cell("A5").has_formula()); TS_ASSERT(!ws.get_cell("A5").has_formula());
} }
@ -439,8 +437,8 @@ public:
void test_guess_types() void test_guess_types()
{ {
bool guess; bool guess;
xlnt::cell::type dtype; xlnt::value::type dtype;
std::vector<std::pair<bool, xlnt::cell::type>> test_cases = {{true, xlnt::cell::type::numeric}, {false, xlnt::cell::type::string}}; std::vector<std::pair<bool, xlnt::value::type>> test_cases = {{true, xlnt::value::type::numeric}, {false, xlnt::value::type::string}};
for(const auto &expected : test_cases) for(const auto &expected : test_cases)
{ {
@ -448,7 +446,7 @@ public:
auto path = PathHelper::GetDataDirectory("/genuine/guess_types.xlsx"); auto path = PathHelper::GetDataDirectory("/genuine/guess_types.xlsx");
auto wb = xlnt::reader::load_workbook(path, guess); auto wb = xlnt::reader::load_workbook(path, guess);
auto ws = wb.get_active_sheet(); auto ws = wb.get_active_sheet();
TS_ASSERT_EQUALS(ws.get_cell("D2").get_data_type(), dtype); TS_ASSERT(ws.get_cell("D2").get_value().is(dtype));
} }
} }

View File

@ -193,7 +193,7 @@ public:
double float_value = 1.0 / 3.0; double float_value = 1.0 / 3.0;
xlnt::workbook book; xlnt::workbook book;
auto sheet = book.get_active_sheet(); auto sheet = book.get_active_sheet();
sheet.get_cell("A1") = float_value; sheet.get_cell("A1").set_value(float_value);
auto dest_filename = temp_file.GetFilename(); auto dest_filename = temp_file.GetFilename();
book.save(dest_filename); book.save(dest_filename);
@ -201,6 +201,6 @@ public:
test_book.load(dest_filename); test_book.load(dest_filename);
auto test_sheet = test_book.get_active_sheet(); auto test_sheet = test_book.get_active_sheet();
TS_ASSERT_LESS_THAN_EQUALS(test_sheet.get_cell("A1").get_internal_value_numeric() - float_value, 0.00001); TS_ASSERT_LESS_THAN_EQUALS(test_sheet.get_cell("A1").get_value().as<double>() - float_value, 0.00001);
} }
}; };

View File

@ -56,9 +56,9 @@ public:
void test_worksheet_dimension() void test_worksheet_dimension()
{ {
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
ws.get_cell("A1") = "AAA"; ws.get_cell("A1").set_value("AAA");
TS_ASSERT_EQUALS("A1:A1", ws.calculate_dimension().to_string()); TS_ASSERT_EQUALS("A1:A1", ws.calculate_dimension().to_string());
ws.get_cell("B12") = "AAA"; ws.get_cell("B12").set_value("AAA");
TS_ASSERT_EQUALS("A1:B12", ws.calculate_dimension().to_string()); TS_ASSERT_EQUALS("A1:B12", ws.calculate_dimension().to_string());
} }
@ -132,9 +132,9 @@ public:
{ {
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
ws.get_cell("A1").set_null(); ws.get_cell("A1").set_value(xlnt::value::null());
ws.get_cell("B2") = "0"; ws.get_cell("B2").set_value("0");
ws.get_cell("C4") = 0; ws.get_cell("C4").set_value(0);
ws.get_cell("D1").set_comment(xlnt::comment("Comment", "Comment")); ws.get_cell("D1").set_comment(xlnt::comment("Comment", "Comment"));
ws.garbage_collect(); ws.garbage_collect();
@ -196,8 +196,8 @@ public:
ws.append(std::vector<std::string> {"This is A1", "This is B1"}); ws.append(std::vector<std::string> {"This is A1", "This is B1"});
TS_ASSERT_EQUALS("This is A1", ws.get_cell("A1")); TS_ASSERT_EQUALS("This is A1", ws.get_cell("A1").get_value());
TS_ASSERT_EQUALS("This is B1", ws.get_cell("B1")); TS_ASSERT_EQUALS("This is B1", ws.get_cell("B1").get_value());
} }
void test_append_dict_letter() void test_append_dict_letter()
@ -206,8 +206,8 @@ public:
ws.append(std::unordered_map<std::string, std::string> {{"A", "This is A1"}, {"C", "This is C1"}}); ws.append(std::unordered_map<std::string, std::string> {{"A", "This is A1"}, {"C", "This is C1"}});
TS_ASSERT_EQUALS("This is A1", ws.get_cell("A1")); TS_ASSERT_EQUALS("This is A1", ws.get_cell("A1").get_value());
TS_ASSERT_EQUALS("This is C1", ws.get_cell("C1")); TS_ASSERT_EQUALS("This is C1", ws.get_cell("C1").get_value());
} }
void test_append_dict_index() void test_append_dict_index()
@ -216,8 +216,8 @@ public:
ws.append(std::unordered_map<int, std::string> {{0, "This is A1"}, {2, "This is C1"}}); ws.append(std::unordered_map<int, std::string> {{0, "This is A1"}, {2, "This is C1"}});
TS_ASSERT_EQUALS("This is A1", ws.get_cell("A1")); TS_ASSERT_EQUALS("This is A1", ws.get_cell("A1").get_value());
TS_ASSERT_EQUALS("This is C1", ws.get_cell("C1")); TS_ASSERT_EQUALS("This is C1", ws.get_cell("C1").get_value());
} }
void test_append_2d_list() void test_append_2d_list()
@ -229,40 +229,40 @@ public:
auto vals = ws.get_range("A1:B2"); auto vals = ws.get_range("A1:B2");
TS_ASSERT_EQUALS(vals[0][0], "This is A1"); TS_ASSERT_EQUALS(vals[0][0].get_value(), "This is A1");
TS_ASSERT_EQUALS(vals[0][1], "This is B1"); TS_ASSERT_EQUALS(vals[0][1].get_value(), "This is B1");
TS_ASSERT_EQUALS(vals[1][0], "This is A2"); TS_ASSERT_EQUALS(vals[1][0].get_value(), "This is A2");
TS_ASSERT_EQUALS(vals[1][1], "This is B2"); TS_ASSERT_EQUALS(vals[1][1].get_value(), "This is B2");
} }
void test_rows() void test_rows()
{ {
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
ws.get_cell("A1") = "first"; ws.get_cell("A1").set_value("first");
ws.get_cell("C9") = "last"; ws.get_cell("C9").set_value("last");
auto rows = ws.rows(); auto rows = ws.rows();
TS_ASSERT_EQUALS(rows.length(), 9); TS_ASSERT_EQUALS(rows.length(), 9);
TS_ASSERT_EQUALS(rows[0][0], "first"); TS_ASSERT_EQUALS(rows[0][0].get_value(), "first");
TS_ASSERT_EQUALS(rows[8][2], "last"); TS_ASSERT_EQUALS(rows[8][2].get_value(), "last");
} }
void test_cols() void test_cols()
{ {
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
ws.get_cell("A1") = "first"; ws.get_cell("A1").set_value("first");
ws.get_cell("C9") = "last"; ws.get_cell("C9").set_value("last");
auto cols = ws.columns(); auto cols = ws.columns();
TS_ASSERT_EQUALS(cols.length(), 3); TS_ASSERT_EQUALS(cols.length(), 3);
TS_ASSERT_EQUALS(cols[0][0], "first"); TS_ASSERT_EQUALS(cols[0][0].get_value(), "first");
TS_ASSERT_EQUALS(cols[2][8], "last"); TS_ASSERT_EQUALS(cols[2][8].get_value(), "last");
} }
void test_auto_filter() void test_auto_filter()
@ -298,44 +298,184 @@ public:
void test_write_empty() void test_write_empty()
{ {
TS_SKIP("not implemented");
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
auto xml_string = xlnt::writer::write_worksheet(ws); auto xml_string = xlnt::writer::write_worksheet(ws);
pugi::xml_document doc; pugi::xml_document doc;
doc.load(xml_string.c_str()); doc.load(xml_string.c_str());
auto expected_string =
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <sheetPr>"
" <outlinePr summaryRight=\"1\" summaryBelow=\"1\"/>"
" </sheetPr>"
" <dimension ref=\"A1:A1\"/>"
" <sheetViews>"
" <sheetView workbookViewId=\"0\">"
" <selection sqref=\"A1\" activeCell=\"A1\"/>"
" </sheetView>"
" </sheetViews>"
" <sheetFormatPr baseColWidth=\"10\" defaultRowHeight=\"15\"/>"
" <sheetData/>"
" <pageMargins left=\"0.75\" right=\"0.75\" top=\"1\" bottom=\"1\" header=\"0.5\" footer=\"0.5\"/>"
"</worksheet>";
pugi::xml_document expected_doc;
expected_doc.load(expected_string);
TS_ASSERT(Helper::compare_xml(expected_doc, doc));
} }
void test_page_margins() void test_page_margins()
{ {
TS_SKIP("not implemented");
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
ws.get_page_margins().set_left(2);
ws.get_page_margins().set_right(2);
ws.get_page_margins().set_top(2);
ws.get_page_margins().set_bottom(2);
ws.get_page_margins().set_header(1.5);
ws.get_page_margins().set_footer(1.5);
auto xml_string = xlnt::writer::write_worksheet(ws); auto xml_string = xlnt::writer::write_worksheet(ws);
pugi::xml_document doc; pugi::xml_document doc;
doc.load(xml_string.c_str()); doc.load(xml_string.c_str());
auto expected_string =
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <sheetPr>"
" <outlinePr summaryRight=\"1\" summaryBelow=\"1\"/>"
" </sheetPr>"
" <dimension ref=\"A1:A1\"/>"
" <sheetViews>"
" <sheetView workbookViewId=\"0\">"
" <selection sqref=\"A1\" activeCell=\"A1\"/>"
" </sheetView>"
" </sheetViews>"
" <sheetFormatPr baseColWidth=\"10\" defaultRowHeight=\"15\"/>"
" <sheetData/>"
" <pageMargins left=\"2\" right=\"2\" top=\"2\" bottom=\"2\" header=\"1.5\" footer=\"1.5\"/>"
"</worksheet>";
pugi::xml_document expected_doc;
expected_doc.load(expected_string);
TS_ASSERT(Helper::compare_xml(expected_doc, doc));
} }
void test_merge() void test_merge()
{ {
TS_SKIP("not implemented");
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
std::vector<std::string> string_table = {"Cell A1", "Cell B1"};
auto xml_string = xlnt::writer::write_worksheet(ws); auto expected_string1 =
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <sheetPr>"
" <outlinePr summaryRight=\"1\" summaryBelow=\"1\"/>"
" </sheetPr>"
" <dimension ref=\"A1:B1\"/>"
" <sheetViews>"
" <sheetView workbookViewId=\"0\">"
" <selection sqref=\"A1\" activeCell=\"A1\"/>"
" </sheetView>"
" </sheetViews>"
" <sheetFormatPr baseColWidth=\"10\" defaultRowHeight=\"15\"/>"
" <sheetData>"
" <row r=\"1\" spans=\"1:2\">"
" <c r=\"A1\" t=\"s\">"
" <v>0</v>"
" </c>"
" <c r=\"B1\" t=\"s\">"
" <v>1</v>"
" </c>"
" </row>"
" </sheetData>"
" <pageMargins left=\"0.75\" right=\"0.75\" top=\"1\" bottom=\"1\" header=\"0.5\" footer=\"0.5\"/>"
"</worksheet>";
ws.get_cell("A1").set_value("Cell A1");
ws.get_cell("B1").set_value("Cell B1");
auto xml_string = xlnt::writer::write_worksheet(ws, string_table);
pugi::xml_document doc; pugi::xml_document doc;
doc.load(xml_string.c_str()); doc.load(xml_string.c_str());
pugi::xml_document expected_doc;
expected_doc.load(expected_string1);
TS_ASSERT(Helper::compare_xml(expected_doc, doc));
ws.merge_cells("A1:B1");
xml_string = xlnt::writer::write_worksheet(ws, string_table);
auto expected_string2 =
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <sheetPr>"
" <outlinePr summaryRight=\"1\" summaryBelow=\"1\"/>"
" </sheetPr>"
" <dimension ref=\"A1:B1\"/>"
" <sheetViews>"
" <sheetView workbookViewId=\"0\">"
" <selection sqref=\"A1\" activeCell=\"A1\"/>"
" </sheetView>"
" </sheetViews>"
" <sheetFormatPr baseColWidth=\"10\" defaultRowHeight=\"15\"/>"
" <sheetData>"
" <row r=\"1\" spans=\"1:2\">"
" <c r=\"A1\" t=\"s\">"
" <v>0</v>"
" </c>"
" <c r=\"B1\" t=\"s\"/>"
" </row>"
" </sheetData>"
" <mergeCells count=\"1\">"
" <mergeCell ref=\"A1:B1\"/>"
" </mergeCells>"
" <pageMargins left=\"0.75\" right=\"0.75\" top=\"1\" bottom=\"1\" header=\"0.5\" footer=\"0.5\"/>"
"</worksheet>";
doc.load(xml_string.c_str());
expected_doc.load(expected_string2);
TS_ASSERT(Helper::compare_xml(expected_doc, doc));
ws.unmerge_cells("A1:B1");
xml_string = xlnt::writer::write_worksheet(ws, string_table);
auto expected_string3 =
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <sheetPr>"
" <outlinePr summaryRight=\"1\" summaryBelow=\"1\"/>"
" </sheetPr>"
" <dimension ref=\"A1:B1\"/>"
" <sheetViews>"
" <sheetView workbookViewId=\"0\">"
" <selection sqref=\"A1\" activeCell=\"A1\"/>"
" </sheetView>"
" </sheetViews>"
" <sheetFormatPr baseColWidth=\"10\" defaultRowHeight=\"15\"/>"
" <sheetData>"
" <row r=\"1\" spans=\"1:2\">"
" <c r=\"A1\" t=\"s\">"
" <v>0</v>"
" </c>"
" <c r=\"B1\" t=\"s\"/>"
" </row>"
" </sheetData>"
" <pageMargins left=\"0.75\" right=\"0.75\" top=\"1\" bottom=\"1\" header=\"0.5\" footer=\"0.5\"/>"
"</worksheet>";
doc.load(xml_string.c_str());
expected_doc.load(expected_string3);
TS_ASSERT(Helper::compare_xml(expected_doc, doc));
} }
void test_printer_settings() void test_printer_settings()
{ {
TS_SKIP("not implemented");
xlnt::worksheet ws(wb_); xlnt::worksheet ws(wb_);
ws.get_page_setup().set_orientation(xlnt::page_setup::orientation::landscape); ws.get_page_setup().set_orientation(xlnt::page_setup::orientation::landscape);
@ -433,45 +573,35 @@ public:
void test_positioning_point() void test_positioning_point()
{ {
TS_SKIP("not implemented");
/*
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
*/ TS_ASSERT_EQUALS(ws.get_point_pos(150, 40), xlnt::cell_reference("C3"));
} }
void test_positioning_roundtrip() void test_positioning_roundtrip()
{ {
TS_SKIP("not implemented");
/*
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("A1").get_anchor()), xlnt::cell_reference("A1")); TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("A1").get_anchor()), xlnt::cell_reference("A1"));
TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("D52").get_anchor()), xlnt::cell_reference("D52")); TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("D52").get_anchor()), xlnt::cell_reference("D52"));
TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("X11").get_anchor()), xlnt::cell_reference("X11")); TS_ASSERT_EQUALS(ws.get_point_pos(ws.get_cell("X11").get_anchor()), xlnt::cell_reference("X11"));
*/
} }
void test_page_setup() void test_page_setup()
{ {
TS_SKIP("not implemented");
/*
xlnt::page_setup p; xlnt::page_setup p;
TS_ASSERT(p.get_page_setup().empty()); TS_ASSERT_EQUALS(p.get_scale(), 1);
p.set_scale(1); p.set_scale(2);
TS_ASSERT_EQUALS(p.get_page_setup().at("scale"), 1); TS_ASSERT_EQUALS(p.get_scale(), 2);
*/
} }
void test_page_options() void test_page_options()
{ {
TS_SKIP("not implemented");
/*
xlnt::page_setup p; xlnt::page_setup p;
TS_ASSERT(p.get_options().empty()); TS_ASSERT(!p.get_horizontal_centered());
TS_ASSERT(!p.get_vertical_centered());
p.set_horizontal_centered(true); p.set_horizontal_centered(true);
p.set_vertical_centered(true); p.set_vertical_centered(true);
TS_ASSERT_EQUALS(p.get_options().at("verticalCentered"), "1"); TS_ASSERT(p.get_horizontal_centered());
TS_ASSERT_EQUALS(p.get_options().at("horizontalCentered"), "1"); TS_ASSERT(p.get_vertical_centered());
*/
} }
private: private:

View File

@ -58,7 +58,7 @@ public:
void test_write_worksheet() void test_write_worksheet()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("F42") = "hello"; ws.get_cell("F42").set_value("hello");
auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {}); auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content));
} }
@ -67,7 +67,7 @@ public:
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_page_setup().set_sheet_state(xlnt::page_setup::sheet_state::hidden); ws.get_page_setup().set_sheet_state(xlnt::page_setup::sheet_state::hidden);
ws.get_cell("F42") = "hello"; ws.get_cell("F42").set_value("hello");
auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {}); auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content));
} }
@ -75,8 +75,8 @@ public:
void test_write_bool() void test_write_bool()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("F42") = false; ws.get_cell("F42").set_value(false);
ws.get_cell("F43") = true; ws.get_cell("F43").set_value(true);
auto content = xlnt::writer::write_worksheet(ws, {}, {}); auto content = xlnt::writer::write_worksheet(ws, {}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_bool.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_bool.xml", content));
} }
@ -84,8 +84,8 @@ public:
void test_write_formula() void test_write_formula()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("F1") = 10; ws.get_cell("F1").set_value(10);
ws.get_cell("F2") = 32; ws.get_cell("F2").set_value(32);
ws.get_cell("F3").set_formula("F1+F2"); ws.get_cell("F3").set_formula("F1+F2");
auto content = xlnt::writer::write_worksheet(ws, {}, {}); auto content = xlnt::writer::write_worksheet(ws, {}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_formula.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_formula.xml", content));
@ -96,7 +96,7 @@ public:
xlnt::workbook wb_guess_types; xlnt::workbook wb_guess_types;
wb_guess_types.set_guess_types(true); wb_guess_types.set_guess_types(true);
auto ws = wb_guess_types.create_sheet(); auto ws = wb_guess_types.create_sheet();
ws.get_cell("F1") = "13%"; ws.get_cell("F1").set_value("13%");
auto style_id_by_hash = xlnt::style_writer(wb_guess_types).get_style_by_hash(); auto style_id_by_hash = xlnt::style_writer(wb_guess_types).get_style_by_hash();
auto content = xlnt::writer::write_worksheet(ws, {}, style_id_by_hash); auto content = xlnt::writer::write_worksheet(ws, {}, style_id_by_hash);
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_style.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_style.xml", content));
@ -105,7 +105,7 @@ public:
void test_write_height() void test_write_height()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("F1") = 10; ws.get_cell("F1").set_value(10);
ws.get_row_properties(ws.get_cell("F1").get_row()).set_height(30); ws.get_row_properties(ws.get_cell("F1").get_row()).set_height(30);
auto content = xlnt::writer::write_worksheet(ws, {}, {}); auto content = xlnt::writer::write_worksheet(ws, {}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_height.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_height.xml", content));
@ -114,7 +114,7 @@ public:
void test_write_hyperlink() void test_write_hyperlink()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("A1") = "test"; ws.get_cell("A1").set_value("test");
ws.get_cell("A1").set_hyperlink("http://test.com"); ws.get_cell("A1").set_hyperlink("http://test.com");
auto content = xlnt::writer::write_worksheet(ws, {"test"}, {}); auto content = xlnt::writer::write_worksheet(ws, {"test"}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_hyperlink.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_hyperlink.xml", content));
@ -124,10 +124,10 @@ public:
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
TS_ASSERT_EQUALS(0, ws.get_relationships().size()); TS_ASSERT_EQUALS(0, ws.get_relationships().size());
ws.get_cell("A1") = "test"; ws.get_cell("A1").set_value("test");
ws.get_cell("A1").set_hyperlink("http://test.com/"); ws.get_cell("A1").set_hyperlink("http://test.com/");
TS_ASSERT_EQUALS(1, ws.get_relationships().size()); TS_ASSERT_EQUALS(1, ws.get_relationships().size());
ws.get_cell("A2") = "test"; ws.get_cell("A2").set_value("test");
ws.get_cell("A2").set_hyperlink("http://test2.com/"); ws.get_cell("A2").set_hyperlink("http://test2.com/");
TS_ASSERT_EQUALS(2, ws.get_relationships().size()); TS_ASSERT_EQUALS(2, ws.get_relationships().size());
auto content = xlnt::writer::write_worksheet_rels(ws); auto content = xlnt::writer::write_worksheet_rels(ws);
@ -138,16 +138,16 @@ public:
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("A1").set_hyperlink("http://test.com"); ws.get_cell("A1").set_hyperlink("http://test.com");
TS_ASSERT_EQUALS("http://test.com", ws.get_cell("A1")); TS_ASSERT_EQUALS("http://test.com", ws.get_cell("A1").get_value());
ws.get_cell("A1") = "test"; ws.get_cell("A1").set_value("test");
TS_ASSERT_EQUALS("test", ws.get_cell("A1")); TS_ASSERT_EQUALS("test", ws.get_cell("A1").get_value());
} }
void test_write_auto_filter() void test_write_auto_filter()
{ {
xlnt::workbook wb; xlnt::workbook wb;
auto ws = wb.get_sheet_by_index(0); auto ws = wb.get_sheet_by_index(0);
ws.get_cell("F42") = "hello"; ws.get_cell("F42").set_value("hello");
ws.auto_filter("A1:F1"); ws.auto_filter("A1:F1");
auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {}); auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_auto_filter.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_auto_filter.xml", content));
@ -159,7 +159,7 @@ public:
void test_freeze_panes_horiz() void test_freeze_panes_horiz()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("F42") = "hello"; ws.get_cell("F42").set_value("hello");
ws.freeze_panes("A4"); ws.freeze_panes("A4");
auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {}); auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_horiz.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_horiz.xml", content));
@ -168,7 +168,7 @@ public:
void test_freeze_panes_vert() void test_freeze_panes_vert()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("F42") = "hello"; ws.get_cell("F42").set_value("hello");
ws.freeze_panes("D1"); ws.freeze_panes("D1");
auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {}); auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_vert.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_vert.xml", content));
@ -177,7 +177,7 @@ public:
void test_freeze_panes_both() void test_freeze_panes_both()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("F42") = "hello"; ws.get_cell("F42").set_value("hello");
ws.freeze_panes("D4"); ws.freeze_panes("D4");
auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {}); auto content = xlnt::writer::write_worksheet(ws, {"hello"}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_both.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_both.xml", content));
@ -186,7 +186,7 @@ public:
void test_long_number() void test_long_number()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("A1") = 9781231231230; ws.get_cell("A1").set_value(9781231231230);
auto content = xlnt::writer::write_worksheet(ws, {}, {}); auto content = xlnt::writer::write_worksheet(ws, {}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/long_number.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/long_number.xml", content));
} }
@ -194,7 +194,7 @@ public:
void test_short_number() void test_short_number()
{ {
auto ws = wb_.create_sheet(); auto ws = wb_.create_sheet();
ws.get_cell("A1") = 1234567890; ws.get_cell("A1").set_value(1234567890);
auto content = xlnt::writer::write_worksheet(ws, {}, {}); auto content = xlnt::writer::write_worksheet(ws, {}, {});
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/short_number.xml", content)); TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/short_number.xml", content));
} }