Add QTime read write support
This commit is contained in:
@@ -67,6 +67,11 @@ double datetimeToNumber(const QDateTime &dt, bool is1904)
|
||||
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)
|
||||
{
|
||||
if (!is1904 && num > 60)
|
||||
|
||||
@@ -42,6 +42,7 @@ class QString;
|
||||
class QStringList;
|
||||
class QColor;
|
||||
class QDateTime;
|
||||
class QTime;
|
||||
|
||||
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 double datetimeToNumber(const QDateTime &dt, 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 QString xl_col_to_name(int col_num);
|
||||
|
||||
@@ -444,6 +444,8 @@ int Worksheet::write(int row, int column, const QVariant &value, const Format &f
|
||||
ret = writeBool(row,column, value.toBool(), format);
|
||||
} else if (value.toDateTime().isValid()) { //DateTime
|
||||
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
|
||||
if (!d->workbook->isStringsToNumbersEnabled() && value.userType() == QMetaType::QString) {
|
||||
//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();
|
||||
if (!cell->formula().isEmpty())
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -851,6 +860,38 @@ int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, const For
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -77,6 +77,9 @@ public:
|
||||
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(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(int row, int column, const QUrl &url, const Format &format=Format(), const QString &display=QString(), const QString &tip=QString());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user