wumu
2024-10-09 d93a5a61f94ac4cf795ba4a8970d9eeba037f4c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// xlsxcell.cpp
 
#include <cmath>
 
#include <QtGlobal>
#include <QDebug>
#include <QDateTime>
#include <QDate>
#include <QTime>
 
#include "xlsxcell.h"
#include "xlsxcell_p.h"
#include "xlsxformat.h"
#include "xlsxformat_p.h"
#include "xlsxutility_p.h"
#include "xlsxworksheet.h"
#include "xlsxworkbook.h"
 
QT_BEGIN_NAMESPACE_XLSX
 
CellPrivate::CellPrivate(Cell *p) :
    q_ptr(p)
{
 
}
 
CellPrivate::CellPrivate(const CellPrivate * const cp)
    : parent(cp->parent)
    , cellType(cp->cellType)
    , value(cp->value)
    , formula(cp->formula)
    , format(cp->format)
    , richString(cp->richString)
    , styleNumber(cp->styleNumber)
{
 
}
 
/*!
  \class Cell
  \inmodule QtXlsx
  \brief The Cell class provides a API that is used to handle the worksheet cell.
 
*/
 
/*!
  \enum Cell::CellType
  \value BooleanType      Boolean type
  \value NumberType       Number type, can be blank or used with forumula
  \value ErrorType        Error type
  \value SharedStringType Shared string type
  \value StringType       String type, can be used with forumula
  \value InlineStringType Inline string type
  */
 
/*!
 * \internal
 * Created by Worksheet only.
 */
// qint32 styleIndex = (-1)
Cell::Cell(const QVariant &data, 
    CellType type, 
    const Format &format, 
    Worksheet *parent,
    qint32 styleIndex ) :
    d_ptr(new CellPrivate(this))
{
    d_ptr->value = data;
    d_ptr->cellType = type;
    d_ptr->format = format;
    d_ptr->parent = parent;
    d_ptr->styleNumber = styleIndex; 
}
 
/*!
 * \internal
 */
Cell::Cell(const Cell * const cell):
    d_ptr(new CellPrivate(cell->d_ptr))
{
    d_ptr->q_ptr = this;
}
 
/*!
 * Destroys the Cell and cleans up.
 */
Cell::~Cell()
{
    if ( NULL != d_ptr )
        delete d_ptr;
}
 
/*!
 * Return the dataType of this Cell
 */
Cell::CellType Cell::cellType() const
{
    Q_D(const Cell);
 
    return d->cellType;
}
 
/*!
 * Return the data content of this Cell
 */
QVariant Cell::value() const
{
    Q_D(const Cell); 
 
    return d->value; 
}
 
/*!
* Return the data content of this Cell for reading 
*/
QVariant Cell::readValue() const
{
    Q_D(const Cell);
 
    QVariant ret; // return value 
    ret = d->value;
 
    Format fmt = this->format();
 
    if (isDateTime())
    {
        QVariant vDT = dateTime();
        if ( vDT.isNull() )
        {
            return QVariant();
        }
 
        // https://github.com/QtExcel/QXlsx/issues/171
        // https://www.qt.io/blog/whats-new-in-qmetatype-qvariant
        #if QT_VERSION >= 0x060000 // Qt 6.0 or over
                if ( vDT.metaType().id() == QMetaType::QDateTime )
                {
                    ret = vDT;
                }
                else if ( vDT.metaType().id() == QMetaType::QDate )
                {
                    ret = vDT;
                }
                else if ( vDT.metaType().id() == QMetaType::QTime )
                {
                    ret = vDT;
                }
                else
                {
                    return QVariant();
                }
        #else
                if ( vDT.type() == QVariant::DateTime )
                {
                    ret = vDT;
                }
                else if ( vDT.type() == QVariant::Date )
                {
                    ret = vDT;
                }
                else if ( vDT.type() == QVariant::Time )
                {
                    ret = vDT;
                }
                else
                {
                    return QVariant();
                }
        #endif
 
        // QDateTime dt = dateTime();
        // ret = dt;
        
        // QString strFormat = fmt.numberFormat();
        // if (!strFormat.isEmpty())
        // {
        //     // TODO: use number format
        // }
 
        // qint32 styleNo = d->styleNumber;
 
        // if (styleNo == 10)
        // {
        // }
 
        // if (styleNo == 11)
        // {
            // QTime timeValue = dt.time(); // only time. (HH:mm:ss) 
            // ret = timeValue;
            // return ret;
        // }
 
        // if (styleNo == 12)
        // {
        // }
 
        // if (styleNo == 13) // (HH:mm:ss)
        // {
            // double dValue = d->value.toDouble();
            // int day = int(dValue); // unit is day.
            // double deciamlPointValue1 = dValue - double(day);
 
            // double dHour = deciamlPointValue1 * (double(1.0) / double(24.0));
            // int hour = int(dHour);
 
            // double deciamlPointValue2 = deciamlPointValue1 - (double(hour) * (double(1.0) / double(24.0)));
            // double dMin = deciamlPointValue2 * (double(1.0) / double(60.0));
            // int min = int(dMin);
 
            // double deciamlPointValue3 = deciamlPointValue2 - (double(min) * (double(1.0) / double(60.0)));
            // double dSec = deciamlPointValue3 * (double(1.0) / double(60.0));
            // int sec = int(dSec);
    
            // int totalHour = hour + (day * 24);
 
            // QString strTime;
            // strTime = QString("%1:%2:%3").arg(totalHour).arg(min).arg(sec);
            // ret = strTime;
 
            // return ret;
        // }
 
        // return ret;
        // */
    }
 
    if (hasFormula())
    {
        QString formulaString = this->formula().formulaText();
        ret = formulaString;
        return ret; // return formula string 
    }
 
    return ret;
}
 
/*!
 * Return the style used by this Cell. If no style used, 0 will be returned.
 */
Format Cell::format() const
{
    Q_D(const Cell);
 
    return d->format;
}
 
/*!
 * Returns true if the cell has one formula.
 */
bool Cell::hasFormula() const
{
    Q_D(const Cell);
 
    return d->formula.isValid();
}
 
/*!
 * Return the formula contents if the dataType is Formula
 */
CellFormula Cell::formula() const
{
    Q_D(const Cell);
 
    return d->formula;
}
 
/*!
 * Returns whether the value is probably a dateTime or not
 */
bool Cell::isDateTime() const
{
    Q_D(const Cell);
 
    Cell::CellType cellType = d->cellType;
    double dValue = d->value.toDouble(); // number
//    QString strValue = d->value.toString().toUtf8();
    bool isValidFormat = d->format.isValid();
    bool isDateTimeFormat = d->format.isDateTimeFormat(); // datetime format
 
    // dev67
    if ( cellType == NumberType ||
         cellType == DateType ||
         cellType == CustomType )
    {
        if ( dValue >= 0 &&
             isValidFormat &&
             isDateTimeFormat )
        {
            return true;
        }
    }
 
    return false;
}
 
/*!
 * Return the data time value.
 */
/*
QDateTime Cell::dateTime() const
{
    Q_D(const Cell);
 
    if (!isDateTime())
        return QDateTime();
 
    return datetimeFromNumber(d->value.toDouble(), d->parent->workbook()->isDate1904());
}
*/
QVariant Cell::dateTime() const
{
    Q_D(const Cell);
 
    if (!isDateTime())
    {
        return QVariant();
    }
 
    // dev57
 
    QVariant ret;
    double dValue = d->value.toDouble();
    bool isDate1904 = d->parent->workbook()->isDate1904();
    ret = datetimeFromNumber(dValue, isDate1904);
    return ret;
}
 
/*!
 * Returns whether the cell is probably a rich string or not
 */
bool Cell::isRichString() const
{
    Q_D(const Cell);
 
    if ( d->cellType != SharedStringType &&
            d->cellType != InlineStringType &&
            d->cellType != StringType )
    {
        return false;
    }
 
    return d->richString.isRichString();
}
 
qint32 Cell::styleNumber() const 
{
    Q_D(const Cell);
 
    qint32 ret = d->styleNumber;
    return ret; 
}
 
bool Cell::isDateType(CellType cellType, const Format &format)
{
    if ( cellType == NumberType ||
         cellType == DateType ||
         cellType == CustomType )
    {
        return format.isValid() && format.isDateTimeFormat();
    }
    return false;
}
 
QT_END_NAMESPACE_XLSX