123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- #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)
- {
-
- 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()
- {
- }
- CellFormula::CellFormula()
- {
-
- }
- CellFormula::CellFormula(const char *formula, FormulaType type)
- :d(new CellFormulaPrivate(QString::fromLatin1(formula), CellRange(), type))
- {
- }
- CellFormula::CellFormula(const QString &formula, FormulaType type)
- :d(new CellFormulaPrivate(formula, CellRange(), type))
- {
- }
- CellFormula::CellFormula(const QString &formula, const CellRange &ref, FormulaType type)
- :d(new CellFormulaPrivate(formula, ref, type))
- {
- }
- CellFormula::CellFormula(const CellFormula &other)
- :d(other.d)
- {
- }
- CellFormula &CellFormula::operator =(const CellFormula &other)
- {
- d = other.d;
- return *this;
- }
- CellFormula::~CellFormula()
- {
- }
- CellFormula::FormulaType CellFormula::formulaType() const
- {
- return d ? d->type : NormalType;
- }
- QString CellFormula::formulaText() const
- {
- return d ? d->formula : QString();
- }
- CellRange CellFormula::reference() const
- {
- return d ? d->reference : CellRange();
- }
- bool CellFormula::isValid() const
- {
- return d;
- }
- int CellFormula::sharedIndex() const
- {
- return d && d->type == SharedType ? d->si : (-1);
- }
- bool CellFormula::saveToXml(QXmlStreamWriter &writer) const
- {
-
-
-
-
-
-
-
-
-
-
-
- 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:
- return false;
- break;
- }
-
-
-
-
- writer.writeStartElement(QStringLiteral("f"));
- if (!t.isEmpty())
- {
- writer.writeAttribute(QStringLiteral("t"), t);
- }
-
-
-
-
-
-
-
-
-
- if ( d->type == CellFormula::SharedType ||
- d->type == CellFormula::ArrayType ||
- d->type == CellFormula::DataTableType )
- {
- 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)
- {
- int si = d->si;
- writer.writeAttribute(QStringLiteral("si"), QString::number(si));
- }
- if (!d->formula.isEmpty())
- {
- QString strFormula = d->formula;
- writer.writeCharacters(strFormula);
- }
- writer.writeEndElement();
- return true;
- }
- 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 if (typeString == QLatin1String("normal")) {
- d->type = NormalType;
- }
- else if (typeString == QLatin1String("dataTable")) {
- d->type = DataTableType;
- }
- else {
-
-
-
- d->type = NormalType;
-
- }
-
-
-
-
-
- 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 ( 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;
- }
- bool CellFormula::operator ==(const CellFormula &formula) const
- {
- return d->formula == formula.d->formula && d->type == formula.d->type
- && d->si ==formula.d->si;
- }
- 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
|