Merge pull request #311 from Crzyrndm/dev-unimplemented-functions-PR

Implementations for unimplemented functions
This commit is contained in:
Crzyrndm 2018-07-21 11:31:25 +12:00 committed by GitHub
commit 4dae88f004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 744 additions and 163 deletions

View File

@ -262,26 +262,21 @@ public:
/// </summary> /// </summary>
class hyperlink hyperlink() const; class hyperlink hyperlink() const;
/// <summary>
/// Adds a hyperlink to this cell pointing to the URL of the given value.
/// </summary>
void hyperlink(const std::string &url);
/// <summary> /// <summary>
/// Adds a hyperlink to this cell pointing to the URI of the given value and sets /// Adds a hyperlink to this cell pointing to the URI of the given value and sets
/// the text value of the cell to the given parameter. /// the text value of the cell to the given parameter.
/// </summary> /// </summary>
void hyperlink(const std::string &url, const std::string &display); void hyperlink(const std::string &url, const std::string &display = "");
/// <summary> /// <summary>
/// Adds an internal hyperlink to this cell pointing to the given cell. /// Adds an internal hyperlink to this cell pointing to the given cell.
/// </summary> /// </summary>
void hyperlink(xlnt::cell target); void hyperlink(xlnt::cell target, const std::string& display = "");
/// <summary> /// <summary>
/// Adds an internal hyperlink to this cell pointing to the given range. /// Adds an internal hyperlink to this cell pointing to the given range.
/// </summary> /// </summary>
void hyperlink(xlnt::range target); void hyperlink(xlnt::range target, const std::string& display = "");
/// <summary> /// <summary>
/// Returns true if this cell has a hyperlink set. /// Returns true if this cell has a hyperlink set.
@ -613,9 +608,9 @@ public:
bool operator==(const cell &comparand) const; bool operator==(const cell &comparand) const;
/// <summary> /// <summary>
/// Returns true if this cell is uninitialized. /// Returns false if this cell the same cell as comparand (compared by reference).
/// </summary> /// </summary>
bool operator==(std::nullptr_t) const; bool operator!=(const cell &comparand) const;
private: private:
friend class style; friend class style;
@ -651,6 +646,11 @@ private:
/// </summary> /// </summary>
XLNT_API bool operator==(std::nullptr_t, const cell &cell); XLNT_API bool operator==(std::nullptr_t, const cell &cell);
/// <summary>
/// Returns true if this cell is uninitialized.
/// </summary>
XLNT_API bool operator==(const cell &cell, std::nullptr_t);
/// <summary> /// <summary>
/// Convenience function for writing cell to an ostream. /// Convenience function for writing cell to an ostream.
/// Uses cell::to_string() internally. /// Uses cell::to_string() internally.

View File

@ -44,12 +44,22 @@ class XLNT_API hyperlink
public: public:
bool external() const; bool external() const;
class relationship relationship() const; class relationship relationship() const;
// external target
std::string url() const; std::string url() const;
// internal target
std::string target_range() const; std::string target_range() const;
bool has_display() const;
void display(const std::string &value); void display(const std::string &value);
std::string display() const; const std::string& display() const;
bool has_tooltip() const;
void tooltip(const std::string &value); void tooltip(const std::string &value);
std::string tooltip() const; const std::string& tooltip() const;
bool has_location() const;
void location(const std::string &value);
const std::string& location() const;
private: private:
friend class cell; friend class cell;

View File

@ -34,7 +34,7 @@ namespace xlnt {
/// <summary> /// <summary>
/// Typedef a rich_text_run as a pair of string and optional font. /// Typedef a rich_text_run as a pair of string and optional font.
/// </summary> /// </summary>
struct rich_text_run struct XLNT_API rich_text_run
{ {
std::string first; std::string first;
optional<font> second; optional<font> second;

View File

@ -252,19 +252,37 @@ public:
/// Returns the internal indexed color representing this color. If this is not an RGB color, /// Returns the internal indexed color representing this color. If this is not an RGB color,
/// an invalid_attribute exception will be thrown. /// an invalid_attribute exception will be thrown.
/// </summary> /// </summary>
rgb_color rgb() const; const rgb_color& rgb() const;
/// <summary>
/// Returns the internal indexed color representing this color. If this is not an RGB color,
/// an invalid_attribute exception will be thrown.
/// </summary>
rgb_color &rgb();
/// <summary> /// <summary>
/// Returns the internal indexed color representing this color. If this is not an indexed color, /// Returns the internal indexed color representing this color. If this is not an indexed color,
/// an invalid_attribute exception will be thrown. /// an invalid_attribute exception will be thrown.
/// </summary> /// </summary>
indexed_color indexed() const; const indexed_color& indexed() const;
/// <summary>
/// Returns the internal indexed color representing this color. If this is not an indexed color,
/// an invalid_attribute exception will be thrown.
/// </summary>
indexed_color &indexed();
/// <summary> /// <summary>
/// Returns the internal indexed color representing this color. If this is not a theme color, /// Returns the internal indexed color representing this color. If this is not a theme color,
/// an invalid_attribute exception will be thrown. /// an invalid_attribute exception will be thrown.
/// </summary> /// </summary>
theme_color theme() const; const theme_color& theme() const;
/// <summary>
/// Returns the internal indexed color representing this color. If this is not a theme color,
/// an invalid_attribute exception will be thrown.
/// </summary>
theme_color& theme();
/// <summary> /// <summary>
/// Returns the tint of this color. /// Returns the tint of this color.

View File

@ -120,7 +120,7 @@ public:
/// <summary> /// <summary>
/// Default constructor. /// Default constructor.
/// </summary> /// </summary>
missing_number_format(); missing_number_format() = default;
/// <summary> /// <summary>
/// Default copy constructor. /// Default copy constructor.

View File

@ -107,7 +107,7 @@ public:
/// Create a string representing this path separated by the provided /// Create a string representing this path separated by the provided
/// separator or the system-default separator if not provided. /// separator or the system-default separator if not provided.
/// </summary> /// </summary>
std::string string() const; const std::string& string() const;
#ifdef _MSC_VER #ifdef _MSC_VER
/// <summary> /// <summary>
@ -176,6 +176,11 @@ public:
/// </summary> /// </summary>
bool operator==(const path &other) const; bool operator==(const path &other) const;
/// <summary>
/// Returns true if left path is equal to right path.
/// </summary>
bool operator!=(const path &other) const;
private: private:
/// <summary> /// <summary>
/// Returns the character that separates directories in the path. /// Returns the character that separates directories in the path.

View File

@ -67,7 +67,7 @@ public:
/// <summary> /// <summary>
/// Constructs a new document security object with default values. /// Constructs a new document security object with default values.
/// </summary> /// </summary>
document_security(); document_security() = default;
/// <summary> /// <summary>
/// If true, the workbook is locked for revisions. /// If true, the workbook is locked for revisions.

View File

@ -290,11 +290,11 @@ public:
/// </summary> /// </summary>
bool operator!=(const range &comparand) const; bool operator!=(const range &comparand) const;
private: private:
/// <summary> /// <summary>
/// The worksheet this range is within /// The worksheet this range is within
/// </summary> /// </summary>
worksheet ws_; class worksheet ws_;
/// <summary> /// <summary>
/// The reference of this range /// The reference of this range

View File

@ -56,12 +56,7 @@ public:
/// top_left:bottom_right. /// top_left:bottom_right.
/// </summary> /// </summary>
explicit range_reference(const char *range_string); explicit range_reference(const char *range_string);
/// <summary>
/// Constructs a range reference from a pair of cell references.
/// </summary>
explicit range_reference(const std::pair<cell_reference, cell_reference> &reference_pair);
/// <summary> /// <summary>
/// Constructs a range reference from cell references indicating top /// Constructs a range reference from cell references indicating top
/// left and bottom right coordinates of the range. /// left and bottom right coordinates of the range.

View File

@ -341,38 +341,24 @@ cell_reference cell::reference() const
return {d_->column_, d_->row_}; return {d_->column_, d_->row_};
} }
bool cell::operator==(std::nullptr_t) const
{
return d_ == nullptr;
}
bool cell::operator==(const cell &comparand) const bool cell::operator==(const cell &comparand) const
{ {
return d_ == comparand.d_; return d_ == comparand.d_;
} }
cell &cell::operator=(const cell &rhs) bool cell::operator!=(const cell &comparand) const
{ {
d_->column_ = rhs.d_->column_; return d_ != comparand.d_;
d_->format_ = rhs.d_->format_;
d_->formula_ = rhs.d_->formula_;
d_->hyperlink_ = rhs.d_->hyperlink_;
d_->is_merged_ = rhs.d_->is_merged_;
d_->parent_ = rhs.d_->parent_;
d_->row_ = rhs.d_->row_;
d_->type_ = rhs.d_->type_;
d_->value_numeric_ = rhs.d_->value_numeric_;
d_->value_text_ = rhs.d_->value_text_;
return *this;
} }
cell &cell::operator=(const cell &rhs) = default;
hyperlink cell::hyperlink() const hyperlink cell::hyperlink() const
{ {
return xlnt::hyperlink(&d_->hyperlink_.get()); return xlnt::hyperlink(&d_->hyperlink_.get());
} }
void cell::hyperlink(const std::string &url) void cell::hyperlink(const std::string &url, const std::string &display)
{ {
if (url.empty() || std::find(url.begin(), url.end(), ':') == url.end()) if (url.empty() || std::find(url.begin(), url.end(), ':') == url.end())
{ {
@ -388,7 +374,8 @@ void cell::hyperlink(const std::string &url)
auto relationships = manifest.relationships(ws.path(), relationship_type::hyperlink); auto relationships = manifest.relationships(ws.path(), relationship_type::hyperlink);
auto relation = std::find_if(relationships.cbegin(), relationships.cend(), auto relation = std::find_if(relationships.cbegin(), relationships.cend(),
[&url](xlnt::relationship rel) { return rel.target().path().string() == url; }); [&url](xlnt::relationship rel) { return rel.target().path().string() == url; });
if (relation != relationships.end()) { if (relation != relationships.end())
{
d_->hyperlink_.get().relationship = *relation; d_->hyperlink_.get().relationship = *relation;
} }
else else
@ -400,29 +387,20 @@ void cell::hyperlink(const std::string &url)
target_mode::external); target_mode::external);
// TODO: make manifest::register_relationship return the created relationship instead of rel id // TODO: make manifest::register_relationship return the created relationship instead of rel id
d_->hyperlink_.get().relationship = manifest.relationship(ws.path(), rel_id); d_->hyperlink_.get().relationship = manifest.relationship(ws.path(), rel_id);
} }
// if a value is already present, the display string is ignored
if (!has_value()) // hyperlink on an empty cell sets the value to the hyperlink string if (has_value())
{ {
value(url); d_->hyperlink_.get().display.set(to_string());
}
else
{
d_->hyperlink_.get().display.set(display.empty() ? url : display);
value(hyperlink().display());
} }
} }
void cell::hyperlink(const std::string &url, const std::string &display) void cell::hyperlink(xlnt::cell target, const std::string& display)
{
if (!display.empty()) // if the display string isn't empty use that
{
value(display);
}
else // empty display string sets the value to the link text
{
value(url);
}
hyperlink(url);
d_->hyperlink_.get().display = display;
}
void cell::hyperlink(xlnt::cell target)
{ {
// TODO: should this computed value be a method on a cell? // TODO: should this computed value be a method on a cell?
const auto cell_address = target.worksheet().title() + "!" + target.reference().to_string(); const auto cell_address = target.worksheet().title() + "!" + target.reference().to_string();
@ -430,8 +408,37 @@ void cell::hyperlink(xlnt::cell target)
d_->hyperlink_ = detail::hyperlink_impl(); d_->hyperlink_ = detail::hyperlink_impl();
d_->hyperlink_.get().relationship = xlnt::relationship("", relationship_type::hyperlink, d_->hyperlink_.get().relationship = xlnt::relationship("", relationship_type::hyperlink,
uri(""), uri(cell_address), target_mode::internal); uri(""), uri(cell_address), target_mode::internal);
d_->hyperlink_.get().display = cell_address; // if a value is already present, the display string is ignored
value(cell_address); if (has_value())
{
d_->hyperlink_.get().display.set(to_string());
}
else
{
d_->hyperlink_.get().display.set(display.empty() ? cell_address : display);
value(hyperlink().display());
}
}
void cell::hyperlink(xlnt::range target, const std::string &display)
{
// TODO: should this computed value be a method on a cell?
const auto range_address = target.target_worksheet().title() + "!" + target.reference().to_string();
d_->hyperlink_ = detail::hyperlink_impl();
d_->hyperlink_.get().relationship = xlnt::relationship("", relationship_type::hyperlink,
uri(""), uri(range_address), target_mode::internal);
// if a value is already present, the display string is ignored
if (has_value())
{
d_->hyperlink_.get().display.set(to_string());
}
else
{
d_->hyperlink_.get().display.set(display.empty() ? range_address : display);
value(hyperlink().display());
}
} }
void cell::formula(const std::string &formula) void cell::formula(const std::string &formula)
@ -472,6 +479,15 @@ void cell::clear_formula()
} }
} }
std::string cell::error() const
{
if (d_->type_ != type::error)
{
throw xlnt::exception("called error() when cell type is not error");
}
return value<std::string>();
}
void cell::error(const std::string &error) void cell::error(const std::string &error)
{ {
if (error.length() == 0 || error[0] != '#') if (error.length() == 0 || error[0] != '#')
@ -743,6 +759,16 @@ calendar cell::base_date() const
return workbook().base_date(); return workbook().base_date();
} }
bool operator==(std::nullptr_t, const cell &cell)
{
return cell.data_type() == cell::type::empty;
}
bool operator==(const cell &cell, std::nullptr_t)
{
return nullptr == cell;
}
XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell) XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell)
{ {
return stream << cell.to_string(); return stream << cell.to_string();
@ -802,8 +828,11 @@ void cell::value(const std::string &value_string, bool infer_type)
void cell::clear_format() void cell::clear_format()
{ {
format().d_->references -= format().d_->references > 0 ? 1 : 0; if (d_->format_.is_set())
d_->format_.clear(); {
format().d_->references -= format().d_->references > 0 ? 1 : 0;
d_->format_.clear();
}
} }
void cell::clear_style() void cell::clear_style()

View File

@ -67,24 +67,49 @@ bool hyperlink::external() const
return d_->relationship.target_mode() == target_mode::external; return d_->relationship.target_mode() == target_mode::external;
} }
void hyperlink::display(const std::string &value) bool hyperlink::has_display() const
{ {
d_->display = value; return d_->display.is_set();
} }
std::string hyperlink::display() const void hyperlink::display(const std::string &value)
{ {
return d_->display; d_->display.set(value);
}
const std::string& hyperlink::display() const
{
return d_->display.get();
}
bool hyperlink::has_tooltip() const
{
return d_->tooltip.is_set();
} }
void hyperlink::tooltip(const std::string &value) void hyperlink::tooltip(const std::string &value)
{ {
d_->tooltip = value; d_->tooltip.set(value);
} }
std::string hyperlink::tooltip() const const std::string& hyperlink::tooltip() const
{ {
return d_->tooltip; return d_->tooltip.get();
}
bool hyperlink::has_location() const
{
return d_->location.is_set();
}
void hyperlink::location(const std::string &value)
{
d_->location.set(value);
}
const std::string &hyperlink::location() const
{
return d_->location.get();
} }
} // namespace xlnt } // namespace xlnt

View File

@ -65,6 +65,11 @@ std::vector<rich_text_run> rich_text::runs() const
return runs_; return runs_;
} }
void rich_text::runs(const std::vector<rich_text_run> &new_runs)
{
runs_ = new_runs;
}
void rich_text::add_run(const rich_text_run &t) void rich_text::add_run(const rich_text_run &t)
{ {
runs_.push_back(t); runs_.push_back(t);

View File

@ -27,22 +27,26 @@
#include <xlnt/cell/cell_type.hpp> #include <xlnt/cell/cell_type.hpp>
#include <xlnt/cell/comment.hpp> #include <xlnt/cell/comment.hpp>
#include <xlnt/cell/rich_text.hpp>
#include <xlnt/cell/index_types.hpp> #include <xlnt/cell/index_types.hpp>
#include <xlnt/cell/rich_text.hpp>
#include <xlnt/packaging/relationship.hpp> #include <xlnt/packaging/relationship.hpp>
#include <xlnt/utils/optional.hpp> #include <xlnt/utils/optional.hpp>
#include <detail/implementations/format_impl.hpp>
#include <detail/implementations/hyperlink_impl.hpp> #include <detail/implementations/hyperlink_impl.hpp>
namespace xlnt { namespace xlnt {
namespace detail { namespace detail {
struct format_impl;
struct worksheet_impl; struct worksheet_impl;
struct cell_impl struct cell_impl
{ {
cell_impl(); cell_impl();
cell_impl(const cell_impl &other) = default;
cell_impl(cell_impl &&other) = default;
cell_impl &operator=(const cell_impl &other) = default;
cell_impl &operator=(cell_impl &&other) = default;
cell_type type_; cell_type type_;
worksheet_impl *parent_; worksheet_impl *parent_;
@ -72,9 +76,8 @@ inline bool operator==(const cell_impl &lhs, const cell_impl &rhs)
&& lhs.value_numeric_ == rhs.value_numeric_ && lhs.value_numeric_ == rhs.value_numeric_
&& lhs.formula_ == rhs.formula_ && lhs.formula_ == rhs.formula_
&& lhs.hyperlink_ == rhs.hyperlink_ && lhs.hyperlink_ == rhs.hyperlink_
// comparing pointers is unlikely to be what is wanted && (lhs.format_.is_set() == rhs.format_.is_set() && (!lhs.format_.is_set() || *lhs.format_.get() == *rhs.format_.get()))
/*&& lhs.format_ == rhs.format_ && (lhs.comment_.is_set() == rhs.comment_.is_set() && (!lhs.comment_.is_set() || *lhs.comment_.get() == *rhs.comment_.get()));
&& lhs.comment_ == rhs.comment_*/;
} }
} // namespace detail } // namespace detail

View File

@ -26,15 +26,18 @@
#include <string> #include <string>
#include <xlnt/packaging/relationship.hpp> #include <xlnt/packaging/relationship.hpp>
#include <xlnt/utils/optional.hpp>
namespace xlnt { namespace xlnt {
namespace detail { namespace detail {
// [serialised]
struct hyperlink_impl struct hyperlink_impl
{ {
xlnt::relationship relationship; xlnt::relationship relationship;
std::string tooltip; xlnt::optional<std::string> location;
std::string display; xlnt::optional<std::string> tooltip;
xlnt::optional<std::string> display;
}; };
inline bool operator==(const hyperlink_impl &lhs, const hyperlink_impl &rhs) inline bool operator==(const hyperlink_impl &lhs, const hyperlink_impl &rhs)

View File

@ -57,6 +57,11 @@ std::size_t indexed_color::index() const
return index_; return index_;
} }
void indexed_color::index(std::size_t index)
{
index_ = index;
}
// theme_color implementation // theme_color implementation
theme_color::theme_color(std::size_t index) : index_(index) theme_color::theme_color(std::size_t index) : index_(index)
@ -68,6 +73,11 @@ std::size_t theme_color::index() const
return index_; return index_;
} }
void theme_color::index(std::size_t index)
{
index_ = index;
}
// rgb_color implementation // rgb_color implementation
std::string rgb_color::hex_string() const std::string rgb_color::hex_string() const
@ -223,19 +233,37 @@ void color::auto_(bool value)
auto__ = value; auto__ = value;
} }
indexed_color color::indexed() const const indexed_color& color::indexed() const
{ {
assert_type(color_type::indexed); assert_type(color_type::indexed);
return indexed_; return indexed_;
} }
theme_color color::theme() const indexed_color &color::indexed()
{
assert_type(color_type::indexed);
return indexed_;
}
const theme_color& color::theme() const
{ {
assert_type(color_type::theme); assert_type(color_type::theme);
return theme_; return theme_;
} }
rgb_color color::rgb() const theme_color &color::theme()
{
assert_type(color_type::theme);
return theme_;
}
const rgb_color& color::rgb() const
{
assert_type(color_type::rgb);
return rgb_;
}
rgb_color &color::rgb()
{ {
assert_type(color_type::rgb); assert_type(color_type::rgb);
return rgb_; return rgb_;

View File

@ -81,6 +81,11 @@ bool conditional_format::operator!=(const conditional_format &other) const
return !(*this == other); return !(*this == other);
} }
bool conditional_format::has_border() const
{
return d_->border_id.is_set();
}
xlnt::border conditional_format::border() const xlnt::border conditional_format::border() const
{ {
return d_->parent->borders.at(d_->border_id.get()); return d_->parent->borders.at(d_->border_id.get());
@ -92,6 +97,11 @@ conditional_format conditional_format::border(const xlnt::border &new_border)
return *this; return *this;
} }
bool conditional_format::has_fill() const
{
return d_->fill_id.is_set();
}
xlnt::fill conditional_format::fill() const xlnt::fill conditional_format::fill() const
{ {
return d_->parent->fills.at(d_->fill_id.get()); return d_->parent->fills.at(d_->fill_id.get());
@ -103,6 +113,11 @@ conditional_format conditional_format::fill(const xlnt::fill &new_fill)
return *this; return *this;
} }
bool conditional_format::has_font() const
{
return d_->font_id.is_set();
}
xlnt::font conditional_format::font() const xlnt::font conditional_format::font() const
{ {
return d_->parent->fonts.at(d_->font_id.get()); return d_->parent->fonts.at(d_->font_id.get());

View File

@ -26,6 +26,26 @@
namespace xlnt { namespace xlnt {
protection protection::unlocked_and_visible()
{
return protection();
}
protection protection::locked_and_visible()
{
return protection().locked(true);
}
protection protection::unlocked_and_hidden()
{
return protection().hidden(true);
}
protection protection::locked_and_hidden()
{
return protection().locked(true).hidden(true);
}
protection::protection() protection::protection()
: locked_(false), hidden_(false) : locked_(false), hidden_(false)
{ {

View File

@ -66,6 +66,11 @@ std::size_t style::builtin_id() const
return d_->builtin_id.get(); return d_->builtin_id.get();
} }
bool style::builtin() const
{
return d_->builtin_id.is_set();
}
std::string style::name() const std::string style::name() const
{ {
return d_->name; return d_->name;
@ -87,6 +92,11 @@ bool style::operator==(const style &other) const
return name() == other.name(); return name() == other.name();
} }
bool style::operator!=(const style &other) const
{
return !operator==(other);
}
xlnt::alignment style::alignment() const xlnt::alignment style::alignment() const
{ {
return d_->parent->alignments.at(d_->alignment_id.get()); return d_->parent->alignments.at(d_->alignment_id.get());

View File

@ -141,6 +141,22 @@ path::path(const std::string &path_string)
{ {
} }
path::path(const std::string &path_string, char sep)
: internal_(path_string)
{
char curr_sep = guess_separator();
if (curr_sep != sep)
{
for (char& c : internal_) // simple find and replace
{
if (c == curr_sep)
{
c = sep;
}
}
}
}
// general attributes // general attributes
bool path::is_relative() const bool path::is_relative() const
@ -205,7 +221,7 @@ std::pair<std::string, std::string> path::split_extension() const
// conversion // conversion
std::string path::string() const const std::string& path::string() const
{ {
return internal_; return internal_;
} }
@ -331,4 +347,9 @@ bool path::operator==(const path &other) const
return internal_ == other.internal_; return internal_ == other.internal_;
} }
bool path::operator!=(const path &other) const
{
return !operator==(other);
}
} // namespace xlnt } // namespace xlnt

View File

@ -27,9 +27,8 @@
namespace xlnt { namespace xlnt {
variant::variant() variant::variant()
{ : type_(type::null)
{}
}
variant::variant(const std::string &value) variant::variant(const std::string &value)
: type_(type::lpstr), : type_(type::lpstr),
@ -41,7 +40,7 @@ variant::variant(const char *value) : variant(std::string(value))
{ {
} }
variant::variant(std::int32_t value) variant::variant(int32_t value)
: type_(type::i4), : type_(type::i4),
i4_value_(value) i4_value_(value)
{ {

View File

@ -31,7 +31,7 @@
namespace xlnt { namespace xlnt {
range::range(worksheet ws, const range_reference &reference, major_order order, bool skip_null) range::range(class worksheet ws, const range_reference &reference, major_order order, bool skip_null)
: ws_(ws), : ws_(ws),
ref_(reference), ref_(reference),
order_(order), order_(order),
@ -69,6 +69,11 @@ cell_vector range::operator[](std::size_t index)
return vector(index); return vector(index);
} }
const cell_vector range::operator[](std::size_t index) const
{
return vector(index);
}
const worksheet &range::target_worksheet() const const worksheet &range::target_worksheet() const
{ {
return ws_; return ws_;
@ -112,6 +117,22 @@ cell_vector range::vector(std::size_t vector_index)
return cell_vector(ws_, cursor, ref_, order_, skip_null_, false); return cell_vector(ws_, cursor, ref_, order_, skip_null_, false);
} }
const cell_vector range::vector(std::size_t vector_index) const
{
auto cursor = ref_.top_left();
if (order_ == major_order::row)
{
cursor.row(cursor.row() + static_cast<row_t>(vector_index));
}
else
{
cursor.column_index(cursor.column_index() + static_cast<column_t::index_t>(vector_index));
}
return cell_vector(ws_, cursor, ref_, order_, skip_null_, false);
}
bool range::contains(const cell_reference &ref) bool range::contains(const cell_reference &ref)
{ {
return ref_.top_left().column_index() <= ref.column_index() return ref_.top_left().column_index() <= ref.column_index()
@ -188,6 +209,11 @@ cell range::cell(const cell_reference &ref)
return (*this)[ref.row() - 1][ref.column().index - 1]; return (*this)[ref.row() - 1][ref.column().index - 1];
} }
const cell range::cell(const cell_reference &ref) const
{
return (*this)[ref.row() - 1][ref.column().index - 1];
}
cell_vector range::front() cell_vector range::front()
{ {
return *begin(); return *begin();

View File

@ -29,18 +29,20 @@
#include <xlnt/cell/cell.hpp> #include <xlnt/cell/cell.hpp>
#include <xlnt/cell/comment.hpp> #include <xlnt/cell/comment.hpp>
#include <xlnt/cell/hyperlink.hpp> #include <xlnt/cell/hyperlink.hpp>
#include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/alignment.hpp> #include <xlnt/styles/alignment.hpp>
#include <xlnt/styles/border.hpp> #include <xlnt/styles/border.hpp>
#include <xlnt/styles/fill.hpp> #include <xlnt/styles/fill.hpp>
#include <xlnt/styles/format.hpp> #include <xlnt/styles/format.hpp>
#include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp> #include <xlnt/styles/protection.hpp>
#include <xlnt/styles/style.hpp> #include <xlnt/styles/style.hpp>
#include <xlnt/worksheet/worksheet.hpp>
#include <xlnt/utils/date.hpp> #include <xlnt/utils/date.hpp>
#include <xlnt/utils/datetime.hpp> #include <xlnt/utils/datetime.hpp>
#include <xlnt/utils/time.hpp> #include <xlnt/utils/time.hpp>
#include <xlnt/utils/timedelta.hpp> #include <xlnt/utils/timedelta.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/worksheet.hpp>
class cell_test_suite : public test_suite class cell_test_suite : public test_suite
{ {
@ -79,6 +81,7 @@ public:
register_test(test_anchor); register_test(test_anchor);
register_test(test_hyperlink); register_test(test_hyperlink);
register_test(test_comment); register_test(test_comment);
register_test(test_copy_and_compare);
} }
private: private:
@ -251,11 +254,27 @@ private:
xlnt::workbook wb; xlnt::workbook wb;
auto ws = wb.active_sheet(); auto ws = wb.active_sheet();
auto cell = ws.cell(xlnt::cell_reference(1, 1)); auto cell = ws.cell(xlnt::cell_reference(1, 1));
// error string can't be empty
xlnt_assert_throws(cell.error(""), xlnt::exception);
// error string has to have a leading '#'
xlnt_assert_throws(cell.error("not an error"), xlnt::exception);
for (auto error_code : xlnt::cell::error_codes()) for (auto error_code : xlnt::cell::error_codes())
{ {
// error type from the string format
cell.value(error_code.first, true); cell.value(error_code.first, true);
xlnt_assert(cell.data_type() == xlnt::cell::type::error); xlnt_assert(cell.data_type() == xlnt::cell::type::error);
std::string error;
xlnt_assert_throws_nothing(error = cell.error());
xlnt_assert_equals(error, error_code.first);
// clearing the value clears the error state
cell.clear_value();
xlnt_assert_throws(cell.error(), xlnt::exception);
// can explicitly set the error
xlnt_assert_throws_nothing(cell.error(error_code.first));
std::string error2;
xlnt_assert_throws_nothing(error2 = cell.error());
xlnt_assert_equals(error2, error_code.first);
} }
} }
@ -665,13 +684,81 @@ private:
{ {
xlnt::workbook wb; xlnt::workbook wb;
auto cell = wb.active_sheet().cell("A1"); auto cell = wb.active_sheet().cell("A1");
xlnt_assert(!cell.has_hyperlink()); xlnt_assert(!cell.has_hyperlink());
xlnt_assert_throws(cell.hyperlink(), xlnt::invalid_attribute); xlnt_assert_throws(cell.hyperlink(), xlnt::invalid_attribute);
xlnt_assert_throws(cell.hyperlink("notaurl"), xlnt::invalid_parameter); xlnt_assert_throws(cell.hyperlink("notaurl"), xlnt::invalid_parameter);
xlnt_assert_throws(cell.hyperlink(""), xlnt::invalid_parameter); xlnt_assert_throws(cell.hyperlink(""), xlnt::invalid_parameter);
cell.hyperlink("http://example.com"); // link without optional display
const std::string link1("http://example.com");
cell.hyperlink(link1);
xlnt_assert(cell.has_hyperlink()); xlnt_assert(cell.has_hyperlink());
xlnt_assert_equals(cell.hyperlink().relationship().target().to_string(), "http://example.com"); xlnt_assert(cell.hyperlink().external());
xlnt_assert_equals(cell.hyperlink().url(), link1);
xlnt_assert_equals(cell.hyperlink().relationship().target().to_string(), link1);
xlnt_assert_equals(cell.hyperlink().display(), link1);
cell.clear_value();
// link with display
const std::string link2("http://example2.com");
const std::string display_txt("notaurl");
cell.hyperlink(link2, display_txt);
xlnt_assert(cell.has_hyperlink());
xlnt_assert(cell.hyperlink().external());
xlnt_assert_equals(cell.hyperlink().url(), link2);
xlnt_assert_equals(cell.hyperlink().relationship().target().to_string(), link2);
xlnt_assert_equals(cell.hyperlink().display(), display_txt);
// value
int cell_test_val = 123;
cell.value(cell_test_val);
std::string cell_value_str = std::to_string(cell_test_val);
cell.hyperlink(link2, display_txt);
xlnt_assert_equals(cell.value<int>(), 123);
xlnt_assert_equals(cell.hyperlink().display(), cell_value_str); // display text ignored
cell.clear_value();
// cell overload without display
const std::string cell_target_str("A2");
auto cell_target = wb.active_sheet().cell(cell_target_str);
std::string link3 = wb.active_sheet().title() + '!' + cell_target_str; // Sheet1!A2
cell.hyperlink(cell_target);
xlnt_assert(cell.has_hyperlink());
xlnt_assert(!cell.hyperlink().external());
xlnt_assert_equals(cell.hyperlink().target_range(), link3);
xlnt_assert_equals(cell.hyperlink().display(), link3);
cell.clear_value();
// cell overload with display
cell.hyperlink(cell_target, display_txt);
xlnt_assert(cell.has_hyperlink());
xlnt_assert(!cell.hyperlink().external());
xlnt_assert_equals(cell.hyperlink().target_range(), link3);
xlnt_assert_equals(cell.hyperlink().display(), display_txt);
// value
cell.value(cell_test_val);
cell.hyperlink(cell_target, display_txt);
xlnt_assert_equals(cell.value<int>(), 123);
xlnt_assert_equals(cell.hyperlink().display(), cell_value_str); // display text ignored
cell.clear_value();
// range overload without display
const std::string range_target_str("A2:A5");
xlnt::range range_target(wb.active_sheet(), xlnt::range_reference(range_target_str));
std::string link4 = wb.active_sheet().title() + '!' + range_target_str; // Sheet1!A2:A5
cell.hyperlink(range_target);
xlnt_assert(cell.has_hyperlink());
xlnt_assert(!cell.hyperlink().external());
xlnt_assert_equals(cell.hyperlink().target_range(), link4);
xlnt_assert_equals(cell.hyperlink().display(), link4);
cell.clear_value();
// range overload with display
cell.hyperlink(range_target, display_txt);
xlnt_assert(cell.has_hyperlink());
xlnt_assert(!cell.hyperlink().external());
xlnt_assert_equals(cell.hyperlink().target_range(), link4);
xlnt_assert_equals(cell.hyperlink().display(), display_txt);
// value
cell.value(cell_test_val);
cell.hyperlink(range_target, display_txt);
xlnt_assert_equals(cell.value<int>(), 123);
xlnt_assert_equals(cell.hyperlink().display(), cell_value_str); // display text ignored
cell.clear_value();
} }
void test_comment() void test_comment()
@ -688,6 +775,31 @@ private:
xlnt_assert(!cell.has_comment()); xlnt_assert(!cell.has_comment());
xlnt_assert_throws(cell.comment(), xlnt::exception); xlnt_assert_throws(cell.comment(), xlnt::exception);
} }
void test_copy_and_compare()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
auto cell1 = ws.cell("A1");
auto cell2 = ws.cell("A2");
xlnt_assert_equals(cell1, cell1);
xlnt_assert_equals(cell2, cell2);
xlnt_assert_differs(cell1, cell2);
xlnt_assert_differs(cell2, cell1);
// nullptr equality
xlnt_assert(nullptr == cell1);
xlnt_assert(cell1 == nullptr);
cell1.value("test");
xlnt_assert(!(nullptr == cell1));
xlnt_assert(!(cell1 == nullptr));
// copy
xlnt::cell cell3(cell1);
xlnt_assert_equals(cell1, cell3);
// assign
cell3 = cell2;
xlnt_assert_equals(cell2, cell3);
}
}; };
static cell_test_suite x{}; static cell_test_suite x{};

View File

@ -35,6 +35,7 @@ public:
rich_text_test_suite() rich_text_test_suite()
{ {
register_test(test_operators); register_test(test_operators);
register_test(test_runs);
} }
void test_operators() void test_operators()
@ -100,5 +101,19 @@ public:
text_family_differs.add_run(run_family_differs); text_family_differs.add_run(run_family_differs);
xlnt_assert_differs(text_formatted, text_family_differs); xlnt_assert_differs(text_formatted, text_family_differs);
} }
void test_runs()
{
xlnt::rich_text rt;
xlnt_assert(rt.runs().empty());
std::vector<xlnt::rich_text_run> test_runs{xlnt::rich_text_run{"1_abc_test_123"}, xlnt::rich_text_run{"2_abc_test_123"}, xlnt::rich_text_run{"3_abc_test_123"}};
// just one
rt.add_run(test_runs[0]);
xlnt_assert_equals(1, rt.runs().size());
xlnt_assert_equals(test_runs[0], rt.runs()[0]);
// whole set
rt.runs(test_runs);
xlnt_assert_equals(test_runs, rt.runs());
}
}; };
static rich_text_test_suite x{}; static rich_text_test_suite x{};

View File

@ -38,37 +38,39 @@ public:
void test_known_colors() void test_known_colors()
{ {
const std::vector<std::pair<xlnt::color, std::string>> known_colors const std::vector<std::pair<xlnt::color, std::string>> known_colors{
{ {xlnt::color::black(), "FF000000"},
{ xlnt::color::black(), "FF000000" }, {xlnt::color::white(), "FFFFFFFF"},
{ xlnt::color::white(), "FFFFFFFF" }, {xlnt::color::red(), "FFFF0000"},
{ xlnt::color::red(), "FFFF0000" }, {xlnt::color::darkred(), "FF8B0000"},
{ xlnt::color::darkred(), "FF8B0000" }, {xlnt::color::blue(), "FF0000FF"},
{ xlnt::color::blue(), "FF0000FF" }, {xlnt::color::darkblue(), "FF00008B"},
{ xlnt::color::darkblue(), "FF00008B" }, {xlnt::color::green(), "FF00FF00"},
{ xlnt::color::green(), "FF00FF00" }, {xlnt::color::darkgreen(), "FF008B00"},
{ xlnt::color::darkgreen(), "FF008B00" }, {xlnt::color::yellow(), "FFFFFF00"},
{ xlnt::color::yellow(), "FFFFFF00" }, {xlnt::color::darkyellow(), "FFCCCC00"}};
{ xlnt::color::darkyellow(), "FFCCCC00" }
};
for (auto pair : known_colors) for (auto pair : known_colors)
{ {
xlnt_assert_equals(pair.first.rgb().hex_string(), pair.second); xlnt_assert_equals(pair.first.rgb().hex_string(), pair.second);
} }
} }
void test_non_rgb_colors() void test_non_rgb_colors()
{ {
xlnt::color indexed = xlnt::indexed_color(1); xlnt::color indexed = xlnt::indexed_color(1);
xlnt_assert(!indexed.auto_()); xlnt_assert(!indexed.auto_());
xlnt_assert_equals(indexed.indexed().index(), 1); xlnt_assert_equals(indexed.indexed().index(), 1);
indexed.indexed().index(2);
xlnt_assert_equals(indexed.indexed().index(), 2);
xlnt_assert_throws(indexed.theme(), xlnt::invalid_attribute); xlnt_assert_throws(indexed.theme(), xlnt::invalid_attribute);
xlnt_assert_throws(indexed.rgb(), xlnt::invalid_attribute); xlnt_assert_throws(indexed.rgb(), xlnt::invalid_attribute);
xlnt::color theme = xlnt::theme_color(3); xlnt::color theme = xlnt::theme_color(3);
xlnt_assert(!theme.auto_()); xlnt_assert(!theme.auto_());
xlnt_assert_equals(theme.theme().index(), 3); xlnt_assert_equals(theme.theme().index(), 3);
theme.theme().index(4);
xlnt_assert_equals(theme.theme().index(), 4);
xlnt_assert_throws(theme.indexed(), xlnt::invalid_attribute); xlnt_assert_throws(theme.indexed(), xlnt::invalid_attribute);
xlnt_assert_throws(theme.rgb(), xlnt::invalid_attribute); xlnt_assert_throws(theme.rgb(), xlnt::invalid_attribute);
} }

View File

@ -0,0 +1,64 @@
// Copyright (c) 2014-2018 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <xlnt/styles/conditional_format.hpp>
#include <helpers/test_suite.hpp>
#include <xlnt/xlnt.hpp>
class conditional_format_test_suite : public test_suite
{
public:
conditional_format_test_suite()
{
register_test(test_all);
}
void test_all()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
auto format = ws.conditional_format(xlnt::range_reference("A1:A10"), xlnt::condition::text_contains("test"));
xlnt_assert(!format.has_border());
xlnt_assert(!format.has_fill());
xlnt_assert(!format.has_font());
// set border
auto border = xlnt::border().diagonal(xlnt::diagonal_direction::both);
format.border(border);
xlnt_assert(format.has_border());
xlnt_assert_equals(format.border(), border);
// set fill
auto fill = xlnt::fill(xlnt::gradient_fill().type(xlnt::gradient_fill_type::path));
format.fill(fill);
xlnt_assert(format.has_fill());
xlnt_assert_equals(format.fill(), fill);
// set font
auto font = xlnt::font().color(xlnt::color::darkblue());
format.font(font);
xlnt_assert(format.has_font());
xlnt_assert_equals(format.font(), font);
// copy ctor
auto format_copy(format);
xlnt_assert_equals(format, format_copy);
}
};
static conditional_format_test_suite x;

View File

@ -0,0 +1,55 @@
// Copyright (c) 2014-2018 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <xlnt/styles/protection.hpp>
#include <helpers/test_suite.hpp>
#include <xlnt/xlnt.hpp>
class protection_test_suite : public test_suite
{
public:
protection_test_suite()
{
register_test(test_all);
}
void test_all()
{
auto prot = xlnt::protection::unlocked_and_visible();
xlnt_assert(!prot.hidden());
xlnt_assert(!prot.locked());
prot = xlnt::protection::locked_and_visible();
xlnt_assert(!prot.hidden());
xlnt_assert(prot.locked());
prot = xlnt::protection::unlocked_and_hidden();
xlnt_assert(prot.hidden());
xlnt_assert(!prot.locked());
prot = xlnt::protection::locked_and_hidden();
xlnt_assert(prot.hidden());
xlnt_assert(prot.locked());
}
};
static protection_test_suite x;

View File

@ -0,0 +1,66 @@
// Copyright (c) 2014-2018 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <xlnt/styles/style.hpp>
#include <xlnt/xlnt.hpp>
#include <helpers/test_suite.hpp>
class style_test_suite : public test_suite
{
public:
style_test_suite()
{
register_test(test_all);
}
void test_all()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
auto test_style = wb.create_style("test_style");
test_style.number_format(xlnt::number_format::date_ddmmyyyy());
auto copy_style(test_style);
xlnt_assert_equals(test_style, copy_style);
// number format
xlnt_assert_equals(copy_style.name(), "test_style");
xlnt_assert_equals(copy_style.number_format(), xlnt::number_format::date_ddmmyyyy());
//xlnt_assert(!copy_style.number_format_applied()); // this doesn't seem to have sensible behaviour?
copy_style.number_format(xlnt::number_format::date_datetime(), true); // true applied param
xlnt_assert_equals(copy_style.number_format(), xlnt::number_format::date_datetime());
xlnt_assert(copy_style.number_format_applied());
copy_style.number_format(xlnt::number_format::date_dmminus(), false); // false applied param
xlnt_assert_equals(copy_style.number_format(), xlnt::number_format::date_dmminus());
xlnt_assert(!copy_style.number_format_applied());
xlnt_assert(!copy_style.pivot_button());
copy_style.pivot_button(true);
xlnt_assert(copy_style.pivot_button());
xlnt_assert(!copy_style.quote_prefix());
copy_style.quote_prefix(true);
xlnt_assert(copy_style.quote_prefix());
}
};
static style_test_suite x;

View File

@ -26,6 +26,7 @@
#include <helpers/path_helper.hpp> #include <helpers/path_helper.hpp>
#include <helpers/temporary_file.hpp> #include <helpers/temporary_file.hpp>
#include <helpers/test_suite.hpp> #include <helpers/test_suite.hpp>
#include <xlnt/utils/path.hpp>
class path_test_suite : public test_suite class path_test_suite : public test_suite
{ {

View File

@ -0,0 +1,69 @@
// Copyright (c) 2014-2018 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#include <xlnt/utils/variant.hpp>
#include <helpers/test_suite.hpp>
class variant_test_suite : public test_suite
{
public:
variant_test_suite()
: test_suite()
{
register_test(test_null);
register_test(test_int32);
register_test(test_string);
}
void test_null()
{
xlnt::variant var_null;
xlnt_assert_equals(var_null.value_type(), xlnt::variant::type::null);
xlnt_assert(var_null.is(xlnt::variant::type::null));
}
void test_int32()
{
xlnt::variant var_int(std::int32_t(10));
xlnt_assert_equals(var_int.value_type(), xlnt::variant::type::i4);
xlnt_assert(var_int.is(xlnt::variant::type::i4));
xlnt_assert_throws_nothing(var_int.get<std::int32_t>());
xlnt_assert_equals(10, var_int.get<std::int32_t>());
}
void test_string()
{
xlnt::variant var_str1("test1");
xlnt_assert_equals(var_str1.value_type(), xlnt::variant::type::lpstr);
xlnt_assert(var_str1.is(xlnt::variant::type::lpstr));
xlnt_assert_throws_nothing(var_str1.get<std::string>());
xlnt_assert_equals("test1", var_str1.get<std::string>());
xlnt::variant var_str2(std::string("test2"));
xlnt_assert_equals(var_str2.value_type(), xlnt::variant::type::lpstr);
xlnt_assert(var_str2.is(xlnt::variant::type::lpstr));
xlnt_assert_throws_nothing(var_str2.get<std::string>());
xlnt_assert_equals("test2", var_str2.get<std::string>());
}
};
static variant_test_suite x;

View File

@ -575,7 +575,7 @@ public:
std::vector<std::uint8_t> destination; std::vector<std::uint8_t> destination;
source_workbook.save(destination); source_workbook.save(destination);
source_workbook.save("temp.xlsx"); source_workbook.save("temp" + source.filename());
#ifdef _MSC_VER #ifdef _MSC_VER
std::ifstream source_stream(source.wstring(), std::ios::binary); std::ifstream source_stream(source.wstring(), std::ios::binary);

View File

@ -37,10 +37,40 @@ class range_test_suite : public test_suite
public: public:
range_test_suite() range_test_suite()
{ {
register_test(test_construction);
register_test(test_batch_formatting); register_test(test_batch_formatting);
register_test(test_clear_cells); register_test(test_clear_cells);
} }
void test_construction()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
xlnt::range range_1(ws, xlnt::range_reference("A1:D10"));
xlnt_assert_equals(range_1.target_worksheet(), ws);
xlnt_assert_equals(1, range_1.front()[0].row()); // NOTE: querying row/column here desperately needs some shortcuts
xlnt_assert_equals(xlnt::column_t("D"), range_1.front().back().column());
xlnt_assert_equals(10, range_1.back()[0].row());
xlnt_assert_equals(xlnt::column_t("D"), range_1.back().back().column());
// assert default parameters in ctor
xlnt::range range_2(ws, xlnt::range_reference("A1:D10"), xlnt::major_order::row, false);
xlnt_assert_equals(range_1, range_2);
// assert copy
xlnt::range range_3(range_2);
xlnt_assert_equals(range_1, range_3);
// column order
xlnt::range range_4(ws, xlnt::range_reference("A1:D10"), xlnt::major_order::column);
xlnt_assert_equals(xlnt::column_t("A"), range_4.front()[0].column()); // NOTE: querying row/column here desperately needs some shortcuts
xlnt_assert_equals(10, range_4.front().back().row());
xlnt_assert_equals(xlnt::column_t("D"), range_4.back()[0].column());
xlnt_assert_equals(10, range_4.back().back().row());
// assignment
range_3 = range_4;
xlnt_assert_equals(range_3, range_4);
}
void test_batch_formatting() void test_batch_formatting()
{ {
xlnt::workbook wb; xlnt::workbook wb;

View File

@ -50,7 +50,6 @@ public:
register_test(test_remove_named_range_bad); register_test(test_remove_named_range_bad);
register_test(test_cell_alternate_coordinates); register_test(test_cell_alternate_coordinates);
register_test(test_cell_range_name); register_test(test_cell_range_name);
register_test(test_hyperlink_value);
register_test(test_rows); register_test(test_rows);
register_test(test_no_rows); register_test(test_no_rows);
register_test(test_no_cols); register_test(test_no_cols);
@ -225,50 +224,6 @@ public:
xlnt_assert(c_range_coord[0][0] == c_cell); xlnt_assert(c_range_coord[0][0] == c_cell);
} }
void test_hyperlink_value()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
std::string test_link = "http://test.com";
xlnt::cell_reference test_cell("A1");
ws.cell(test_cell).hyperlink(test_link);
// when a hyperlink is added to an empty cell, the display text becomes == to the link
xlnt_assert_equals(ws.cell(test_cell).hyperlink().url(), test_link);
xlnt_assert_equals(ws.cell(test_cell).value<std::string>(), test_link);
// if the display value changes, the hyperlink remains the same
std::string test_string = "test";
ws.cell(test_cell).value(test_string);
xlnt_assert_equals(test_string, ws.cell(test_cell).value<std::string>());
xlnt_assert_equals(ws.cell(test_cell).hyperlink().url(), test_link);
// changing the link doesn't change the cell value
std::string test_link2 = "http://test-123.com";
ws.cell(test_cell).hyperlink(test_link2);
xlnt_assert_equals(test_string, ws.cell(test_cell).value<std::string>());
xlnt_assert_equals(ws.cell(test_cell).hyperlink().url(), test_link2);
// and we can edit both value and hyperlink together
std::string test_string3 = "123test";
std::string test_link3 = "http://123-test.com";
ws.cell(test_cell).hyperlink(test_link3, test_string3);
xlnt_assert_equals(test_string3, ws.cell(test_cell).value<std::string>());
xlnt_assert_equals(ws.cell(test_cell).hyperlink().url(), test_link3);
// hyperlinks can also be applied to cells with non-string values
int numeric_test_val = 123;
std::string test_link4 = "http://test-numeric.com";
xlnt::cell_reference numeric_test_cell("B1");
ws.cell(numeric_test_cell).value(numeric_test_val);
ws.cell(numeric_test_cell).hyperlink(test_link4);
xlnt_assert_equals(ws.cell(numeric_test_cell).hyperlink().url(), test_link4);
xlnt_assert_equals(ws.cell(numeric_test_cell).value<int>(), numeric_test_val);
// and there should be no issues if two cells use the same hyperlink
ws.cell(numeric_test_cell).hyperlink(test_link3); // still in use on 'A1'
// 'A1'
xlnt_assert_equals(test_string3, ws.cell(test_cell).value<std::string>());
xlnt_assert_equals(ws.cell(test_cell).hyperlink().url(), test_link3);
// 'B1'
xlnt_assert_equals(ws.cell(numeric_test_cell).hyperlink().url(), test_link3);
xlnt_assert_equals(ws.cell(numeric_test_cell).value<int>(), numeric_test_val);
}
void test_rows() void test_rows()
{ {
xlnt::workbook wb; xlnt::workbook wb;