22#include <com/sun/star/util/NumberFormatsSupplier.hpp>
23#include <com/sun/star/util/NumberFormatter.hpp>
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/util/Date.hpp>
26#include <com/sun/star/util/Time.hpp>
27#include <com/sun/star/util/DateTime.hpp>
28#include <com/sun/star/util/XNumberFormatTypes.hpp>
29#include <com/sun/star/util/NumberFormat.hpp>
45using ::com::sun::star::util::XNumberFormatter;
46using ::com::sun::star::util::NumberFormatter;
47using ::com::sun::star::util::XNumberFormatsSupplier;
48using ::com::sun::star::util::NumberFormatsSupplier;
49using ::com::sun::star::beans::XPropertySet;
50using ::com::sun::star::lang::Locale;
51using ::com::sun::star::util::DateTime;
52using ::com::sun::star::util::XNumberFormatTypes;
54namespace NumberFormat = ::com::sun::star::util::NumberFormat;
60double lcl_convertDateToDays(sal_uInt16
const i_day, sal_uInt16
const i_month,
61 sal_Int16
const i_year)
79 ::sal_Int32
const i_numberFormatType)
84 ENSURE_OR_THROW(i_formatter.is(),
"StandardFormatNormalizer: no formatter!");
85 Reference<XNumberFormatsSupplier>
const xSupplier(i_formatter->getNumberFormatsSupplier(),
87 Reference<XNumberFormatTypes>
const xTypes(xSupplier->getNumberFormats(), UNO_QUERY_THROW);
88 m_nFormatKey = xTypes->getStandardFormat(i_numberFormatType,
104 explicit DoubleNormalization(Reference<XNumberFormatter>
const& i_formatter)
109 virtual double convertToDouble(Any
const& i_value)
const override
111 double returnValue = std::numeric_limits<double>::quiet_NaN();
112 OSL_VERIFY(i_value >>= returnValue);
119class IntegerNormalization :
public StandardFormatNormalizer
122 explicit IntegerNormalization(Reference<XNumberFormatter>
const& i_formatter)
123 : StandardFormatNormalizer(i_formatter, NumberFormat::
NUMBER)
127 virtual double convertToDouble(Any
const& i_value)
const override
130 OSL_VERIFY(i_value >>= value);
137class BooleanNormalization :
public StandardFormatNormalizer
140 explicit BooleanNormalization(Reference<XNumberFormatter>
const& i_formatter)
141 : StandardFormatNormalizer(i_formatter, NumberFormat::
LOGICAL)
145 virtual double convertToDouble(Any
const& i_value)
const override
148 OSL_VERIFY(i_value >>= value);
149 return value ? 1 : 0;
155class DateTimeNormalization :
public StandardFormatNormalizer
158 explicit DateTimeNormalization(Reference<XNumberFormatter>
const& i_formatter)
159 : StandardFormatNormalizer(i_formatter, NumberFormat::
DATETIME)
163 virtual double convertToDouble(Any
const& i_value)
const override
165 double returnValue = std::numeric_limits<double>::quiet_NaN();
169 ENSURE_OR_RETURN(i_value >>= aDateTimeValue,
"allowed for DateTime values only",
174 = lcl_convertDateToDays(aDateTimeValue.Day, aDateTimeValue.Month, aDateTimeValue.Year);
177 returnValue += lcl_convertTimeToDays(aDateTimeValue.Hours, aDateTimeValue.Minutes,
178 aDateTimeValue.Seconds, aDateTimeValue.NanoSeconds);
187class DateNormalization :
public StandardFormatNormalizer
190 explicit DateNormalization(Reference<XNumberFormatter>
const& i_formatter)
191 : StandardFormatNormalizer(i_formatter, NumberFormat::
DATE)
195 virtual double convertToDouble(Any
const& i_value)
const override
197 double returnValue = std::numeric_limits<double>::quiet_NaN();
200 css::util::Date aDateValue;
201 ENSURE_OR_RETURN(i_value >>= aDateValue,
"allowed for Date values only", returnValue);
204 returnValue = lcl_convertDateToDays(aDateValue.Day, aDateValue.Month, aDateValue.Year);
213class TimeNormalization :
public StandardFormatNormalizer
216 explicit TimeNormalization(Reference<XNumberFormatter>
const& i_formatter)
217 : StandardFormatNormalizer(i_formatter, NumberFormat::
TIME)
221 virtual double convertToDouble(Any
const& i_value)
const override
223 double returnValue = std::numeric_limits<double>::quiet_NaN();
226 css::util::Time aTimeValue;
227 ENSURE_OR_RETURN(i_value >>= aTimeValue,
"allowed for tools::Time values only",
231 returnValue += lcl_convertTimeToDays(aTimeValue.Hours, aTimeValue.Minutes,
232 aTimeValue.Seconds, aTimeValue.NanoSeconds);
250 Reference<XComponentContext> xContext = ::comphelper::getProcessComponentContext();
252 Reference<XNumberFormatter>
const xFormatter(NumberFormatter::create(xContext),
258 Reference<XNumberFormatsSupplier>
const xSupplier
259 = NumberFormatsSupplier::createWithLocale(xContext, aLocale);
262 css::util::Date
const aNullDate(1, 1, 1900);
263 Reference<XPropertySet>
const xFormatSettings(xSupplier->getNumberFormatSettings(),
265 xFormatSettings->setPropertyValue(
"NullDate",
Any(aNullDate));
268 xFormatter->attachNumberFormatsSupplier(xSupplier);
282 std::shared_ptr<StandardFormatNormalizer>& o_formatter)
290 OUString
const sTypeName(i_valueType.getTypeName());
291 TypeClass
const eTypeClass = i_valueType.getTypeClass();
314 else if ((eTypeClass == TypeClass_BYTE) || (eTypeClass == TypeClass_SHORT)
315 || (eTypeClass == TypeClass_UNSIGNED_SHORT) || (eTypeClass == TypeClass_LONG)
316 || (eTypeClass == TypeClass_UNSIGNED_LONG) || (eTypeClass == TypeClass_HYPER))
322 SAL_WARN(
"svtools.table",
"unsupported type '" << sTypeName <<
"'!");
327 o_formatter =
pos->second;
329 return bool(o_formatter);
336 , bAttemptedFormatterCreation(false)
345 OUString sStringValue;
346 if (!i_value.hasValue())
349 if (!(i_value >>= sStringValue))
353 std::shared_ptr<StandardFormatNormalizer> pNormalizer;
358 double const formatterCompliantValue = pNormalizer->convertToDouble(i_value);
359 sal_Int32
const formatKey = pNormalizer->getFormatKey();
361 formatterCompliantValue);
static sal_Int32 DateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
const LanguageTag & GetLanguageTag() const
OUString convertToString(const css::uno::Any &i_cellValue)
bool getValueNormalizer(css::uno::Type const &i_valueType, std::shared_ptr< StandardFormatNormalizer > &o_formatter)
bool ensureNumberFormatter()
css::uno::Reference< css::util::XNumberFormatter > xNumberFormatter
bool bAttemptedFormatterCreation
NormalizerCache aNormalizers
#define ENSURE_OR_THROW(c, m)
#define ENSURE_OR_RETURN(c, m, r)
#define DBG_UNHANDLED_EXCEPTION(...)
#define SAL_WARN(area, stream)
const LanguageTag & getLocale()
constexpr sal_Int32 nNullDateDays
rtl::OUString getTypeName(rtl::OUString const &rEnvDcp)