| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- /****************************************************************************
- ** 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 "xlsxutility_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_), 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;
- }
- /*!
- * \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->ca)
- writer.writeAttribute(QStringLiteral("ca"), QStringLiteral("1"));
- if (d->type == CellFormula::SharedType)
- writer.writeAttribute(QStringLiteral("si"), QString::number(d->si));
- if (!d->formula.isEmpty())
- 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);
- }
- 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;
- }
- /*!
- * \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
|