xlsxcell.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
  3. ** All right reserved.
  4. **
  5. ** Permission is hereby granted, free of charge, to any person obtaining
  6. ** a copy of this software and associated documentation files (the
  7. ** "Software"), to deal in the Software without restriction, including
  8. ** without limitation the rights to use, copy, modify, merge, publish,
  9. ** distribute, sublicense, and/or sell copies of the Software, and to
  10. ** permit persons to whom the Software is furnished to do so, subject to
  11. ** the following conditions:
  12. **
  13. ** The above copyright notice and this permission notice shall be
  14. ** included in all copies or substantial portions of the Software.
  15. **
  16. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. **
  24. ****************************************************************************/
  25. #include "xlsxcell.h"
  26. #include "xlsxcell_p.h"
  27. #include "xlsxformat.h"
  28. #include "xlsxformat_p.h"
  29. #include "xlsxutility_p.h"
  30. #include "xlsxworksheet.h"
  31. #include "xlsxworkbook.h"
  32. #include <QDateTime>
  33. QT_BEGIN_NAMESPACE_XLSX
  34. CellPrivate::CellPrivate(Cell *p) :
  35. q_ptr(p)
  36. {
  37. }
  38. CellPrivate::CellPrivate(const CellPrivate * const cp)
  39. : value(cp->value), formula(cp->formula), cellType(cp->cellType)
  40. , format(cp->format), richString(cp->richString), parent(cp->parent)
  41. {
  42. }
  43. /*!
  44. \class Cell
  45. \inmodule QtXlsx
  46. \brief The Cell class provides a API that is used to handle the worksheet cell.
  47. */
  48. /*!
  49. \enum Cell::CellType
  50. \value BooleanType Boolean type
  51. \value NumberType Number type, can be blank or used with forumula
  52. \value ErrorType Error type
  53. \value SharedStringType Shared string type
  54. \value StringType String type, can be used with forumula
  55. \value InlineStringType Inline string type
  56. */
  57. /*!
  58. * \internal
  59. * Created by Worksheet only.
  60. */
  61. Cell::Cell(const QVariant &data, CellType type, const Format &format, Worksheet *parent) :
  62. d_ptr(new CellPrivate(this))
  63. {
  64. d_ptr->value = data;
  65. d_ptr->cellType = type;
  66. d_ptr->format = format;
  67. d_ptr->parent = parent;
  68. }
  69. /*!
  70. * \internal
  71. */
  72. Cell::Cell(const Cell * const cell):
  73. d_ptr(new CellPrivate(cell->d_ptr))
  74. {
  75. d_ptr->q_ptr = this;
  76. }
  77. /*!
  78. * Destroys the Cell and cleans up.
  79. */
  80. Cell::~Cell()
  81. {
  82. delete d_ptr;
  83. }
  84. /*!
  85. * Return the dataType of this Cell
  86. */
  87. Cell::CellType Cell::cellType() const
  88. {
  89. Q_D(const Cell);
  90. return d->cellType;
  91. }
  92. /*!
  93. * Return the data content of this Cell
  94. */
  95. QVariant Cell::value() const
  96. {
  97. Q_D(const Cell);
  98. return d->value;
  99. }
  100. /*!
  101. * Return the style used by this Cell. If no style used, 0 will be returned.
  102. */
  103. Format Cell::format() const
  104. {
  105. Q_D(const Cell);
  106. return d->format;
  107. }
  108. /*!
  109. * Returns true if the cell has one formula.
  110. */
  111. bool Cell::hasFormula() const
  112. {
  113. Q_D(const Cell);
  114. return d->formula.isValid();
  115. }
  116. /*!
  117. * Return the formula contents if the dataType is Formula
  118. */
  119. CellFormula Cell::formula() const
  120. {
  121. Q_D(const Cell);
  122. return d->formula;
  123. }
  124. /*!
  125. * Returns whether the value is probably a dateTime or not
  126. */
  127. bool Cell::isDateTime() const
  128. {
  129. Q_D(const Cell);
  130. if (d->cellType == NumberType && d->value.toDouble() >=0
  131. && d->format.isValid() && d->format.isDateTimeFormat()) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. /*!
  137. * Return the data time value.
  138. */
  139. QDateTime Cell::dateTime() const
  140. {
  141. Q_D(const Cell);
  142. if (!isDateTime())
  143. return QDateTime();
  144. return datetimeFromNumber(d->value.toDouble(), d->parent->workbook()->isDate1904());
  145. }
  146. /*!
  147. * Returns whether the cell is probably a rich string or not
  148. */
  149. bool Cell::isRichString() const
  150. {
  151. Q_D(const Cell);
  152. if (d->cellType != SharedStringType && d->cellType != InlineStringType
  153. && d->cellType != StringType)
  154. return false;
  155. return d->richString.isRichString();
  156. }
  157. QT_END_NAMESPACE_XLSX