1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-01-16 04:42:53 +08:00

testing formula

This commit is contained in:
Jay Two 2019-01-30 12:18:27 +09:00
parent 927a223daf
commit d72094f087

View File

@ -1,27 +1,5 @@
/****************************************************************************
** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
// xlsxcellformula.cpp
#include "xlsxcellformula.h"
#include "xlsxcellformula_p.h"
#include "xlsxutility_p.h"
@ -171,57 +149,203 @@ 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
{
writer.writeStartElement(QStringLiteral("f"));
// 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) {
switch (d->type)
{
case CellFormula::ArrayType:
t = QStringLiteral("array");
break;
case CellFormula::SharedType:
t = QStringLiteral("shared");
break;
default:
// branch: shared-formula {{
case CellFormula::NormalType:
t = QStringLiteral("normal");
break;
case CellFormula::DataTableType:
t = QStringLiteral("dataTable");
break;
// }}
default: // ERROR: 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);
if (d->reference.isValid())
writer.writeAttribute(QStringLiteral("ref"), d->reference.toString());
// 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"));
// 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. 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)
{
if (d->formula.isEmpty())
d->si = 0;
else
d->si = 1;
writer.writeAttribute(QStringLiteral("si"), QString::number(d->si));
writer.writeAttribute(QStringLiteral("si"), QString::number(d->si));
}
// }}
if (!d->formula.isEmpty())
writer.writeCharacters(d->formula);
writer.writeEndElement(); //f
writer.writeEndElement(); // f
return true;
}
/*!
* \internal
* \remark pair with saveToXml()
*/
bool CellFormula::loadFromXml(QXmlStreamReader &reader)
{
@ -231,25 +355,59 @@ bool CellFormula::loadFromXml(QXmlStreamReader &reader)
QXmlStreamAttributes attributes = reader.attributes();
QString typeString = attributes.value(QLatin1String("t")).toString();
if (typeString == QLatin1String("array"))
d->type = ArrayType;
else if (typeString == QLatin1String("shared"))
d->type = SharedType;
else
d->type = NormalType;
if (attributes.hasAttribute(QLatin1String("ref"))) {
QString refString = attributes.value(QLatin1String("ref")).toString();
d->reference = CellRange(refString);
// 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;
}
QString ca = attributes.value(QLatin1String("si")).toString();
d->ca = parseXsdBoolean(ca, false);
// 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);
}
}
if (attributes.hasAttribute(QLatin1String("si")))
d->si = attributes.value(QLatin1String("si")).toString().toInt();
// 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")).toString().toInt();
}
d->formula = reader.readElementText();
return true;
}