Debao Zhang
10 years ago
13 changed files with 555 additions and 262 deletions
@ -0,0 +1,249 @@ |
|||
/****************************************************************************
|
|||
** 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. |
|||
** |
|||
****************************************************************************/ |
|||
#include "xlsxcellformula.h" |
|||
#include "xlsxcellformula_p.h" |
|||
|
|||
#include <QXmlStreamReader> |
|||
#include <QXmlStreamWriter> |
|||
|
|||
QT_BEGIN_NAMESPACE_XLSX |
|||
|
|||
CellFormulaPrivate::CellFormulaPrivate(const QString &formula_, const CellRange &ref_, CellFormula::FormulaType type_) |
|||
:formula(formula_), type(type_), reference(ref_), 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), 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::formulaContent() 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; |
|||
} |
|||
|
|||
/*!
|
|||
* \internal |
|||
*/ |
|||
bool CellFormula::saveToXml(QXmlStreamWriter &writer) const |
|||
{ |
|||
writer.writeStartElement(QStringLiteral("f")); |
|||
QString t; |
|||
switch (d->type) { |
|||
case CellFormula::ArrayType: |
|||
t = QStringLiteral("array"); |
|||
break; |
|||
case CellFormula::SharedType: |
|||
t = QStringLiteral("shared"); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
if (!t.isEmpty()) |
|||
writer.writeAttribute(QStringLiteral("t"), t); |
|||
if (d->reference.isValid()) |
|||
writer.writeAttribute(QStringLiteral("ref"), d->reference.toString()); |
|||
if (d->type == CellFormula::SharedType) |
|||
writer.writeAttribute(QStringLiteral("si"), QString::number(d->si)); |
|||
|
|||
writer.writeCharacters(d->formula); |
|||
writer.writeEndElement(); //f
|
|||
|
|||
return true; |
|||
} |
|||
|
|||
/*!
|
|||
* \internal |
|||
*/ |
|||
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(); |
|||
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); |
|||
} else if (attributes.hasAttribute(QLatin1String("si"))) { |
|||
d->si = attributes.value(QLatin1String("si")).toString().toInt(); |
|||
} |
|||
|
|||
d->formula = reader.readElementText(); |
|||
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 |
@ -0,0 +1,74 @@ |
|||
/****************************************************************************
|
|||
** 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. |
|||
** |
|||
****************************************************************************/ |
|||
#ifndef QXLSX_XLSXCELLFORMULA_H |
|||
#define QXLSX_XLSXCELLFORMULA_H |
|||
|
|||
#include "xlsxglobal.h" |
|||
#include <QExplicitlySharedDataPointer> |
|||
|
|||
class QXmlStreamWriter; |
|||
class QXmlStreamReader; |
|||
|
|||
QT_BEGIN_NAMESPACE_XLSX |
|||
|
|||
class CellFormulaPrivate; |
|||
class CellRange; |
|||
|
|||
class Q_XLSX_EXPORT CellFormula |
|||
{ |
|||
public: |
|||
enum FormulaType { |
|||
NormalType, |
|||
ArrayType, |
|||
DataTableType, |
|||
SharedType |
|||
}; |
|||
|
|||
CellFormula(); |
|||
CellFormula(const char *formula, FormulaType type=NormalType); |
|||
CellFormula(const QString &formula, FormulaType type=NormalType); |
|||
CellFormula(const QString &formula, const CellRange &ref, FormulaType type); |
|||
CellFormula(const CellFormula &other); |
|||
~CellFormula(); |
|||
CellFormula &operator =(const CellFormula &other); |
|||
bool isValid() const; |
|||
|
|||
FormulaType formulaType() const; |
|||
QString formulaContent() const; |
|||
CellRange reference() const; |
|||
int sharedIndex() const; |
|||
|
|||
bool operator == (const CellFormula &formula) const; |
|||
bool operator != (const CellFormula &formula) const; |
|||
|
|||
bool saveToXml(QXmlStreamWriter &writer) const; |
|||
bool loadFromXml(QXmlStreamReader &reader); |
|||
private: |
|||
QExplicitlySharedDataPointer<CellFormulaPrivate> d; |
|||
}; |
|||
|
|||
QT_END_NAMESPACE_XLSX |
|||
|
|||
#endif // QXLSX_XLSXCELLFORMULA_H
|
@ -0,0 +1,63 @@ |
|||
/****************************************************************************
|
|||
** 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. |
|||
** |
|||
****************************************************************************/ |
|||
#ifndef XLSXCELLFORMULA_P_H |
|||
#define XLSXCELLFORMULA_P_H |
|||
|
|||
//
|
|||
// W A R N I N G
|
|||
// -------------
|
|||
//
|
|||
// This file is not part of the Qt Xlsx API. It exists for the convenience
|
|||
// of the Qt Xlsx. This header file may change from
|
|||
// version to version without notice, or even be removed.
|
|||
//
|
|||
// We mean it.
|
|||
//
|
|||
|
|||
#include "xlsxglobal.h" |
|||
#include "xlsxcellformula.h" |
|||
#include "xlsxcellrange.h" |
|||
|
|||
#include <QSharedData> |
|||
#include <QString> |
|||
|
|||
QT_BEGIN_NAMESPACE_XLSX |
|||
|
|||
class CellFormulaPrivate : public QSharedData |
|||
{ |
|||
public: |
|||
CellFormulaPrivate(const QString &formula, const CellRange &reference, CellFormula::FormulaType type); |
|||
CellFormulaPrivate(const CellFormulaPrivate &other); |
|||
~CellFormulaPrivate(); |
|||
|
|||
QString formula; //formula contents
|
|||
CellFormula::FormulaType type; |
|||
CellRange reference; |
|||
int si; |
|||
}; |
|||
|
|||
QT_END_NAMESPACE_XLSX |
|||
|
|||
#endif // XLSXCELLFORMULA_P_H
|
Loading…
Reference in new issue