2018-01-22 22:38:48 +08:00
|
|
|
// Copyright (c) 2014-2018 Thomas Fussell
|
2014-06-06 04:19:31 +08:00
|
|
|
//
|
|
|
|
// 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
|
2016-12-04 20:29:10 +08:00
|
|
|
|
2014-05-22 05:48:51 +08:00
|
|
|
#pragma once
|
|
|
|
|
2017-03-20 02:39:33 +08:00
|
|
|
#include <functional>
|
2015-10-14 01:56:07 +08:00
|
|
|
#include <iterator>
|
2014-05-22 05:48:51 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
#include <xlnt/xlnt_config.hpp>
|
2017-04-04 07:24:36 +08:00
|
|
|
#include <xlnt/styles/alignment.hpp>
|
|
|
|
#include <xlnt/styles/border.hpp>
|
|
|
|
#include <xlnt/styles/conditional_format.hpp>
|
|
|
|
#include <xlnt/styles/fill.hpp>
|
|
|
|
#include <xlnt/styles/font.hpp>
|
|
|
|
#include <xlnt/styles/number_format.hpp>
|
|
|
|
#include <xlnt/styles/protection.hpp>
|
2015-11-02 14:07:43 +08:00
|
|
|
#include <xlnt/worksheet/cell_vector.hpp>
|
2016-12-04 20:29:10 +08:00
|
|
|
#include <xlnt/worksheet/major_order.hpp>
|
2016-01-25 00:15:49 +08:00
|
|
|
#include <xlnt/worksheet/range_iterator.hpp>
|
2015-11-02 14:07:43 +08:00
|
|
|
#include <xlnt/worksheet/range_reference.hpp>
|
|
|
|
#include <xlnt/worksheet/worksheet.hpp>
|
2014-05-22 05:48:51 +08:00
|
|
|
|
|
|
|
namespace xlnt {
|
2014-07-20 05:42:04 +08:00
|
|
|
|
2016-01-25 00:15:49 +08:00
|
|
|
class const_range_iterator;
|
|
|
|
class range_iterator;
|
|
|
|
|
2015-11-21 09:41:32 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A range is a 2D collection of cells with defined extens that can be iterated upon.
|
|
|
|
/// </summary>
|
2016-10-26 08:21:58 +08:00
|
|
|
class XLNT_API range
|
2014-05-22 05:48:51 +08:00
|
|
|
{
|
2016-01-18 14:23:31 +08:00
|
|
|
public:
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Alias for the iterator type
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-01-25 00:15:49 +08:00
|
|
|
using iterator = range_iterator;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Alias for the const iterator type
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-01-25 00:15:49 +08:00
|
|
|
using const_iterator = const_range_iterator;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Alias for the reverse iterator type
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Alias for the const reverse iterator type
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
2016-01-18 14:23:31 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Constructs a range on the given worksheet.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
range(worksheet ws, const range_reference &reference,
|
|
|
|
major_order order = major_order::row, bool skip_null = false);
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Desctructor
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-05-22 05:48:51 +08:00
|
|
|
~range();
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Default copy constructor.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-11-10 08:52:18 +08:00
|
|
|
range(const range &) = default;
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2017-10-27 01:45:56 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Erases all cell data from the worksheet for cells within this range.
|
|
|
|
/// </summary>
|
|
|
|
void clear_cells();
|
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a vector pointing to the n-th row or column in this range (depending
|
|
|
|
/// on major order).
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
cell_vector vector(std::size_t n);
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a vector pointing to the n-th row or column in this range (depending
|
|
|
|
/// on major order).
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
const cell_vector vector(std::size_t n) const;
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a cell in the range relative to its top left cell.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-12-03 23:46:48 +08:00
|
|
|
class cell cell(const cell_reference &ref);
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a cell in the range relative to its top left cell.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-12-02 21:37:50 +08:00
|
|
|
const class cell cell(const cell_reference &ref) const;
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2018-07-10 09:18:21 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The worksheet this range targets
|
|
|
|
/// </summary>
|
|
|
|
const worksheet & target_worksheet() const;
|
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns the reference defining the bounds of this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-12-02 21:37:50 +08:00
|
|
|
range_reference reference() const;
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns the number of rows or columns in this range (depending on major order).
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-07-20 04:59:05 +08:00
|
|
|
std::size_t length() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns true if the given cell exists in the parent worksheet of this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2015-10-14 01:56:07 +08:00
|
|
|
bool contains(const cell_reference &ref);
|
2017-03-20 02:22:29 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Sets the alignment of all cells in the range to new_alignment and returns the range.
|
2017-03-20 02:22:29 +08:00
|
|
|
/// </summary>
|
|
|
|
range alignment(const xlnt::alignment &new_alignment);
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Sets the border of all cells in the range to new_border and returns the range.
|
2017-03-20 02:22:29 +08:00
|
|
|
/// </summary>
|
|
|
|
range border(const xlnt::border &new_border);
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Sets the fill of all cells in the range to new_fill and returns the range.
|
2017-03-20 02:22:29 +08:00
|
|
|
/// </summary>
|
|
|
|
range fill(const xlnt::fill &new_fill);
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Sets the font of all cells in the range to new_font and returns the range.
|
2017-03-20 02:22:29 +08:00
|
|
|
/// </summary>
|
|
|
|
range font(const xlnt::font &new_font);
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Sets the number format of all cells in the range to new_number_format and
|
|
|
|
/// returns the range.
|
2017-03-20 02:22:29 +08:00
|
|
|
/// </summary>
|
|
|
|
range number_format(const xlnt::number_format &new_number_format);
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Sets the protection of all cells in the range to new_protection and returns the range.
|
2017-03-20 02:22:29 +08:00
|
|
|
/// </summary>
|
|
|
|
range protection(const xlnt::protection &new_protection);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the named style applied to all cells in this range to a style named style_name.
|
|
|
|
/// </summary>
|
|
|
|
range style(const class style &new_style);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the named style applied to all cells in this range to a style named style_name.
|
|
|
|
/// If this style has not been previously created in the workbook, a
|
|
|
|
/// key_not_found exception will be thrown.
|
|
|
|
/// </summary>
|
|
|
|
range style(const std::string &style_name);
|
2016-11-20 14:01:32 +08:00
|
|
|
|
2017-04-04 07:24:36 +08:00
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
2017-04-04 07:49:38 +08:00
|
|
|
xlnt::conditional_format conditional_format(const condition &when);
|
2017-04-04 07:24:36 +08:00
|
|
|
|
2017-03-24 08:05:25 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns the first row or column in this range.
|
2017-03-24 08:05:25 +08:00
|
|
|
/// </summary>
|
|
|
|
cell_vector front();
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns the first row or column in this range.
|
2017-03-24 08:05:25 +08:00
|
|
|
/// </summary>
|
|
|
|
const cell_vector front() const;
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns the last row or column in this range.
|
2017-03-24 08:05:25 +08:00
|
|
|
/// </summary>
|
|
|
|
cell_vector back();
|
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns the last row or column in this range.
|
2017-03-24 08:05:25 +08:00
|
|
|
/// </summary>
|
|
|
|
const cell_vector back() const;
|
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns an iterator to the first row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-05-22 05:48:51 +08:00
|
|
|
iterator begin();
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns an iterator to one past the last row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-05-22 05:48:51 +08:00
|
|
|
iterator end();
|
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns an iterator to the first row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2015-11-21 09:41:32 +08:00
|
|
|
const_iterator begin() const;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns an iterator to one past the last row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2015-11-21 09:41:32 +08:00
|
|
|
const_iterator end() const;
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns an iterator to the first row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-05-22 05:48:51 +08:00
|
|
|
const_iterator cbegin() const;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns an iterator to one past the last row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-05-22 05:48:51 +08:00
|
|
|
const_iterator cend() const;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a reverse iterator to the first row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
reverse_iterator rbegin();
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a reverse iterator to one past the last row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
reverse_iterator rend();
|
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a reverse iterator to the first row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
const_reverse_iterator rbegin() const;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a reverse iterator to one past the last row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
const_reverse_iterator rend() const;
|
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a reverse iterator to the first row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
const_reverse_iterator crbegin() const;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Returns a reverse iterator to one past the last row or column in this range.
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2016-06-14 11:36:26 +08:00
|
|
|
const_reverse_iterator crend() const;
|
2014-05-22 05:48:51 +08:00
|
|
|
|
2017-03-20 02:22:29 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Applies function f to all cells in the range
|
2017-03-20 02:22:29 +08:00
|
|
|
/// </summary>
|
|
|
|
void apply(std::function<void(class cell)> f);
|
|
|
|
|
2017-03-31 11:52:57 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the n-th row or column in this range.
|
|
|
|
/// </summary>
|
|
|
|
cell_vector operator[](std::size_t n);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the n-th row or column in this range.
|
|
|
|
/// </summary>
|
|
|
|
const cell_vector operator[](std::size_t n) const;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns true if this range is equivalent to comparand.
|
|
|
|
/// </summary>
|
|
|
|
bool operator==(const range &comparand) const;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns true if this range is not equivalent to comparand.
|
|
|
|
/// </summary>
|
|
|
|
bool operator!=(const range &comparand) const;
|
|
|
|
|
2018-07-02 06:29:58 +08:00
|
|
|
private:
|
2016-11-20 14:01:32 +08:00
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// The worksheet this range is within
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2018-07-02 06:29:58 +08:00
|
|
|
class worksheet ws_;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// The reference of this range
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-05-22 05:48:51 +08:00
|
|
|
range_reference ref_;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Whether this range should be iterated by columns or rows first
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2014-07-20 05:42:04 +08:00
|
|
|
major_order order_;
|
2016-11-20 14:01:32 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2017-03-31 11:52:57 +08:00
|
|
|
/// Whether null rows/columns and cells should be skipped during iteration
|
2016-11-20 14:01:32 +08:00
|
|
|
/// </summary>
|
2015-11-21 09:41:32 +08:00
|
|
|
bool skip_null_;
|
2014-05-22 05:48:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace xlnt
|