Add QDateTime support for QXlsx::Cell
This commit is contained in:
+26
-1
@@ -24,6 +24,11 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
#include "xlsxcell.h"
|
#include "xlsxcell.h"
|
||||||
#include "xlsxcell_p.h"
|
#include "xlsxcell_p.h"
|
||||||
|
#include "xlsxformat.h"
|
||||||
|
#include "xlsxutility_p.h"
|
||||||
|
#include "xlsxworksheet.h"
|
||||||
|
#include "xlsxworkbook.h"
|
||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE_XLSX
|
QT_BEGIN_NAMESPACE_XLSX
|
||||||
|
|
||||||
@@ -44,12 +49,13 @@ CellPrivate::CellPrivate(Cell *p) :
|
|||||||
* \internal
|
* \internal
|
||||||
* Created by Worksheet only.
|
* Created by Worksheet only.
|
||||||
*/
|
*/
|
||||||
Cell::Cell(const QVariant &data, DataType type, Format *format) :
|
Cell::Cell(const QVariant &data, DataType type, Format *format, Worksheet *parent) :
|
||||||
d_ptr(new CellPrivate(this))
|
d_ptr(new CellPrivate(this))
|
||||||
{
|
{
|
||||||
d_ptr->value = data;
|
d_ptr->value = data;
|
||||||
d_ptr->dataType = type;
|
d_ptr->dataType = type;
|
||||||
d_ptr->format = format;
|
d_ptr->format = format;
|
||||||
|
d_ptr->parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -88,4 +94,23 @@ QString Cell::formula() const
|
|||||||
return d->formula;
|
return d->formula;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns whether the value is probably a dateTime or not
|
||||||
|
*/
|
||||||
|
bool Cell::isDateTime() const
|
||||||
|
{
|
||||||
|
Q_D(const Cell);
|
||||||
|
if (d->dataType == Numeric && d->format && d->format->isDateTimeFormat())
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDateTime Cell::dateTime() const
|
||||||
|
{
|
||||||
|
Q_D(const Cell);
|
||||||
|
if (!isDateTime())
|
||||||
|
return QDateTime();
|
||||||
|
return datetimeFromNumber(d->value.toDouble(), d->parent->workbook()->isDate1904());
|
||||||
|
}
|
||||||
|
|
||||||
QT_END_NAMESPACE_XLSX
|
QT_END_NAMESPACE_XLSX
|
||||||
|
|||||||
+4
-1
@@ -54,11 +54,14 @@ public:
|
|||||||
Format * format() const;
|
Format * format() const;
|
||||||
QString formula() const;
|
QString formula() const;
|
||||||
|
|
||||||
|
bool isDateTime() const;
|
||||||
|
QDateTime dateTime() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Worksheet;
|
friend class Worksheet;
|
||||||
friend class WorksheetPrivate;
|
friend class WorksheetPrivate;
|
||||||
|
|
||||||
Cell(const QVariant &data=QVariant(), DataType type=Blank, Format *format=0);
|
Cell(const QVariant &data=QVariant(), DataType type=Blank, Format *format=0, Worksheet *parent=0);
|
||||||
|
|
||||||
CellPrivate * const d_ptr;
|
CellPrivate * const d_ptr;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ public:
|
|||||||
Cell::DataType dataType;
|
Cell::DataType dataType;
|
||||||
Format *format;
|
Format *format;
|
||||||
|
|
||||||
|
Worksheet *parent;
|
||||||
Cell *q_ptr;
|
Cell *q_ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#include "xlsxformat.h"
|
#include "xlsxformat.h"
|
||||||
#include "xlsxformat_p.h"
|
#include "xlsxformat_p.h"
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
|
#include <QRegularExpression>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE_XLSX
|
QT_BEGIN_NAMESPACE_XLSX
|
||||||
@@ -87,6 +88,7 @@ void Format::setNumberFormatIndex(int format)
|
|||||||
Q_D(Format);
|
Q_D(Format);
|
||||||
d->dirty = true;
|
d->dirty = true;
|
||||||
d->numberData.formatIndex = format;
|
d->numberData.formatIndex = format;
|
||||||
|
d->numberData._valid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -102,15 +104,39 @@ QString Format::numberFormat() const
|
|||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Set number \a format.
|
* Set number \a format.
|
||||||
|
* http://office.microsoft.com/en-001/excel-help/create-a-custom-number-format-HP010342372.aspx
|
||||||
*/
|
*/
|
||||||
void Format::setNumberFormat(const QString &format)
|
void Format::setNumberFormat(const QString &format)
|
||||||
{
|
{
|
||||||
Q_D(Format);
|
Q_D(Format);
|
||||||
|
if (format.isEmpty())
|
||||||
|
return;
|
||||||
d->dirty = true;
|
d->dirty = true;
|
||||||
d->numberData.formatString = format;
|
d->numberData.formatString = format;
|
||||||
d->numberData._valid = false; //formatIndex must be re-generated
|
d->numberData._valid = false; //formatIndex must be re-generated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns whether the number format is probably a dateTime or not
|
||||||
|
*/
|
||||||
|
bool Format::isDateTimeFormat() const
|
||||||
|
{
|
||||||
|
Q_D(const Format);
|
||||||
|
if (d->numberData._valid && d->numberData.formatString.isEmpty()) {
|
||||||
|
int idx = d->numberData.formatIndex;
|
||||||
|
//Built in date time number index
|
||||||
|
if ((idx >= 15 && idx <= 22) || (idx >= 45 && idx <= 47))
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
//Gauss from the number string
|
||||||
|
QString formatCode = d->numberData.formatString;
|
||||||
|
formatCode.remove(QRegularExpression(QStringLiteral("\\[(Green|White|Blue|Magenta|Yellow|Cyan|Red)\\]")));
|
||||||
|
if (formatCode.contains(QRegularExpression(QStringLiteral("[dmhys]"))))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \internal
|
* \internal
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -31,6 +31,8 @@
|
|||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
|
||||||
|
class FormatTest;
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE_XLSX
|
QT_BEGIN_NAMESPACE_XLSX
|
||||||
|
|
||||||
class Styles;
|
class Styles;
|
||||||
@@ -133,6 +135,7 @@ public:
|
|||||||
void setNumberFormatIndex(int format);
|
void setNumberFormatIndex(int format);
|
||||||
QString numberFormat() const;
|
QString numberFormat() const;
|
||||||
void setNumberFormat(const QString &format);
|
void setNumberFormat(const QString &format);
|
||||||
|
bool isDateTimeFormat() const;
|
||||||
|
|
||||||
int fontSize() const;
|
int fontSize() const;
|
||||||
void setFontSize(int size);
|
void setFontSize(int size);
|
||||||
@@ -210,6 +213,8 @@ private:
|
|||||||
friend class Styles;
|
friend class Styles;
|
||||||
friend class Worksheet;
|
friend class Worksheet;
|
||||||
friend class WorksheetPrivate;
|
friend class WorksheetPrivate;
|
||||||
|
friend class ::FormatTest;
|
||||||
|
|
||||||
Format();
|
Format();
|
||||||
|
|
||||||
bool numFmtIndexValid() const;
|
bool numFmtIndexValid() const;
|
||||||
|
|||||||
+29
-13
@@ -378,7 +378,7 @@ int Worksheet::writeString(int row, int column, const QString &value, Format *fo
|
|||||||
|
|
||||||
d->sharedStrings()->addSharedString(content);
|
d->sharedStrings()->addSharedString(content);
|
||||||
|
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(content, Cell::String, format));
|
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(content, Cell::String, format, this));
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@@ -396,7 +396,7 @@ int Worksheet::writeInlineString(int row, int column, const QString &value, Form
|
|||||||
error = -2;
|
error = -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::InlineString, format));
|
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::InlineString, format, this));
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@@ -407,7 +407,7 @@ int Worksheet::writeNumeric(int row, int column, double value, Format *format)
|
|||||||
if (d->checkDimensions(row, column))
|
if (d->checkDimensions(row, column))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, format));
|
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, format, this));
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -424,7 +424,7 @@ int Worksheet::writeFormula(int row, int column, const QString &content, Format
|
|||||||
if (formula.startsWith(QLatin1String("=")))
|
if (formula.startsWith(QLatin1String("=")))
|
||||||
formula.remove(0,1);
|
formula.remove(0,1);
|
||||||
|
|
||||||
Cell *data = new Cell(result, Cell::Formula, format);
|
Cell *data = new Cell(result, Cell::Formula, format, this);
|
||||||
data->d_ptr->formula = formula;
|
data->d_ptr->formula = formula;
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(data);
|
d->cellTable[row][column] = QSharedPointer<Cell>(data);
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
@@ -438,7 +438,7 @@ int Worksheet::writeBlank(int row, int column, Format *format)
|
|||||||
if (d->checkDimensions(row, column))
|
if (d->checkDimensions(row, column))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(QVariant(), Cell::Blank, format));
|
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(QVariant(), Cell::Blank, format, this));
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -450,7 +450,7 @@ int Worksheet::writeBool(int row, int column, bool value, Format *format)
|
|||||||
if (d->checkDimensions(row, column))
|
if (d->checkDimensions(row, column))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Boolean, format));
|
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Boolean, format, this));
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -469,7 +469,7 @@ int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, Format *f
|
|||||||
|
|
||||||
double value = datetimeToNumber(dt, d->workbook->isDate1904());
|
double value = datetimeToNumber(dt, d->workbook->isDate1904());
|
||||||
|
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, format));
|
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(value, Cell::Numeric, format, this));
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -517,7 +517,7 @@ int Worksheet::writeHyperlink(int row, int column, const QUrl &url, Format *form
|
|||||||
|
|
||||||
//Write the hyperlink string as normal string.
|
//Write the hyperlink string as normal string.
|
||||||
d->sharedStrings()->addSharedString(displayString);
|
d->sharedStrings()->addSharedString(displayString);
|
||||||
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(displayString, Cell::String, format));
|
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(displayString, Cell::String, format, this));
|
||||||
d->workbook->styles()->addFormat(format);
|
d->workbook->styles()->addFormat(format);
|
||||||
|
|
||||||
//Store the hyperlink data in a separate table
|
//Store the hyperlink data in a separate table
|
||||||
@@ -1124,6 +1124,7 @@ QSharedPointer<Cell> WorksheetPrivate::readNumericCellData(XmlStreamReader &read
|
|||||||
|
|
||||||
void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
|
void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
|
||||||
{
|
{
|
||||||
|
Q_Q(Worksheet);
|
||||||
Q_ASSERT(reader.name() == QLatin1String("sheetData"));
|
Q_ASSERT(reader.name() == QLatin1String("sheetData"));
|
||||||
|
|
||||||
while(!(reader.name() == QLatin1String("sheetData") && reader.tokenType() == QXmlStreamReader::EndElement)) {
|
while(!(reader.name() == QLatin1String("sheetData") && reader.tokenType() == QXmlStreamReader::EndElement)) {
|
||||||
@@ -1162,6 +1163,8 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
|
|||||||
if (attributes.hasAttribute(QLatin1String("s"))) {
|
if (attributes.hasAttribute(QLatin1String("s"))) {
|
||||||
int idx = attributes.value(QLatin1String("s")).toInt();
|
int idx = attributes.value(QLatin1String("s")).toInt();
|
||||||
format = workbook->styles()->xfFormat(idx);
|
format = workbook->styles()->xfFormat(idx);
|
||||||
|
if (!format)
|
||||||
|
qDebug()<<"Invalid style index: "<<idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attributes.hasAttribute(QLatin1String("t"))) {
|
if (attributes.hasAttribute(QLatin1String("t"))) {
|
||||||
@@ -1174,7 +1177,7 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
|
|||||||
int sst_idx = reader.readElementText().toInt();
|
int sst_idx = reader.readElementText().toInt();
|
||||||
sharedStrings()->incRefByStringIndex(sst_idx);
|
sharedStrings()->incRefByStringIndex(sst_idx);
|
||||||
QString value = sharedStrings()->getSharedString(sst_idx);
|
QString value = sharedStrings()->getSharedString(sst_idx);
|
||||||
QSharedPointer<Cell> data(new Cell(value ,Cell::String, format));
|
QSharedPointer<Cell> data(new Cell(value ,Cell::String, format, q));
|
||||||
cellTable[pos.x()][pos.y()] = QSharedPointer<Cell>(data);
|
cellTable[pos.x()][pos.y()] = QSharedPointer<Cell>(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1185,7 +1188,7 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
|
|||||||
if (reader.tokenType() == QXmlStreamReader::StartElement) {
|
if (reader.tokenType() == QXmlStreamReader::StartElement) {
|
||||||
if (reader.name() == QLatin1String("t")) {
|
if (reader.name() == QLatin1String("t")) {
|
||||||
QString value = reader.readElementText();
|
QString value = reader.readElementText();
|
||||||
QSharedPointer<Cell> data(new Cell(value, Cell::InlineString, format));
|
QSharedPointer<Cell> data(new Cell(value, Cell::InlineString, format, q));
|
||||||
cellTable[pos.x()][pos.y()] = data;
|
cellTable[pos.x()][pos.y()] = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1195,13 +1198,14 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
|
|||||||
reader.readNextStartElement();
|
reader.readNextStartElement();
|
||||||
if (reader.name() == QLatin1String("v")) {
|
if (reader.name() == QLatin1String("v")) {
|
||||||
QString value = reader.readElementText();
|
QString value = reader.readElementText();
|
||||||
QSharedPointer<Cell> data(new Cell(value.toInt() ? true : false, Cell::Boolean, format));
|
QSharedPointer<Cell> data(new Cell(value.toInt() ? true : false, Cell::Boolean, format, q));
|
||||||
cellTable[pos.x()][pos.y()] = data;
|
cellTable[pos.x()][pos.y()] = data;
|
||||||
}
|
}
|
||||||
} else if (type == QLatin1String("str")) {
|
} else if (type == QLatin1String("str")) {
|
||||||
//formula type
|
//formula type
|
||||||
QSharedPointer<Cell> data = readNumericCellData(reader);
|
QSharedPointer<Cell> data = readNumericCellData(reader);
|
||||||
data->d_ptr->format = format;
|
data->d_ptr->format = format;
|
||||||
|
data->d_ptr->parent = q;
|
||||||
cellTable[pos.x()][pos.y()] = data;
|
cellTable[pos.x()][pos.y()] = data;
|
||||||
} else if (type == QLatin1String("e")) {
|
} else if (type == QLatin1String("e")) {
|
||||||
//error type, such as #DIV/0! #NULL! #REF! etc
|
//error type, such as #DIV/0! #NULL! #REF! etc
|
||||||
@@ -1215,20 +1219,23 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader)
|
|||||||
f_str = reader.readElementText();
|
f_str = reader.readElementText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QSharedPointer<Cell> data(new Cell(v_str, Cell::Error, format));
|
QSharedPointer<Cell> data(new Cell(v_str, Cell::Error, format, q));
|
||||||
if (!f_str.isEmpty())
|
if (!f_str.isEmpty())
|
||||||
data->d_ptr->formula = f_str;
|
data->d_ptr->formula = f_str;
|
||||||
cellTable[pos.x()][pos.y()] = data;
|
cellTable[pos.x()][pos.y()] = data;
|
||||||
} else if (type == QLatin1String("n")) {
|
} else if (type == QLatin1String("n")) {
|
||||||
QSharedPointer<Cell> data = readNumericCellData(reader);
|
QSharedPointer<Cell> data = readNumericCellData(reader);
|
||||||
data->d_ptr->format = format;
|
data->d_ptr->format = format;
|
||||||
|
data->d_ptr->parent = q;
|
||||||
cellTable[pos.x()][pos.y()] = data;
|
cellTable[pos.x()][pos.y()] = data;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//default is "n"
|
//default is "n"
|
||||||
QSharedPointer<Cell> data = readNumericCellData(reader);
|
QSharedPointer<Cell> data = readNumericCellData(reader);
|
||||||
data->d_ptr->format = format;
|
data->d_ptr->format = format;
|
||||||
cellTable[pos.x()][pos.y()] = data; }
|
data->d_ptr->parent = q;
|
||||||
|
cellTable[pos.x()][pos.y()] = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1364,4 +1371,13 @@ SharedStrings *WorksheetPrivate::sharedStrings() const
|
|||||||
return workbook->sharedStrings();
|
return workbook->sharedStrings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Return the workbook
|
||||||
|
*/
|
||||||
|
Workbook *Worksheet::workbook() const
|
||||||
|
{
|
||||||
|
Q_D(const Worksheet);
|
||||||
|
return d->workbook;
|
||||||
|
}
|
||||||
|
|
||||||
QT_END_NAMESPACE_XLSX
|
QT_END_NAMESPACE_XLSX
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ public:
|
|||||||
QString sheetName() const;
|
QString sheetName() const;
|
||||||
void setSheetName(const QString &sheetName);
|
void setSheetName(const QString &sheetName);
|
||||||
|
|
||||||
|
Workbook *workbook() const;
|
||||||
~Worksheet();
|
~Worksheet();
|
||||||
private:
|
private:
|
||||||
friend class Package;
|
friend class Package;
|
||||||
|
|||||||
+2
-1
@@ -8,4 +8,5 @@ SUBDIRS=\
|
|||||||
propsapp \
|
propsapp \
|
||||||
document \
|
document \
|
||||||
sharedstrings \
|
sharedstrings \
|
||||||
styles
|
styles \
|
||||||
|
format
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ private Q_SLOTS:
|
|||||||
void testReadWriteBool();
|
void testReadWriteBool();
|
||||||
void testReadWriteBlank();
|
void testReadWriteBlank();
|
||||||
void testReadWriteFormula();
|
void testReadWriteFormula();
|
||||||
|
void testReadWriteDateTime();
|
||||||
};
|
};
|
||||||
|
|
||||||
DocumentTest::DocumentTest()
|
DocumentTest::DocumentTest()
|
||||||
@@ -156,6 +157,46 @@ void DocumentTest::testReadWriteFormula()
|
|||||||
QFile::remove("test.xlsx");
|
QFile::remove("test.xlsx");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DocumentTest::testReadWriteDateTime()
|
||||||
|
{
|
||||||
|
Document xlsx1;
|
||||||
|
QDateTime dt(QDate(2012, 11, 12), QTime(6, 0), Qt::UTC);
|
||||||
|
|
||||||
|
xlsx1.write("A1", dt);
|
||||||
|
|
||||||
|
Format *format = xlsx1.createFormat();
|
||||||
|
format->setFontColor(Qt::blue);
|
||||||
|
format->setBorderStyle(Format::BorderDashDotDot);
|
||||||
|
format->setFillPattern(Format::PatternSolid);
|
||||||
|
xlsx1.write("A2", dt, format);
|
||||||
|
|
||||||
|
Format *format3 = xlsx1.createFormat();
|
||||||
|
format3->setNumberFormat("dd/mm/yyyy");
|
||||||
|
xlsx1.write("A3", dt, format3);
|
||||||
|
|
||||||
|
xlsx1.saveAs("test.xlsx");
|
||||||
|
|
||||||
|
Document xlsx2("test.xlsx");
|
||||||
|
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->isDateTime(), true);
|
||||||
|
QCOMPARE(xlsx2.cellAt("A1")->dateTime(), dt);
|
||||||
|
|
||||||
|
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A2")->isDateTime(), true);
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A2")->dateTime(), dt);
|
||||||
|
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A3")->dataType(), Cell::Numeric);
|
||||||
|
// QVERIFY(xlsx2.cellAt("A3")->format()!=0);
|
||||||
|
// qDebug()<<xlsx2.cellAt("A3")->format()->numberFormat();
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true);
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt);
|
||||||
|
// QCOMPARE(xlsx2.cellAt("A3")->format()->numberFormat(), QString("dd/mm/yyyy"));
|
||||||
|
|
||||||
|
QFile::remove("test.xlsx");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(DocumentTest)
|
QTEST_APPLESS_MAIN(DocumentTest)
|
||||||
|
|
||||||
#include "tst_documenttest.moc"
|
#include "tst_documenttest.moc"
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
QT += testlib xlsx # xlsx-private
|
||||||
|
CONFIG += testcase
|
||||||
|
DEFINES += XLSX_TEST
|
||||||
|
|
||||||
|
TARGET = tst_format
|
||||||
|
CONFIG += console
|
||||||
|
CONFIG -= app_bundle
|
||||||
|
|
||||||
|
TEMPLATE = app
|
||||||
|
|
||||||
|
SOURCES += tst_formattest.cpp
|
||||||
|
DEFINES += SRCDIR=\\\"$$PWD/\\\"
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include "xlsxformat.h"
|
||||||
|
#include <QString>
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
|
QTXLSX_USE_NAMESPACE
|
||||||
|
|
||||||
|
class FormatTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
FormatTest();
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void testDateTimeFormat();
|
||||||
|
void testDateTimeFormat_data();
|
||||||
|
};
|
||||||
|
|
||||||
|
FormatTest::FormatTest()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void FormatTest::testDateTimeFormat()
|
||||||
|
{
|
||||||
|
QFETCH(QString, data);
|
||||||
|
QFETCH(bool, res);
|
||||||
|
|
||||||
|
Format fmt;
|
||||||
|
fmt.setNumberFormat(data);
|
||||||
|
|
||||||
|
QCOMPARE(fmt.isDateTimeFormat(), res);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FormatTest::testDateTimeFormat_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("data");
|
||||||
|
QTest::addColumn<bool>("res");
|
||||||
|
|
||||||
|
QTest::newRow("0") << QString("yyyy-mm-dd")<<true;
|
||||||
|
QTest::newRow("1") << QString("m/d/yy")<<true;
|
||||||
|
QTest::newRow("2") << QString("h:mm AM/PM")<<true;
|
||||||
|
QTest::newRow("3") << QString("m/d/yy h:mm")<<true;
|
||||||
|
QTest::newRow("4") << QString("[h]:mm:ss")<<true;
|
||||||
|
QTest::newRow("5") << QString("[h]")<<true;
|
||||||
|
QTest::newRow("6") << QString("[m]")<<true;
|
||||||
|
|
||||||
|
QTest::newRow("20") << QString("[Red]#,##0 ;[Yellow](#,##0)")<<false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTEST_APPLESS_MAIN(FormatTest)
|
||||||
|
|
||||||
|
#include "tst_formattest.moc"
|
||||||
@@ -18,6 +18,7 @@ private Q_SLOTS:
|
|||||||
void testAddFormat2();
|
void testAddFormat2();
|
||||||
void testSolidFillBackgroundColor();
|
void testSolidFillBackgroundColor();
|
||||||
|
|
||||||
|
void testReadNumFmts();
|
||||||
void testReadFonts();
|
void testReadFonts();
|
||||||
void testReadFills();
|
void testReadFills();
|
||||||
void testReadBorders();
|
void testReadBorders();
|
||||||
@@ -128,6 +129,21 @@ void StylesTest::testReadBorders()
|
|||||||
QCOMPARE(styles.m_bordersList.size(), 2);
|
QCOMPARE(styles.m_bordersList.size(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StylesTest::testReadNumFmts()
|
||||||
|
{
|
||||||
|
QByteArray xmlData ="<numFmts count=\"2\">"
|
||||||
|
"<numFmt numFmtId=\"164\" formatCode=\"yyyy-mm-ddThh:mm:ss\"/>"
|
||||||
|
"<numFmt numFmtId=\"165\" formatCode=\"dd/mm/yyyy\"/>"
|
||||||
|
"</numFmts>";
|
||||||
|
|
||||||
|
QXlsx::Styles styles(true);
|
||||||
|
QXlsx::XmlStreamReader reader(xmlData);
|
||||||
|
reader.readNextStartElement();//So current node is numFmts
|
||||||
|
styles.readNumFmts(reader);
|
||||||
|
|
||||||
|
QCOMPARE(styles.m_customNumFmts.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(StylesTest)
|
QTEST_APPLESS_MAIN(StylesTest)
|
||||||
|
|
||||||
#include "tst_stylestest.moc"
|
#include "tst_stylestest.moc"
|
||||||
|
|||||||
Reference in New Issue
Block a user