From 397888525226f88445c44203c915dea6501d3e93 Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Sun, 3 Nov 2013 13:22:07 +0800 Subject: [PATCH] Add support for rows group --- src/xlsx/xlsxworksheet.cpp | 61 ++++++++++++++++++++++++++++++++++---- src/xlsx/xlsxworksheet.h | 2 ++ src/xlsx/xlsxworksheet_p.h | 8 ++++- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index df95723..03d421c 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -903,12 +903,16 @@ void WorksheetPrivate::writeSheetData(XmlStreamWriter &writer) writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format->xfIndex())); writer.writeAttribute(QStringLiteral("customFormat"), QStringLiteral("1")); } - if (rowInfo->height != 15) { + if (rowInfo->height != 15 && rowInfo->height != 0) { writer.writeAttribute(QStringLiteral("ht"), QString::number(rowInfo->height)); writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("1")); } if (rowInfo->hidden) writer.writeAttribute(QStringLiteral("hidden"), QStringLiteral("1")); + if (rowInfo->outlineLevel > 0) + writer.writeAttribute(QStringLiteral("outlineLevel"), QString::number(rowInfo->outlineLevel)); + if (rowInfo->collapsed) + writer.writeAttribute(QStringLiteral("collapsed"), QStringLiteral("1")); } for (int col_num = dimension.firstColumn(); col_num <= dimension.lastColumn(); col_num++) { @@ -1196,6 +1200,42 @@ bool Worksheet::setColumn(const QString &colFirst, const QString &colLast, doubl return setColumn(col1, col2, width, format, hidden); } +/*! + Groups rows from rowFirst to rowLast. Returns false if error occurs. + */ +bool Worksheet::groupRows(int rowFirst, int rowLast, bool collapsed) +{ + Q_D(Worksheet); + + for (int row=rowFirst; row<=rowLast; ++row) { + if (d->rowsInfo.contains(row)) { + d->rowsInfo[row]->outlineLevel += 1; + } else { + QSharedPointer info(new XlsxRowInfo); + info->outlineLevel += 1; + d->rowsInfo.insert(row, info); + } + if (collapsed) + d->rowsInfo[row]->hidden = true; + } + if (collapsed) { + if (!d->rowsInfo.contains(rowLast+1)) + d->rowsInfo.insert(rowLast+1, QSharedPointer(new XlsxRowInfo)); + d->rowsInfo[rowLast+1]->collapsed = true; + } + return true; +} + +/*! + Groups columns from colFirst to colLast. Returns false if error occurs. +*/ +bool Worksheet::groupColumns(int colFirst, int colLast, bool collapsed) +{ + Q_D(Worksheet); + + return false; +} + /*! Return the range that contains cell data. */ @@ -1447,7 +1487,9 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader) if (attributes.hasAttribute(QLatin1String("customFormat")) || attributes.hasAttribute(QLatin1String("customHeight")) - || attributes.hasAttribute(QLatin1String("hidden"))) { + || attributes.hasAttribute(QLatin1String("hidden")) + || attributes.hasAttribute(QLatin1String("outlineLevel")) + || attributes.hasAttribute(QLatin1String("collapsed"))) { QSharedPointer info(new XlsxRowInfo); if (attributes.hasAttribute(QLatin1String("customFormat")) && attributes.hasAttribute(QLatin1String("s"))) { @@ -1457,11 +1499,18 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader) if (attributes.hasAttribute(QLatin1String("customHeight")) && attributes.hasAttribute(QLatin1String("ht"))) { info->height = attributes.value(QLatin1String("ht")).toDouble(); } - if (attributes.hasAttribute(QLatin1String("hidden"))) - info->hidden = true; + //both "hidden" and "collapsed" default are false + info->hidden = attributes.value(QLatin1String("hidden")) == QLatin1String("1"); + info->collapsed = attributes.value(QLatin1String("collapsed")) == QLatin1String("1"); - int row = attributes.value(QLatin1String("r")).toInt(); - rowsInfo[row] = info; + if (attributes.hasAttribute(QLatin1String("outlineLevel"))) + info->outlineLevel = attributes.value(QLatin1String("outlineLevel")).toInt(); + + //"r" is optional too. + if (attributes.hasAttribute(QLatin1String("r"))) { + int row = attributes.value(QLatin1String("r")).toInt()-1; + rowsInfo[row] = info; + } } } else if (reader.name() == QLatin1String("c")) { diff --git a/src/xlsx/xlsxworksheet.h b/src/xlsx/xlsxworksheet.h index 0aa5632..5560891 100755 --- a/src/xlsx/xlsxworksheet.h +++ b/src/xlsx/xlsxworksheet.h @@ -80,6 +80,8 @@ public: bool setRow(const QString &row, double height, Format* format=0, bool hidden=false); bool setColumn(int colFirst, int colLast, double width, Format* format=0, bool hidden=false); bool setColumn(const QString &colFirst, const QString &colLast, double width, Format* format=0, bool hidden=false); + bool groupRows(int rowFirst, int rowLast, bool collapsed = true); + bool groupColumns(int colFirst, int colLast, bool collapsed = true); CellRange dimension() const; bool isWindowProtected() const; diff --git a/src/xlsx/xlsxworksheet_p.h b/src/xlsx/xlsxworksheet_p.h index ae8caac..f7e1c1e 100644 --- a/src/xlsx/xlsxworksheet_p.h +++ b/src/xlsx/xlsxworksheet_p.h @@ -123,7 +123,8 @@ struct XlsxObjectPositionData struct XlsxRowInfo { XlsxRowInfo(double height=0, Format *format=0, bool hidden=false) : - height(height), format(format), hidden(hidden) + height(height), format(format), hidden(hidden), outlineLevel(0) + , collapsed(false) { } @@ -131,12 +132,15 @@ struct XlsxRowInfo double height; Format *format; bool hidden; + int outlineLevel; + bool collapsed; }; struct XlsxColumnInfo { XlsxColumnInfo(int firstColumn=0, int lastColumn=1, double width=0, Format *format=0, bool hidden=false) : firstColumn(firstColumn), lastColumn(lastColumn), width(width), format(format), hidden(hidden) + , outlineLevel(0), collapsed(false) { } @@ -145,6 +149,8 @@ struct XlsxColumnInfo double width; Format *format; bool hidden; + int outlineLevel; + bool collapsed; }; class XLSX_AUTOTEST_EXPORT WorksheetPrivate