xlnt/source/workbook/workbook.cpp

833 lines
20 KiB
C++
Raw Normal View History

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>
2014-06-11 06:36:31 +08:00
#ifdef _WIN32
2014-06-11 05:12:15 +08:00
#include <Windows.h>
2014-06-11 06:36:31 +08:00
#endif
2014-06-11 05:12:15 +08:00
2014-08-14 06:56:34 +08:00
#include <xlnt/common/exceptions.hpp>
#include <xlnt/common/relationship.hpp>
#include <xlnt/common/zip_file.hpp>
2015-10-14 12:03:48 +08:00
#include <xlnt/drawing/drawing.hpp>
2015-10-24 02:42:36 +08:00
#include <xlnt/reader/excel_reader.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/number_format.hpp>
#include <xlnt/styles/protection.hpp>
2014-08-14 06:56:34 +08:00
#include <xlnt/workbook/document_properties.hpp>
2015-10-15 06:05:13 +08:00
#include <xlnt/workbook/named_range.hpp>
2015-10-14 12:03:48 +08:00
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/worksheet.hpp>
2015-10-24 02:42:36 +08:00
#include <xlnt/writer/excel_writer.hpp>
2014-08-14 06:56:34 +08:00
2014-06-06 04:19:31 +08:00
#include "detail/cell_impl.hpp"
#include "detail/include_pugixml.hpp"
2014-06-06 04:19:31 +08:00
#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
2015-10-24 02:42:36 +08:00
workbook_impl::workbook_impl() : active_sheet_index_(0), guess_types_(false), data_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");
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);
2014-05-21 22:20:30 +08:00
}
workbook::iterator::iterator(workbook &wb, std::size_t index) : wb_(wb), index_(index)
{
}
2014-06-11 05:12:15 +08:00
workbook::iterator::iterator(const iterator &rhs) : wb_(rhs.wb_), index_(rhs.index_)
{
}
worksheet workbook::iterator::operator*()
{
return wb_[index_];
}
workbook::iterator &workbook::iterator::operator++()
{
index_++;
return *this;
}
workbook::iterator workbook::iterator::operator++(int)
{
iterator old(wb_, index_);
++*this;
return old;
}
bool workbook::iterator::operator==(const iterator &comparand) const
{
return index_ == comparand.index_ && wb_ == comparand.wb_;
}
workbook::const_iterator::const_iterator(const workbook &wb, std::size_t index) : wb_(wb), index_(index)
{
}
2014-06-11 05:12:15 +08:00
workbook::const_iterator::const_iterator(const const_iterator &rhs) : wb_(rhs.wb_), index_(rhs.index_)
{
}
const worksheet workbook::const_iterator::operator*()
{
return wb_.get_sheet_by_index(index_);
}
workbook::const_iterator &workbook::const_iterator::operator++()
{
index_++;
return *this;
}
workbook::const_iterator workbook::const_iterator::operator++(int)
{
const_iterator old(wb_, index_);
++*this;
return old;
}
bool workbook::const_iterator::operator==(const const_iterator &comparand) const
{
return index_ == comparand.index_ && wb_ == comparand.wb_;
}
2014-05-21 22:20:30 +08:00
worksheet workbook::get_sheet_by_name(const std::string &name)
{
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()
{
2014-05-21 22:20:30 +08:00
std::string title = "Sheet1";
int index = 1;
2014-05-21 22:20:30 +08:00
while(get_sheet_by_name(title) != nullptr)
{
title = "Sheet" + std::to_string(++index);
}
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), "xl/worksheets/sheet" + std::to_string(d_->worksheets_.size()) + ".xml", relationship::type::worksheet);
return worksheet(&d_->worksheets_.back());
2014-05-21 22:20:30 +08:00
}
void workbook::add_sheet(xlnt::worksheet worksheet)
{
for(auto ws : *this)
{
if(worksheet == ws)
{
throw std::runtime_error("worksheet already in workbook");
}
}
d_->worksheets_.emplace_back(*worksheet.d_);
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)
{
if(worksheet == ws)
{
return i;
}
i++;
}
throw std::runtime_error("worksheet isn't owned by this workbook");
}
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-24 02:42:36 +08:00
*this = excel_reader::load_workbook(stream);
return true;
}
2014-06-05 06:42:17 +08:00
bool workbook::load(const std::vector<unsigned char> &data)
{
2015-10-24 02:42:36 +08:00
*this = excel_reader::load_workbook(data);
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-24 02:42:36 +08:00
*this = excel_reader::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_;
}
2014-07-20 02:43:48 +08:00
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)
{
2014-05-31 06:42:25 +08:00
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");
}
2015-10-27 11:06:00 +08:00
auto sheet_filename = "xl/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())
{
throw std::runtime_error("no matching rel found");
}
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
}
2015-10-15 06:05:13 +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)
{
std::string sheet_index_string(ws_filename);
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--;
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);
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]);
}
2014-05-21 22:20:30 +08:00
worksheet workbook::create_sheet(std::size_t index, const std::string &title)
{
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)
{
if(title.length() > 31)
{
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-06-06 04:19:31 +08:00
throw sheet_title_exception(title);
2014-05-21 22:20:30 +08:00
}
2014-07-20 04:59:05 +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())
{
unique_title = title + std::to_string(suffix);
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
}
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());
}
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
{
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)
{
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-15 06:05:13 +08:00
data = save_virtual_workbook(*this);
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-15 06:05:13 +08:00
return save_workbook(*this, filename);
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
}
std::vector<relationship> xlnt::workbook::get_relationships() const
{
return d_->relationships_;
2014-05-22 07:17:56 +08:00
}
std::vector<content_type> xlnt::workbook::get_content_types() const
{
std::vector<content_type> content_types;
2014-06-19 03:12:44 +08:00
content_types.push_back({ true, "xml", "", "application/xml" });
content_types.push_back({ true, "rels", "", "application/vnd.openxmlformats-package.relationships+xml" });
2014-07-20 02:43:48 +08:00
content_types.push_back({ false, "", "/xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" });
for(std::size_t i = 0; i < get_sheet_names().size(); i++)
{
content_types.push_back({false, "", "/xl/worksheets/sheet" + std::to_string(i + 1) + ".xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"});
}
content_types.push_back({false, "", "/xl/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml"});
content_types.push_back({false, "", "/xl/styles.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"});
content_types.push_back({false, "", "/xl/sharedStrings.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"});
content_types.push_back({false, "", "/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml"});
content_types.push_back({false, "", "/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml"});
return content_types;
}
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_;
}
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)
{
ws.set_parent(left);
}
for(auto ws : right)
{
ws.set_parent(right);
}
}
2014-07-20 04:59:05 +08:00
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);
}
workbook::workbook(const workbook &other) : workbook()
{
*d_.get() = *other.d_.get();
for(auto ws : *this)
{
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
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-10-24 02:42:36 +08:00
d_->number_formats_.push_back(number_format_);
2015-10-19 03:30:46 +08:00
}
void workbook::set_code_name(const std::string &/*code_name*/)
{
2015-10-19 03:30:46 +08:00
}
bool workbook::has_loaded_theme()
{
return false;
}
std::string workbook::get_loaded_theme()
{
return "";
}
std::vector<named_range> workbook::get_named_ranges() const
{
std::vector<named_range> named_ranges;
for(auto ws : *this)
{
2015-10-19 03:30:46 +08:00
for(auto &ws_named_range : ws.d_->named_ranges_)
{
named_ranges.push_back(ws_named_range.second);
}
}
2015-10-19 03:30:46 +08:00
return named_ranges;
}
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
for(std::size_t i = 0; i < d_->styles_.size(); i++)
{
if(d_->styles_[i] == style_)
{
return i;
}
}
d_->styles_.push_back(style_);
return d_->styles_.size() - 1;
2015-10-19 03:30:46 +08:00
}
const number_format &workbook::get_number_format(std::size_t style_id) const
{
2015-10-24 02:42:36 +08:00
auto number_format_id = d_->styles_[style_id].number_format_id_;
for(const auto &number_format_ : d_->number_formats_)
{
if(number_format_.get_id() == number_format_id)
{
return number_format_;
}
}
auto nf = number_format::from_builtin_id(static_cast<int>(number_format_id));
d_->number_formats_.push_back(nf);
return d_->number_formats_.back();
2015-10-19 03:30:46 +08:00
}
const font &workbook::get_font(std::size_t style_id) const
{
2015-10-24 02:42:36 +08:00
return d_->fonts_[d_->styles_[style_id].font_id_];
2015-10-19 03:30:46 +08:00
}
std::size_t workbook::set_font(const font &font_, std::size_t style_id)
{
2015-10-21 01:53:47 +08:00
auto match = std::find(d_->fonts_.begin(), d_->fonts_.end(), font_);
auto font_index = 0;
if(match == d_->fonts_.end())
{
d_->fonts_.push_back(font_);
font_index = d_->fonts_.size() - 1;
}
else
{
font_index = match - d_->fonts_.begin();
}
auto existing_style = d_->styles_[style_id];
2015-10-19 03:30:46 +08:00
2015-10-24 02:42:36 +08:00
if(font_index == existing_style.font_id_)
{
2015-10-21 01:53:47 +08:00
// no change
return style_id;
}
2015-10-21 01:53:47 +08:00
auto new_style = existing_style;
2015-10-24 02:42:36 +08:00
new_style.font_id_ = font_index;
2015-10-21 01:53:47 +08:00
auto style_match = std::find(d_->styles_.begin(), d_->styles_.end(), new_style);
if(style_match != d_->styles_.end())
{
return style_match - d_->styles_.begin();
}
2015-10-19 03:30:46 +08:00
2015-10-21 01:53:47 +08:00
d_->styles_.push_back(new_style);
2015-10-19 03:30:46 +08:00
2015-10-21 01:53:47 +08:00
return d_->styles_.size() - 1;
2015-10-19 03:30:46 +08:00
}
const fill &workbook::get_fill(std::size_t style_id) const
{
2015-10-24 02:42:36 +08:00
return d_->fills_[d_->styles_[style_id].fill_id_];
2015-10-19 03:30:46 +08:00
}
std::size_t workbook::set_fill(const fill &fill_, std::size_t style_id)
{
2015-10-21 01:53:47 +08:00
return style_id;
2015-10-19 03:30:46 +08:00
}
const border &workbook::get_border(std::size_t style_id) const
{
2015-10-24 02:42:36 +08:00
return d_->borders_[d_->styles_[style_id].border_id_];
2015-10-19 03:30:46 +08:00
}
std::size_t workbook::set_border(const border &border_, std::size_t style_id)
{
2015-10-21 01:53:47 +08:00
return style_id;
2015-10-19 03:30:46 +08:00
}
const alignment &workbook::get_alignment(std::size_t style_id) const
{
2015-10-24 02:42:36 +08:00
return d_->styles_[style_id].alignment_;
2015-10-19 03:30:46 +08:00
}
std::size_t workbook::set_alignment(const alignment &alignment_, std::size_t style_id)
{
2015-10-21 01:53:47 +08:00
return style_id;
2015-10-19 03:30:46 +08:00
}
const protection &workbook::get_protection(std::size_t style_id) const
{
2015-10-24 02:42:36 +08:00
return d_->styles_[style_id].protection_;
2015-10-19 03:30:46 +08:00
}
std::size_t workbook::set_protection(const protection &protection_, std::size_t style_id)
{
2015-10-21 01:53:47 +08:00
return style_id;
2015-10-19 03:30:46 +08:00
}
bool workbook::get_pivot_button(std::size_t style_id) const
{
2015-10-21 01:53:47 +08:00
return d_->styles_[style_id].pivot_button_;
2015-10-19 03:30:46 +08:00
}
bool workbook::get_quote_prefix(std::size_t style_id) const
{
2015-10-21 01:53:47 +08:00
return d_->styles_[style_id].quote_prefix_;
2015-10-19 03:30:46 +08:00
}
std::size_t workbook::set_number_format(const xlnt::number_format &format, std::size_t style_id)
{
2015-10-21 01:53:47 +08:00
auto match = std::find(d_->number_formats_.begin(), d_->number_formats_.end(), format);
2015-10-24 02:42:36 +08:00
std::size_t format_id = 0;
2015-10-19 03:30:46 +08:00
2015-10-21 01:53:47 +08:00
if(match == d_->number_formats_.end())
2015-10-15 06:05:13 +08:00
{
2015-10-21 01:53:47 +08:00
d_->number_formats_.push_back(format);
2015-10-24 02:42:36 +08:00
if(format.get_id() == -1)
{
d_->number_formats_.back().set_id(d_->next_custom_format_id_++);
}
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
{
2015-10-24 02:42:36 +08:00
format_id = match->get_id();
2015-10-19 12:03:52 +08:00
}
2015-10-21 01:53:47 +08:00
if(d_->styles_.empty())
2015-10-19 12:03:52 +08:00
{
2015-10-21 01:53:47 +08:00
style new_style;
2015-10-24 02:42:36 +08:00
new_style.id_ = 0;
new_style.border_id_ = 0;
new_style.fill_id_ = 0;
new_style.font_id_ = 0;
new_style.number_format_id_ = format_id;
new_style.number_format_apply_ = true;
if(d_->borders_.empty())
{
d_->borders_.push_back(new_style.get_border());
}
if(d_->fills_.empty())
{
d_->fills_.push_back(new_style.get_fill());
}
if(d_->fonts_.empty())
{
d_->fonts_.push_back(new_style.get_font());
}
2015-10-21 01:53:47 +08:00
d_->styles_.push_back(new_style);
2015-10-24 02:42:36 +08:00
2015-10-21 01:53:47 +08:00
return 0;
2015-10-19 12:03:52 +08:00
}
2015-10-24 02:42:36 +08:00
// If the style is unchanged, just return it.
2015-10-21 01:53:47 +08:00
auto existing_style = d_->styles_[style_id];
2015-10-24 02:42:36 +08:00
existing_style.number_format_apply_ = true;
2015-10-19 12:03:52 +08:00
2015-10-24 02:42:36 +08:00
if(format_id == existing_style.number_format_id_)
2015-10-19 12:03:52 +08:00
{
2015-10-21 01:53:47 +08:00
// no change
return style_id;
2015-10-19 12:03:52 +08:00
}
2015-10-24 02:42:36 +08:00
// Make a new style with this format.
2015-10-21 01:53:47 +08:00
auto new_style = existing_style;
2015-10-24 02:42:36 +08:00
new_style.number_format_id_ = format_id;
2015-10-21 01:53:47 +08:00
new_style.number_format_ = format;
2015-10-24 02:42:36 +08:00
// Check if the new style is already applied to a different cell. If so, reuse it.
2015-10-21 01:53:47 +08:00
auto style_match = std::find(d_->styles_.begin(), d_->styles_.end(), new_style);
2015-10-19 12:03:52 +08:00
2015-10-21 01:53:47 +08:00
if(style_match != d_->styles_.end())
2015-10-19 12:03:52 +08:00
{
2015-10-24 02:42:36 +08:00
return style_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_style.id_ = d_->styles_.size();
2015-10-21 01:53:47 +08:00
d_->styles_.push_back(new_style);
2015-10-19 12:03:52 +08:00
2015-10-24 02:42:36 +08:00
return new_style.id_;
2015-10-21 01:53:47 +08:00
}
2015-10-19 12:03:52 +08:00
2015-10-21 01:53:47 +08:00
std::vector<style> workbook::get_styles() const
{
return d_->styles_;
2015-10-19 12:03:52 +08:00
}
2015-10-21 01:53:47 +08:00
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
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
std::vector<border> workbook::get_borders() const
{
return d_->borders_;
}
std::vector<fill> workbook::get_fills() const
{
return d_->fills_;
}
2015-10-19 12:03:52 +08:00
2015-10-19 03:30:46 +08:00
} // namespace xlnt