2015-12-25 06:10:02 +08:00
|
|
|
// Copyright (c) 2014-2016 Thomas Fussell
|
2015-10-02 06:14:42 +08:00
|
|
|
// Copyright (c) 2010-2015 openpyxl
|
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
|
2014-05-21 22:20:30 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-06-14 03:05:24 +08:00
|
|
|
#include <memory>
|
2014-05-21 22:20:30 +08:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-11-20 11:54:54 +08:00
|
|
|
#include <xlnt/xlnt_config.hpp>
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/packaging/relationship.hpp>
|
2014-07-20 02:43:48 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
namespace xlnt {
|
|
|
|
|
2015-10-15 06:05:13 +08:00
|
|
|
class alignment;
|
2016-03-09 11:32:32 +08:00
|
|
|
class app_properties;
|
2015-10-15 06:05:13 +08:00
|
|
|
class border;
|
2016-03-14 11:46:01 +08:00
|
|
|
class format;
|
2015-10-15 06:05:13 +08:00
|
|
|
class color;
|
2016-01-25 00:15:49 +08:00
|
|
|
class const_worksheet_iterator;
|
2014-07-20 04:59:05 +08:00
|
|
|
class document_properties;
|
2014-05-21 22:20:30 +08:00
|
|
|
class drawing;
|
2015-10-15 06:05:13 +08:00
|
|
|
class fill;
|
|
|
|
class font;
|
2016-03-10 17:12:51 +08:00
|
|
|
class format;
|
2015-10-30 01:46:56 +08:00
|
|
|
class manifest;
|
2015-10-15 06:05:13 +08:00
|
|
|
class named_range;
|
2015-10-19 03:30:46 +08:00
|
|
|
class number_format;
|
2015-10-15 06:05:13 +08:00
|
|
|
class pattern_fill;
|
|
|
|
class protection;
|
2014-05-30 08:52:14 +08:00
|
|
|
class range;
|
2014-05-21 22:20:30 +08:00
|
|
|
class range_reference;
|
|
|
|
class relationship;
|
2015-10-19 03:30:46 +08:00
|
|
|
class style;
|
2015-10-30 07:37:07 +08:00
|
|
|
class theme;
|
2014-05-21 22:20:30 +08:00
|
|
|
class worksheet;
|
2016-01-25 00:15:49 +08:00
|
|
|
class worksheet_iterator;
|
2015-10-15 06:05:13 +08:00
|
|
|
class zip_file;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
enum class encoding;
|
2014-05-31 06:42:25 +08:00
|
|
|
|
2016-01-18 14:23:31 +08:00
|
|
|
namespace detail {
|
|
|
|
struct workbook_impl;
|
|
|
|
} // namespace detail
|
2014-06-16 01:06:47 +08:00
|
|
|
|
2014-06-13 23:41:32 +08:00
|
|
|
/// <summary>
|
|
|
|
/// workbook is the container for all other parts of the document.
|
|
|
|
/// </summary>
|
2015-11-04 13:36:14 +08:00
|
|
|
class XLNT_CLASS workbook
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2016-01-18 14:23:31 +08:00
|
|
|
public:
|
2016-01-25 00:15:49 +08:00
|
|
|
using iterator = worksheet_iterator;
|
|
|
|
using const_iterator = const_worksheet_iterator;
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
static std::size_t index_from_ws_filename(const std::string &filename);
|
2014-07-25 05:31:46 +08:00
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
// constructors
|
2014-06-16 01:06:47 +08:00
|
|
|
workbook();
|
2015-10-14 01:56:07 +08:00
|
|
|
workbook(encoding e);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-07-24 08:51:28 +08:00
|
|
|
workbook &operator=(workbook other);
|
|
|
|
workbook(workbook &&other);
|
|
|
|
workbook(const workbook &other);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-07-24 08:51:28 +08:00
|
|
|
friend void swap(workbook &left, workbook &right);
|
|
|
|
|
2015-11-23 01:41:27 +08:00
|
|
|
encoding get_encoding() const;
|
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
worksheet get_active_sheet();
|
2014-07-25 05:31:46 +08:00
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
bool get_guess_types() const;
|
2014-07-25 05:31:46 +08:00
|
|
|
void set_guess_types(bool guess);
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
bool get_data_only() const;
|
2014-07-25 05:31:46 +08:00
|
|
|
void set_data_only(bool data_only);
|
2016-03-06 10:39:50 +08:00
|
|
|
|
|
|
|
bool get_read_only() const;
|
|
|
|
void set_read_only(bool read_only);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
// create
|
2014-05-21 22:20:30 +08:00
|
|
|
worksheet create_sheet();
|
|
|
|
worksheet create_sheet(std::size_t index);
|
2015-11-11 07:58:54 +08:00
|
|
|
worksheet create_sheet(const std::string &title);
|
|
|
|
worksheet create_sheet(std::size_t index, const std::string &title);
|
|
|
|
worksheet create_sheet(const std::string &title, const relationship &rel);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
// add
|
2014-05-21 22:20:30 +08:00
|
|
|
void add_sheet(worksheet worksheet);
|
|
|
|
void add_sheet(worksheet worksheet, std::size_t index);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
// remove
|
2014-05-21 22:20:30 +08:00
|
|
|
void remove_sheet(worksheet worksheet);
|
|
|
|
void clear();
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
// container operations
|
2015-11-11 07:58:54 +08:00
|
|
|
worksheet get_sheet_by_name(const std::string &sheet_name);
|
|
|
|
const worksheet get_sheet_by_name(const std::string &sheet_name) const;
|
2014-05-30 08:52:14 +08:00
|
|
|
worksheet get_sheet_by_index(std::size_t index);
|
|
|
|
const worksheet get_sheet_by_index(std::size_t index) const;
|
2015-11-11 07:58:54 +08:00
|
|
|
bool contains(const std::string &key) const;
|
2014-05-21 22:20:30 +08:00
|
|
|
int get_index(worksheet worksheet);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
worksheet operator[](const std::string &name);
|
2014-06-11 05:12:15 +08:00
|
|
|
worksheet operator[](std::size_t index);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
iterator begin();
|
|
|
|
iterator end();
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-23 01:41:27 +08:00
|
|
|
const_iterator begin() const;
|
|
|
|
const_iterator end() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
const_iterator cbegin() const;
|
|
|
|
const_iterator cend() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::vector<std::string> get_sheet_names() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
document_properties &get_properties();
|
|
|
|
const document_properties &get_properties() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-03-09 11:32:32 +08:00
|
|
|
app_properties &get_app_properties();
|
|
|
|
const app_properties &get_app_properties() const;
|
|
|
|
|
2015-11-01 22:43:01 +08:00
|
|
|
// named ranges
|
2015-10-15 06:05:13 +08:00
|
|
|
std::vector<named_range> get_named_ranges() const;
|
2015-11-11 07:58:54 +08:00
|
|
|
void create_named_range(const std::string &name, worksheet worksheet, const range_reference &reference);
|
2015-11-11 08:47:31 +08:00
|
|
|
void create_named_range(const std::string &name, worksheet worksheet, const std::string &reference_string);
|
2015-11-11 07:58:54 +08:00
|
|
|
bool has_named_range(const std::string &name) const;
|
|
|
|
range get_named_range(const std::string &name);
|
|
|
|
void remove_named_range(const std::string &name);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
// serialization
|
2014-06-05 06:42:17 +08:00
|
|
|
bool save(std::vector<unsigned char> &data);
|
2015-11-11 07:58:54 +08:00
|
|
|
bool save(const std::string &filename);
|
2014-06-05 06:42:17 +08:00
|
|
|
bool load(const std::vector<unsigned char> &data);
|
2015-11-11 07:58:54 +08:00
|
|
|
bool load(const std::string &filename);
|
2015-10-24 02:42:36 +08:00
|
|
|
bool load(std::istream &stream);
|
2015-10-15 06:05:13 +08:00
|
|
|
bool load(zip_file &archive);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
bool operator==(const workbook &rhs) const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 12:03:48 +08:00
|
|
|
bool operator!=(const workbook &rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-07-20 04:59:05 +08:00
|
|
|
bool operator==(std::nullptr_t) const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-14 12:03:48 +08:00
|
|
|
bool operator!=(std::nullptr_t) const
|
|
|
|
{
|
|
|
|
return !(*this == std::nullptr_t{});
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void create_relationship(const std::string &id, const std::string &target, relationship::type type);
|
|
|
|
relationship get_relationship(const std::string &id) const;
|
2015-10-30 07:37:07 +08:00
|
|
|
const std::vector<relationship> &get_relationships() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
void add_alignment(const alignment &a);
|
|
|
|
void add_border(const border &b);
|
|
|
|
void add_fill(const fill &f);
|
|
|
|
void add_font(const font &f);
|
|
|
|
void add_color(const color &c);
|
|
|
|
void add_number_format(const number_format &format);
|
|
|
|
void add_protection(const protection &p);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-03-10 17:12:51 +08:00
|
|
|
const std::vector<format> &get_cell_style_formats() const;
|
|
|
|
const std::vector<format> &get_cell_formats() const;
|
2015-11-03 06:25:10 +08:00
|
|
|
const std::vector<style> &get_styles() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 06:25:10 +08:00
|
|
|
const std::vector<color> &get_colors() const;
|
|
|
|
const std::vector<border> &get_borders() const;
|
|
|
|
const std::vector<fill> &get_fills() const;
|
|
|
|
const std::vector<font> &get_fonts() const;
|
|
|
|
const std::vector<number_format> &get_number_formats() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 06:25:10 +08:00
|
|
|
color add_indexed_color(const color &rgb_color);
|
|
|
|
color get_indexed_color(const color &indexed_color) const;
|
2016-03-14 11:46:01 +08:00
|
|
|
|
|
|
|
format &add_default_cell_format();
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-19 03:30:46 +08:00
|
|
|
const number_format &get_number_format(std::size_t style_id) const;
|
|
|
|
std::size_t set_number_format(const number_format &format, std::size_t style_id);
|
|
|
|
const font &get_font(std::size_t style_id) const;
|
|
|
|
std::size_t set_font(const font &font_, std::size_t style_id);
|
|
|
|
const fill &get_fill(std::size_t style_id) const;
|
|
|
|
std::size_t set_fill(const fill &fill_, std::size_t style_id);
|
|
|
|
const border &get_border(std::size_t style_id) const;
|
|
|
|
std::size_t set_border(const border &border_, std::size_t style_id);
|
|
|
|
const alignment &get_alignment(std::size_t style_id) const;
|
|
|
|
std::size_t set_alignment(const alignment &alignment_, std::size_t style_id);
|
|
|
|
const protection &get_protection(std::size_t style_id) const;
|
|
|
|
std::size_t set_protection(const protection &protection_, std::size_t style_id);
|
|
|
|
bool get_pivot_button(std::size_t style_id) const;
|
|
|
|
bool get_quote_prefix(std::size_t style_id) const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void set_code_name(const std::string &code_name);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 07:37:07 +08:00
|
|
|
bool has_loaded_theme() const;
|
|
|
|
const theme &get_loaded_theme() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-03-10 17:12:51 +08:00
|
|
|
const format &get_cell_style_format(std::size_t format_id) const;
|
|
|
|
std::size_t add_cell_style_format(const format &format_);
|
|
|
|
|
|
|
|
const format &get_cell_format(std::size_t format_id) const;
|
|
|
|
std::size_t add_cell_format(const format &format_);
|
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const style &get_style(std::size_t style_id) const;
|
|
|
|
std::size_t add_style(const style &style_);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
manifest &get_manifest();
|
|
|
|
const manifest &get_manifest() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 07:37:07 +08:00
|
|
|
const std::vector<relationship> &get_root_relationships() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-04-27 01:09:17 +08:00
|
|
|
void add_shared_string(const std::string &shared, bool allow_duplicates=false);
|
2015-11-11 07:58:54 +08:00
|
|
|
std::vector<std::string> &get_shared_strings();
|
|
|
|
const std::vector<std::string> &get_shared_strings() const;
|
2016-03-10 17:12:51 +08:00
|
|
|
|
|
|
|
void set_thumbnail(const std::vector<std::uint8_t> &thumbnail);
|
|
|
|
const std::vector<std::uint8_t> &get_thumbnail() const;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-01-18 14:23:31 +08:00
|
|
|
private:
|
2014-05-30 08:52:14 +08:00
|
|
|
friend class worksheet;
|
2014-07-20 04:59:05 +08:00
|
|
|
std::shared_ptr<detail::workbook_impl> d_;
|
2014-05-21 22:20:30 +08:00
|
|
|
};
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
} // namespace xlnt
|