wumu
2025-04-27 20ffcfb5507daf34f81346ca3dfa4c031e7b2fe3
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// xlsxcellformula.cpp
 
#include <QtGlobal>
#include <QObject>
#include <QString>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QDebug>
 
#include "xlsxcellformula.h"
#include "xlsxcellformula_p.h"
#include "xlsxutility_p.h"
 
QT_BEGIN_NAMESPACE_XLSX
 
CellFormulaPrivate::CellFormulaPrivate(const QString &formula_, const CellRange &ref_, CellFormula::FormulaType type_)
    :formula(formula_), type(type_), reference(ref_), ca(false), si(0)
{
    //Remove the formula '=' sign if exists
    if (formula.startsWith(QLatin1String("=")))
        formula.remove(0,1);
    else if (formula.startsWith(QLatin1String("{=")) && formula.endsWith(QLatin1String("}")))
        formula = formula.mid(2, formula.length()-3);
}
 
CellFormulaPrivate::CellFormulaPrivate(const CellFormulaPrivate &other)
    : QSharedData(other)
    , formula(other.formula), type(other.type), reference(other.reference)
    , ca(other.ca), si(other.si)
{
 
}
 
CellFormulaPrivate::~CellFormulaPrivate()
{
 
}
 
/*!
  \class CellFormula
  \inmodule QtXlsx
  \brief The CellFormula class provides a API that is used to handle the cell formula.
 
*/
 
/*!
  \enum CellFormula::FormulaType
  \value NormalType
  \value ArrayType
  \value DataTableType
  \value SharedType
*/
 
/*!
 *  Creates a new formula.
 */
CellFormula::CellFormula()
{
    //The d pointer is initialized with a null pointer
}
 
/*!
 *  Creates a new formula with the given \a formula and \a type.
 */
CellFormula::CellFormula(const char *formula, FormulaType type)
    :d(new CellFormulaPrivate(QString::fromLatin1(formula), CellRange(), type))
{
 
}
 
/*!
 *  Creates a new formula with the given \a formula and \a type.
 */
CellFormula::CellFormula(const QString &formula, FormulaType type)
    :d(new CellFormulaPrivate(formula, CellRange(), type))
{
 
}
 
/*!
 *  Creates a new formula with the given \a formula, \a ref and \a type.
 */
CellFormula::CellFormula(const QString &formula, const CellRange &ref, FormulaType type)
    :d(new CellFormulaPrivate(formula, ref, type))
{
 
}
 
/*!
   Creates a new formula with the same attributes as the \a other formula.
 */
CellFormula::CellFormula(const CellFormula &other)
    :d(other.d)
{
}
 
/*!
   Assigns the \a other formula to this formula, and returns a
   reference to this formula.
 */
CellFormula &CellFormula::operator =(const CellFormula &other)
{
    d = other.d;
    return *this;
}
 
/*!
 * Destroys this formula.
 */
CellFormula::~CellFormula()
{
 
}
 
/*!
 * Returns the type of the formula.
 */
CellFormula::FormulaType CellFormula::formulaType() const
{
    return d ? d->type : NormalType;
}
 
/*!
 * Returns the contents of the formula.
 */
QString CellFormula::formulaText() const
{
    return d ? d->formula : QString();
}
 
/*!
 * Returns the reference cells of the formula. For normal formula,
 * this will return an invalid CellRange object.
 */
CellRange CellFormula::reference() const
{
    return d ? d->reference : CellRange();
}
 
/*!
 * Returns whether the formula is valid.
 */
bool CellFormula::isValid() const
{
    return d;
}
 
/*!
 * Returns the shared index for shared formula.
 */
int CellFormula::sharedIndex() const
{
    return d && d->type == SharedType ? d->si : (-1);
}
 
/* aca (Always Calculate Array) // not-implmented attribute
 *
 * Only applies to array formulas.
 *
 * true indicates that the entire array shall be calculated in full.
 * If false the individual cells of the array shall be calculated as needed.
 *
 * The aca value shall be ignored unless the value of the corresponding
 *  t attribute is array.
 *
 *  [Note: The primary case where an array formula must be calculated in
 * part instead of in full is when some cells in the array depend on other
 * cells that are semi-calculated, e.g., contains the function =(). end note]
 *
 *  The possible values for this attribute are defined by the W3C XML Schema
 *  boolean datatype.
 */
 
/* bx (Assigns Value to Name) // not-implmented attribute
 *
 * Specifies that this formula assigns a value to a name.
 *
 * The possible values for this attribute are defined by the W3C XML
 * Schema boolean datatype.
 */
 
/* del1 (Input 1 Deleted) // not-implmented attribute
 *
 * Whether the first input cell for data table has been deleted.
 * Applies to data table formula only. Written on master cell of data table
 * formula only.
 *
 * The possible values for this attribute are defined by the W3C XML Schema
 * boolean datatype.
*/
 
/* del2 (Input 2 Deleted) // not-impplmented attribute
 *
 * Whether the second input cell for data table has been deleted.
 * Applies to data table formula only. Written on master cell of data
 * table formula only.
 *
 * The possible values for this attribute are defined by the W3C XML Schema
 * boolean datatype.
 */
 
/* dt2D (Data Table 2-D) // not-implmented attribute
 *
 * Data table is two-dimentional. Only applies to the data tables function.
 * Written on master cell of data table formula only.
 *
 * The possible values for this attribute are defined by the W3C XML Schema
 * boolean datatype.
 */
 
/* dtr (Data Table Row) // not-implmented attribute
 *
 * true if one-dimentional data table is a row, otherwise it's a column.
 * Only applies to the data tables function. Written on master cell of data
 * table formula only.
 *
 * The possible values for this attribute are defined by the W3C XML Schema
 *  boolean datatype.
 */
 
/* r1 (Data Table Cell 1) // not-implmented attribute
 *
 * First input cell for data table. Only applies to the data tables array
 * function "TABLE()". Written on master cell of data table formula only.
 *
 * The possible values for this attribute are defined by the ST_CellRef
 * simple type (§18.18.7).
 */
 
/* r2 (Input Cell 2) // not-implmented attribute
 *
 * Second input cell for data table when dt2D is '1'. Only applies to the
 * data tables array function "TABLE()".Written on master cell of data table
 * formula only.
 *
 * The possible values for this attribute are defined by the ST_CellRef
 * simple type (§18.18.7).
 */
 
/*!
 * \internal
 * \remark pair with loadFromXml()
 */
bool CellFormula::saveToXml(QXmlStreamWriter &writer) const
{
 
    // t (Formula Type)
    //
    // Type of formula.
    // The possible values for this attribute are defined by the
    // ST_CellFormulaType simple type (§18.18.6).
    //
    // 18.18.6 ST_CellFormulaType (Formula Type)
    // array (Array Formula)
    // dataTable (Table Formula)
    // normal (Normal)
    // shared (Shared Formula)
 
    QString t;
    switch (d->type)
    {
    case CellFormula::ArrayType:
        t = QStringLiteral("array");
        break;
    case CellFormula::SharedType:
        t = QStringLiteral("shared");
        break;
    case CellFormula::NormalType:
        t = QStringLiteral("normal");
        break;
    case CellFormula::DataTableType:
        t = QStringLiteral("dataTable");
        break;
    default: // undefined type
        return false;
        break;
    }
 
    // f (Formula)
    //
    // Formula for the cell. The formula expression is contained in the
    // character node of this element.
    writer.writeStartElement(QStringLiteral("f"));
 
    if (!t.isEmpty())
    {
        writer.writeAttribute(QStringLiteral("t"), t); // write type(t)
    }
 
    // ref (Range of Cells)
    //
    // Range of cells which the formula applies to.
    // Only required for shared formula, array formula or data table.
    // Only written on the master formula,
    // not subsequent formulas belonging to the same shared group, array,
    // or data table.
    // The possible values for this attribute are defined by the ST_Ref
    // simple type (§18.18.62).
 
    if ( d->type == CellFormula::SharedType ||
         d->type == CellFormula::ArrayType ||
         d->type == CellFormula::DataTableType )
    {
        if (d->reference.isValid())
        {
            writer.writeAttribute(QStringLiteral("ref"), d->reference.toString());
        }
    }
 
    // ca (Calculate Cell)
    //
    // Indicates that this formula needs to be recalculated the next time
    // calculation is performed. [Example: This is always set on volatile
    // functions, like =(), and circular references. end example]
    // The possible values for this attribute are defined by the W3C XML
    // Schema boolean datatype.
    //
    // 3.2.2 boolean
    // 3.2.2.1 Lexical representation
    // An instance of a datatype that is defined as ·boolean· can have the
    // following legal literals {true, false, 1, 0}.
 
    if (d->ca)
    {
        writer.writeAttribute(QStringLiteral("ca"), QStringLiteral("1"));
    }
 
    // si (Shared Group Index)
    // Optional attribute to optimize load performance by sharing formulas.
    //
    // When a formula is a shared formula (t value is shared) then this value
    // indicates the group to which this particular cell's formula belongs. The
    // first formula in a group of shared formulas is saved in the f element.
    // This is considered the 'master' formula cell. Subsequent cells sharing
    // this formula need not have the formula written in their f element.
    // Instead, the attribute si value for a particular cell is used to figure
    // what the formula expression should be based on the cell's relative
    // location to the master formula cell.
 
    if (d->type == CellFormula::SharedType)
    {
        int si = d->si;
        writer.writeAttribute(QStringLiteral("si"), QString::number(si));
    }
 
    if (!d->formula.isEmpty())
    {
        QString strFormula = d->formula;
        writer.writeCharacters(strFormula); // write formula
    }
 
    writer.writeEndElement(); // f
 
    return true;
}
 
/*!
 * \internal
 * \remark pair with saveToXml()
 */
bool CellFormula::loadFromXml(QXmlStreamReader &reader)
{
    Q_ASSERT(reader.name() == QLatin1String("f"));
    if (!d)
        d = new CellFormulaPrivate(QString(), CellRange(), NormalType);
 
    QXmlStreamAttributes attributes = reader.attributes();
    QString typeString = attributes.value(QLatin1String("t")).toString();
 
    // branch: shared-formula
    //
    if (typeString == QLatin1String("array")) {
        d->type = ArrayType;
    }
    else if (typeString == QLatin1String("shared")) {
        d->type = SharedType;
    }
    else if (typeString == QLatin1String("normal")) {
        d->type = NormalType;
    }
    else if (typeString == QLatin1String("dataTable")) {
        d->type = DataTableType;
    }
    else {
        /*
        // undefined type
        // qDebug() << "Undefined type" << typeString;
        return false;
        // */
 
        // dev40 {{
        // https://github.com/QtExcel/QXlsx/issues/38
        d->type = NormalType; // Change: normal Type is not mentioned in the xml file!!!!!
        // }}
    }
 
    // branch: shared-formula
    //
    // ref (Range of Cells)
    // Range of cells which the formula applies to.
    // Only required for shared formula, array formula or data table.
    if ( d->type == CellFormula::SharedType ||
         d->type == CellFormula::ArrayType ||
         d->type == CellFormula::DataTableType )
    {
        if (attributes.hasAttribute(QLatin1String("ref")))
        {
            QString refString = attributes.value(QLatin1String("ref")).toString();
            d->reference = CellRange(refString);
        }
    }
 
    // branch: shared-formula
    //
    // si (Shared Group Index)
    // Optional attribute to optimize load performance by sharing formulas.
    // When a formula is a shared formula (t value is shared) then this value
    // indicates the group to which this particular cell's formula belongs.
    if ( d->type == CellFormula::SharedType )
    {
        QString ca = attributes.value(QLatin1String("si")).toString();
        d->ca = parseXsdBoolean(ca, false);
 
        if (attributes.hasAttribute(QLatin1String("si")))
        {
            d->si = attributes.value(QLatin1String("si")).toInt();
        }
    }
 
    d->formula = reader.readElementText(); // read formula
 
    return true;
}
 
/*!
 * \internal
 */
bool CellFormula::operator ==(const CellFormula &formula) const
{
    return d->formula == formula.d->formula && d->type == formula.d->type
            && d->si ==formula.d->si;
}
 
/*!
 * \internal
 */
bool CellFormula::operator !=(const CellFormula &formula) const
{
    return d->formula != formula.d->formula || d->type != formula.d->type
            || d->si !=formula.d->si;
}
 
QT_END_NAMESPACE_XLSX