2014-07-25 05:31:46 +08:00
|
|
|
#include <algorithm>
|
2015-10-17 06:35:11 +08:00
|
|
|
#include <regex>
|
2014-07-25 05:31:46 +08:00
|
|
|
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/utils/hash_combine.hpp>
|
2014-08-14 06:56:34 +08:00
|
|
|
#include <xlnt/styles/number_format.hpp>
|
2014-07-24 04:00:09 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
namespace {
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
const std::unordered_map<std::size_t, std::string> &builtin_formats()
|
2015-11-03 05:45:05 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
static const std::unordered_map<std::size_t, std::string> *formats =
|
|
|
|
new std::unordered_map<std::size_t, std::string>
|
2015-11-03 05:45:05 +08:00
|
|
|
({
|
|
|
|
{ 0, "General" },
|
|
|
|
{ 1, "0" },
|
|
|
|
{ 2, "0.00" },
|
|
|
|
{ 3, "#,##0" },
|
|
|
|
{ 4, "#,##0.00" },
|
|
|
|
{ 5, "\"$\"#,##0_);(\"$\"#,##0)" },
|
|
|
|
{ 6, "\"$\"#,##0_);[Red](\"$\"#,##0)" },
|
|
|
|
{ 7, "\"$\"#,##0.00_);(\"$\"#,##0.00)" },
|
|
|
|
{ 8, "\"$\"#,##0.00_);[Red](\"$\"#,##0.00)" },
|
|
|
|
{ 9, "0%" },
|
|
|
|
{ 10, "0.00%" },
|
|
|
|
{ 11, "0.00E+00" },
|
|
|
|
{ 12, "# ?/?" },
|
|
|
|
{ 13, "# \?\?/??" }, // escape trigraph
|
|
|
|
{ 14, "mm-dd-yy" },
|
|
|
|
{ 15, "d-mmm-yy" },
|
|
|
|
{ 16, "d-mmm" },
|
|
|
|
{ 17, "mmm-yy" },
|
|
|
|
{ 18, "h:mm AM/PM" },
|
|
|
|
{ 19, "h:mm:ss AM/PM" },
|
|
|
|
{ 20, "h:mm" },
|
|
|
|
{ 21, "h:mm:ss" },
|
|
|
|
{ 22, "m/d/yy h:mm" },
|
|
|
|
|
|
|
|
{ 37, "#,##0_);(#,##0)" },
|
|
|
|
{ 38, "#,##0_);[Red](#,##0)" },
|
|
|
|
{ 39, "#,##0.00_);(#,##0.00)" },
|
|
|
|
{ 40, "#,##0.00_);[Red](#,##0.00)" },
|
|
|
|
|
|
|
|
{ 41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)" },
|
|
|
|
{ 42, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)" },
|
|
|
|
{ 43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)" },
|
|
|
|
|
|
|
|
{ 44, "_(\"$\"* #,##0.00_)_(\"$\"* \\(#,##0.00\\)_(\"$\"* \"-\"??_)_(@_)" },
|
|
|
|
{ 45, "mm:ss" },
|
|
|
|
{ 46, "[h]:mm:ss" },
|
|
|
|
{ 47, "mmss.0" },
|
|
|
|
{ 48, "##0.0E+0" },
|
|
|
|
{ 49, "@" }
|
|
|
|
|
|
|
|
// EXCEL differs from the standard in the following:
|
|
|
|
//{14, "m/d/yyyy"},
|
|
|
|
//{22, "m/d/yyyy h:mm"},
|
|
|
|
//{37, "#,##0_);(#,##0)"},
|
|
|
|
//{38, "#,##0_);[Red]"},
|
|
|
|
//{39, "#,##0.00_);(#,##0.00)"},
|
|
|
|
//{40, "#,##0.00_);[Red]"},
|
|
|
|
//{47, "mm:ss.0"},
|
|
|
|
//{55, "yyyy/mm/dd"}
|
|
|
|
});
|
|
|
|
|
|
|
|
return *formats;
|
2014-08-02 04:46:54 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
} // namespace
|
2014-08-02 04:46:54 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
namespace xlnt {
|
2014-08-02 04:46:54 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::general()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(0), 0);
|
|
|
|
return *format;
|
2015-10-07 00:31:49 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::text()
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(49), 49);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::number()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(1), 1);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::number_00()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(2), 2);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::number_comma_separated1()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(4), 4);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::number_comma_separated2()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("#,##0.00_-");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::percentage()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(9), 9);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::percentage_00()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(10), 10);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_yyyymmdd2()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("yyyy-mm-dd");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_yyyymmdd()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("yy-mm-dd");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_ddmmyyyy()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("dd/mm/yy");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_dmyslash()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("d/m/y");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_dmyminus()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("d-m-y");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_dmminus()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("d-m");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_myminus()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("m-y");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx14()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(14), 14);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx15()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(15), 15);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx16()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(16), 16);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx17()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(17), 17);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx22()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(22), 22);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_datetime()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("yyyy-mm-dd h:mm:ss");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time1()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(18), 18);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time2()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(19), 19);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time3()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(20), 20);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time4()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(21), 21);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time5()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(45), 45);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time6()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format(builtin_formats().at(21), 21);
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time7()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("i:s.S");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time8()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("h:mm:ss@");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_timedelta()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("[hh]:mm:ss");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_yyyymmddslash()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("yy/mm/dd@");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::currency_usd_simple()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("\"$\"#,##0.00_-");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::currency_usd()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("$#,##0_-");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::currency_eur_simple()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("[$EUR ]#,##0.00_-");
|
|
|
|
return *format;
|
2014-08-02 04:46:54 +08:00
|
|
|
}
|
2015-10-19 03:30:46 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
number_format::number_format() : number_format(general())
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
number_format::number_format(std::size_t id) : number_format(from_builtin_id(id))
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
number_format::number_format(const std::string &format_string) : id_set_(false), id_(0)
|
2015-11-03 05:45:05 +08:00
|
|
|
{
|
|
|
|
set_format_string(format_string);
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
number_format::number_format(const std::string &format_string, std::size_t id) : id_set_(false), id_(0)
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
2015-10-24 02:42:36 +08:00
|
|
|
set_format_string(format_string, id);
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
number_format number_format::from_builtin_id(std::size_t builtin_id)
|
2014-07-24 08:51:28 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (builtin_formats().find(builtin_id) == builtin_formats().end())
|
2014-07-24 08:51:28 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
throw std::runtime_error("unknown id: " + std::to_string(builtin_id));
|
2014-07-24 08:51:28 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
auto format_string = builtin_formats().at(builtin_id);
|
|
|
|
return number_format(format_string, builtin_id);
|
2014-07-24 08:51:28 +08:00
|
|
|
}
|
2014-07-24 04:00:09 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string number_format::get_format_string() const
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
|
|
|
return format_string_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t number_format::hash() const
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
std::size_t seed = id_;
|
2015-10-24 02:42:36 +08:00
|
|
|
hash_combine(seed, format_string_);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
return seed;
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void number_format::set_format_string(const std::string &format_string)
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
format_string_ = format_string;
|
2015-11-03 05:45:05 +08:00
|
|
|
id_ = 0;
|
|
|
|
id_set_ = false;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
for (const auto &pair : builtin_formats())
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
if (pair.second == format_string)
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
id_ = pair.first;
|
|
|
|
id_set_ = true;
|
|
|
|
break;
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
2015-10-07 00:31:49 +08:00
|
|
|
}
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
void number_format::set_format_string(const std::string &format_string, std::size_t id)
|
2015-11-03 05:45:05 +08:00
|
|
|
{
|
|
|
|
format_string_ = format_string;
|
|
|
|
id_ = id;
|
|
|
|
id_set_ = true;
|
|
|
|
}
|
|
|
|
|
2014-07-24 04:00:09 +08:00
|
|
|
} // namespace xlnt
|