xlnt/source/workbook/workbook.cpp

1228 lines
31 KiB
C++
Raw Normal View History

2015-12-25 06:10:02 +08:00
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
//
// 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-22 05:48:51 +08:00
#include <algorithm>
2014-06-11 05:12:15 +08:00
#include <array>
2014-06-05 06:42:17 +08:00
#include <fstream>
#include <set>
2014-05-21 22:20:30 +08:00
#include <sstream>
2015-10-14 12:03:48 +08:00
#include <xlnt/drawing/drawing.hpp>
#include <xlnt/packaging/app_properties.hpp>
2015-11-03 22:06:01 +08:00
#include <xlnt/packaging/document_properties.hpp>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/packaging/relationship.hpp>
#include <xlnt/packaging/zip_file.hpp>
2015-11-23 02:02:37 +08:00
#include <xlnt/serialization/encoding.hpp>
2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/excel_serializer.hpp>
2015-10-19 03:30:46 +08:00
#include <xlnt/styles/alignment.hpp>
2015-10-21 11:30:10 +08:00
#include <xlnt/styles/border.hpp>
2015-10-19 03:30:46 +08:00
#include <xlnt/styles/fill.hpp>
#include <xlnt/styles/font.hpp>
#include <xlnt/styles/format.hpp>
2015-10-19 03:30:46 +08:00
#include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp>
2015-10-30 07:37:07 +08:00
#include <xlnt/styles/style.hpp>
#include <xlnt/utils/exceptions.hpp>
2016-01-25 00:15:49 +08:00
#include <xlnt/workbook/const_worksheet_iterator.hpp>
2015-10-15 06:05:13 +08:00
#include <xlnt/workbook/named_range.hpp>
2015-10-30 07:37:07 +08:00
#include <xlnt/workbook/theme.hpp>
2015-10-14 12:03:48 +08:00
#include <xlnt/workbook/workbook.hpp>
2016-01-25 00:15:49 +08:00
#include <xlnt/workbook/worksheet_iterator.hpp>
2015-10-14 12:03:48 +08:00
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/worksheet.hpp>
2014-08-14 06:56:34 +08:00
#include <detail/cell_impl.hpp>
#include <detail/include_windows.hpp>
#include <detail/workbook_impl.hpp>
#include <detail/worksheet_impl.hpp>
2014-05-21 22:20:30 +08:00
namespace xlnt {
2014-05-31 06:42:25 +08:00
namespace detail {
2014-07-25 05:31:46 +08:00
workbook_impl::workbook_impl()
2016-03-06 10:39:50 +08:00
: active_sheet_index_(0), guess_types_(false), data_only_(false), read_only_(false), next_custom_format_id_(164)
2014-05-21 22:20:30 +08:00
{
}
2014-07-25 05:31:46 +08:00
2014-05-31 06:42:25 +08:00
} // namespace detail
workbook::workbook() : d_(new detail::workbook_impl())
{
create_sheet("Sheet");
2015-11-02 12:52:19 +08:00
2014-07-20 02:43:48 +08:00
create_relationship("rId2", "sharedStrings.xml", relationship::type::shared_strings);
create_relationship("rId3", "styles.xml", relationship::type::styles);
create_relationship("rId4", "theme/theme1.xml", relationship::type::theme);
2015-11-02 12:52:19 +08:00
2015-11-03 05:45:05 +08:00
add_number_format(number_format::general());
2015-11-23 02:02:37 +08:00
d_->encoding_ = encoding::utf8;
2015-11-03 05:45:05 +08:00
2015-11-02 12:52:19 +08:00
d_->manifest_.add_default_type("rels", "application/vnd.openxmlformats-package.relationships+xml");
d_->manifest_.add_default_type("xml", "application/xml");
d_->manifest_.add_override_type("/xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
d_->manifest_.add_override_type("/xl/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml");
d_->manifest_.add_override_type("/xl/styles.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
d_->manifest_.add_override_type("/xl/sharedStrings.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml");
d_->manifest_.add_override_type("/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml");
d_->manifest_.add_override_type("/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml");
2014-05-21 22:20:30 +08:00
}
2015-11-23 01:41:27 +08:00
workbook::workbook(encoding e) : workbook()
{
d_->encoding_ = e;
}
worksheet workbook::get_sheet_by_name(const std::string &name)
2014-05-21 22:20:30 +08:00
{
for (auto &impl : d_->worksheets_)
2014-05-21 22:20:30 +08:00
{
if (impl.title_ == name)
{
return worksheet(&impl);
}
2014-05-21 22:20:30 +08:00
}
return worksheet();
}
worksheet workbook::get_sheet_by_index(std::size_t index)
{
return worksheet(&d_->worksheets_[index]);
}
const worksheet workbook::get_sheet_by_index(std::size_t index) const
{
return worksheet(&d_->worksheets_.at(index));
2014-05-21 22:20:30 +08:00
}
worksheet workbook::get_active_sheet()
{
return worksheet(&d_->worksheets_[d_->active_sheet_index_]);
2014-05-21 22:20:30 +08:00
}
bool workbook::has_named_range(const std::string &name) const
2014-05-21 22:20:30 +08:00
{
for (auto worksheet : *this)
2014-05-21 22:20:30 +08:00
{
if (worksheet.has_named_range(name))
2014-05-21 22:20:30 +08:00
{
return true;
}
}
return false;
}
worksheet workbook::create_sheet()
{
2016-03-06 10:39:50 +08:00
if(get_read_only()) throw xlnt::read_only_workbook_exception();
std::string title = "Sheet";
int index = 0;
while (get_sheet_by_name(title) != nullptr)
2014-05-21 22:20:30 +08:00
{
title = "Sheet" + std::to_string(++index);
2014-05-21 22:20:30 +08:00
}
2015-11-02 12:52:19 +08:00
std::string sheet_filename = "worksheets/sheet" + std::to_string(d_->worksheets_.size() + 1) + ".xml";
2014-06-10 12:29:49 +08:00
2014-06-14 03:05:24 +08:00
d_->worksheets_.push_back(detail::worksheet_impl(this, title));
create_relationship("rId" + std::to_string(d_->relationships_.size() + 1),
2015-11-02 12:52:19 +08:00
sheet_filename,
relationship::type::worksheet);
2015-11-02 12:52:19 +08:00
d_->manifest_.add_override_type("/xl/" + sheet_filename, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml");
return worksheet(&d_->worksheets_.back());
2014-05-21 22:20:30 +08:00
}
void workbook::add_sheet(xlnt::worksheet worksheet)
{
2016-03-06 10:39:50 +08:00
if(worksheet.d_->parent_ != this) throw xlnt::value_error();
xlnt::detail::worksheet_impl impl(*worksheet.d_);
*create_sheet().d_ = impl;
2014-05-21 22:20:30 +08:00
}
void workbook::add_sheet(xlnt::worksheet worksheet, std::size_t index)
{
add_sheet(worksheet);
2014-05-31 06:42:25 +08:00
std::swap(d_->worksheets_[index], d_->worksheets_.back());
2014-05-21 22:20:30 +08:00
}
int workbook::get_index(xlnt::worksheet worksheet)
{
int i = 0;
for (auto ws : *this)
2014-05-21 22:20:30 +08:00
{
if (worksheet == ws)
2014-05-21 22:20:30 +08:00
{
return i;
}
i++;
}
throw std::runtime_error("worksheet isn't owned by this workbook");
}
2015-11-11 08:47:31 +08:00
void workbook::create_named_range(const std::string &name, worksheet range_owner, const std::string &reference_string)
{
create_named_range(name, range_owner, range_reference(reference_string));
}
void workbook::create_named_range(const std::string &name, worksheet range_owner, const range_reference &reference)
2014-05-21 22:20:30 +08:00
{
auto match = get_sheet_by_name(range_owner.get_title());
if (match != nullptr)
2014-05-21 22:20:30 +08:00
{
match.create_named_range(name, reference);
return;
2014-05-21 22:20:30 +08:00
}
throw std::runtime_error("worksheet isn't owned by this workbook");
}
void workbook::remove_named_range(const std::string &name)
2014-05-21 22:20:30 +08:00
{
for (auto ws : *this)
2014-05-21 22:20:30 +08:00
{
if (ws.has_named_range(name))
2014-05-21 22:20:30 +08:00
{
ws.remove_named_range(name);
2014-05-21 22:20:30 +08:00
return;
}
}
throw std::runtime_error("named range not found");
2014-05-21 22:20:30 +08:00
}
range workbook::get_named_range(const std::string &name)
2014-05-21 22:20:30 +08:00
{
for (auto ws : *this)
2014-05-21 22:20:30 +08:00
{
if (ws.has_named_range(name))
2014-05-21 22:20:30 +08:00
{
return ws.get_named_range(name);
2014-05-21 22:20:30 +08:00
}
}
throw std::runtime_error("named range not found");
2014-05-21 22:20:30 +08:00
}
2015-10-24 02:42:36 +08:00
bool workbook::load(std::istream &stream)
{
2015-10-30 07:37:07 +08:00
excel_serializer serializer_(*this);
serializer_.load_stream_workbook(stream);
return true;
}
2014-06-05 06:42:17 +08:00
bool workbook::load(const std::vector<unsigned char> &data)
{
2015-10-30 07:37:07 +08:00
excel_serializer serializer_(*this);
serializer_.load_virtual_workbook(data);
2015-10-24 02:42:36 +08:00
return true;
2014-06-05 06:42:17 +08:00
}
bool workbook::load(const std::string &filename)
2014-05-21 22:20:30 +08:00
{
2015-10-30 07:37:07 +08:00
excel_serializer serializer_(*this);
serializer_.load_workbook(filename);
2014-06-05 06:42:17 +08:00
return true;
2014-05-21 22:20:30 +08:00
}
2014-07-25 05:31:46 +08:00
void workbook::set_guess_types(bool guess)
{
d_->guess_types_ = guess;
}
bool workbook::get_guess_types() const
{
return d_->guess_types_;
}
void workbook::create_relationship(const std::string &id, const std::string &target, relationship::type type)
{
d_->relationships_.push_back(relationship(type, id, target));
}
relationship workbook::get_relationship(const std::string &id) const
{
for (auto &rel : d_->relationships_)
{
if (rel.get_id() == id)
{
return rel;
}
}
throw std::runtime_error("");
}
2014-05-21 22:20:30 +08:00
void workbook::remove_sheet(worksheet ws)
{
auto match_iter = std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(),
[=](detail::worksheet_impl &comp) { return worksheet(&comp) == ws; });
if (match_iter == d_->worksheets_.end())
2014-05-21 22:20:30 +08:00
{
throw std::runtime_error("worksheet not owned by this workbook");
}
auto sheet_filename = "worksheets/sheet" + std::to_string(d_->worksheets_.size()) + ".xml";
auto rel_iter = std::find_if(d_->relationships_.begin(), d_->relationships_.end(),
[=](relationship &r) { return r.get_target_uri() == sheet_filename; });
if (rel_iter == d_->relationships_.end())
2015-10-27 11:06:00 +08:00
{
throw std::runtime_error("no matching rel found");
}
2015-10-27 11:06:00 +08:00
d_->relationships_.erase(rel_iter);
d_->worksheets_.erase(match_iter);
2014-05-21 22:20:30 +08:00
}
worksheet workbook::create_sheet(std::size_t index)
{
create_sheet();
if (index != d_->worksheets_.size() - 1)
{
std::swap(d_->worksheets_.back(), d_->worksheets_[index]);
d_->worksheets_.pop_back();
}
return worksheet(&d_->worksheets_[index]);
2014-05-21 22:20:30 +08:00
}
// TODO: There should be a better way to do this...
std::size_t workbook::index_from_ws_filename(const std::string &ws_filename)
2015-10-15 06:05:13 +08:00
{
std::string sheet_index_string(ws_filename);
2015-10-15 06:05:13 +08:00
sheet_index_string = sheet_index_string.substr(0, sheet_index_string.find('.'));
sheet_index_string = sheet_index_string.substr(sheet_index_string.find_last_of('/'));
auto iter = sheet_index_string.end();
iter--;
while (isdigit(*iter))
iter--;
2015-10-15 06:05:13 +08:00
auto first_digit = static_cast<std::size_t>(iter - sheet_index_string.begin());
sheet_index_string = sheet_index_string.substr(first_digit + 1);
auto sheet_index = static_cast<std::size_t>(std::stoll(sheet_index_string) - 1);
2015-10-15 06:05:13 +08:00
return sheet_index;
}
worksheet workbook::create_sheet(const std::string &title, const relationship &rel)
{
d_->worksheets_.push_back(detail::worksheet_impl(this, title));
auto index = index_from_ws_filename(rel.get_target_uri());
if (index != d_->worksheets_.size() - 1)
{
std::swap(d_->worksheets_.back(), d_->worksheets_[index]);
d_->worksheets_.pop_back();
}
return worksheet(&d_->worksheets_[index]);
}
worksheet workbook::create_sheet(std::size_t index, const std::string &title)
2014-05-21 22:20:30 +08:00
{
auto ws = create_sheet(index);
ws.set_title(title);
2014-05-21 22:20:30 +08:00
return ws;
}
worksheet workbook::create_sheet(const std::string &title)
2014-05-21 22:20:30 +08:00
{
if (title.length() > 31)
2014-05-21 22:20:30 +08:00
{
2014-06-06 04:19:31 +08:00
throw sheet_title_exception(title);
2014-05-21 22:20:30 +08:00
}
if (std::find_if(title.begin(), title.end(), [](char c) {
return c == '*' || c == ':' || c == '/' || c == '\\' || c == '?' || c == '[' || c == ']';
}) != title.end())
2014-05-21 22:20:30 +08:00
{
2014-06-06 04:19:31 +08:00
throw sheet_title_exception(title);
2014-05-21 22:20:30 +08:00
}
std::string unique_title = title;
if (std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(), [&](detail::worksheet_impl &ws) {
return worksheet(&ws).get_title() == unique_title;
}) != d_->worksheets_.end())
2014-05-21 22:20:30 +08:00
{
2014-07-20 04:59:05 +08:00
std::size_t suffix = 1;
while (std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(), [&](detail::worksheet_impl &ws) {
return worksheet(&ws).get_title() == unique_title;
}) != d_->worksheets_.end())
2014-07-20 04:59:05 +08:00
{
unique_title = title + std::to_string(suffix);
2014-07-20 04:59:05 +08:00
suffix++;
}
2014-05-21 22:20:30 +08:00
}
auto ws = create_sheet();
2014-07-20 04:59:05 +08:00
ws.set_title(unique_title);
return ws;
2014-05-21 22:20:30 +08:00
}
2015-11-23 01:41:27 +08:00
encoding workbook::get_encoding() const
{
return d_->encoding_;
}
workbook::iterator workbook::begin()
2014-05-21 22:20:30 +08:00
{
return iterator(*this, 0);
2014-05-21 22:20:30 +08:00
}
workbook::iterator workbook::end()
2014-05-21 22:20:30 +08:00
{
return iterator(*this, d_->worksheets_.size());
}
2015-11-23 01:41:27 +08:00
workbook::const_iterator workbook::begin() const
{
return cbegin();
}
workbook::const_iterator workbook::end() const
{
return cend();
}
workbook::const_iterator workbook::cbegin() const
{
return const_iterator(*this, 0);
}
workbook::const_iterator workbook::cend() const
{
return const_iterator(*this, d_->worksheets_.size());
2014-05-21 22:20:30 +08:00
}
std::vector<std::string> workbook::get_sheet_names() const
2014-05-21 22:20:30 +08:00
{
std::vector<std::string> names;
for (auto ws : *this)
2014-05-21 22:20:30 +08:00
{
names.push_back(ws.get_title());
}
2014-05-21 22:20:30 +08:00
return names;
}
worksheet workbook::operator[](const std::string &name)
2014-05-21 22:20:30 +08:00
{
2016-03-06 10:39:50 +08:00
if(!contains(name)) throw xlnt::key_error();
return get_sheet_by_name(name);
2014-05-21 22:20:30 +08:00
}
2014-06-11 05:12:15 +08:00
worksheet workbook::operator[](std::size_t index)
2014-05-21 22:20:30 +08:00
{
return worksheet(&d_->worksheets_[index]);
2014-05-21 22:20:30 +08:00
}
void workbook::clear()
{
d_->worksheets_.clear();
2014-07-20 02:43:48 +08:00
d_->relationships_.clear();
d_->active_sheet_index_ = 0;
d_->drawings_.clear();
2014-07-20 04:59:05 +08:00
d_->properties_ = document_properties();
2014-05-21 22:20:30 +08:00
}
2014-06-05 06:42:17 +08:00
bool workbook::save(std::vector<unsigned char> &data)
{
2015-10-30 07:37:07 +08:00
excel_serializer serializer(*this);
serializer.save_virtual_workbook(data);
2014-06-11 05:12:15 +08:00
return true;
2014-06-05 06:42:17 +08:00
}
bool workbook::save(const std::string &filename)
2014-05-21 22:20:30 +08:00
{
2015-10-30 07:37:07 +08:00
excel_serializer serializer(*this);
serializer.save_workbook(filename);
2015-10-30 07:37:07 +08:00
return true;
2014-05-21 22:20:30 +08:00
}
2014-07-20 04:59:05 +08:00
bool workbook::operator==(std::nullptr_t) const
{
return d_.get() == nullptr;
}
2014-05-21 22:20:30 +08:00
bool workbook::operator==(const workbook &rhs) const
{
2014-06-14 03:05:24 +08:00
return d_.get() == rhs.d_.get();
2014-05-21 22:20:30 +08:00
}
2015-10-30 07:37:07 +08:00
const std::vector<relationship> &xlnt::workbook::get_relationships() const
{
return d_->relationships_;
}
2014-07-20 04:59:05 +08:00
document_properties &workbook::get_properties()
{
return d_->properties_;
}
const document_properties &workbook::get_properties() const
{
return d_->properties_;
}
app_properties &workbook::get_app_properties()
{
return d_->app_properties_;
}
const app_properties &workbook::get_app_properties() const
{
return d_->app_properties_;
}
2014-07-24 08:51:28 +08:00
void swap(workbook &left, workbook &right)
2014-07-20 04:59:05 +08:00
{
2014-07-24 08:51:28 +08:00
using std::swap;
swap(left.d_, right.d_);
for (auto ws : left)
2014-07-24 08:51:28 +08:00
{
ws.set_parent(left);
}
for (auto ws : right)
2014-07-24 08:51:28 +08:00
{
ws.set_parent(right);
}
}
2014-07-24 08:51:28 +08:00
workbook &workbook::operator=(workbook other)
{
swap(*this, other);
return *this;
}
workbook::workbook(workbook &&other) : workbook()
{
swap(*this, other);
}
2014-07-24 08:51:28 +08:00
workbook::workbook(const workbook &other) : workbook()
{
*d_.get() = *other.d_.get();
for (auto ws : *this)
2014-07-24 08:51:28 +08:00
{
ws.set_parent(*this);
}
2014-07-20 04:59:05 +08:00
}
2014-07-25 05:31:46 +08:00
bool workbook::get_data_only() const
{
return d_->data_only_;
}
void workbook::set_data_only(bool data_only)
{
d_->data_only_ = data_only;
}
2015-10-19 03:30:46 +08:00
2016-03-06 10:39:50 +08:00
bool workbook::get_read_only() const
{
return d_->read_only_;
}
void workbook::set_read_only(bool read_only)
{
d_->read_only_ = read_only;
}
2015-10-24 02:42:36 +08:00
void workbook::add_border(const xlnt::border &border_)
2015-10-19 03:30:46 +08:00
{
2015-10-24 02:42:36 +08:00
d_->borders_.push_back(border_);
2015-10-19 03:30:46 +08:00
}
2015-10-24 02:42:36 +08:00
void workbook::add_fill(const fill &fill_)
2015-10-19 03:30:46 +08:00
{
2015-10-24 02:42:36 +08:00
d_->fills_.push_back(fill_);
2015-10-19 03:30:46 +08:00
}
2015-10-24 02:42:36 +08:00
void workbook::add_font(const font &font_)
2015-10-19 03:30:46 +08:00
{
2015-10-24 02:42:36 +08:00
d_->fonts_.push_back(font_);
2015-10-19 03:30:46 +08:00
}
2015-10-24 02:42:36 +08:00
void workbook::add_number_format(const number_format &number_format_)
2015-10-19 03:30:46 +08:00
{
2015-11-03 05:45:05 +08:00
if (d_->number_formats_.size() == 1 && d_->number_formats_.front() == number_format::general())
{
d_->number_formats_.front() = number_format_;
}
else
{
d_->number_formats_.push_back(number_format_);
}
2015-11-02 12:52:19 +08:00
2015-11-03 05:45:05 +08:00
if(!number_format_.has_id())
2015-11-02 12:52:19 +08:00
{
d_->number_formats_.back().set_id(d_->next_custom_format_id_++);
}
2015-10-19 03:30:46 +08:00
}
2016-03-14 11:46:01 +08:00
void workbook::add_protection(const xlnt::protection &p)
{
}
void workbook::add_alignment(const xlnt::alignment &a)
{
}
void workbook::set_code_name(const std::string & /*code_name*/)
2015-10-19 03:30:46 +08:00
{
}
2015-10-30 07:37:07 +08:00
bool workbook::has_loaded_theme() const
2015-10-19 03:30:46 +08:00
{
return false;
}
2015-10-30 07:37:07 +08:00
const theme &workbook::get_loaded_theme() const
2015-10-19 03:30:46 +08:00
{
2015-10-30 07:37:07 +08:00
return d_->theme_;
2015-10-19 03:30:46 +08:00
}
std::vector<named_range> workbook::get_named_ranges() const
{
std::vector<named_range> named_ranges;
for (auto ws : *this)
{
for (auto &ws_named_range : ws.d_->named_ranges_)
2015-10-19 03:30:46 +08:00
{
named_ranges.push_back(ws_named_range.second);
}
}
2015-10-19 03:30:46 +08:00
return named_ranges;
}
std::size_t workbook::add_cell_style_format(const xlnt::format &format_)
{
d_->cell_style_formats_.push_back(format_);
d_->cell_style_formats_.back().id_ = d_->cell_style_formats_.size() - 1;
return d_->cell_style_formats_.back().id_;
}
std::size_t workbook::add_cell_format(const xlnt::format &format_)
{
d_->cell_formats_.push_back(format_);
d_->cell_formats_.back().id_ = d_->cell_formats_.size() - 1;
return d_->cell_formats_.back().id_;
}
2015-10-24 02:42:36 +08:00
std::size_t workbook::add_style(const xlnt::style &style_)
2015-10-19 03:30:46 +08:00
{
2015-10-24 02:42:36 +08:00
d_->styles_.push_back(style_);
2015-10-29 03:08:54 +08:00
d_->styles_.back().id_ = d_->styles_.size() - 1;
2015-10-29 03:08:54 +08:00
return d_->styles_.back().id_;
2015-10-19 03:30:46 +08:00
}
const std::vector<style> &workbook::get_named_styles() const
{
static std::vector<style> named_styles;
return named_styles;
}
2015-11-03 06:25:10 +08:00
color workbook::add_indexed_color(const color &rgb_color)
{
std::size_t index = 0;
for (auto &c : d_->colors_)
{
if (c.get_rgb_string() == rgb_color.get_rgb_string())
{
return color(color::type::indexed, index);
}
index++;
}
d_->colors_.push_back(rgb_color);
return color(color::type::indexed, index);
}
color workbook::get_indexed_color(const color &indexed_color) const
{
return d_->colors_.at(indexed_color.get_index());
}
const number_format &workbook::get_number_format(std::size_t format_id) const
2015-10-19 03:30:46 +08:00
{
auto number_format_id = d_->cell_formats_[format_id].number_format_id_;
for (const auto &number_format_ : d_->number_formats_)
2015-10-24 02:42:36 +08:00
{
2015-11-03 05:45:05 +08:00
if (number_format_.has_id() && number_format_.get_id() == number_format_id)
2015-10-24 02:42:36 +08:00
{
return number_format_;
}
}
2015-11-03 05:45:05 +08:00
auto nf = number_format::from_builtin_id(number_format_id);
2015-10-24 02:42:36 +08:00
d_->number_formats_.push_back(nf);
2015-10-24 02:42:36 +08:00
return d_->number_formats_.back();
2015-10-19 03:30:46 +08:00
}
2015-10-31 06:54:04 +08:00
const font &workbook::get_font(std::size_t font_id) const
2015-10-19 03:30:46 +08:00
{
2015-10-31 06:54:04 +08:00
return d_->fonts_[font_id];
2015-10-19 03:30:46 +08:00
}
2016-03-14 11:46:01 +08:00
format &workbook::add_default_cell_format()
{
format new_format;
new_format.id_ = 0;
new_format.border_id_ = 0;
new_format.fill_id_ = 0;
new_format.font_id_ = 0;
new_format.number_format_id_ = 0;
if (d_->borders_.empty())
{
d_->borders_.push_back(new_format.get_border());
}
if (d_->fills_.empty())
{
d_->fills_.push_back(new_format.get_fill());
}
if (d_->fonts_.empty())
{
d_->fonts_.push_back(new_format.get_font());
}
if (d_->number_formats_.empty())
{
d_->number_formats_.push_back(new_format.get_number_format());
}
d_->cell_formats_.push_back(new_format);
return d_->cell_formats_[0];
}
// find font in font list
// if not found, add to font list and make font_id equal to list length - 1
// otherwise, font_id is the index of the matching font in font list
// if there are no cell formats defined yet, create a new format with components set to default and font as parameter font, return 0
// if the font is identical to the font for the cell's format, just return the provided cell format id
// make a new cell format based on the existing cell format
// set font on this style to the parameter font
// see if this modified format already exists in the font list, if so return its id
// otherwise, add the new cell format to the list of formats and return its id
std::size_t workbook::set_font(const font &font_, std::size_t format_id)
2015-10-19 03:30:46 +08:00
{
2015-10-21 01:53:47 +08:00
auto match = std::find(d_->fonts_.begin(), d_->fonts_.end(), font_);
2015-11-23 01:41:27 +08:00
std::size_t font_id = 0;
if (match == d_->fonts_.end())
2015-10-21 01:53:47 +08:00
{
d_->fonts_.push_back(font_);
2015-11-23 01:41:27 +08:00
font_id = d_->fonts_.size() - 1;
2015-10-21 01:53:47 +08:00
}
else
{
2015-11-23 01:41:27 +08:00
font_id = match - d_->fonts_.begin();
2015-10-21 01:53:47 +08:00
}
if (d_->cell_formats_.empty())
2015-11-23 01:41:27 +08:00
{
2016-03-14 11:46:01 +08:00
auto &added = add_default_cell_format();
added.font_id_ = font_id;
added.font_ = font_;
added.font_apply_ = true;
2015-11-23 01:41:27 +08:00
return 0;
}
2016-03-14 11:46:01 +08:00
auto &existing_format = d_->cell_formats_[format_id];
existing_format.font_apply_ = true;
2015-11-23 01:41:27 +08:00
2016-03-14 11:46:01 +08:00
// If the style is unchanged, just return it.
if (font_id == existing_format.font_id_)
{
return format_id;
}
2016-03-14 11:46:01 +08:00
// Make a new format based on existing format.
auto new_format = existing_format;
new_format.font_id_ = font_id;
new_format.font_ = font_;
2015-11-23 01:41:27 +08:00
2016-03-14 11:46:01 +08:00
// Check if the new modified style is already applied to a different cell. If so, reuse it.
auto format_match = std::find(d_->cell_formats_.begin(), d_->cell_formats_.end(), new_format);
2015-10-21 01:53:47 +08:00
if (format_match != d_->cell_formats_.end())
2015-10-21 01:53:47 +08:00
{
return format_match->get_id();
2015-10-21 01:53:47 +08:00
}
2015-11-23 01:41:27 +08:00
// No match found, so add it.
new_format.id_ = d_->cell_formats_.size();
d_->cell_formats_.push_back(new_format);
return new_format.id_;
2015-10-19 03:30:46 +08:00
}
2016-03-14 11:46:01 +08:00
const format &workbook::get_cell_format(std::size_t format_id) const
{
return d_->cell_formats_.at(format_id);
}
2015-10-31 06:54:04 +08:00
const fill &workbook::get_fill(std::size_t fill_id) const
2015-10-19 03:30:46 +08:00
{
2015-10-31 06:54:04 +08:00
return d_->fills_[fill_id];
2015-10-19 03:30:46 +08:00
}
2016-03-14 11:46:01 +08:00
std::size_t workbook::set_fill(const fill & fill_, std::size_t format_id)
2015-10-19 03:30:46 +08:00
{
2016-03-14 11:46:01 +08:00
auto match = std::find(d_->fills_.begin(), d_->fills_.end(), fill_);
std::size_t fill_id = 0;
if (match == d_->fills_.end())
{
d_->fills_.push_back(fill_);
fill_id = d_->fills_.size() - 1;
}
else
{
fill_id = match - d_->fills_.begin();
}
if (d_->cell_formats_.empty())
{
auto &added = add_default_cell_format();
added.fill_id_ = fill_id;
added.fill_ = fill_;
added.fill_apply_ = true;
return 0;
}
auto &existing_format = d_->cell_formats_[format_id];
existing_format.fill_apply_ = true;
// If the style is unchanged, just return it.
if (fill_id == existing_format.fill_id_)
{
return format_id;
}
// Make a new format based on existing format.
auto new_format = existing_format;
new_format.fill_id_ = fill_id;
new_format.fill_ = fill_;
// Check if the new modified style is already applied to a different cell. If so, reuse it.
auto format_match = std::find(d_->cell_formats_.begin(), d_->cell_formats_.end(), new_format);
if (format_match != d_->cell_formats_.end())
{
return format_match->get_id();
}
// No match found, so add it.
new_format.id_ = d_->cell_formats_.size();
d_->cell_formats_.push_back(new_format);
return new_format.id_;
2015-10-19 03:30:46 +08:00
}
2015-10-31 06:54:04 +08:00
const border &workbook::get_border(std::size_t border_id) const
2015-10-19 03:30:46 +08:00
{
2015-10-31 06:54:04 +08:00
return d_->borders_[border_id];
2015-10-19 03:30:46 +08:00
}
2016-03-14 11:46:01 +08:00
std::size_t workbook::set_border(const border & border_, std::size_t format_id)
2015-10-19 03:30:46 +08:00
{
2016-03-14 11:46:01 +08:00
auto match = std::find(d_->borders_.begin(), d_->borders_.end(), border_);
std::size_t border_id = 0;
if (match == d_->borders_.end())
{
d_->borders_.push_back(border_);
border_id = d_->borders_.size() - 1;
}
else
{
border_id = match - d_->borders_.begin();
}
if (d_->cell_formats_.empty())
{
auto &added = add_default_cell_format();
added.border_id_ = border_id;
added.border_ = border_;
added.border_apply_ = true;
return 0;
}
auto &existing_format = d_->cell_formats_[format_id];
existing_format.border_apply_ = true;
// If the style is unchanged, just return it.
if (border_id == existing_format.border_id_)
{
return format_id;
}
// Make a new format based on existing format.
auto new_format = existing_format;
new_format.border_id_ = border_id;
new_format.border_ = border_;
// Check if the new modified style is already applied to a different cell. If so, reuse it.
auto format_match = std::find(d_->cell_formats_.begin(), d_->cell_formats_.end(), new_format);
if (format_match != d_->cell_formats_.end())
{
return format_match->get_id();
}
// No match found, so add it.
new_format.id_ = d_->cell_formats_.size();
d_->cell_formats_.push_back(new_format);
return new_format.id_;
2015-10-19 03:30:46 +08:00
}
const alignment &workbook::get_alignment(std::size_t format_id) const
2015-10-19 03:30:46 +08:00
{
return d_->cell_formats_[format_id].alignment_;
2015-10-19 03:30:46 +08:00
}
2016-03-14 11:46:01 +08:00
// differs from border, fill, font, number_format because alignment is a member of format
std::size_t workbook::set_alignment(const alignment & alignment_, std::size_t format_id)
2015-10-19 03:30:46 +08:00
{
2016-03-14 11:46:01 +08:00
if (d_->cell_formats_.empty())
{
auto &added = add_default_cell_format();
added.alignment_ = alignment_;
added.alignment_apply_ = true;
return 0;
}
auto &existing_format = d_->cell_formats_[format_id];
existing_format.alignment_apply_ = true;
// If the style is unchanged, just return it.
if (alignment_ == existing_format.alignment_)
{
return format_id;
}
// Make a new format based on existing format.
auto new_format = existing_format;
new_format.alignment_ = alignment_;
// Check if the new modified style is already applied to a different cell. If so, reuse it.
auto format_match = std::find(d_->cell_formats_.begin(), d_->cell_formats_.end(), new_format);
if (format_match != d_->cell_formats_.end())
{
return format_match->get_id();
}
// No match found, so add it.
new_format.id_ = d_->cell_formats_.size();
d_->cell_formats_.push_back(new_format);
return new_format.id_;
2015-10-19 03:30:46 +08:00
}
const protection &workbook::get_protection(std::size_t format_id) const
2015-10-19 03:30:46 +08:00
{
return d_->cell_formats_[format_id].protection_;
2015-10-19 03:30:46 +08:00
}
2016-03-14 11:46:01 +08:00
// differs from border, fill, font, number_format because protection is a member of format
std::size_t workbook::set_protection(const protection & protection_, std::size_t format_id)
2015-10-19 03:30:46 +08:00
{
2016-03-14 11:46:01 +08:00
if (d_->cell_formats_.empty())
{
auto &added = add_default_cell_format();
added.protection_ = protection_;
added.protection_apply_ = true;
return 0;
}
auto &existing_format = d_->cell_formats_[format_id];
existing_format.protection_apply_ = true;
// If the style is unchanged, just return it.
if (protection_ == existing_format.protection_)
{
return format_id;
}
// Make a new format based on existing format.
auto new_format = existing_format;
new_format.protection_ = protection_;
// Check if the new modified style is already applied to a different cell. If so, reuse it.
auto format_match = std::find(d_->cell_formats_.begin(), d_->cell_formats_.end(), new_format);
if (format_match != d_->cell_formats_.end())
{
return format_match->get_id();
}
// No match found, so add it.
new_format.id_ = d_->cell_formats_.size();
d_->cell_formats_.push_back(new_format);
return new_format.id_;
2015-10-19 03:30:46 +08:00
}
bool workbook::get_pivot_button(std::size_t format_id) const
2015-10-19 03:30:46 +08:00
{
return d_->cell_formats_[format_id].pivot_button_;
2015-10-19 03:30:46 +08:00
}
bool workbook::get_quote_prefix(std::size_t format_id) const
2015-10-19 03:30:46 +08:00
{
return d_->cell_formats_[format_id].quote_prefix_;
2015-10-19 03:30:46 +08:00
}
2015-11-23 01:41:27 +08:00
//TODO: this is terrible!
2016-03-14 11:46:01 +08:00
std::size_t workbook::set_number_format(const xlnt::number_format &number_format_, std::size_t format_id)
2015-10-19 03:30:46 +08:00
{
2016-03-14 11:46:01 +08:00
auto match = std::find(d_->number_formats_.begin(), d_->number_formats_.end(), number_format_);
std::size_t number_format_id = 0;
if (match == d_->number_formats_.end())
2015-10-15 06:05:13 +08:00
{
2016-03-14 11:46:01 +08:00
d_->number_formats_.push_back(number_format_);
2016-03-14 11:46:01 +08:00
if (!number_format_.has_id())
2015-10-24 02:42:36 +08:00
{
d_->number_formats_.back().set_id(d_->next_custom_format_id_++);
}
2016-03-14 11:46:01 +08:00
number_format_id = d_->number_formats_.back().get_id();
2015-10-15 06:05:13 +08:00
}
2015-10-21 01:53:47 +08:00
else
2015-10-19 12:03:52 +08:00
{
2016-03-14 11:46:01 +08:00
number_format_id = match->get_id();
2015-10-19 12:03:52 +08:00
}
if (d_->cell_formats_.empty())
2015-10-19 12:03:52 +08:00
{
2016-03-14 11:46:01 +08:00
auto &added = add_default_cell_format();
added.number_format_id_ = number_format_id;
added.number_format_ = number_format_;
added.number_format_apply_ = true;
2015-10-21 01:53:47 +08:00
return 0;
2015-10-19 12:03:52 +08:00
}
2016-03-14 11:46:01 +08:00
auto &existing_format = d_->cell_formats_[format_id];
existing_format.number_format_apply_ = true;
2016-03-14 11:46:01 +08:00
// If the style is unchanged, just return it.
if (number_format_id == existing_format.number_format_id_)
2015-10-19 12:03:52 +08:00
{
2016-03-14 11:46:01 +08:00
return format_id;
2015-10-19 12:03:52 +08:00
}
2016-03-14 11:46:01 +08:00
// Make a new format based on existing format.
auto new_format = existing_format;
2016-03-14 11:46:01 +08:00
new_format.number_format_id_ = number_format_id;
new_format.number_format_ = number_format_;
2016-03-14 11:46:01 +08:00
// Check if the new modified style is already applied to a different cell. If so, reuse it.
auto format_match = std::find(d_->cell_formats_.begin(), d_->cell_formats_.end(), new_format);
if (format_match != d_->cell_formats_.end())
2015-10-19 12:03:52 +08:00
{
return format_match->get_id();
2015-10-19 12:03:52 +08:00
}
2015-10-24 02:42:36 +08:00
// No match found, so add it.
new_format.id_ = d_->cell_formats_.size();
d_->cell_formats_.push_back(new_format);
return new_format.id_;
}
const std::vector<format> &workbook::get_cell_formats() const
{
return d_->cell_formats_;
}
const std::vector<format> &workbook::get_cell_style_formats() const
{
return d_->cell_style_formats_;
2015-10-21 01:53:47 +08:00
}
2015-11-03 06:25:10 +08:00
const std::vector<style> &workbook::get_styles() const
2015-10-21 01:53:47 +08:00
{
return d_->styles_;
2015-10-19 12:03:52 +08:00
}
2015-11-03 06:25:10 +08:00
const std::vector<number_format> &workbook::get_number_formats() const
2015-10-19 12:03:52 +08:00
{
2015-10-21 01:53:47 +08:00
return d_->number_formats_;
2015-10-19 12:03:52 +08:00
}
2015-10-21 01:53:47 +08:00
2015-11-03 06:25:10 +08:00
const std::vector<font> &workbook::get_fonts() const
2015-10-19 12:03:52 +08:00
{
2015-10-21 01:53:47 +08:00
return d_->fonts_;
2015-10-19 12:03:52 +08:00
}
2015-10-24 02:42:36 +08:00
2015-11-03 06:25:10 +08:00
const std::vector<border> &workbook::get_borders() const
2015-10-24 02:42:36 +08:00
{
return d_->borders_;
}
2015-11-03 06:25:10 +08:00
const std::vector<fill> &workbook::get_fills() const
2015-10-24 02:42:36 +08:00
{
return d_->fills_;
}
2015-10-29 03:08:54 +08:00
void workbook::add_color(const color &rgb)
{
d_->colors_.push_back(rgb);
}
2015-11-03 06:25:10 +08:00
const std::vector<color> &workbook::get_colors() const
2015-10-29 03:08:54 +08:00
{
return d_->colors_;
}
2015-10-30 01:46:56 +08:00
manifest &workbook::get_manifest()
{
return d_->manifest_;
}
2015-10-30 01:46:56 +08:00
const manifest &workbook::get_manifest() const
{
return d_->manifest_;
}
2015-10-30 07:37:07 +08:00
const std::vector<relationship> &workbook::get_root_relationships() const
{
if (d_->root_relationships_.empty())
2015-10-30 07:37:07 +08:00
{
d_->root_relationships_.push_back(
relationship(relationship::type::core_properties, "rId1", "docProps/core.xml"));
d_->root_relationships_.push_back(
relationship(relationship::type::extended_properties, "rId2", "docProps/app.xml"));
2015-10-31 06:54:04 +08:00
d_->root_relationships_.push_back(relationship(relationship::type::office_document, "rId3", "xl/workbook.xml"));
2015-10-30 07:37:07 +08:00
}
2015-10-30 07:37:07 +08:00
return d_->root_relationships_;
}
std::vector<std::string> &workbook::get_shared_strings()
2015-10-31 06:54:04 +08:00
{
return d_->shared_strings_;
}
const std::vector<std::string> &workbook::get_shared_strings() const
2015-10-31 06:54:04 +08:00
{
return d_->shared_strings_;
}
void workbook::add_shared_string(const std::string &shared, bool allow_duplicates)
2015-10-31 06:54:04 +08:00
{
if (!allow_duplicates)
{
//TODO: inefficient, use a set or something?
for (auto &s : d_->shared_strings_)
{
if (s == shared) return;
}
}
2015-11-02 12:52:19 +08:00
2015-10-31 06:54:04 +08:00
d_->shared_strings_.push_back(shared);
}
2016-03-06 10:39:50 +08:00
bool workbook::contains(const std::string &sheet_title) const
{
for(auto ws : *this)
{
if(ws.get_title() == sheet_title) return true;
}
return false;
}
void workbook::set_thumbnail(const std::vector<std::uint8_t> &thumbnail)
{
d_->thumbnail_.assign(thumbnail.begin(), thumbnail.end());
}
const std::vector<std::uint8_t> &workbook::get_thumbnail() const
{
return d_->thumbnail_;
}
2015-10-19 03:30:46 +08:00
} // namespace xlnt