Browse Source

Add QTime read write support

master
Debao Zhang 11 years ago
parent
commit
e232e7eae9
  1. 5
      src/xlsx/xlsxutility.cpp
  2. 2
      src/xlsx/xlsxutility_p.h
  3. 41
      src/xlsx/xlsxworksheet.cpp
  4. 3
      src/xlsx/xlsxworksheet.h
  5. 30
      tests/auto/document/tst_documenttest.cpp
  6. 22
      tests/auto/utility/tst_utilitytest.cpp

5
src/xlsx/xlsxutility.cpp

@ -67,6 +67,11 @@ double datetimeToNumber(const QDateTime &dt, bool is1904)
return excel_time; return excel_time;
} }
double timeToNumber(const QTime &time)
{
return QTime(0,0).msecsTo(time) / (1000*60*60*24.0);
}
QDateTime datetimeFromNumber(double num, bool is1904) QDateTime datetimeFromNumber(double num, bool is1904)
{ {
if (!is1904 && num > 60) if (!is1904 && num > 60)

2
src/xlsx/xlsxutility_p.h

@ -42,6 +42,7 @@ class QString;
class QStringList; class QStringList;
class QColor; class QColor;
class QDateTime; class QDateTime;
class QTime;
namespace QXlsx { namespace QXlsx {
@ -49,6 +50,7 @@ XLSX_AUTOTEST_EXPORT int intPow(int x, int p);
XLSX_AUTOTEST_EXPORT QStringList splitPath(const QString &path); XLSX_AUTOTEST_EXPORT QStringList splitPath(const QString &path);
XLSX_AUTOTEST_EXPORT double datetimeToNumber(const QDateTime &dt, bool is1904=false); XLSX_AUTOTEST_EXPORT double datetimeToNumber(const QDateTime &dt, bool is1904=false);
XLSX_AUTOTEST_EXPORT QDateTime datetimeFromNumber(double num, bool is1904=false); XLSX_AUTOTEST_EXPORT QDateTime datetimeFromNumber(double num, bool is1904=false);
XLSX_AUTOTEST_EXPORT double timeToNumber(const QTime &t);
XLSX_AUTOTEST_EXPORT QPoint xl_cell_to_rowcol(const QString &cell_str); XLSX_AUTOTEST_EXPORT QPoint xl_cell_to_rowcol(const QString &cell_str);
XLSX_AUTOTEST_EXPORT QString xl_col_to_name(int col_num); XLSX_AUTOTEST_EXPORT QString xl_col_to_name(int col_num);

41
src/xlsx/xlsxworksheet.cpp

@ -444,6 +444,8 @@ int Worksheet::write(int row, int column, const QVariant &value, const Format &f
ret = writeBool(row,column, value.toBool(), format); ret = writeBool(row,column, value.toBool(), format);
} else if (value.toDateTime().isValid()) { //DateTime } else if (value.toDateTime().isValid()) { //DateTime
ret = writeDateTime(row, column, value.toDateTime(), format); ret = writeDateTime(row, column, value.toDateTime(), format);
} else if (value.toTime().isValid()) { //Time
ret = writeTime(row, column, value.toTime(), format);
} else if (value.toDouble(&ok), ok) { //Number } else if (value.toDouble(&ok), ok) { //Number
if (!d->workbook->isStringsToNumbersEnabled() && value.userType() == QMetaType::QString) { if (!d->workbook->isStringsToNumbersEnabled() && value.userType() == QMetaType::QString) {
//Don't convert string to number if the flag not enabled. //Don't convert string to number if the flag not enabled.
@ -509,6 +511,13 @@ QVariant Worksheet::read(int row, int column) const
return QVariant(); return QVariant();
if (!cell->formula().isEmpty()) if (!cell->formula().isEmpty())
return QVariant(QLatin1String("=")+cell->formula()); return QVariant(QLatin1String("=")+cell->formula());
if (cell->isDateTime()) {
double val = cell->value().toDouble();
QDateTime dt = cell->dateTime();
if (val < 1)
return dt.time();
return dt;
}
return cell->value(); return cell->value();
} }
@ -851,6 +860,38 @@ int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, const For
return 0; return 0;
} }
/*!
\overload
*/
int Worksheet::writeTime(const QString &row_column, const QTime &t, const Format &format)
{
//convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column);
if (pos == QPoint(-1, -1))
return -1;
return writeTime(pos.x(), pos.y(), t, format);
}
/*!
Write a QTime \a value to the cell (\a row, \a column) with the \a format
*/
int Worksheet::writeTime(int row, int column, const QTime &t, const Format &format)
{
Q_D(Worksheet);
if (d->checkDimensions(row, column))
return -1;
Format fmt = format;
if (!fmt.isValid())
fmt.setNumberFormat(QStringLiteral("hh:mm:ss"));
d->workbook->styles()->addXfFormat(fmt);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(timeToNumber(t), Cell::Numeric, fmt, this));
return 0;
}
/*! /*!
\overload \overload
*/ */

3
src/xlsx/xlsxworksheet.h

@ -77,6 +77,9 @@ public:
int writeBool(int row, int column, bool value, const Format &format=Format()); int writeBool(int row, int column, bool value, const Format &format=Format());
int writeDateTime(const QString &row_column, const QDateTime& dt, const Format &format=Format()); int writeDateTime(const QString &row_column, const QDateTime& dt, const Format &format=Format());
int writeDateTime(int row, int column, const QDateTime& dt, const Format &format=Format()); int writeDateTime(int row, int column, const QDateTime& dt, const Format &format=Format());
int writeTime(const QString &row_column, const QTime& t, const Format &format=Format());
int writeTime(int row, int column, const QTime& t, const Format &format=Format());
int writeHyperlink(const QString &row_column, const QUrl &url, const Format &format=Format(), const QString &display=QString(), const QString &tip=QString()); int writeHyperlink(const QString &row_column, const QUrl &url, const Format &format=Format(), const QString &display=QString(), const QString &tip=QString());
int writeHyperlink(int row, int column, const QUrl &url, const Format &format=Format(), const QString &display=QString(), const QString &tip=QString()); int writeHyperlink(int row, int column, const QUrl &url, const Format &format=Format(), const QString &display=QString(), const QString &tip=QString());

30
tests/auto/document/tst_documenttest.cpp

@ -21,6 +21,7 @@ private Q_SLOTS:
void testReadWriteBlank(); void testReadWriteBlank();
void testReadWriteFormula(); void testReadWriteFormula();
void testReadWriteDateTime(); void testReadWriteDateTime();
void testReadWriteTime();
}; };
DocumentTest::DocumentTest() DocumentTest::DocumentTest()
@ -192,6 +193,8 @@ void DocumentTest::testReadWriteDateTime()
format3.setNumberFormat("dd/mm/yyyy"); format3.setNumberFormat("dd/mm/yyyy");
xlsx1.write("A3", dt, format3); xlsx1.write("A3", dt, format3);
xlsx1.write("A4", "2013-12-14"); //Auto convert to QDateTime, by QVariant
xlsx1.saveAs(&device); xlsx1.saveAs(&device);
device.open(QIODevice::ReadOnly); device.open(QIODevice::ReadOnly);
@ -210,6 +213,33 @@ void DocumentTest::testReadWriteDateTime()
QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true); QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true);
QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt); QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt);
QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy")); QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy"));
QCOMPARE(xlsx2.cellAt("A4")->dataType(), Cell::Numeric);
QCOMPARE(xlsx2.cellAt("A4")->isDateTime(), true);
QCOMPARE(xlsx2.cellAt("A4")->dateTime(), QDateTime(QDate(2013,12,14)));
}
void DocumentTest::testReadWriteTime()
{
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1;
xlsx1.write("A1", QTime()); //Blank cell
xlsx1.write("A2", QTime(1, 22));
xlsx1.saveAs(&device);
device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Blank);
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
QCOMPARE(xlsx2.cellAt("A2")->isDateTime(), true);
qDebug()<<xlsx2.cellAt("A2")->value().toDouble();
//QCOMPARE(xlsx2.read("A2").toTime(), QTime(1, 22)); //01:21:59.999 ???
} }
QTEST_APPLESS_MAIN(DocumentTest) QTEST_APPLESS_MAIN(DocumentTest)

22
tests/auto/utility/tst_utilitytest.cpp

@ -44,6 +44,9 @@ private Q_SLOTS:
void test_datetimeToNumber_data(); void test_datetimeToNumber_data();
void test_datetimeToNumber(); void test_datetimeToNumber();
void test_timeToNumber_data();
void test_timeToNumber();
void test_datetimeFromNumber_data(); void test_datetimeFromNumber_data();
void test_datetimeFromNumber(); void test_datetimeFromNumber();
}; };
@ -133,6 +136,25 @@ void UtilityTest::test_datetimeToNumber()
QCOMPARE(QXlsx::datetimeToNumber(dt, is1904), num); QCOMPARE(QXlsx::datetimeToNumber(dt, is1904), num);
} }
void UtilityTest::test_timeToNumber_data()
{
QTest::addColumn<QTime>("t");
QTest::addColumn<double>("num");
QTest::newRow("0") << QTime(0,0) << 0.0;
QTest::newRow("0.0625") << QTime(1, 30) << 0.0625;
QTest::newRow("0.25") << QTime(6, 0) << 0.25;
QTest::newRow("0.5") << QTime(12, 0) << 0.5;
}
void UtilityTest::test_timeToNumber()
{
QFETCH(QTime, t);
QFETCH(double, num);
QCOMPARE(QXlsx::timeToNumber(t), num);
}
void UtilityTest::test_datetimeFromNumber_data() void UtilityTest::test_datetimeFromNumber_data()
{ {
QTest::addColumn<QDateTime>("dt"); QTest::addColumn<QDateTime>("dt");

Loading…
Cancel
Save