OpenXLSX 1.10.0
Loading...
Searching...
No Matches
XLNumberFormatter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include "XLCellValue.hpp"
6
7namespace OpenXLSX {
8
9 // TokenType for formatting instructions
10 enum class XLFormatTokenType {
11 Literal, // Literal string/character (e.g., "-", "/", " ", text)
12 Year, // yy or yyyy
13 Month, // m, mm, mmm, mmmm, mmmmm
14 Day, // d, dd, ddd, dddd
15 Hour, // h, hh
16 Minute, // m, mm (resolved via context)
17 Second, // s, ss
18 AMPM, // AM/PM, A/P
19 DigitZero, // 0 (forced digit, zero padded)
20 DigitOpt, // # (optional digit)
21 Decimal, // . (decimal point)
22 Thousands, // , (thousands separator)
23 Percent, // % (multiply value by 100)
24 TextPlaceholder // @ (text placeholder)
25 };
26
27 // FormatToken represents a parsed chunk of the format string
30 std::string value; // Holds the original placeholder string or literal text
31 int count; // Number of characters in the placeholder (e.g., "yyyy" = 4)
32 };
33
34 // FormatSection represents a single section of a multi-section format string
36 std::vector<XLFormatToken> tokens;
37 bool isDateTime{false};
38 bool isNumeric{false};
39 bool hasPercent{false};
41 };
42
43 // XLNumberFormatter parses an Excel number format string and applies it to an XLCellValue
45 public:
50 explicit XLNumberFormatter(const std::string& formatString);
51
57 std::string format(const XLCellValue& value) const;
58
59 private:
60 std::string m_formatString;
61 std::vector<FormatSection> m_sections;
62
63 // Core parsing phase
64 void parse();
65 void parseSection(const std::string& sectionStr);
66
67 // Formatting subroutines
68 std::string formatDateTime(double excelDate, const FormatSection& section) const;
69 std::string formatNumeric(double number, const FormatSection& section, bool addMinusSign) const;
70 std::string formatText(const std::string& text, const FormatSection& section) const;
71 };
72
73} // namespace OpenXLSX
Class encapsulating a cell value.
Definition XLCellValue.hpp:79
Definition XLNumberFormatter.hpp:44
std::string format(const XLCellValue &value) const
Applies the format to a given cell value.
Definition XLNumberFormatter.cpp:18
Definition IZipArchive.hpp:18
XLFormatTokenType
Definition XLNumberFormatter.hpp:10
Definition XLNumberFormatter.hpp:35
int decimalPlaces
Definition XLNumberFormatter.hpp:40
bool isNumeric
Definition XLNumberFormatter.hpp:38
bool isDateTime
Definition XLNumberFormatter.hpp:37
bool hasPercent
Definition XLNumberFormatter.hpp:39
std::vector< XLFormatToken > tokens
Definition XLNumberFormatter.hpp:36
Definition XLNumberFormatter.hpp:28
XLFormatTokenType type
Definition XLNumberFormatter.hpp:29
int count
Definition XLNumberFormatter.hpp:31
std::string value
Definition XLNumberFormatter.hpp:30