clean up worksheet api

This commit is contained in:
Thomas Fussell 2017-03-20 20:17:09 -04:00
parent 497699700f
commit d8c9d14a98
6 changed files with 150 additions and 194 deletions

View File

@ -115,11 +115,6 @@ public:
/// </summary>
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
/// <summary>
/// Swaps the data held in workbooks "left" and "right".
/// </summary>
friend void swap(workbook &left, workbook &right);
/// <summary>
/// Constructs and returns an empty workbook similar to a default.
/// Excel workbook
@ -832,6 +827,11 @@ private:
/// </summary>
void update_sheet_properties();
/// <summary>
/// Swaps the data held in this workbook with workbook other.
/// </summary>
void swap(workbook &other);
/// <summary>
/// An opaque pointer to a structure that holds all of the data relating to this workbook.
/// </summary>

View File

@ -172,47 +172,41 @@ public:
// container
/// <summary>
///
/// </summary>
class cell cell(column_t column, row_t row);
/// <summary>
///
/// </summary>
const class cell cell(column_t column, row_t row) const;
/// <summary>
///
/// </summary>
class cell cell(const cell_reference &reference);
/// <summary>
///
/// </summary>
const class cell cell(const cell_reference &reference) const;
/// <summary>
///
/// Returns true if this sheet has an initialized cell at the given reference.
/// </summary>
bool has_cell(const cell_reference &reference) const;
/// <summary>
///
/// Returns the cell at the given reference. If the cell doesn't exist, it
/// will be initialized to null before being returned.
/// </summary>
class cell cell(const cell_reference &reference);
/// <summary>
/// Returns the cell at the given reference. If the cell doesn't exist, an
/// invalid_parameter exception will be thrown.
/// </summary>
const class cell cell(const cell_reference &reference) const;
/// <summary>
/// Returns the range defined by reference string. If reference string is the name of
/// a previously-defined named range in the sheet, it will be returned.
/// </summary>
class range range(const std::string &reference_string);
/// <summary>
///
/// </summary>
class range range(const range_reference &reference);
/// <summary>
///
/// Returns the range defined by reference string. If reference string is the name of
/// a previously-defined named range in the sheet, it will be returned.
/// </summary>
const class range range(const std::string &reference_string) const;
/// <summary>
///
/// Returns the range defined by reference.
/// </summary>
class range range(const range_reference &reference);
/// <summary>
/// Returns the range defined by reference.
/// </summary>
const class range range(const range_reference &reference) const;
@ -302,16 +296,6 @@ public:
/// </summary>
cell_reference point_pos(int left, int top) const;
/// <summary>
///
/// </summary>
cell_reference point_pos(const std::pair<int, int> &point) const;
/// <summary>
///
/// </summary>
std::string unique_sheet_name(const std::string &value) const;
// named range
/// <summary>
@ -327,15 +311,22 @@ public:
/// <summary>
///
/// </summary>
bool has_named_range(const std::string &name);
bool has_named_range(const std::string &name) const;
/// <summary>
///
/// Returns the named range with the given name. Throws key_not_found
/// exception if the named range doesn't exist.
/// </summary>
class range named_range(const std::string &name);
/// <summary>
///
/// Returns the named range with the given name. Throws key_not_found
/// exception if the named range doesn't exist.
/// </summary>
const class range named_range(const std::string &name) const;
/// <summary>
/// Removes a named range with the given name.
/// </summary>
void remove_named_range(const std::string &name);
@ -477,26 +468,6 @@ public:
/// </summary>
const class cell operator[](const cell_reference &reference) const;
/// <summary>
///
/// </summary>
class range operator[](const range_reference &reference);
/// <summary>
///
/// </summary>
const class range operator[](const range_reference &reference) const;
/// <summary>
///
/// </summary>
class range operator[](const std::string &range_string);
/// <summary>
///
/// </summary>
const class range operator[](const std::string &range_string) const;
/// <summary>
///
/// </summary>
@ -515,189 +486,181 @@ public:
// page
/// <summary>
///
/// Returns true if this worksheet has a page setup.
/// </summary>
bool has_page_setup() const;
/// <summary>
///
/// Returns the page setup for this sheet.
/// </summary>
xlnt::page_setup page_setup() const;
/// <summary>
///
/// Sets the page setup for this sheet to setup.
/// </summary>
void page_setup(const struct page_setup &setup);
/// <summary>
///
/// Returns true if this page has margins.
/// </summary>
bool has_page_margins() const;
/// <summary>
///
/// Returns the margins of this sheet.
/// </summary>
xlnt::page_margins page_margins() const;
/// <summary>
///
/// Sets the margins of this sheet to margins.
/// </summary>
void page_margins(const class page_margins &margins);
// auto filter
/// <summary>
///
/// Returns the current auto-filter of this sheet.
/// </summary>
range_reference auto_filter() const;
/// <summary>
///
/// Sets the auto-filter of this sheet to the range defined by range_string.
/// </summary>
void auto_filter(const std::string &range_string);
/// <summary>
///
/// Sets the auto-filter of this sheet to the given range.
/// </summary>
void auto_filter(const xlnt::range &range);
/// <summary>
///
/// Sets the auto-filter of this sheet to the range defined by reference.
/// </summary>
void auto_filter(const range_reference &reference);
/// <summary>
///
/// Clear the auto-filter from this sheet.
/// </summary>
void clear_auto_filter();
/// <summary>
///
/// Returns true if this sheet has an auto-filter set.
/// </summary>
bool has_auto_filter() const;
/// <summary>
///
/// Reserve n rows. This can be optionally called before adding many rows
/// to improve performance.
/// </summary>
void reserve(std::size_t n);
/// <summary>
///
/// Returns true if this sheet has a header/footer.
/// </summary>
bool has_header_footer() const;
/// <summary>
///
/// Returns the header/footer of this sheet.
/// </summary>
class header_footer header_footer() const;
/// <summary>
///
/// Sets the header/footer of this sheet to new_header_footer.
/// </summary>
void header_footer(const class header_footer &new_header_footer);
/// <summary>
///
/// </summary>
void parent(class workbook &wb);
/// <summary>
///
/// </summary>
std::vector<std::string> formula_attributes() const;
/// <summary>
///
/// Returns the sheet state of this sheet.
/// </summary>
xlnt::sheet_state sheet_state() const;
/// <summary>
///
/// Sets the sheet state of this sheet.
/// </summary>
void sheet_state(xlnt::sheet_state state);
/// <summary>
///
/// Returns an iterator to the first row in this sheet.
/// </summary>
iterator begin();
/// <summary>
///
/// Returns an iterator one past the last row in this sheet.
/// </summary>
iterator end();
/// <summary>
///
/// Return a constant iterator to the first row in this sheet.
/// </summary>
const_iterator begin() const;
/// <summary>
///
/// Returns a constant iterator to one past the last row in this sheet.
/// </summary>
const_iterator end() const;
/// <summary>
///
/// Return a constant iterator to the first row in this sheet.
/// </summary>
const_iterator cbegin() const;
/// <summary>
///
/// Returns a constant iterator to one past the last row in this sheet.
/// </summary>
const_iterator cend() const;
/// <summary>
///
/// Returns a range containing all of the cells in this sheet. If skip_null is true,
/// iterating over the range will only return rows and cells that are non-null.
/// </summary>
class range iter_cells(bool skip_null);
/// <summary>
///
/// Sets rows to repeat at top during printing.
/// </summary>
void print_title_rows(row_t first_row, row_t last_row);
/// <summary>
///
/// Sets rows to repeat at top during printing.
/// </summary>
void print_title_rows(row_t last_row);
/// <summary>
///
/// Sets columns to repeat at left during printing.
/// </summary>
void print_title_cols(column_t first_column, column_t last_column);
/// <summary>
///
/// Sets columns to repeat at left during printing.
/// </summary>
void print_title_cols(column_t last_column);
/// <summary>
///
/// Returns a string representation of the defined print titles.
/// </summary>
std::string print_titles() const;
/// <summary>
///
/// Sets the print area of this sheet to print_area.
/// </summary>
void print_area(const std::string &print_area);
/// <summary>
///
/// Returns the print area defined for this sheet.
/// </summary>
range_reference print_area() const;
/// <summary>
///
/// Returns true if this sheet has any number of views defined.
/// </summary>
bool has_view() const;
/// <summary>
///
/// Returns the view at the given index.
/// </summary>
sheet_view view(std::size_t index = 0) const;
/// <summary>
///
/// Adds new_view to the set of available views for this sheet.
/// </summary>
void add_view(const sheet_view &new_view);
@ -738,12 +701,17 @@ private:
friend class detail::xlsx_producer;
/// <summary>
///
/// Constructs a worksheet impl wrapper from d.
/// </summary>
worksheet(detail::worksheet_impl *d);
/// <summary>
/// Creates a comments part in the manifest as a relationship target of this sheet.
/// </summary>
void register_comments_in_manifest();
/// <summary>
/// Add the calcChain part to the workbook if it doesn't already exist.
/// Creates a calcChain part in the manifest if it doesn't already exist.
/// </summary>
void register_calc_chain_in_manifest();
@ -753,12 +721,12 @@ private:
void garbage_collect_formulae();
/// <summary>
///
/// Sets the parent of this worksheet to wb.
/// </summary>
worksheet(detail::worksheet_impl *d);
void parent(class workbook &wb);
/// <summary>
///
/// The pointer to this sheet's implementation.
/// </summary>
detail::worksheet_impl *d_;
};

View File

@ -523,7 +523,7 @@ workbook workbook::empty()
workbook::workbook()
{
auto wb_template = empty();
swap(*this, wb_template);
swap(wb_template);
}
workbook::workbook(detail::workbook_impl *impl)
@ -1137,8 +1137,10 @@ bool workbook::operator!=(const workbook &rhs) const
return d_.get() != rhs.d_.get();
}
void swap(workbook &left, workbook &right)
void workbook::swap(workbook &right)
{
auto &left = *this;
using std::swap;
swap(left.d_, right.d_);
@ -1171,7 +1173,7 @@ void swap(workbook &left, workbook &right)
workbook &workbook::operator=(workbook other)
{
swap(*this, other);
swap(other);
d_->stylesheet_.get().parent = this;
return *this;
@ -1180,7 +1182,7 @@ workbook &workbook::operator=(workbook other)
workbook::workbook(workbook &&other)
: workbook(nullptr)
{
swap(*this, other);
swap(other);
}
workbook::workbook(const workbook &other)

View File

@ -19,7 +19,8 @@ public:
{
for (auto column = 1; column <= 10; ++column)
{
ws.cell(column, row).value(xlnt::cell_reference(column, row).to_string());
auto ref = xlnt::cell_reference(column, row);
ws[ref].value(ref.to_string());
}
}

View File

@ -928,17 +928,17 @@ public:
void test_unique_sheet_name()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
auto active_name = ws.title();
auto next_name = ws.unique_sheet_name(active_name);
auto first_created = wb.create_sheet();
auto second_created = wb.create_sheet();
TS_ASSERT_DIFFERS(active_name, next_name);
TS_ASSERT_DIFFERS(first_created.title(), second_created.title());
}
void test_page_margins()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
auto margins = ws.page_margins();
@ -948,6 +948,16 @@ public:
margins.footer(3);
margins.left(4);
margins.right(5);
ws.page_margins(margins);
TS_ASSERT(ws.has_page_margins());
TS_ASSERT_EQUALS(ws.page_margins().top(), 0);
TS_ASSERT_EQUALS(ws.page_margins().bottom(), 1);
TS_ASSERT_EQUALS(ws.page_margins().header(), 2);
TS_ASSERT_EQUALS(ws.page_margins().footer(), 3);
TS_ASSERT_EQUALS(ws.page_margins().left(), 4);
TS_ASSERT_EQUALS(ws.page_margins().right(), 5);
}
void test_garbage_collect()
@ -1028,14 +1038,14 @@ public:
ws1[xlnt::cell_reference("A2")].value(true);
TS_ASSERT_EQUALS(ws1[xlnt::cell_reference("A2")].value<bool>(), true);
TS_ASSERT_EQUALS((*(*ws1[xlnt::range_reference("A2:A2")].begin()).begin()).value<bool>(), true);
TS_ASSERT_EQUALS((*(*ws1.range("A2:A2").begin()).begin()).value<bool>(), true);
ws1.create_named_range("rangey", "A2:A2");
TS_ASSERT_EQUALS(ws1[std::string("rangey")], ws1.range("A2:A2"));
TS_ASSERT_EQUALS(ws1[std::string("A2:A2")], ws1.range("A2:A2"));
TS_ASSERT(ws1[std::string("rangey")] != ws1.range("A2:A3"));
TS_ASSERT_EQUALS(ws1.range("rangey"), ws1.range("A2:A2"));
TS_ASSERT_EQUALS(ws1.range("A2:A2"), ws1.range("A2:A2"));
TS_ASSERT(ws1.range("rangey") != ws1.range("A2:A3"));
TS_ASSERT_EQUALS(ws1[std::string("rangey")].cell("A1"), ws1.cell("A2"));
TS_ASSERT_EQUALS(ws1.range("rangey").cell("A1"), ws1.cell("A2"));
}
void test_reserve()
@ -1110,7 +1120,7 @@ public:
xlnt::workbook wb;
auto ws = wb.active_sheet();
TS_ASSERT_EQUALS(ws.point_pos({0, 0}), "A1");
TS_ASSERT_EQUALS(ws.point_pos(0, 0), "A1");
}
void test_named_range_named_cell_reference()

View File

@ -82,22 +82,6 @@ bool worksheet::has_frozen_panes() const
|| d_->views_.front().pane().state == pane_state::frozen_split);
}
std::string worksheet::unique_sheet_name(const std::string &value) const
{
auto names = workbook().sheet_titles();
auto match = std::find(names.begin(), names.end(), value);
std::size_t append = 0;
while (match != names.end())
{
append++;
match = std::find(names.begin(), names.end(), value + std::to_string(append));
}
return append == 0 ? value : value + std::to_string(append);
}
void worksheet::create_named_range(const std::string &name, const std::string &reference_string)
{
create_named_range(name, range_reference(reference_string));
@ -373,16 +357,6 @@ void worksheet::unfreeze_panes()
primary_view.clear_pane();
}
cell worksheet::cell(column_t column, row_t row)
{
return cell(cell_reference(column, row));
}
const cell worksheet::cell(column_t column, row_t row) const
{
return cell(cell_reference(column, row));
}
cell worksheet::cell(const cell_reference &reference)
{
if (d_->cell_map_.find(reference.row()) == d_->cell_map_.end())
@ -439,6 +413,21 @@ range worksheet::named_range(const std::string &name)
return range(d_->named_ranges_[name].targets()[0].second);
}
const range worksheet::named_range(const std::string &name) const
{
if (!workbook().has_named_range(name))
{
throw key_not_found();
}
if (!has_named_range(name))
{
throw key_not_found();
}
return range(d_->named_ranges_[name].targets()[0].second);
}
column_t worksheet::lowest_column() const
{
if (d_->cell_map_.empty())
@ -510,6 +499,21 @@ range_reference worksheet::calculate_dimension() const
range worksheet::range(const std::string &reference_string)
{
if (has_named_range(reference_string))
{
return named_range(reference_string);
}
return range(range_reference(reference_string));
}
const range worksheet::range(const std::string &reference_string) const
{
if (has_named_range(reference_string))
{
return named_range(reference_string);
}
return range(range_reference(reference_string));
}
@ -518,11 +522,6 @@ range worksheet::range(const range_reference &reference)
return xlnt::range(*this, reference);
}
const range worksheet::range(const std::string &reference_string) const
{
return range(range_reference(reference_string));
}
const range worksheet::range(const range_reference &reference) const
{
return xlnt::range(*this, reference);
@ -773,21 +772,7 @@ const cell worksheet::operator[](const cell_reference &ref) const
return cell(ref);
}
range worksheet::operator[](const range_reference &ref)
{
return range(ref);
}
range worksheet::operator[](const std::string &name)
{
if (has_named_range(name))
{
return named_range(name);
}
return range(range_reference(name));
}
bool worksheet::has_named_range(const std::string &name)
bool worksheet::has_named_range(const std::string &name) const
{
return d_->named_ranges_.find(name) != d_->named_ranges_.end();
}
@ -812,16 +797,6 @@ class header_footer worksheet::header_footer() const
return d_->header_footer_.get();
}
void worksheet::parent(xlnt::workbook &wb)
{
d_->parent_ = &wb;
}
std::vector<std::string> worksheet::formula_attributes() const
{
return {};
}
cell_reference worksheet::point_pos(int left, int top) const
{
column_t current_column = 1;
@ -843,11 +818,6 @@ cell_reference worksheet::point_pos(int left, int top) const
return {current_column - 1, current_row - 1};
}
cell_reference worksheet::point_pos(const std::pair<int, int> &point) const
{
return point_pos(point.first, point.second);
}
void worksheet::sheet_state(xlnt::sheet_state state)
{
page_setup().sheet_state(state);
@ -1080,4 +1050,9 @@ void worksheet::garbage_collect_formulae()
workbook().garbage_collect_formulae();
}
void worksheet::parent(xlnt::workbook &wb)
{
d_->parent_ = &wb;
}
} // namespace xlnt