Merge remote-tracking branch 'origin/dev-unimplemented-functions' into dev-resolve-simple-issues

# Conflicts:
#	source/utils/variant.cpp
This commit is contained in:
Crzyrndm 2018-07-03 14:21:24 +12:00
commit cf4026536c
19 changed files with 316 additions and 122 deletions

View File

@ -262,26 +262,21 @@ public:
/// </summary>
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>
/// 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.
/// </summary>
void hyperlink(const std::string &url, const std::string &display);
void hyperlink(const std::string &url, const std::string &display = "");
/// <summary>
/// Adds an internal hyperlink to this cell pointing to the given cell.
/// </summary>
void hyperlink(xlnt::cell target);
void hyperlink(xlnt::cell target, const std::string& display = "");
/// <summary>
/// Adds an internal hyperlink to this cell pointing to the given range.
/// </summary>
void hyperlink(xlnt::range target);
void hyperlink(xlnt::range target, const std::string& display = "");
/// <summary>
/// Returns true if this cell has a hyperlink set.

View File

@ -44,12 +44,22 @@ class XLNT_API hyperlink
public:
bool external() const;
class relationship relationship() const;
// external target
std::string url() const;
// internal target
std::string target_range() const;
bool has_display() const;
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);
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:
friend class cell;

View File

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

View File

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

View File

@ -117,6 +117,11 @@ public:
/// </summary>
const class cell cell(const cell_reference &ref) const;
/// <summary>
/// The worksheet this range targets
/// </summary>
const worksheet &worksheet() const;
/// <summary>
/// Returns the reference defining the bounds of this range.
/// </summary>
@ -289,7 +294,7 @@ private:
/// <summary>
/// The worksheet this range is within
/// </summary>
worksheet ws_;
class worksheet ws_;
/// <summary>
/// The reference of this range

View File

@ -57,11 +57,6 @@ public:
/// </summary>
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>
/// Constructs a range reference from cell references indicating top
/// left and bottom right coordinates of the range.

View File

@ -372,7 +372,7 @@ hyperlink cell::hyperlink() const
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())
{
@ -388,7 +388,8 @@ void cell::hyperlink(const std::string &url)
auto relationships = manifest.relationships(ws.path(), relationship_type::hyperlink);
auto relation = std::find_if(relationships.cbegin(), relationships.cend(),
[&url](xlnt::relationship rel) { return rel.target().path().string() == url; });
if (relation != relationships.end()) {
if (relation != relationships.end())
{
d_->hyperlink_.get().relationship = *relation;
}
else
@ -401,28 +402,19 @@ void cell::hyperlink(const std::string &url)
// TODO: make manifest::register_relationship return the created relationship instead of rel id
d_->hyperlink_.get().relationship = manifest.relationship(ws.path(), rel_id);
}
if (!has_value()) // hyperlink on an empty cell sets the value to the hyperlink string
// if a value is already present, the display string is ignored
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)
{
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)
void cell::hyperlink(xlnt::cell target, const std::string& display)
{
// TODO: should this computed value be a method on a cell?
const auto cell_address = target.worksheet().title() + "!" + target.reference().to_string();
@ -430,8 +422,37 @@ void cell::hyperlink(xlnt::cell target)
d_->hyperlink_ = detail::hyperlink_impl();
d_->hyperlink_.get().relationship = xlnt::relationship("", relationship_type::hyperlink,
uri(""), uri(cell_address), target_mode::internal);
d_->hyperlink_.get().display = cell_address;
value(cell_address);
// 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() ? 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.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)
@ -472,6 +493,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)
{
if (error.length() == 0 || error[0] != '#')
@ -743,6 +773,11 @@ calendar cell::base_date() const
return workbook().base_date();
}
bool operator==(std::nullptr_t, const cell &cell)
{
return cell.data_type() == cell::type::empty;
}
XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell)
{
return stream << cell.to_string();
@ -801,10 +836,13 @@ void cell::value(const std::string &value_string, bool infer_type)
}
void cell::clear_format()
{
if (d_->format_.is_set())
{
format().d_->references -= format().d_->references > 0 ? 1 : 0;
d_->format_.clear();
}
}
void cell::clear_style()
{

View File

@ -67,24 +67,49 @@ bool hyperlink::external() const
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)
{
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

View File

@ -65,6 +65,11 @@ std::vector<rich_text_run> rich_text::runs() const
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)
{
runs_.push_back(t);

View File

@ -26,15 +26,18 @@
#include <string>
#include <xlnt/packaging/relationship.hpp>
#include <xlnt/utils/optional.hpp>
namespace xlnt {
namespace detail {
// [serialised]
struct hyperlink_impl
{
xlnt::relationship relationship;
std::string tooltip;
std::string display;
xlnt::optional<std::string> location;
xlnt::optional<std::string> tooltip;
xlnt::optional<std::string> display;
};
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_;
}
void indexed_color::index(std::size_t index)
{
index_ = index;
}
// theme_color implementation
theme_color::theme_color(std::size_t index) : index_(index)
@ -68,6 +73,11 @@ std::size_t theme_color::index() const
return index_;
}
void theme_color::index(std::size_t index)
{
index_ = index;
}
// rgb_color implementation
std::string rgb_color::hex_string() const

View File

@ -81,6 +81,11 @@ bool conditional_format::operator!=(const conditional_format &other) const
return !(*this == other);
}
bool conditional_format::has_border() const
{
return d_->border_id.is_set();
}
xlnt::border conditional_format::border() const
{
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;
}
bool conditional_format::has_fill() const
{
return d_->fill_id.is_set();
}
xlnt::fill conditional_format::fill() const
{
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;
}
bool conditional_format::has_font() const
{
return d_->font_id.is_set();
}
xlnt::font conditional_format::font() const
{
return d_->parent->fonts.at(d_->font_id.get());

View File

@ -26,6 +26,26 @@
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()
: locked_(false), hidden_(false)
{

View File

@ -66,6 +66,11 @@ std::size_t style::builtin_id() const
return d_->builtin_id.get();
}
bool style::builtin() const
{
return d_->builtin_id.is_set();
}
std::string style::name() const
{
return d_->name;

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
bool path::is_relative() const

View File

@ -31,7 +31,7 @@
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),
ref_(reference),
order_(order),
@ -71,6 +71,16 @@ cell_vector range::operator[](std::size_t index)
return vector(index);
}
const cell_vector range::operator[](std::size_t index) const
{
return vector(index);
}
const worksheet &range::worksheet() const
{
return ws_;
}
range_reference range::reference() const
{
return ref_;
@ -109,6 +119,22 @@ cell_vector range::vector(std::size_t vector_index)
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)
{
return ref_.top_left().column_index() <= ref.column_index()
@ -185,6 +211,11 @@ cell range::cell(const cell_reference &ref)
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()
{
return *begin();

View File

@ -25,9 +25,9 @@
#include <sstream>
#include <helpers/test_suite.hpp>
#include <helpers/assertions.hpp>
#include <xlnt/xlnt.hpp>
#include <helpers/assertions.hpp>
#include <helpers/test_suite.hpp>
class cell_test_suite : public test_suite
{
@ -140,8 +140,7 @@ private:
xlnt::cell::type::error,
xlnt::cell::type::formula_string,
xlnt::cell::type::number,
xlnt::cell::type::shared_string
};
xlnt::cell::type::shared_string};
for (const auto &datatype : datatypes)
{
@ -209,7 +208,6 @@ private:
xlnt_assert(!cell.has_formula());
}
void test_not_formula()
{
xlnt::workbook wb;
@ -654,13 +652,81 @@ private:
{
xlnt::workbook wb;
auto cell = wb.active_sheet().cell("A1");
xlnt_assert(!cell.has_hyperlink());
xlnt_assert_throws(cell.hyperlink(), xlnt::invalid_attribute);
xlnt_assert_throws(cell.hyperlink("notaurl"), 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_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()

View File

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

View File

@ -46,7 +46,6 @@ public:
register_test(test_remove_named_range_bad);
register_test(test_cell_alternate_coordinates);
register_test(test_cell_range_name);
register_test(test_hyperlink_value);
register_test(test_rows);
register_test(test_no_rows);
register_test(test_no_cols);
@ -221,50 +220,6 @@ public:
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()
{
xlnt::workbook wb;