2015-12-25 06:10:02 +08:00
|
|
|
// Copyright (c) 2014-2016 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
// 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
|
2016-06-11 01:40:50 +08:00
|
|
|
|
2016-06-11 22:03:58 +08:00
|
|
|
#include <algorithm>
|
2016-03-10 17:12:51 +08:00
|
|
|
#include <cctype> // for std::tolower
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <pugixml.hpp>
|
2016-05-01 04:19:45 +08:00
|
|
|
#include <unordered_set>
|
2016-06-11 01:40:50 +08:00
|
|
|
#include <utility> // for std::move
|
2016-03-09 13:57:37 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <detail/style_serializer.hpp>
|
2016-06-11 01:40:50 +08:00
|
|
|
#include <detail/stylesheet.hpp>
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <detail/workbook_impl.hpp>
|
2016-05-01 04:19:45 +08:00
|
|
|
#include <xlnt/cell/cell.hpp>
|
2015-10-30 01:46:56 +08:00
|
|
|
#include <xlnt/styles/alignment.hpp>
|
|
|
|
#include <xlnt/styles/border.hpp>
|
|
|
|
#include <xlnt/styles/fill.hpp>
|
|
|
|
#include <xlnt/styles/font.hpp>
|
2016-05-15 01:57:07 +08:00
|
|
|
#include <xlnt/styles/format.hpp>
|
|
|
|
#include <xlnt/styles/style.hpp>
|
2015-10-30 01:46:56 +08:00
|
|
|
#include <xlnt/styles/number_format.hpp>
|
|
|
|
#include <xlnt/styles/protection.hpp>
|
2016-05-01 04:19:45 +08:00
|
|
|
#include <xlnt/workbook/worksheet_iterator.hpp>
|
|
|
|
#include <xlnt/worksheet/cell_iterator.hpp>
|
|
|
|
#include <xlnt/worksheet/cell_vector.hpp>
|
2016-05-14 02:40:17 +08:00
|
|
|
#include <xlnt/worksheet/range.hpp>
|
2016-05-01 04:19:45 +08:00
|
|
|
#include <xlnt/worksheet/worksheet.hpp>
|
|
|
|
#include <xlnt/worksheet/range_iterator.hpp>
|
2015-10-30 01:46:56 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
// Miscellaneous Functions
|
|
|
|
|
2016-06-13 08:15:00 +08:00
|
|
|
struct EnumClassHash
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
std::size_t operator()(T t) const
|
|
|
|
{
|
|
|
|
return static_cast<std::size_t>(t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
std::string string_lower(std::string str)
|
2016-03-09 11:32:32 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
for (std::size_t i = 0; i < str.size(); i++)
|
2016-03-09 11:32:32 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
str[i] = std::tolower(str[i]);
|
2016-03-09 11:32:32 +08:00
|
|
|
}
|
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
return str;
|
2016-03-09 11:32:32 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
bool is_true(const std::string &bool_string)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-05-14 02:40:17 +08:00
|
|
|
return bool_string == "1" || bool_string == "true";
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
bool is_false(const std::string &bool_string)
|
|
|
|
{
|
|
|
|
return bool_string == "0" || bool_string == "false";
|
|
|
|
}
|
|
|
|
|
2016-06-12 01:07:07 +08:00
|
|
|
std::size_t string_to_size_t(const std::string &s)
|
|
|
|
{
|
|
|
|
#if ULLONG_MAX == SIZE_MAX
|
|
|
|
return std::stoull(s);
|
|
|
|
#else
|
|
|
|
return std::stoul(s);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
//
|
|
|
|
// enum serialization
|
|
|
|
//
|
|
|
|
|
|
|
|
// font::underline_style serialization
|
|
|
|
|
|
|
|
const std::unordered_map<std::string, xlnt::font::underline_style> &get_string_underline_style_map()
|
|
|
|
{
|
|
|
|
static std::unordered_map<std::string, xlnt::font::underline_style> *map = nullptr;
|
|
|
|
|
|
|
|
if (map == nullptr)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
map = new std::unordered_map<std::string, xlnt::font::underline_style>
|
|
|
|
{
|
|
|
|
{ "double", xlnt::font::underline_style::double_ },
|
|
|
|
{ "double-accounting", xlnt::font::underline_style::double_accounting },
|
|
|
|
{ "none", xlnt::font::underline_style::none },
|
|
|
|
{ "single", xlnt::font::underline_style::single },
|
|
|
|
{ "single-accounting", xlnt::font::underline_style::single_accounting }
|
|
|
|
};
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2016-06-13 08:15:00 +08:00
|
|
|
const std::unordered_map<xlnt::font::underline_style, std::string, EnumClassHash> &get_underline_style_string_map()
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
static std::unordered_map<xlnt::font::underline_style, std::string, EnumClassHash> *map = nullptr;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
if (map == nullptr)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
map = new std::unordered_map<xlnt::font::underline_style, std::string, EnumClassHash>;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
for (auto pair : get_string_underline_style_map())
|
|
|
|
{
|
|
|
|
map->emplace(pair.second, pair.first);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
xlnt::font::underline_style underline_style_from_string(const std::string &underline_string)
|
|
|
|
{
|
|
|
|
return get_string_underline_style_map().at(string_lower(underline_string));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
std::string underline_style_to_string(xlnt::font::underline_style underline_style)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
return get_underline_style_string_map().at(underline_style);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// fill::pattern_type serialization
|
|
|
|
|
|
|
|
const std::unordered_map<std::string, xlnt::fill::pattern_type> &get_string_pattern_fill_type_map()
|
|
|
|
{
|
|
|
|
static std::unordered_map<std::string, xlnt::fill::pattern_type> *map = nullptr;
|
|
|
|
|
|
|
|
if (map == nullptr)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
map = new std::unordered_map<std::string, xlnt::fill::pattern_type>
|
|
|
|
{
|
|
|
|
{ "darkdown", xlnt::fill::pattern_type::darkdown },
|
|
|
|
{ "darkgray", xlnt::fill::pattern_type::darkgray },
|
|
|
|
{ "darkgrid", xlnt::fill::pattern_type::darkgrid },
|
|
|
|
{ "darkhorizontal", xlnt::fill::pattern_type::darkhorizontal },
|
|
|
|
{ "darktrellis", xlnt::fill::pattern_type::darktrellis },
|
|
|
|
{ "darkup", xlnt::fill::pattern_type::darkup },
|
|
|
|
{ "darkvertical", xlnt::fill::pattern_type::darkvertical },
|
|
|
|
{ "gray0625", xlnt::fill::pattern_type::gray0625 },
|
|
|
|
{ "gray125", xlnt::fill::pattern_type::gray125 },
|
|
|
|
{ "lightdown", xlnt::fill::pattern_type::lightdown },
|
|
|
|
{ "lightgray", xlnt::fill::pattern_type::lightgray },
|
|
|
|
{ "lightgrid", xlnt::fill::pattern_type::lightgrid },
|
|
|
|
{ "lighthorizontal", xlnt::fill::pattern_type::lighthorizontal },
|
|
|
|
{ "lighttrellis", xlnt::fill::pattern_type::lighttrellis },
|
|
|
|
{ "lightup", xlnt::fill::pattern_type::lightup },
|
|
|
|
{ "lightvertical", xlnt::fill::pattern_type::lightvertical },
|
|
|
|
{ "mediumgray", xlnt::fill::pattern_type::mediumgray },
|
|
|
|
{ "none", xlnt::fill::pattern_type::none },
|
|
|
|
{ "solid", xlnt::fill::pattern_type::solid }
|
|
|
|
};
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2016-06-13 08:15:00 +08:00
|
|
|
const std::unordered_map<xlnt::fill::pattern_type, std::string, EnumClassHash> &get_pattern_fill_type_string_map()
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
static std::unordered_map<xlnt::fill::pattern_type, std::string, EnumClassHash> *map = nullptr;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
if (map == nullptr)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
map = new std::unordered_map<xlnt::fill::pattern_type, std::string, EnumClassHash>;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
for (auto pair : get_string_pattern_fill_type_map())
|
|
|
|
{
|
|
|
|
map->emplace(pair.second, pair.first);
|
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
xlnt::fill::pattern_type pattern_fill_type_from_string(const std::string &fill_type)
|
|
|
|
{
|
|
|
|
return get_string_pattern_fill_type_map().at(string_lower(fill_type));
|
2015-10-30 01:46:56 +08:00
|
|
|
};
|
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
std::string pattern_fill_type_to_string(xlnt::fill::pattern_type fill_type)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
return get_pattern_fill_type_string_map().at(fill_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// border_style serialization
|
|
|
|
|
|
|
|
const std::unordered_map<std::string, xlnt::border_style> &get_string_border_style_map()
|
|
|
|
{
|
|
|
|
static std::unordered_map<std::string, xlnt::border_style> *map = nullptr;
|
|
|
|
|
|
|
|
if (map == nullptr)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
map = new std::unordered_map<std::string, xlnt::border_style>
|
|
|
|
{
|
|
|
|
{ "dashdot", xlnt::border_style::dashdot },
|
|
|
|
{ "dashdotdot", xlnt::border_style::dashdotdot },
|
|
|
|
{ "dashed", xlnt::border_style::dashed },
|
|
|
|
{ "dotted", xlnt::border_style::dotted },
|
|
|
|
{ "double", xlnt::border_style::double_ },
|
|
|
|
{ "hair", xlnt::border_style::hair },
|
|
|
|
{ "medium", xlnt::border_style::medium },
|
|
|
|
{ "mediumdashdot", xlnt::border_style::mediumdashdot },
|
|
|
|
{ "mediumdashdotdot", xlnt::border_style::mediumdashdotdot },
|
|
|
|
{ "mediumdashed", xlnt::border_style::mediumdashed },
|
|
|
|
{ "none", xlnt::border_style::none },
|
|
|
|
{ "slantdashdot", xlnt::border_style::slantdashdot },
|
|
|
|
{ "thick", xlnt::border_style::thick },
|
|
|
|
{ "thin", xlnt::border_style::thin }
|
|
|
|
};
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2016-06-13 08:15:00 +08:00
|
|
|
const std::unordered_map<xlnt::border_style, std::string, EnumClassHash> &get_border_style_string_map()
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
static std::unordered_map<xlnt::border_style, std::string, EnumClassHash> *map = nullptr;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
if (map == nullptr)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
map = new std::unordered_map<xlnt::border_style, std::string, EnumClassHash>;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
for (auto pair : get_string_border_style_map())
|
|
|
|
{
|
|
|
|
map->emplace(pair.second, pair.first);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
xlnt::border_style border_style_from_string(const std::string &border_style_string)
|
|
|
|
{
|
|
|
|
return get_string_border_style_map().at(string_lower(border_style_string));
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
std::string border_style_to_string(xlnt::border_style border_style)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
return get_border_style_string_map().at(border_style);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// vertical_alignment serialization
|
|
|
|
|
|
|
|
const std::unordered_map<std::string, xlnt::vertical_alignment> &get_string_vertical_alignment_map()
|
|
|
|
{
|
|
|
|
static std::unordered_map<std::string, xlnt::vertical_alignment> *map = nullptr;
|
|
|
|
|
|
|
|
if (map == nullptr)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
map = new std::unordered_map<std::string, xlnt::vertical_alignment>
|
|
|
|
{
|
|
|
|
{ "bottom", xlnt::vertical_alignment::bottom },
|
|
|
|
{ "center", xlnt::vertical_alignment::center },
|
|
|
|
{ "justify", xlnt::vertical_alignment::justify },
|
|
|
|
{ "none", xlnt::vertical_alignment::none },
|
|
|
|
{ "top", xlnt::vertical_alignment::top }
|
|
|
|
};
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2016-06-13 08:15:00 +08:00
|
|
|
const std::unordered_map<xlnt::vertical_alignment, std::string, EnumClassHash> &get_vertical_alignment_string_map()
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
static std::unordered_map<xlnt::vertical_alignment, std::string, EnumClassHash> *map = nullptr;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
if (map == nullptr)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
map = new std::unordered_map<xlnt::vertical_alignment, std::string, EnumClassHash>;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
for (auto pair : get_string_vertical_alignment_map())
|
|
|
|
{
|
|
|
|
map->emplace(pair.second, pair.first);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
xlnt::vertical_alignment vertical_alignment_from_string(const std::string &vertical_alignment_string)
|
|
|
|
{
|
|
|
|
return get_string_vertical_alignment_map().at(string_lower(vertical_alignment_string));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
std::string vertical_alignment_to_string(xlnt::vertical_alignment vertical_alignment)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
return get_vertical_alignment_string_map().at(vertical_alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// horizontal_alignment
|
|
|
|
|
|
|
|
|
|
|
|
const std::unordered_map<std::string, xlnt::horizontal_alignment> &get_string_horizontal_alignment_map()
|
|
|
|
{
|
|
|
|
static std::unordered_map<std::string, xlnt::horizontal_alignment> *map = nullptr;
|
|
|
|
|
|
|
|
if (map == nullptr)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
map = new std::unordered_map<std::string, xlnt::horizontal_alignment>
|
|
|
|
{
|
|
|
|
{ "center", xlnt::horizontal_alignment::center },
|
|
|
|
{ "center-continous", xlnt::horizontal_alignment::center_continuous },
|
|
|
|
{ "general", xlnt::horizontal_alignment::general },
|
|
|
|
{ "justify", xlnt::horizontal_alignment::justify },
|
|
|
|
{ "left", xlnt::horizontal_alignment::left },
|
|
|
|
{ "none", xlnt::horizontal_alignment::none },
|
|
|
|
{ "right", xlnt::horizontal_alignment::right }
|
|
|
|
};
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2016-06-13 08:15:00 +08:00
|
|
|
const std::unordered_map<xlnt::horizontal_alignment, std::string, EnumClassHash> &get_horizontal_alignment_string_map()
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
static std::unordered_map<xlnt::horizontal_alignment, std::string, EnumClassHash> *map = nullptr;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
if (map == nullptr)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-06-13 08:15:00 +08:00
|
|
|
map = new std::unordered_map<xlnt::horizontal_alignment, std::string, EnumClassHash>;
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
for (auto pair : get_string_horizontal_alignment_map())
|
|
|
|
{
|
|
|
|
map->emplace(pair.second, pair.first);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
return *map;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
xlnt::horizontal_alignment horizontal_alignment_from_string(const std::string &horizontal_alignment_string)
|
|
|
|
{
|
|
|
|
return get_string_horizontal_alignment_map().at(string_lower(horizontal_alignment_string));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string horizontal_alignment_to_string(xlnt::horizontal_alignment horizontal_alignment)
|
|
|
|
{
|
|
|
|
return get_horizontal_alignment_string_map().at(horizontal_alignment);
|
2016-03-10 17:12:51 +08:00
|
|
|
}
|
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
// Reading
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::protection read_protection(const pugi::xml_node protection_node)
|
2016-03-10 17:12:51 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::protection prot;
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-07-06 09:27:35 +08:00
|
|
|
if (is_true(protection_node.attribute("locked").value()))
|
|
|
|
{
|
|
|
|
prot.set_locked(true);
|
|
|
|
}
|
|
|
|
else if (!is_false(protection_node.attribute("locked").value()))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("bad protection value");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_true(protection_node.attribute("hidden").value()))
|
|
|
|
{
|
|
|
|
prot.set_hidden(true);
|
|
|
|
}
|
|
|
|
else if (!is_false(protection_node.attribute("hidden").value()))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("bad protection value");
|
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
|
2016-07-06 09:27:35 +08:00
|
|
|
return prot;
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::alignment read_alignment(const pugi::xml_node alignment_node)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::alignment align;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
align.set_wrap_text(is_true(alignment_node.attribute("wrapText").value()));
|
|
|
|
align.set_shrink_to_fit(is_true(alignment_node.attribute("shrinkToFit").value()));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-05 00:09:51 +08:00
|
|
|
if (alignment_node.attribute("vertical"))
|
2016-05-04 03:37:34 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
std::string vertical = alignment_node.attribute("vertical").value();
|
2016-06-11 01:40:50 +08:00
|
|
|
align.set_vertical(vertical_alignment_from_string(vertical));
|
2016-05-04 03:37:34 +08:00
|
|
|
}
|
|
|
|
|
2016-07-05 00:09:51 +08:00
|
|
|
if (alignment_node.attribute("horizontal"))
|
2016-03-10 17:12:51 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
std::string horizontal = alignment_node.attribute("horizontal").value();
|
2016-06-11 01:40:50 +08:00
|
|
|
align.set_horizontal(horizontal_alignment_from_string(horizontal));
|
2016-05-04 03:37:34 +08:00
|
|
|
}
|
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
return align;
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_number_formats(const pugi::xml_node number_formats_node, std::vector<xlnt::number_format> &number_formats)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
number_formats.clear();
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto num_fmt_node : number_formats_node.children("numFmt"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
std::string format_string(num_fmt_node.attribute("formatCode").value());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (format_string == "GENERAL")
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
|
|
|
format_string = "General";
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::number_format nf;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
nf.set_format_string(format_string);
|
2016-07-04 07:22:08 +08:00
|
|
|
nf.set_id(string_to_size_t(num_fmt_node.attribute("numFmtId").value()));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
number_formats.push_back(nf);
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::color read_color(const pugi::xml_node &color_node)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
if (color_node.attribute("rgb"))
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
return xlnt::color(xlnt::color::type::rgb, color_node.attribute("rgb").value());
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2016-07-04 07:22:08 +08:00
|
|
|
else if (color_node.attribute("theme"))
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
return xlnt::color(xlnt::color::type::theme, string_to_size_t(color_node.attribute("theme").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2016-07-04 07:22:08 +08:00
|
|
|
else if (color_node.attribute("indexed"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
return xlnt::color(xlnt::color::type::indexed, string_to_size_t(color_node.attribute("indexed").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2016-07-04 07:22:08 +08:00
|
|
|
else if (color_node.attribute("auto"))
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
return xlnt::color(xlnt::color::type::auto_, string_to_size_t(color_node.attribute("auto").value()));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
throw std::runtime_error("bad color");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::font read_font(const pugi::xml_node font_node)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::font new_font;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
new_font.set_size(string_to_size_t(font_node.child("sz").attribute("val").value()));
|
|
|
|
new_font.set_name(font_node.child("name").attribute("val").value());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("color"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_font.set_color(read_color(font_node.child("color")));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("family"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_font.set_family(string_to_size_t(font_node.child("family").attribute("val").value()));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("scheme"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_font.set_scheme(font_node.child("scheme").attribute("val").value());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("b"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
if(font_node.child("b").attribute("val"))
|
2016-03-10 17:12:51 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_font.set_bold(is_true(font_node.child("b").attribute("val").value()));
|
2016-03-10 17:12:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_font.set_bold(true);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("strike"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
if(font_node.child("strike").attribute("val"))
|
2016-03-10 17:12:51 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_font.set_strikethrough(is_true(font_node.child("strike").attribute("val").value()));
|
2016-03-10 17:12:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_font.set_strikethrough(true);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("i"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
if(font_node.child("i").attribute("val"))
|
2016-03-10 17:12:51 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_font.set_italic(is_true(font_node.child("i").attribute("val").value()));
|
2016-03-10 17:12:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_font.set_italic(true);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("u"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
if (font_node.child("u").attribute("val"))
|
2016-03-10 17:12:51 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
std::string underline_string = font_node.child("u").attribute("val").value();
|
2016-03-10 17:12:51 +08:00
|
|
|
new_font.set_underline(underline_style_from_string(underline_string));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_font.set_underline(xlnt::font::underline_style::single);
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
return new_font;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_fonts(const pugi::xml_node &fonts_node, std::vector<xlnt::font> &fonts)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
fonts.clear();
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto font_node : fonts_node.children())
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
|
|
|
fonts.push_back(read_font(font_node));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_indexed_colors(const pugi::xml_node &indexed_colors_node, std::vector<xlnt::color> &colors)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto color_node : indexed_colors_node.children())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
colors.push_back(read_color(color_node));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_colors(const pugi::xml_node &colors_node, std::vector<xlnt::color> &colors)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
|
|
|
colors.clear();
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (colors_node.child("indexedColors"))
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
read_indexed_colors(colors_node.child("indexedColors"), colors);
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::fill read_fill(const pugi::xml_node &fill_node)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::fill new_fill;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (fill_node.child("patternFill"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
new_fill.set_type(xlnt::fill::type::pattern);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto pattern_fill_node = fill_node.child("patternFill");
|
|
|
|
std::string pattern_fill_type_string = pattern_fill_node.attribute("patternType").value();
|
2016-06-13 07:59:59 +08:00
|
|
|
|
|
|
|
if (!pattern_fill_type_string.empty())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
new_fill.set_pattern_type(pattern_fill_type_from_string(pattern_fill_type_string));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (pattern_fill_node.child("bgColor"))
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_fill.get_background_color() = read_color(pattern_fill_node.child("bgColor"));
|
2016-06-13 07:59:59 +08:00
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (pattern_fill_node.child("fgColor"))
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_fill.get_foreground_color() = read_color(pattern_fill_node.child("fgColor"));
|
2016-06-13 07:59:59 +08:00
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
return new_fill;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_fills(const pugi::xml_node &fills_node, std::vector<xlnt::fill> &fills)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
|
|
|
fills.clear();
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto fill_node : fills_node.children())
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
|
|
|
fills.emplace_back();
|
|
|
|
fills.back() = read_fill(fill_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::side read_side(const pugi::xml_node &side_node)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::side new_side;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (side_node.attribute("style"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_side.get_border_style() = border_style_from_string(side_node.attribute("style").value());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (side_node.child("color"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_side.get_color() = read_color(side_node.child("color"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
return new_side;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::border read_border(const pugi::xml_node &border_node)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::border new_border;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("start"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_start() = read_side(border_node.child("start"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("end"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_end() = read_side(border_node.child("end"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("left"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_left() = read_side(border_node.child("left"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("right"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_right() = read_side(border_node.child("right"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("top"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_top() = read_side(border_node.child("top"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("bottom"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_bottom() = read_side(border_node.child("bottom"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("diagonal"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_diagonal() = read_side(border_node.child("diagonal"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("vertical"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_vertical() = read_side(border_node.child("vertical"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if (border_node.child("horizontal"))
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
new_border.get_horizontal() = read_side(border_node.child("horizontal"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
return new_border;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_borders(const pugi::xml_node &borders_node, std::vector<xlnt::border> &borders)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
borders.clear();
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto border_node : borders_node.children())
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
borders.push_back(read_border(border_node));
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool read_base_format(const pugi::xml_node &format_node, const xlnt::detail::stylesheet &stylesheet, xlnt::base_format &f)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
// Alignment
|
2016-07-04 07:22:08 +08:00
|
|
|
f.alignment_applied(format_node.child("alignment") || is_true(format_node.attribute("applyAlignment").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
if (f.alignment_applied())
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto inline_alignment = read_alignment(format_node.child("alignment"));
|
2016-06-11 01:40:50 +08:00
|
|
|
f.set_alignment(inline_alignment);
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
// Border
|
2016-07-04 07:22:08 +08:00
|
|
|
auto border_index = format_node.attribute("borderId") ? string_to_size_t(format_node.attribute("borderId").value()) : 0;
|
2016-06-11 01:40:50 +08:00
|
|
|
f.set_border(stylesheet.borders.at(border_index));
|
2016-07-04 07:22:08 +08:00
|
|
|
f.border_applied(is_true(format_node.attribute("applyBorder").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
// Fill
|
2016-07-04 07:22:08 +08:00
|
|
|
auto fill_index = format_node.attribute("fillId") ? string_to_size_t(format_node.attribute("fillId").value()) : 0;
|
2016-06-11 01:40:50 +08:00
|
|
|
f.set_fill(stylesheet.fills.at(fill_index));
|
2016-07-04 07:22:08 +08:00
|
|
|
f.fill_applied(is_true(format_node.attribute("applyFill").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
// Font
|
2016-07-04 07:22:08 +08:00
|
|
|
auto font_index = format_node.attribute("fontId") ? string_to_size_t(format_node.attribute("fontId").value()) : 0;
|
2016-06-11 01:40:50 +08:00
|
|
|
f.set_font(stylesheet.fonts.at(font_index));
|
2016-07-04 07:22:08 +08:00
|
|
|
f.font_applied(is_true(format_node.attribute("applyFont").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
// Number Format
|
2016-07-04 07:22:08 +08:00
|
|
|
auto number_format_id = string_to_size_t(format_node.attribute("numFmtId").value());
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
bool builtin_format = true;
|
|
|
|
|
|
|
|
for (const auto &num_fmt : stylesheet.number_formats)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
if (static_cast<std::size_t>(num_fmt.get_id()) == number_format_id)
|
|
|
|
{
|
|
|
|
f.set_number_format(num_fmt);
|
|
|
|
builtin_format = false;
|
|
|
|
break;
|
|
|
|
}
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
if (builtin_format)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
f.set_number_format(xlnt::number_format::from_builtin_id(number_format_id));
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
f.number_format_applied(is_true(format_node.attribute("applyNumberFormat").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
// Protection
|
2016-07-04 07:22:08 +08:00
|
|
|
f.protection_applied(format_node.attribute("protection") || is_true(format_node.attribute("applyProtection").value()));
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
if (f.protection_applied())
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto inline_protection = read_protection(format_node.child("protection"));
|
2016-06-11 01:40:50 +08:00
|
|
|
f.set_protection(inline_protection);
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
return true;
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_formats(const pugi::xml_node &formats_node, const xlnt::detail::stylesheet &stylesheet,
|
2016-06-11 01:40:50 +08:00
|
|
|
std::vector<xlnt::format> &formats, std::vector<std::string> &format_styles)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto format_node : formats_node.children("xf"))
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
xlnt::format format;
|
|
|
|
read_base_format(format_node, stylesheet, format);
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
// TODO do all formats have xfId?
|
2016-07-04 07:22:08 +08:00
|
|
|
if(format_node.attribute("xfId"))
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto style_index = string_to_size_t(format_node.attribute("xfId").value());
|
2016-06-11 01:40:50 +08:00
|
|
|
auto style_name = stylesheet.style_name_map.at(style_index);
|
|
|
|
format_styles.push_back(style_name);
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
else
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
format_styles.push_back("");
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
2016-05-15 23:51:32 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
formats.push_back(format);
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xlnt::style read_style(const pugi::xml_node &style_node, const pugi::xml_node &style_format_node, const xlnt::detail::stylesheet &stylesheet)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
|
|
|
xlnt::style s;
|
|
|
|
|
|
|
|
read_base_format(style_format_node, stylesheet, s);
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
s.set_name(style_node.attribute("name").value());
|
|
|
|
s.set_hidden(style_node.attribute("hidden") && is_true(style_node.attribute("hidden").value()));
|
|
|
|
s.set_builtin_id(string_to_size_t(style_node.attribute("builtinId").value()));
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void read_styles(const pugi::xml_node &styles_node, const pugi::xml_node &style_formats_node, const xlnt::detail::stylesheet stylesheet, std::vector<xlnt::style> &styles, std::unordered_map<std::size_t, std::string> &style_names)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
|
|
|
std::size_t style_index = 0;
|
2016-05-14 02:40:17 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto cell_style_format_node : style_formats_node.children())
|
2016-05-14 02:40:17 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
bool match = false;
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
for (auto cell_style_node : styles_node.children())
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto cell_style_format_index = std::stoull(cell_style_node.attribute("xfId").value());
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
if (cell_style_format_index == style_index)
|
|
|
|
{
|
|
|
|
styles.push_back(read_style(cell_style_node, cell_style_format_node, stylesheet));
|
|
|
|
style_names[style_index] = styles.back().get_name();
|
|
|
|
match = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
style_index++;
|
2016-05-14 02:40:17 +08:00
|
|
|
}
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_color(const xlnt::color &color, pugi::xml_node color_node)
|
2016-06-13 07:59:59 +08:00
|
|
|
{
|
|
|
|
switch (color.get_type())
|
|
|
|
{
|
|
|
|
case xlnt::color::type::auto_:
|
2016-07-04 07:22:08 +08:00
|
|
|
color_node.append_attribute("auto").set_value(std::to_string(color.get_auto()).c_str());
|
2016-06-13 07:59:59 +08:00
|
|
|
break;
|
|
|
|
case xlnt::color::type::theme:
|
2016-07-04 07:22:08 +08:00
|
|
|
color_node.append_attribute("theme").set_value(std::to_string(color.get_theme()).c_str());
|
2016-06-13 07:59:59 +08:00
|
|
|
break;
|
|
|
|
case xlnt::color::type::indexed:
|
2016-07-04 07:22:08 +08:00
|
|
|
color_node.append_attribute("indexed").set_value(std::to_string(color.get_index()).c_str());
|
2016-06-13 07:59:59 +08:00
|
|
|
break;
|
|
|
|
case xlnt::color::type::rgb:
|
2016-07-04 07:22:08 +08:00
|
|
|
color_node.append_attribute("rgb").set_value(color.get_rgb_string().c_str());
|
2016-06-13 07:59:59 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("bad type");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_fonts(const std::vector<xlnt::font> &fonts, pugi::xml_node &fonts_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
fonts_node.append_attribute("count").set_value(std::to_string(fonts.size()).c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
// TODO: what does this do?
|
2016-07-04 07:22:08 +08:00
|
|
|
// fonts_node.append_attribute("x14ac:knownFonts", "1");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
for (auto &f : fonts)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto font_node = fonts_node.append_child("font");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (f.is_bold())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto bold_node = font_node.append_child("b");
|
|
|
|
bold_node.append_attribute("val").set_value("1");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (f.is_italic())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto italic_node = font_node.append_child("i");
|
|
|
|
italic_node.append_attribute("val").set_value("1");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (f.is_underline())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto underline_node = font_node.append_child("u");
|
|
|
|
underline_node.append_attribute("val").set_value(underline_style_to_string(f.get_underline()).c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (f.is_strikethrough())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto strike_node = font_node.append_child("strike");
|
|
|
|
strike_node.append_attribute("val").set_value("1");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto size_node = font_node.append_child("sz");
|
|
|
|
size_node.append_attribute("val").set_value(std::to_string(f.get_size()).c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto color_node = font_node.append_child("color");
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
write_color(f.get_color(), color_node);
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto name_node = font_node.append_child("name");
|
|
|
|
name_node.append_attribute("val").set_value(f.get_name().c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (f.has_family())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto family_node = font_node.append_child("family");
|
|
|
|
family_node.append_attribute("val").set_value(std::to_string(f.get_family()).c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (f.has_scheme())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto scheme_node = font_node.append_child("scheme");
|
2016-07-10 03:12:53 +08:00
|
|
|
scheme_node.append_attribute("val").set_value(f.get_scheme().c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-01 04:19:45 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_fills(const std::vector<xlnt::fill> &fills, pugi::xml_node &fills_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
fills_node.append_attribute("count").set_value(std::to_string(fills.size()).c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
for (auto &fill_ : fills)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto fill_node = fills_node.append_child("fill");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
if (fill_.get_type() == xlnt::fill::type::pattern)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto pattern_fill_node = fill_node.append_child("patternFill");
|
|
|
|
pattern_fill_node.append_attribute("patternType").set_value(pattern_fill_type_to_string(fill_.get_pattern_type()).c_str());
|
2016-03-10 17:12:51 +08:00
|
|
|
|
|
|
|
if (fill_.get_pattern_type() != xlnt::fill::pattern_type::solid) continue;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
if (fill_.get_foreground_color())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
write_color(*fill_.get_foreground_color(), pattern_fill_node.append_child("fgColor"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
if (fill_.get_background_color())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
write_color(*fill_.get_background_color(), pattern_fill_node.append_child("bgColor"));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
else if (fill_.get_type() == xlnt::fill::type::solid)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto solid_fill_node = fill_node.append_child("solidFill");
|
|
|
|
solid_fill_node.append_child("color");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-11 01:40:50 +08:00
|
|
|
else if (fill_.get_type() == xlnt::fill::type::gradient)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto gradient_fill_node = fill_node.append_child("gradientFill");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
if (fill_.get_gradient_type() == xlnt::fill::gradient_type::linear)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
gradient_fill_node.append_attribute("degree").set_value(std::to_string(fill_.get_rotation()).c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-06-13 07:59:59 +08:00
|
|
|
else if (fill_.get_gradient_type() == xlnt::fill::gradient_type::path)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
gradient_fill_node.append_attribute("left").set_value(std::to_string(fill_.get_gradient_left()).c_str());
|
|
|
|
gradient_fill_node.append_attribute("right").set_value(std::to_string(fill_.get_gradient_right()).c_str());
|
|
|
|
gradient_fill_node.append_attribute("top").set_value(std::to_string(fill_.get_gradient_top()).c_str());
|
|
|
|
gradient_fill_node.append_attribute("bottom").set_value(std::to_string(fill_.get_gradient_bottom()).c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto start_node = gradient_fill_node.append_child("stop");
|
|
|
|
start_node.append_attribute("position").set_value("0");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto end_node = gradient_fill_node.append_child("stop");
|
|
|
|
end_node.append_attribute("position").set_value("1");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 04:19:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_borders(const std::vector<xlnt::border> &borders, pugi::xml_node &borders_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
borders_node.append_attribute("count").set_value(std::to_string(borders.size()).c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
for (const auto &border_ : borders)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto border_node = borders_node.append_child("border");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
std::vector<std::tuple<std::string, const std::experimental::optional<xlnt::side>>> sides;
|
2015-12-25 04:51:11 +08:00
|
|
|
|
|
|
|
sides.push_back(std::make_tuple("start", border_.get_start()));
|
|
|
|
sides.push_back(std::make_tuple("end", border_.get_end()));
|
|
|
|
sides.push_back(std::make_tuple("left", border_.get_left()));
|
|
|
|
sides.push_back(std::make_tuple("right", border_.get_right()));
|
|
|
|
sides.push_back(std::make_tuple("top", border_.get_top()));
|
|
|
|
sides.push_back(std::make_tuple("bottom", border_.get_bottom()));
|
|
|
|
sides.push_back(std::make_tuple("diagonal", border_.get_diagonal()));
|
|
|
|
sides.push_back(std::make_tuple("vertical", border_.get_vertical()));
|
|
|
|
sides.push_back(std::make_tuple("horizontal", border_.get_horizontal()));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
for (const auto &side_tuple : sides)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-12-25 04:51:11 +08:00
|
|
|
std::string current_name = std::get<0>(side_tuple);
|
|
|
|
const auto current_side = std::get<1>(side_tuple);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
if (current_side)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto side_node = border_node.append_child(current_name.c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
if (current_side->get_border_style())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-06-13 07:59:59 +08:00
|
|
|
auto style_string = border_style_to_string(*current_side->get_border_style());
|
2016-07-04 07:22:08 +08:00
|
|
|
side_node.append_attribute("style").set_value(style_string.c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-12-25 04:51:11 +08:00
|
|
|
if (current_side->get_color())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto color_node = side_node.append_child("color");
|
2016-06-13 07:59:59 +08:00
|
|
|
write_color(*current_side->get_color(), color_node);
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 04:19:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_base_format(const xlnt::base_format &xf, const xlnt::detail::stylesheet &stylesheet, pugi::xml_node xf_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
xf_node.append_attribute("numFmtId").set_value(std::to_string(xf.get_number_format().get_id()).c_str());
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
auto font_id = std::distance(stylesheet.fonts.begin(), std::find(stylesheet.fonts.begin(), stylesheet.fonts.end(), xf.get_font()));
|
2016-07-04 07:22:08 +08:00
|
|
|
xf_node.append_attribute("fontId").set_value(std::to_string(font_id).c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
auto fill_id = std::distance(stylesheet.fills.begin(), std::find(stylesheet.fills.begin(), stylesheet.fills.end(), xf.get_fill()));
|
2016-07-04 07:22:08 +08:00
|
|
|
xf_node.append_attribute("fillId").set_value(std::to_string(fill_id).c_str());
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
auto border_id = std::distance(stylesheet.borders.begin(), std::find(stylesheet.borders.begin(), stylesheet.borders.end(), xf.get_border()));
|
2016-07-04 07:22:08 +08:00
|
|
|
xf_node.append_attribute("borderId").set_value(std::to_string(border_id).c_str());
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
if(xf.number_format_applied()) xf_node.append_attribute("applyNumberFormat").set_value("1");
|
|
|
|
if(xf.fill_applied()) xf_node.append_attribute("applyFill").set_value("1");
|
|
|
|
if(xf.font_applied()) xf_node.append_attribute("applyFont").set_value("1");
|
|
|
|
if(xf.border_applied()) xf_node.append_attribute("applyBorder").set_value("1");
|
|
|
|
if(xf.alignment_applied()) xf_node.append_attribute("applyAlignment").set_value("1");
|
|
|
|
if(xf.protection_applied()) xf_node.append_attribute("applyProtection").set_value("1");
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
if (xf.alignment_applied())
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto alignment_node = xf_node.append_child("alignment");
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
if (xf.get_alignment().has_vertical())
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-06 09:27:35 +08:00
|
|
|
auto vertical = vertical_alignment_to_string(xf.get_alignment().get_vertical());
|
|
|
|
alignment_node.append_attribute("vertical").set_value(vertical.c_str());
|
2016-03-10 17:12:51 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
if (xf.get_alignment().has_horizontal())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-06 09:27:35 +08:00
|
|
|
auto horizontal = horizontal_alignment_to_string(xf.get_alignment().get_horizontal());
|
|
|
|
alignment_node.append_attribute("horizontal").set_value(horizontal.c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
if (xf.get_alignment().get_wrap_text())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
alignment_node.append_attribute("wrapText").set_value("1");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
if (xf.get_alignment().get_shrink_to_fit())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
alignment_node.append_attribute("shrinkToFit").set_value("1");
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 07:59:59 +08:00
|
|
|
if (xf.protection_applied())
|
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto protection_node = xf_node.append_child("protection");
|
2016-07-06 09:27:35 +08:00
|
|
|
protection_node.append_attribute("locked").set_value(xf.get_protection().get_locked() ? "1" : "0");
|
|
|
|
protection_node.append_attribute("hidden").set_value(xf.get_protection().get_hidden() ? "1" : "0");
|
2016-06-13 07:59:59 +08:00
|
|
|
}
|
2016-05-16 03:03:02 +08:00
|
|
|
|
2016-05-01 04:19:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_styles(const xlnt::detail::stylesheet &stylesheet, pugi::xml_node &styles_node, pugi::xml_node &style_formats_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
style_formats_node.append_attribute("count").set_value(std::to_string(stylesheet.styles.size()).c_str());
|
|
|
|
styles_node.append_attribute("count").set_value(std::to_string(stylesheet.styles.size()).c_str());
|
2016-05-15 01:57:07 +08:00
|
|
|
std::size_t style_index = 0;
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
for(auto ¤t_style : stylesheet.styles)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto xf_node = style_formats_node.append_child("xf");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_base_format(current_style, stylesheet, xf_node);
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto cell_style_node = styles_node.append_child("cellStyle");
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
cell_style_node.append_attribute("name").set_value(current_style.get_name().c_str());
|
|
|
|
cell_style_node.append_attribute("xfId").set_value(std::to_string(style_index++).c_str());
|
|
|
|
cell_style_node.append_attribute("builtinId").set_value(std::to_string(current_style.get_builtin_id()).c_str());
|
2016-03-10 17:12:51 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
if (current_style.get_hidden())
|
2016-03-10 17:12:51 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
cell_style_node.append_attribute("hidden").set_value("1");
|
2016-03-10 17:12:51 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2016-05-01 04:19:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_formats(const xlnt::detail::stylesheet &stylesheet, pugi::xml_node &formats_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
formats_node.append_attribute("count").set_value(std::to_string(stylesheet.formats.size()).c_str());
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
auto format_style_iterator = stylesheet.format_styles.begin();
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
for(auto ¤t_format : stylesheet.formats)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto xf_node = formats_node.append_child("xf");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_base_format(current_format, stylesheet, xf_node);
|
2016-05-15 01:57:07 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
const auto format_style_name = *(format_style_iterator++);
|
|
|
|
|
|
|
|
if(!format_style_name.empty())
|
2016-05-15 01:57:07 +08:00
|
|
|
{
|
2016-06-11 01:40:50 +08:00
|
|
|
auto style = std::find_if(stylesheet.styles.begin(), stylesheet.styles.end(),
|
|
|
|
[&](const xlnt::style &s) { return s.get_name() == format_style_name; });
|
|
|
|
auto style_index = std::distance(stylesheet.styles.begin(), style);
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
xf_node.append_attribute("xfId").set_value(std::to_string(style_index).c_str());
|
2016-05-15 01:57:07 +08:00
|
|
|
}
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_dxfs(pugi::xml_node &dxfs_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
dxfs_node.append_attribute("count").set_value("0");
|
2016-05-01 04:19:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_table_styles(pugi::xml_node &table_styles_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
table_styles_node.append_attribute("count").set_value("0");
|
|
|
|
table_styles_node.append_attribute("defaultTableStyle").set_value("TableStyleMedium9");
|
|
|
|
table_styles_node.append_attribute("defaultPivotStyle").set_value("PivotStyleMedium7");
|
2016-05-01 04:19:45 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_colors(const std::vector<xlnt::color> &colors, pugi::xml_node &colors_node)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto indexed_colors_node = colors_node.append_child("indexedColors");
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
for (auto &c : colors)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
indexed_colors_node.append_child("rgbColor").append_attribute("rgb").set_value(c.get_rgb_string().c_str());
|
2016-03-10 17:12:51 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 04:19:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool write_number_formats(const std::vector<xlnt::number_format> &number_formats, pugi::xml_node &number_formats_node)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
number_formats_node.append_attribute("count").set_value(std::to_string(number_formats.size()).c_str());
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
for (const auto &num_fmt : number_formats)
|
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto num_fmt_node = number_formats_node.append_child("numFmt");
|
|
|
|
num_fmt_node.append_attribute("numFmtId").set_value(std::to_string(num_fmt.get_id()).c_str());
|
|
|
|
num_fmt_node.append_attribute("formatCode").set_value(num_fmt.get_format_string().c_str());
|
2016-06-11 01:40:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
|
|
|
style_serializer::style_serializer(detail::stylesheet &stylesheet) : stylesheet_(stylesheet)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool style_serializer::read_stylesheet(const pugi::xml_document &xml)
|
2016-06-11 01:40:50 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto stylesheet_node = xml.child("styleSheet");
|
2016-06-11 01:40:50 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
read_borders(stylesheet_node.child("borders"), stylesheet_.borders);
|
|
|
|
read_fills(stylesheet_node.child("fills"), stylesheet_.fills);
|
|
|
|
read_fonts(stylesheet_node.child("fonts"), stylesheet_.fonts);
|
|
|
|
read_number_formats(stylesheet_node.child("numFmts"), stylesheet_.number_formats);
|
|
|
|
read_colors(stylesheet_node.child("colors"), stylesheet_.colors);
|
|
|
|
read_styles(stylesheet_node.child("cellStyles"), stylesheet_node.child("cellStyleXfs"), stylesheet_, stylesheet_.styles, stylesheet_.style_name_map);
|
|
|
|
read_formats(stylesheet_node.child("cellXfs"), stylesheet_, stylesheet_.formats, stylesheet_.format_styles);
|
2016-06-11 01:40:50 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
bool style_serializer::write_stylesheet(pugi::xml_document &doc)
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto root_node = doc.append_child("styleSheet");
|
|
|
|
root_node.append_attribute("xmlns").set_value("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
|
|
|
|
root_node.append_attribute("xmlns:mc").set_value("http://schemas.openxmlformats.org/markup-compatibility/2006");
|
|
|
|
root_node.append_attribute("mc:Ignorable").set_value("x14ac");
|
|
|
|
root_node.append_attribute("xmlns:x14ac").set_value("http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
if (!stylesheet_.number_formats.empty())
|
2016-05-16 03:03:02 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto number_formats_node = root_node.append_child("numFmts");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_number_formats(stylesheet_.number_formats, number_formats_node);
|
2016-05-16 03:03:02 +08:00
|
|
|
}
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
if (!stylesheet_.fonts.empty())
|
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto fonts_node = root_node.append_child("fonts");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_fonts(stylesheet_.fonts, fonts_node);
|
|
|
|
}
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
if (!stylesheet_.fills.empty())
|
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto fills_node = root_node.append_child("fills");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_fills(stylesheet_.fills, fills_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stylesheet_.borders.empty())
|
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto borders_node = root_node.append_child("borders");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_borders(stylesheet_.borders, borders_node);
|
|
|
|
}
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto cell_style_xfs_node = root_node.append_child("cellStyleXfs");
|
2016-05-14 02:40:17 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto cell_xfs_node = root_node.append_child("cellXfs");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_formats(stylesheet_, cell_xfs_node);
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto cell_styles_node = root_node.append_child("cellStyles");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_styles(stylesheet_, cell_styles_node, cell_style_xfs_node);
|
2016-05-01 04:19:45 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto dxfs_node = root_node.append_child("dxfs");
|
2016-05-01 04:19:45 +08:00
|
|
|
write_dxfs(dxfs_node);
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
auto table_styles_node = root_node.append_child("tableStyles");
|
2016-05-01 04:19:45 +08:00
|
|
|
write_table_styles(table_styles_node);
|
|
|
|
|
2016-06-11 01:40:50 +08:00
|
|
|
if(!stylesheet_.colors.empty())
|
2016-05-01 04:19:45 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto colors_node = root_node.append_child("colors");
|
2016-06-11 01:40:50 +08:00
|
|
|
write_colors(stylesheet_.colors, colors_node);
|
2016-05-01 04:19:45 +08:00
|
|
|
}
|
2015-10-30 01:46:56 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
} // namespace xlnt
|