From b19758e14f0c4fba41dfb0265670281f563aa656 Mon Sep 17 00:00:00 2001 From: eho Date: Wed, 12 Mar 2014 16:52:14 +0100 Subject: [PATCH 1/5] First version of improved setColumn and setRow API. --- src/xlsx/xlsx.pro | 1 - src/xlsx/xlsxdocument.cpp | 176 ++++++++++- src/xlsx/xlsxdocument.h | 30 ++ src/xlsx/xlsxworksheet.cpp | 405 +++++++++++++++++++++---- src/xlsx/xlsxworksheet.h | 35 ++- src/xlsx/xlsxworksheet_p.h | 48 ++- tests/auto/worksheet/tst_worksheet.cpp | 8 +- 7 files changed, 628 insertions(+), 75 deletions(-) diff --git a/src/xlsx/xlsx.pro b/src/xlsx/xlsx.pro index 1449014..1d357fd 100644 --- a/src/xlsx/xlsx.pro +++ b/src/xlsx/xlsx.pro @@ -13,4 +13,3 @@ include(qtxlsx.pri) QMAKE_TARGET_COMPANY = "Debao Zhang" QMAKE_TARGET_COPYRIGHT = "Copyright (C) 2013-2014 Debao Zhang " QMAKE_TARGET_DESCRIPTION = ".Xlsx file wirter for Qt5" - diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index 2701a0e..0158790 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -514,19 +514,6 @@ bool Document::unmergeCells(const CellRange &range) return false; } -/*! - Sets the properties of \a row with the given \a height, \a format and \a hidden. - \a row is 1-indexed. - - Returns false if failed. - */ -bool Document::setRow(int row, double height, const Format &format, bool hidden) -{ - if (Worksheet *sheet = currentWorksheet()) - return sheet->setRow(row, height, format, hidden); - return false; -} - /*! Sets the column properties for all columns from \a colFirst to \a colLast with the given \a width, \a format and \a hidden. Column @@ -559,6 +546,169 @@ bool Document::setColumn(const QString &colFirst, const QString &colLast, double return false; } +bool Document::setColumnWidth(const QString &column, double width) +{ + return setColumnWidth(column,column,width); +} + +bool Document::setColumnFormat(const QString &column, const Format &format) +{ + return setColumnFormat(column,column,format); +} + +bool Document::setColumnHidden(const QString &column, bool hidden) +{ + return setColumnHidden(column,column,hidden); +} + +bool Document::setColumnWidth(const QString &colFirst, const QString &colLast, double width) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setColumnWidth(colFirst, colLast, width); + return false; +} + +bool Document::setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setColumnFormat(colFirst, colLast, format); + return false; +} + +bool Document::setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setColumnWidth(colFirst, colLast, hidden); + return false; +} + +bool Document::setColumnWidth(int column, double width) +{ + return setColumnWidth(column,column,width); +} + +bool Document::setColumnFormat(int column, const Format &format) +{ + return setColumnFormat(column,column,format); +} + +bool Document::setColumnHidden(int column, bool hidden) +{ + return setColumnHidden(column,column,hidden); +} + +bool Document::setColumnWidth(int colFirst, int colLast, double width) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setColumnWidth(colFirst, colLast, width); + return false; +} + +bool Document::setColumnFormat(int colFirst, int colLast, const Format &format) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setColumnFormat(colFirst, colLast, format); + return false; +} + +bool Document::setColumnHidden(int colFirst, int colLast, bool hidden) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setColumnHidden(colFirst, colLast, hidden); + return false; +} + +double Document::columnWidth(int column) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->columnWidth(column); + return 0.0; +} + +Format Document::columnFormat(int column) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->columnFormat(column); + return Format(); +} + +bool Document::isColumnHidden(int column) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->isColumnHidden(column); + return false; +} + +/*! + Sets the properties of \a row with the given \a height, \a format and \a hidden. + \a row is 1-indexed. + + Returns false if failed. + */ +bool Document::setRow(int row, double height, const Format &format, bool hidden) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setRow(row, height, format, hidden); + return false; +} + +bool Document::setRowFormat(int row, const Format &format) +{ + return setRowFormat(row,row, format); +} + +bool Document::setRowFormat(int rowFirst, int rowLast, const Format &format) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setRowFormat(rowFirst, rowLast, format); + return false; +} + +bool Document::setRowHidden(int row, bool hidden) +{ + return setRowHidden(row,row,hidden); +} + +bool Document::setRowHidden(int rowFirst, int rowLast, bool hidden) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setRowHidden(rowFirst, rowLast, hidden); + return false; +} + +bool Document::setRowHeight(int row, double height) +{ + return setRowHeight(row,row,height); +} + +bool Document::setRowHeight(int rowFirst, int rowLast, double height) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->setRowHeight(rowFirst, rowLast, height); + return false; +} + +double Document::rowHeight(int row) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->rowHeight(row); + return 0.0; // ? +} + +Format Document::rowFormat(int row) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->rowFormat(row); + return Format(); // ? +} + +bool Document::isRowHidden(int row) +{ + if (Worksheet *sheet = currentWorksheet()) + return sheet->isRowHidden(row); + return false; // ? +} + /*! Groups rows from \a rowFirst to \a rowLast with the given \a collapsed. Returns false if error occurs. diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index 48e10c8..c3552ea 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -66,8 +66,38 @@ public: bool unmergeCells(const CellRange &range); bool unmergeCells(const QString &range); bool setRow(int row, double height, const Format &format=Format(), bool hidden=false); + bool setColumn(int colFirst, int colLast, double width, const Format &format=Format(), bool hidden=false); bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format=Format(), bool hidden=false); + bool setColumnWidth(const QString &column, double width); + bool setColumnFormat(const QString &column, const Format &format); + bool setColumnHidden(const QString &column, bool hidden); + bool setColumnWidth(const QString &colFirst, const QString &colLast, double width); + bool setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format); + bool setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden); + bool setColumnWidth(int column, double width); + bool setColumnFormat(int column, const Format &format); + bool setColumnHidden(int column, bool hidden); + bool setColumnWidth(int colFirst, int colLast, double width); + bool setColumnFormat(int colFirst, int colLast, const Format &format); + bool setColumnHidden(int colFirst, int colLast, bool hidden); + double columnWidth(int column); + Format columnFormat(int column); + bool isColumnHidden(int column); + + bool setRowHeight(int row, double height); + bool setRowFormat(int row, const Format &format); + bool setRowHidden(int row, bool hidden); + bool setRowHeight(int rowFirst, int rowLast, double height); + bool setRowFormat(int rowFirst, int rowLast, const Format &format); + bool setRowHidden(int rowFirst, int rowLast, bool hidden); + + double rowHeight(int row); + Format rowFormat(int row); + bool isRowHidden(int row); + + + bool groupRows(int rowFirst, int rowLast, bool collapsed = true); bool groupColumns(int colFirst, int colLast, bool collapsed = true); bool addDataValidation(const DataValidation &validation); diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index 121263e..ee61edf 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -1302,10 +1302,19 @@ void WorksheetPrivate::saveXmlSheetData(QXmlStreamWriter &writer) const writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format.xfIndex())); writer.writeAttribute(QStringLiteral("customFormat"), QStringLiteral("1")); } - if (rowInfo->height != 15 && rowInfo->height != 0) { + //!Todo: support customHeight from info struct + //!Todo: where does this magic number '15' come from? + if (rowInfo->customHeight) { writer.writeAttribute(QStringLiteral("ht"), QString::number(rowInfo->height)); writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("1")); + qDebug() << "custom height: " << rowInfo->height; + } else { + writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("0")); + qDebug() << "no height: "; } + + + if (rowInfo->hidden) writer.writeAttribute(QStringLiteral("hidden"), QStringLiteral("1")); if (rowInfo->outlineLevel > 0) @@ -1483,25 +1492,6 @@ void WorksheetPrivate::saveXmlDrawings(QXmlStreamWriter &writer) const writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(relationships->count())); } -/*! - Sets the \a height and \a format of the row \a row. Row height measured in point size. If format - equals 0 then format is ignored. \a row is 1-indexed. - Hides the row if \a hidden is true. - - Returns true if success. - */ -bool Worksheet::setRow(int row, double height, const Format &format, bool hidden) -{ - Q_D(Worksheet); - int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); - - if (d->checkDimensions(row, min_col, false, true)) - return false; - - d->rowsInfo[row] = QSharedPointer(new XlsxRowInfo(height, format, hidden)); - d->workbook->styles()->addXfFormat(format); - return true; -} void WorksheetPrivate::splitColsInfo(int colFirst, int colLast) { @@ -1545,20 +1535,11 @@ void WorksheetPrivate::splitColsInfo(int colFirst, int colLast) } } -/*! - Sets column \a width and \a format for all columns from \a colFirst to \a colLast. Column - width measured as the number of characters of the maximum digit width of the - numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format - equals 0 then format is ignored. Both \a colFirst and \a colLast are all 1-indexed. - Hides the column if \a hidden is true. - - Return true if success. - */ -bool Worksheet::setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden) +bool Worksheet::isColumnRangeValid(int colFirst, int colLast) { Q_D(Worksheet); bool ignore_row = true; - bool ignore_col = (format.isValid() || (width && hidden)) ? false : true; + bool ignore_col = false; if (colFirst > colLast) return false; @@ -1568,6 +1549,13 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, const Format if (d->checkDimensions(0, colFirst, ignore_row, ignore_col)) return false; + return true; +} + +QList Worksheet::getColumnIndexes(int colFirst, int colLast) +{ + Q_D(Worksheet); + d->splitColsInfo(colFirst, colLast); QList nodes; @@ -1582,24 +1570,35 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, const Format } } - for (int idx = 0; idx < nodes.size(); ++idx) { - int colStart = nodes[idx]; - if (d->colsInfo.contains(colStart)) { - QSharedPointer info = d->colsInfo[colStart]; - info->width = width; - info->format = format; - info->hidden = hidden; - } else { - int colEnd = (idx == nodes.size() - 1) ? colLast : nodes[idx+1] - 1; - QSharedPointer info(new XlsxColumnInfo(colStart, colEnd, width, format, hidden)); - d->colsInfo.insert(colFirst, info); - for (int c = colStart; c <= colEnd; ++c) - d->colsInfoHelper[c] = info; - } + return nodes; +} + +/*! + Sets column \a width and \a format for all columns from \a colFirst to \a colLast. Column + width measured as the number of characters of the maximum digit width of the + numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format + equals 0 then format is ignored. Both \a colFirst and \a colLast are all 1-indexed. + Hides the column if \a hidden is true. + + Return true if success. + */ +bool Worksheet::setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden) +{ + Q_D(Worksheet); + + QList > columnInfoList = getColumnInfoList(colFirst, colLast); + foreach(QSharedPointer columnInfo, columnInfoList) { + columnInfo->width = width; + columnInfo->format = format; + columnInfo->hidden = hidden; } - d->workbook->styles()->addXfFormat(format); - return true; + if(columnInfoList.count() > 0) { + d->workbook->styles()->addXfFormat(format); + return true; + } + + return false; } /*! @@ -1619,6 +1618,256 @@ bool Worksheet::setColumn(const QString &colFirst, const QString &colLast, doubl return setColumn(col1, col2, width, format, hidden); } +bool Worksheet::setColumnWidth(const QString &colFirst, const QString &colLast, double width) +{ + int col1 = xl_col_name_to_value(colFirst); + int col2 = xl_col_name_to_value(colLast); + if (col1 == -1 || col2 == -1) + return false; + + return setColumnWidth(col1, col2, width); +} + +bool Worksheet::setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format) +{ + int col1 = xl_col_name_to_value(colFirst); + int col2 = xl_col_name_to_value(colLast); + if (col1 == -1 || col2 == -1) + return false; + + return setColumnFormat(col1, col2, format); +} + +bool Worksheet::setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden) +{ + int col1 = xl_col_name_to_value(colFirst); + int col2 = xl_col_name_to_value(colLast); + if (col1 == -1 || col2 == -1) + return false; + + return setColumnHidden(col1, col2, hidden); +} + +QList > Worksheet::getColumnInfoList(int colFirst, int colLast) +{ + Q_D(Worksheet); + QList > columnsInfoList; + if(isColumnRangeValid(colFirst,colLast)) + { + QList nodes = getColumnIndexes(colFirst, colLast); + + for (int idx = 0; idx < nodes.size(); ++idx) { + int colStart = nodes[idx]; + if (d->colsInfo.contains(colStart)) { + QSharedPointer info = d->colsInfo[colStart]; + columnsInfoList.append(info); + } else { + int colEnd = (idx == nodes.size() - 1) ? colLast : nodes[idx+1] - 1; + QSharedPointer info(new XlsxColumnInfo(colStart, colEnd)); + d->colsInfo.insert(colFirst, info); + columnsInfoList.append(info); + for (int c = colStart; c <= colEnd; ++c) + d->colsInfoHelper[c] = info; + } + } + } + + return columnsInfoList; +} + +QList > Worksheet::getRowInfoList(int rowFirst, int rowLast) +{ + Q_D(Worksheet); + QList > rowInfoList; + + int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); + + for(int row = rowFirst; row <= rowLast; ++row) { + if (d->checkDimensions(row, min_col, false, true)) + continue; + + QSharedPointer rowInfo; + if ((d->rowsInfo[row]).isNull()){ + d->rowsInfo[row] = QSharedPointer(new XlsxRowInfo()); + } + rowInfoList.append(d->rowsInfo[row]); + } + + return rowInfoList; +} + +bool Worksheet::setColumnWidth(int colFirst, int colLast, double width) +{ + QList > columnInfoList = getColumnInfoList(colFirst, colLast); + foreach(QSharedPointer columnInfo, columnInfoList) { + columnInfo->width = width; + } + + return (columnInfoList.count() > 0); +} + +bool Worksheet::setColumnFormat(int colFirst, int colLast, const Format &format) +{ + Q_D(Worksheet); + + QList > columnInfoList = getColumnInfoList(colFirst, colLast); + foreach(QSharedPointer columnInfo, columnInfoList) { + columnInfo->format = format; + } + + if(columnInfoList.count() > 0) { + d->workbook->styles()->addXfFormat(format); + return true; + } + + return false; +} + +bool Worksheet::setColumnHidden(int colFirst, int colLast, bool hidden) +{ + QList > columnInfoList = getColumnInfoList(colFirst, colLast); + foreach(QSharedPointer columnInfo, columnInfoList) { + columnInfo->hidden = hidden; + } + + return (columnInfoList.count() > 0); +} + +double Worksheet::columnWidth(int column) +{ + Q_D(Worksheet); + + QList > columnInfoList = getColumnInfoList(column, column); + if (columnInfoList.count() == 1) { + qDebug() << "Whoopie Width"; + return columnInfoList.at(0)->width ; + } + + qDebug() << "Default Width"; + return d->sheetFormatProps.defaultColWidth; +} + +Format Worksheet::columnFormat(int column) +{ + QList > columnInfoList = getColumnInfoList(column, column); + if (columnInfoList.count() == 1) { + qDebug() << "Whoopie Format"; + return columnInfoList.at(0)->format; + } + + qDebug() << "Default Format"; + return Format(); +} + +bool Worksheet::isColumnHidden(int column) +{ + QList > columnInfoList = getColumnInfoList(column, column); + if (columnInfoList.count() == 1) { + qDebug() << "Whoopie Hidden"; + return columnInfoList.at(0)->hidden; + } + + qDebug() << "Default Hidden"; + return false; +} + +/*! + Sets the \a height and \a format of the row \a row. Row height measured in point size. If format + equals 0 then format is ignored. \a row is 1-indexed. + Hides the row if \a hidden is true. + + Returns true if success. + */ +bool Worksheet::setRow(int row, double height, const Format &format, bool hidden) +{ + return setRow(row,row, height, format, hidden); +} + +bool Worksheet::setRow(int rowFirst,int rowLast, double height, const Format &format, bool hidden) +{ + Q_D(Worksheet); + QList > rowInfoList = getRowInfoList(rowFirst,rowLast); + + foreach(QSharedPointer rowInfo, rowInfoList) { + rowInfo->height = height; + rowInfo->customHeight = true; + rowInfo->format = format; + rowInfo->hidden = hidden; + } + d->workbook->styles()->addXfFormat(format); + return true; +} + +bool Worksheet::setRowHeight(int rowFirst,int rowLast, double height) +{ + QList > rowInfoList = getRowInfoList(rowFirst,rowLast); + + foreach(QSharedPointer rowInfo, rowInfoList) { + rowInfo->height = height; + rowInfo->customHeight = true; + } + + return rowInfoList.count() > 0; +} + +bool Worksheet::setRowFormat(int rowFirst,int rowLast, const Format &format) +{ + Q_D(Worksheet); + + QList > rowInfoList = getRowInfoList(rowFirst,rowLast); + + foreach(QSharedPointer rowInfo, rowInfoList) { + rowInfo->format = format; + } + + d->workbook->styles()->addXfFormat(format); + return rowInfoList.count() > 0; +} + +bool Worksheet::setRowHidden(int rowFirst,int rowLast, bool hidden) +{ + QList > rowInfoList = getRowInfoList(rowFirst,rowLast); + + foreach(QSharedPointer rowInfo, rowInfoList) { + rowInfo->hidden = hidden; + } + + return rowInfoList.count() > 0; +} + +double Worksheet::rowHeight(int row) +{ + Q_D(Worksheet); + int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); + + if (d->checkDimensions(row, min_col, false, true)) + return d->sheetFormatProps.defaultRowHeight; //return default on invalid row? + + return d->rowsInfo[row]->height; +} + +Format Worksheet::rowFormat(int row) +{ + Q_D(Worksheet); + int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); + + if (d->checkDimensions(row, min_col, false, true)) + return Format(); //return default on invalid row? + + return d->rowsInfo[row]->format; +} + +bool Worksheet::isRowHidden(int row) +{ + Q_D(Worksheet); + int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); + + if (d->checkDimensions(row, min_col, false, true)) + return false; //return default on invalid row? + + return d->rowsInfo[row]->hidden; +} + /*! Groups rows from \a rowFirst to \a rowLast with the given \a collapsed. @@ -1829,9 +2078,15 @@ void WorksheetPrivate::loadXmlSheetData(QXmlStreamReader &reader) int idx = attributes.value(QLatin1String("s")).toString().toInt(); info->format = workbook->styles()->xfFormat(idx); } - if (attributes.hasAttribute(QLatin1String("customHeight")) && attributes.hasAttribute(QLatin1String("ht"))) { - info->height = attributes.value(QLatin1String("ht")).toString().toDouble(); + + if (attributes.hasAttribute(QLatin1String("customHeight"))) { + info->customHeight = attributes.value(QLatin1String("customHeight")) == QLatin1String("1"); + //Row height is only specified when customHeight is set + if(attributes.hasAttribute(QLatin1String("ht"))) { + info->height = attributes.value(QLatin1String("ht")).toString().toDouble(); + } } + //both "hidden" and "collapsed" default are false info->hidden = attributes.value(QLatin1String("hidden")) == QLatin1String("1"); info->collapsed = attributes.value(QLatin1String("collapsed")) == QLatin1String("1"); @@ -1953,11 +2208,17 @@ void WorksheetPrivate::loadXmlColumnsInfo(QXmlStreamReader &reader) info->firstColumn = min; info->lastColumn = max; - //!Todo, customWidth support. + //Flag indicating that the column width for the affected column(s) is different from the + // default or has been manually set + if(colAttrs.hasAttribute(QLatin1String("customWidth"))) { + info->customWidth = colAttrs.value(QLatin1String("customWidth")) == QLatin1String("1"); + qDebug() << "Custom width: " << info->customWidth; + } //Note, node may have "width" without "customWidth" if (colAttrs.hasAttribute(QLatin1String("width"))) { double width = colAttrs.value(QLatin1String("width")).toString().toDouble(); info->width = width; + qDebug() << "Width: " << info->width; } info->hidden = colAttrs.value(QLatin1String("hidden")) == QLatin1String("1"); @@ -2052,6 +2313,47 @@ void WorksheetPrivate::loadXmlSheetViews(QXmlStreamReader &reader) } } +void WorksheetPrivate::loadXmlSheetFormatProps(QXmlStreamReader &reader) +{ + Q_ASSERT(reader.name() == QLatin1String("sheetFormatPr")); + QXmlStreamAttributes attributes = reader.attributes(); + XlsxSheetFormatProps formatProps; + + //Retain default values + foreach (QXmlStreamAttribute attrib, attributes) { + if(attrib.name() == QLatin1String("baseColWidth") ) { + formatProps.baseColWidth = attrib.value().toInt(); + } else if(attrib.name() == QLatin1String("customHeight")) { + formatProps.customHeight = attrib.value() == QLatin1String("1"); + } else if(attrib.name() == QLatin1String("defaultColWidth")) { + formatProps.defaultColWidth = attrib.value().toDouble(); + } else if(attrib.name() == QLatin1String("defaultRowHeight")) { + formatProps.defaultRowHeight = attrib.value().toDouble(); + } else if(attrib.name() == QLatin1String("outlineLevelCol")) { + formatProps.outlineLevelCol = attrib.value().toInt(); + } else if(attrib.name() == QLatin1String("outlineLevelRow")) { + formatProps.outlineLevelRow = attrib.value().toInt(); + } else if(attrib.name() == QLatin1String("thickBottom")) { + formatProps.thickBottom = attrib.value() == QLatin1String("1"); + } else if(attrib.name() == QLatin1String("thickTop")) { + formatProps.thickTop = attrib.value() == QLatin1String("1"); + } else if(attrib.name() == QLatin1String("zeroHeight")) { + formatProps.zeroHeight = attrib.value() == QLatin1String("1"); + } + } + + if(formatProps.defaultColWidth == 0.0) { //not set + formatProps.defaultColWidth = WorksheetPrivate::calculateColWidth(formatProps.baseColWidth); + } + +} +double WorksheetPrivate::calculateColWidth(int characters) +{ + //!Todo + //Take normal style' font maximum width and add padding and margin pixels + return characters + 0.5; +} + void WorksheetPrivate::loadXmlHyperlinks(QXmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("hyperlinks")); @@ -2090,6 +2392,7 @@ bool Worksheet::loadFromXmlFile(QIODevice *device) while (!reader.atEnd()) { reader.readNextStartElement(); if (reader.tokenType() == QXmlStreamReader::StartElement) { + qDebug() << "TOKEN: " << reader.name(); if (reader.name() == QLatin1String("dimension")) { QXmlStreamAttributes attributes = reader.attributes(); QString range = attributes.value(QLatin1String("ref")).toString(); @@ -2097,7 +2400,7 @@ bool Worksheet::loadFromXmlFile(QIODevice *device) } else if (reader.name() == QLatin1String("sheetViews")) { d->loadXmlSheetViews(reader); } else if (reader.name() == QLatin1String("sheetFormatPr")) { - + d->loadXmlSheetFormatProps(reader); } else if (reader.name() == QLatin1String("cols")) { d->loadXmlColumnsInfo(reader); } else if (reader.name() == QLatin1String("sheetData")) { diff --git a/src/xlsx/xlsxworksheet.h b/src/xlsx/xlsxworksheet.h index 59a04d6..96004f0 100755 --- a/src/xlsx/xlsxworksheet.h +++ b/src/xlsx/xlsxworksheet.h @@ -50,6 +50,8 @@ class CellRange; class RichString; class Relationships; class Chart; +class XlsxColumnInfo; +class XlsxRowInfo; class WorksheetPrivate; class Q_XLSX_EXPORT Worksheet : public AbstractSheet @@ -99,9 +101,29 @@ public: bool unmergeCells(const CellRange &range); QList mergedCells() const; - bool setRow(int row, double height, const Format &format=Format(), bool hidden=false); - bool setColumn(int colFirst, int colLast, double width, const Format &format=Format(), bool hidden=false); - bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format=Format(), bool hidden=false); + + bool setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden); + bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format, bool hidden); + bool setColumnWidth(const QString &colFirst, const QString &colLast, double width); + bool setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format); + bool setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden); + bool setColumnWidth(int colFirst, int colLast, double width); + bool setColumnFormat(int colFirst, int colLast, const Format &format); + bool setColumnHidden(int colFirst, int colLast, bool hidden); + double columnWidth(int column); + Format columnFormat(int column); + bool isColumnHidden(int column); + + bool setRow(int rowFirst, int rowLast, double height, const Format &format, bool hidden); + bool setRow(int row, double height, const Format &format, bool hidden); + bool setRowHeight(int rowFirst,int rowLast, double height); + bool setRowFormat(int rowFirst,int rowLast, const Format &format); + bool setRowHidden(int rowFirst,int rowLast, bool hidden); + double rowHeight(int row); + Format rowFormat(int row); + bool isRowHidden(int row); + + bool groupRows(int rowFirst, int rowLast, bool collapsed = true); bool groupColumns(int colFirst, int colLast, bool collapsed = true); bool groupColumns(const QString &colFirst, const QString &colLast, bool collapsed = true); @@ -129,6 +151,8 @@ public: void setWhiteSpaceVisible(bool visible); ~Worksheet(); + + private: friend class DocumentPrivate; friend class Workbook; @@ -138,6 +162,11 @@ private: void saveToXmlFile(QIODevice *device) const; bool loadFromXmlFile(QIODevice *device); + + QList > getRowInfoList(int rowFirst, int rowLast); + QList > getColumnInfoList(int colFirst, int colLast); + QList getColumnIndexes(int colFirst, int colLast); + bool isColumnRangeValid(int colFirst, int colLast); }; QT_END_NAMESPACE_XLSX diff --git a/src/xlsx/xlsxworksheet_p.h b/src/xlsx/xlsxworksheet_p.h index 5507eeb..870003d 100644 --- a/src/xlsx/xlsxworksheet_p.h +++ b/src/xlsx/xlsxworksheet_p.h @@ -78,15 +78,50 @@ struct XlsxHyperlinkData QString tooltip; }; +// ECMA-376 Part1 18.3.1.81 +struct XlsxSheetFormatProps +{ + XlsxSheetFormatProps(int baseColWidth = 8, + bool customHeight = false, + double defaultColWidth = 0.0, + double defaultRowHeight = 15, + quint8 outlineLevelCol = 0, + quint8 outlineLevelRow = 0, + bool thickBottom = false, + bool thickTop = false, + bool zeroHeight = false) : + baseColWidth(baseColWidth), + customHeight(customHeight), + defaultColWidth(defaultColWidth), + defaultRowHeight(defaultRowHeight), + outlineLevelCol(outlineLevelCol), + outlineLevelRow(outlineLevelRow), + thickBottom(thickBottom), + thickTop(thickTop), + zeroHeight(zeroHeight) { + } + + int baseColWidth; + bool customHeight; + double defaultColWidth; + double defaultRowHeight; + quint8 outlineLevelCol; + quint8 outlineLevelRow; + bool thickBottom; + bool thickTop; + bool zeroHeight; +}; + struct XlsxRowInfo { XlsxRowInfo(double height=0, const Format &format=Format(), bool hidden=false) : - height(height), format(format), hidden(hidden), outlineLevel(0) + customHeight(false), height(height), format(format), hidden(hidden), outlineLevel(0) , collapsed(false) { } + bool customHeight; double height; Format format; bool hidden; @@ -97,14 +132,15 @@ struct XlsxRowInfo struct XlsxColumnInfo { XlsxColumnInfo(int firstColumn=0, int lastColumn=1, double width=0, const Format &format=Format(), bool hidden=false) : - firstColumn(firstColumn), lastColumn(lastColumn), width(width), format(format), hidden(hidden) + firstColumn(firstColumn), lastColumn(lastColumn), customWidth(false), width(width), format(format), hidden(hidden) , outlineLevel(0), collapsed(false) { } int firstColumn; int lastColumn; - double width; + bool customWidth; + double width; Format format; bool hidden; int outlineLevel; @@ -137,6 +173,7 @@ public: void loadXmlColumnsInfo(QXmlStreamReader &reader); void loadXmlMergeCells(QXmlStreamReader &reader); void loadXmlDataValidations(QXmlStreamReader &reader); + void loadXmlSheetFormatProps(QXmlStreamReader &reader); void loadXmlSheetViews(QXmlStreamReader &reader); void loadXmlHyperlinks(QXmlStreamReader &reader); @@ -166,6 +203,8 @@ public: int default_row_height; bool default_row_zeroed; + XlsxSheetFormatProps sheetFormatProps; + bool windowProtection; bool showFormulas; bool showGridLines; @@ -176,6 +215,9 @@ public: bool showRuler; bool showOutlineSymbols; bool showWhiteSpace; + +private: + static double calculateColWidth(int characters); }; } diff --git a/tests/auto/worksheet/tst_worksheet.cpp b/tests/auto/worksheet/tst_worksheet.cpp index 0f8cdae..e6aa54c 100644 --- a/tests/auto/worksheet/tst_worksheet.cpp +++ b/tests/auto/worksheet/tst_worksheet.cpp @@ -82,10 +82,10 @@ void WorksheetTest::testSheetView() void WorksheetTest::testSetColumn() { QXlsx::Worksheet sheet("", 1, 0, QXlsx::Worksheet::F_NewFromScratch); - sheet.setColumn(1, 11, 20.0); //"A:K" - sheet.setColumn(4, 8, 10.0); //"D:H" - sheet.setColumn(6, 6, 15.0); //"F:F" - sheet.setColumn(1, 9, 8.8); //"A:H" + sheet.setColumnWidth(1, 11, 20.0); //"A:K" + sheet.setColumnWidth(4, 8, 10.0); //"D:H" + sheet.setColumnWidth(6, 6, 15.0); //"F:F" + sheet.setColumnWidth(1, 9, 8.8); //"A:H" QByteArray xmldata = sheet.saveToXmlData(); From bbf3ffd79dbdebc08ea82f02c0375051bc7319aa Mon Sep 17 00:00:00 2001 From: eho Date: Fri, 14 Mar 2014 10:24:30 +0100 Subject: [PATCH 2/5] Fixed a bug where row formatting was not saved when row contained no filled cells. --- src/xlsx/xlsxworksheet.cpp | 63 +++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index ee61edf..b8d7a82 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -1289,51 +1289,44 @@ void WorksheetPrivate::saveXmlSheetData(QXmlStreamWriter &writer) const if (row_spans.contains(span_index)) span = row_spans[span_index]; - if (cellTable.contains(row_num)) { - writer.writeStartElement(QStringLiteral("row")); - writer.writeAttribute(QStringLiteral("r"), QString::number(row_num)); - - if (!span.isEmpty()) - writer.writeAttribute(QStringLiteral("spans"), span); + writer.writeStartElement(QStringLiteral("row")); + writer.writeAttribute(QStringLiteral("r"), QString::number(row_num)); - if (rowsInfo.contains(row_num)) { - QSharedPointer rowInfo = rowsInfo[row_num]; - if (!rowInfo->format.isEmpty()) { - writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format.xfIndex())); - writer.writeAttribute(QStringLiteral("customFormat"), QStringLiteral("1")); - } - //!Todo: support customHeight from info struct - //!Todo: where does this magic number '15' come from? - if (rowInfo->customHeight) { - writer.writeAttribute(QStringLiteral("ht"), QString::number(rowInfo->height)); - writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("1")); - qDebug() << "custom height: " << rowInfo->height; - } else { - writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("0")); - qDebug() << "no height: "; - } + if (!span.isEmpty()) + writer.writeAttribute(QStringLiteral("spans"), span); - - - 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")); + if (rowsInfo.contains(row_num)) { + QSharedPointer rowInfo = rowsInfo[row_num]; + if (!rowInfo->format.isEmpty()) { + writer.writeAttribute(QStringLiteral("s"), QString::number(rowInfo->format.xfIndex())); + writer.writeAttribute(QStringLiteral("customFormat"), QStringLiteral("1")); + } + //!Todo: support customHeight from info struct + //!Todo: where does this magic number '15' come from? + if (rowInfo->customHeight) { + writer.writeAttribute(QStringLiteral("ht"), QString::number(rowInfo->height)); + writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("1")); + } else { + writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("0")); } + 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")); + } + + //Write cell data if row contains filled cells + if (cellTable.contains(row_num)) { for (int col_num = dimension.firstColumn(); col_num <= dimension.lastColumn(); col_num++) { if (cellTable[row_num].contains(col_num)) { saveXmlCellData(writer, row_num, col_num, cellTable[row_num][col_num]); } } - writer.writeEndElement(); //row - } else if (comments.contains(row_num)){ - - } else { - } + writer.writeEndElement(); //row } } From f841a9b119d5a2530cd24bd759e4baef6e86c3ba Mon Sep 17 00:00:00 2001 From: eho Date: Fri, 14 Mar 2014 11:22:15 +0100 Subject: [PATCH 3/5] Cleanup and added comments to new methods. --- src/xlsx/xlsxdocument.cpp | 49 ++++++++++- src/xlsx/xlsxdocument.h | 2 - src/xlsx/xlsxworksheet.cpp | 174 +++++++++++++++++++++++-------------- src/xlsx/xlsxworksheet.h | 3 +- 4 files changed, 159 insertions(+), 69 deletions(-) diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index 0158790..03cc687 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -648,15 +648,27 @@ bool Document::isColumnHidden(int column) bool Document::setRow(int row, double height, const Format &format, bool hidden) { if (Worksheet *sheet = currentWorksheet()) - return sheet->setRow(row, height, format, hidden); + return sheet->setRow(row, row, height, format, hidden); return false; } +/*! + Sets the \a format of the row \a row. + Rows are 1-indexed. + + Returns true if success. +*/ bool Document::setRowFormat(int row, const Format &format) { return setRowFormat(row,row, format); } +/*! + Sets the \a format of the rows including and between \a rowFirst and \a rowLast. + Rows are 1-indexed. + + Returns true if success. +*/ bool Document::setRowFormat(int rowFirst, int rowLast, const Format &format) { if (Worksheet *sheet = currentWorksheet()) @@ -664,11 +676,23 @@ bool Document::setRowFormat(int rowFirst, int rowLast, const Format &format) return false; } +/*! + Sets the \a hidden property of the row \a row. + Rows are 1-indexed. If hidden is true rows will not be visible. + + Returns true if success. +*/ bool Document::setRowHidden(int row, bool hidden) { return setRowHidden(row,row,hidden); } +/*! + Sets the \a hidden property of the rows including and between \a rowFirst and \a rowLast. + Rows are 1-indexed. If hidden is true rows will not be visible. + + Returns true if success. +*/ bool Document::setRowHidden(int rowFirst, int rowLast, bool hidden) { if (Worksheet *sheet = currentWorksheet()) @@ -676,11 +700,25 @@ bool Document::setRowHidden(int rowFirst, int rowLast, bool hidden) return false; } +/*! + Sets the \a height of the row \a row. + Row height measured in point size. + Rows are 1-indexed. + + Returns true if success. +*/ bool Document::setRowHeight(int row, double height) { return setRowHeight(row,row,height); } +/*! + Sets the \a height of the rows including and between \a rowFirst and \a rowLast. + Row height measured in point size. + Rows are 1-indexed. + + Returns true if success. +*/ bool Document::setRowHeight(int rowFirst, int rowLast, double height) { if (Worksheet *sheet = currentWorksheet()) @@ -688,6 +726,9 @@ bool Document::setRowHeight(int rowFirst, int rowLast, double height) return false; } +/*! + Returns height of \a row in points. +*/ double Document::rowHeight(int row) { if (Worksheet *sheet = currentWorksheet()) @@ -695,6 +736,9 @@ double Document::rowHeight(int row) return 0.0; // ? } +/*! + Returns format of \a row. +*/ Format Document::rowFormat(int row) { if (Worksheet *sheet = currentWorksheet()) @@ -702,6 +746,9 @@ Format Document::rowFormat(int row) return Format(); // ? } +/*! + Returns true if \a row is hidden. +*/ bool Document::isRowHidden(int row) { if (Worksheet *sheet = currentWorksheet()) diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index c3552ea..fa5d7d0 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -96,8 +96,6 @@ public: Format rowFormat(int row); bool isRowHidden(int row); - - bool groupRows(int rowFirst, int rowLast, bool collapsed = true); bool groupColumns(int colFirst, int colLast, bool collapsed = true); bool addDataValidation(const DataValidation &validation); diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index b8d7a82..95d769d 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -1611,6 +1611,10 @@ bool Worksheet::setColumn(const QString &colFirst, const QString &colLast, doubl return setColumn(col1, col2, width, format, hidden); } +/*! + Sets width in characters of a range of columns. Columns can be specified as "A", "B" ... "Z". + Returns true on success. + */ bool Worksheet::setColumnWidth(const QString &colFirst, const QString &colLast, double width) { int col1 = xl_col_name_to_value(colFirst); @@ -1621,6 +1625,10 @@ bool Worksheet::setColumnWidth(const QString &colFirst, const QString &colLast, return setColumnWidth(col1, col2, width); } +/*! + Sets format property of a range of columns. Columns are 1-indexed. + Returns true on success. + */ bool Worksheet::setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format) { int col1 = xl_col_name_to_value(colFirst); @@ -1631,6 +1639,11 @@ bool Worksheet::setColumnFormat(const QString &colFirst, const QString &colLast, return setColumnFormat(col1, col2, format); } +/*! + Sets hidden property of a range of columns. Columns are 1-indexed. + Hidden columns are not visible. + Returns true on success. + */ bool Worksheet::setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden) { int col1 = xl_col_name_to_value(colFirst); @@ -1641,54 +1654,10 @@ bool Worksheet::setColumnHidden(const QString &colFirst, const QString &colLast, return setColumnHidden(col1, col2, hidden); } -QList > Worksheet::getColumnInfoList(int colFirst, int colLast) -{ - Q_D(Worksheet); - QList > columnsInfoList; - if(isColumnRangeValid(colFirst,colLast)) - { - QList nodes = getColumnIndexes(colFirst, colLast); - - for (int idx = 0; idx < nodes.size(); ++idx) { - int colStart = nodes[idx]; - if (d->colsInfo.contains(colStart)) { - QSharedPointer info = d->colsInfo[colStart]; - columnsInfoList.append(info); - } else { - int colEnd = (idx == nodes.size() - 1) ? colLast : nodes[idx+1] - 1; - QSharedPointer info(new XlsxColumnInfo(colStart, colEnd)); - d->colsInfo.insert(colFirst, info); - columnsInfoList.append(info); - for (int c = colStart; c <= colEnd; ++c) - d->colsInfoHelper[c] = info; - } - } - } - - return columnsInfoList; -} - -QList > Worksheet::getRowInfoList(int rowFirst, int rowLast) -{ - Q_D(Worksheet); - QList > rowInfoList; - - int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); - - for(int row = rowFirst; row <= rowLast; ++row) { - if (d->checkDimensions(row, min_col, false, true)) - continue; - - QSharedPointer rowInfo; - if ((d->rowsInfo[row]).isNull()){ - d->rowsInfo[row] = QSharedPointer(new XlsxRowInfo()); - } - rowInfoList.append(d->rowsInfo[row]); - } - - return rowInfoList; -} - +/*! + Sets width in characters of a range of columns. Columns are 1-indexed. + Returns true on success. + */ bool Worksheet::setColumnWidth(int colFirst, int colLast, double width) { QList > columnInfoList = getColumnInfoList(colFirst, colLast); @@ -1699,6 +1668,9 @@ bool Worksheet::setColumnWidth(int colFirst, int colLast, double width) return (columnInfoList.count() > 0); } +/*! + Sets format property of a range of columns. Columns are 1-indexed. + */ bool Worksheet::setColumnFormat(int colFirst, int colLast, const Format &format) { Q_D(Worksheet); @@ -1716,6 +1688,9 @@ bool Worksheet::setColumnFormat(int colFirst, int colLast, const Format &format) return false; } +/*! + Sets hidden property of a range of columns. Columns are 1-indexed. + */ bool Worksheet::setColumnHidden(int colFirst, int colLast, bool hidden) { QList > columnInfoList = getColumnInfoList(colFirst, colLast); @@ -1726,41 +1701,44 @@ bool Worksheet::setColumnHidden(int colFirst, int colLast, bool hidden) return (columnInfoList.count() > 0); } +/*! + Returns width of the column in characters of the normal font. Columns are 1-indexed. + */ double Worksheet::columnWidth(int column) { Q_D(Worksheet); QList > columnInfoList = getColumnInfoList(column, column); if (columnInfoList.count() == 1) { - qDebug() << "Whoopie Width"; return columnInfoList.at(0)->width ; } - qDebug() << "Default Width"; return d->sheetFormatProps.defaultColWidth; } +/*! + Returns formatting of the column. Columns are 1-indexed. + */ Format Worksheet::columnFormat(int column) { QList > columnInfoList = getColumnInfoList(column, column); if (columnInfoList.count() == 1) { - qDebug() << "Whoopie Format"; return columnInfoList.at(0)->format; } - qDebug() << "Default Format"; return Format(); } +/*! + Returns true if column is hidden. Columns are 1-indexed. + */ bool Worksheet::isColumnHidden(int column) { QList > columnInfoList = getColumnInfoList(column, column); if (columnInfoList.count() == 1) { - qDebug() << "Whoopie Hidden"; return columnInfoList.at(0)->hidden; } - qDebug() << "Default Hidden"; return false; } @@ -1771,11 +1749,6 @@ bool Worksheet::isColumnHidden(int column) Returns true if success. */ -bool Worksheet::setRow(int row, double height, const Format &format, bool hidden) -{ - return setRow(row,row, height, format, hidden); -} - bool Worksheet::setRow(int rowFirst,int rowLast, double height, const Format &format, bool hidden) { Q_D(Worksheet); @@ -1791,6 +1764,13 @@ bool Worksheet::setRow(int rowFirst,int rowLast, double height, const Format &fo return true; } +/*! + Sets the \a height of the rows including and between \a rowFirst and \a rowLast. + Row height measured in point size. + Rows are 1-indexed. + + Returns true if success. +*/ bool Worksheet::setRowHeight(int rowFirst,int rowLast, double height) { QList > rowInfoList = getRowInfoList(rowFirst,rowLast); @@ -1803,6 +1783,12 @@ bool Worksheet::setRowHeight(int rowFirst,int rowLast, double height) return rowInfoList.count() > 0; } +/*! + Sets the \a format of the rows including and between \a rowFirst and \a rowLast. + Rows are 1-indexed. + + Returns true if success. +*/ bool Worksheet::setRowFormat(int rowFirst,int rowLast, const Format &format) { Q_D(Worksheet); @@ -1817,6 +1803,12 @@ bool Worksheet::setRowFormat(int rowFirst,int rowLast, const Format &format) return rowInfoList.count() > 0; } +/*! + Sets the \a hidden proeprty of the rows including and between \a rowFirst and \a rowLast. + Rows are 1-indexed. If hidden is true rows will not be visible. + + Returns true if success. +*/ bool Worksheet::setRowHidden(int rowFirst,int rowLast, bool hidden) { QList > rowInfoList = getRowInfoList(rowFirst,rowLast); @@ -1828,6 +1820,9 @@ bool Worksheet::setRowHidden(int rowFirst,int rowLast, bool hidden) return rowInfoList.count() > 0; } +/*! + Returns height of \a row in points. +*/ double Worksheet::rowHeight(int row) { Q_D(Worksheet); @@ -1839,6 +1834,9 @@ double Worksheet::rowHeight(int row) return d->rowsInfo[row]->height; } +/*! + Returns format of \a row. +*/ Format Worksheet::rowFormat(int row) { Q_D(Worksheet); @@ -1850,6 +1848,9 @@ Format Worksheet::rowFormat(int row) return d->rowsInfo[row]->format; } +/*! + Returns true if \a row is hidden. +*/ bool Worksheet::isRowHidden(int row) { Q_D(Worksheet); @@ -1861,6 +1862,54 @@ bool Worksheet::isRowHidden(int row) return d->rowsInfo[row]->hidden; } +QList > Worksheet::getColumnInfoList(int colFirst, int colLast) +{ + Q_D(Worksheet); + QList > columnsInfoList; + if(isColumnRangeValid(colFirst,colLast)) + { + QList nodes = getColumnIndexes(colFirst, colLast); + + for (int idx = 0; idx < nodes.size(); ++idx) { + int colStart = nodes[idx]; + if (d->colsInfo.contains(colStart)) { + QSharedPointer info = d->colsInfo[colStart]; + columnsInfoList.append(info); + } else { + int colEnd = (idx == nodes.size() - 1) ? colLast : nodes[idx+1] - 1; + QSharedPointer info(new XlsxColumnInfo(colStart, colEnd)); + d->colsInfo.insert(colFirst, info); + columnsInfoList.append(info); + for (int c = colStart; c <= colEnd; ++c) + d->colsInfoHelper[c] = info; + } + } + } + + return columnsInfoList; +} + +QList > Worksheet::getRowInfoList(int rowFirst, int rowLast) +{ + Q_D(Worksheet); + QList > rowInfoList; + + int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); + + for(int row = rowFirst; row <= rowLast; ++row) { + if (d->checkDimensions(row, min_col, false, true)) + continue; + + QSharedPointer rowInfo; + if ((d->rowsInfo[row]).isNull()){ + d->rowsInfo[row] = QSharedPointer(new XlsxRowInfo()); + } + rowInfoList.append(d->rowsInfo[row]); + } + + return rowInfoList; +} + /*! Groups rows from \a rowFirst to \a rowLast with the given \a collapsed. @@ -2204,14 +2253,12 @@ void WorksheetPrivate::loadXmlColumnsInfo(QXmlStreamReader &reader) //Flag indicating that the column width for the affected column(s) is different from the // default or has been manually set if(colAttrs.hasAttribute(QLatin1String("customWidth"))) { - info->customWidth = colAttrs.value(QLatin1String("customWidth")) == QLatin1String("1"); - qDebug() << "Custom width: " << info->customWidth; + info->customWidth = colAttrs.value(QLatin1String("customWidth")) == QLatin1String("1"); } //Note, node may have "width" without "customWidth" if (colAttrs.hasAttribute(QLatin1String("width"))) { double width = colAttrs.value(QLatin1String("width")).toString().toDouble(); info->width = width; - qDebug() << "Width: " << info->width; } info->hidden = colAttrs.value(QLatin1String("hidden")) == QLatin1String("1"); @@ -2384,8 +2431,7 @@ bool Worksheet::loadFromXmlFile(QIODevice *device) QXmlStreamReader reader(device); while (!reader.atEnd()) { reader.readNextStartElement(); - if (reader.tokenType() == QXmlStreamReader::StartElement) { - qDebug() << "TOKEN: " << reader.name(); + if (reader.tokenType() == QXmlStreamReader::StartElement) { if (reader.name() == QLatin1String("dimension")) { QXmlStreamAttributes attributes = reader.attributes(); QString range = attributes.value(QLatin1String("ref")).toString(); diff --git a/src/xlsx/xlsxworksheet.h b/src/xlsx/xlsxworksheet.h index 96004f0..7c91e05 100755 --- a/src/xlsx/xlsxworksheet.h +++ b/src/xlsx/xlsxworksheet.h @@ -115,15 +115,14 @@ public: bool isColumnHidden(int column); bool setRow(int rowFirst, int rowLast, double height, const Format &format, bool hidden); - bool setRow(int row, double height, const Format &format, bool hidden); bool setRowHeight(int rowFirst,int rowLast, double height); bool setRowFormat(int rowFirst,int rowLast, const Format &format); bool setRowHidden(int rowFirst,int rowLast, bool hidden); + double rowHeight(int row); Format rowFormat(int row); bool isRowHidden(int row); - bool groupRows(int rowFirst, int rowLast, bool collapsed = true); bool groupColumns(int colFirst, int colLast, bool collapsed = true); bool groupColumns(const QString &colFirst, const QString &colLast, bool collapsed = true); From 7ee11a6c02a80e815c1035308a0c9288b40bec80 Mon Sep 17 00:00:00 2001 From: Edwin van der Horst Date: Sat, 15 Mar 2014 11:46:38 +0100 Subject: [PATCH 4/5] Fixed review comments. setRow/Column to WorksheetPrivate. Added deprecated methods warnings. --- src/xlsx/xlsxdocument.cpp | 6 +- src/xlsx/xlsxdocument.h | 5 +- src/xlsx/xlsxworksheet.cpp | 158 +++++++++++++++++++------------------ src/xlsx/xlsxworksheet.h | 15 ++-- src/xlsx/xlsxworksheet_p.h | 5 ++ 5 files changed, 101 insertions(+), 88 deletions(-) diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index 03cc687..eb07fce 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -733,7 +733,7 @@ double Document::rowHeight(int row) { if (Worksheet *sheet = currentWorksheet()) return sheet->rowHeight(row); - return 0.0; // ? + return 0.0; } /*! @@ -743,7 +743,7 @@ Format Document::rowFormat(int row) { if (Worksheet *sheet = currentWorksheet()) return sheet->rowFormat(row); - return Format(); // ? + return Format(); } /*! @@ -753,7 +753,7 @@ bool Document::isRowHidden(int row) { if (Worksheet *sheet = currentWorksheet()) return sheet->isRowHidden(row); - return false; // ? + return false; } /*! diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index fa5d7d0..1e2bad3 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -65,9 +65,10 @@ public: bool mergeCells(const QString &range, const Format &format=Format()); bool unmergeCells(const CellRange &range); bool unmergeCells(const QString &range); - bool setRow(int row, double height, const Format &format=Format(), bool hidden=false); + Q_DECL_DEPRECATED bool setColumn(int colFirst, int colLast, double width, const Format &format=Format(), bool hidden=false); + Q_DECL_DEPRECATED bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format=Format(), bool hidden=false); bool setColumnWidth(const QString &column, double width); bool setColumnFormat(const QString &column, const Format &format); @@ -85,6 +86,8 @@ public: Format columnFormat(int column); bool isColumnHidden(int column); + Q_DECL_DEPRECATED + bool setRow(int row, double height, const Format &format=Format(), bool hidden=false); bool setRowHeight(int row, double height); bool setRowFormat(int row, const Format &format); bool setRowHidden(int row, bool hidden); diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index 95d769d..6001199 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -1528,36 +1528,33 @@ void WorksheetPrivate::splitColsInfo(int colFirst, int colLast) } } -bool Worksheet::isColumnRangeValid(int colFirst, int colLast) +bool WorksheetPrivate::isColumnRangeValid(int colFirst, int colLast) { - Q_D(Worksheet); bool ignore_row = true; bool ignore_col = false; if (colFirst > colLast) return false; - if (d->checkDimensions(0, colLast, ignore_row, ignore_col)) + if (checkDimensions(0, colLast, ignore_row, ignore_col)) return false; - if (d->checkDimensions(0, colFirst, ignore_row, ignore_col)) + if (checkDimensions(0, colFirst, ignore_row, ignore_col)) return false; return true; } -QList Worksheet::getColumnIndexes(int colFirst, int colLast) +QList WorksheetPrivate ::getColumnIndexes(int colFirst, int colLast) { - Q_D(Worksheet); - - d->splitColsInfo(colFirst, colLast); + splitColsInfo(colFirst, colLast); QList nodes; nodes.append(colFirst); for (int col = colFirst; col <= colLast; ++col) { - if (d->colsInfo.contains(col)) { + if (colsInfo.contains(col)) { if (nodes.last() != col) nodes.append(col); - int nextCol = d->colsInfo[col]->lastColumn + 1; + int nextCol = colsInfo[col]->lastColumn + 1; if (nextCol <= colLast) nodes.append(nextCol); } @@ -1579,7 +1576,7 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, const Format { Q_D(Worksheet); - QList > columnInfoList = getColumnInfoList(colFirst, colLast); + QList > columnInfoList = d->getColumnInfoList(colFirst, colLast); foreach(QSharedPointer columnInfo, columnInfoList) { columnInfo->width = width; columnInfo->format = format; @@ -1660,7 +1657,9 @@ bool Worksheet::setColumnHidden(const QString &colFirst, const QString &colLast, */ bool Worksheet::setColumnWidth(int colFirst, int colLast, double width) { - QList > columnInfoList = getColumnInfoList(colFirst, colLast); + Q_D(Worksheet); + + QList > columnInfoList = d->getColumnInfoList(colFirst, colLast); foreach(QSharedPointer columnInfo, columnInfoList) { columnInfo->width = width; } @@ -1675,7 +1674,7 @@ bool Worksheet::setColumnFormat(int colFirst, int colLast, const Format &format) { Q_D(Worksheet); - QList > columnInfoList = getColumnInfoList(colFirst, colLast); + QList > columnInfoList = d->getColumnInfoList(colFirst, colLast); foreach(QSharedPointer columnInfo, columnInfoList) { columnInfo->format = format; } @@ -1693,7 +1692,9 @@ bool Worksheet::setColumnFormat(int colFirst, int colLast, const Format &format) */ bool Worksheet::setColumnHidden(int colFirst, int colLast, bool hidden) { - QList > columnInfoList = getColumnInfoList(colFirst, colLast); + Q_D(Worksheet); + + QList > columnInfoList = d->getColumnInfoList(colFirst, colLast); foreach(QSharedPointer columnInfo, columnInfoList) { columnInfo->hidden = hidden; } @@ -1708,7 +1709,7 @@ double Worksheet::columnWidth(int column) { Q_D(Worksheet); - QList > columnInfoList = getColumnInfoList(column, column); + QList > columnInfoList = d->getColumnInfoList(column, column); if (columnInfoList.count() == 1) { return columnInfoList.at(0)->width ; } @@ -1721,7 +1722,9 @@ double Worksheet::columnWidth(int column) */ Format Worksheet::columnFormat(int column) { - QList > columnInfoList = getColumnInfoList(column, column); + Q_D(Worksheet); + + QList > columnInfoList = d->getColumnInfoList(column, column); if (columnInfoList.count() == 1) { return columnInfoList.at(0)->format; } @@ -1734,7 +1737,9 @@ Format Worksheet::columnFormat(int column) */ bool Worksheet::isColumnHidden(int column) { - QList > columnInfoList = getColumnInfoList(column, column); + Q_D(Worksheet); + + QList > columnInfoList = d->getColumnInfoList(column, column); if (columnInfoList.count() == 1) { return columnInfoList.at(0)->hidden; } @@ -1752,7 +1757,8 @@ bool Worksheet::isColumnHidden(int column) bool Worksheet::setRow(int rowFirst,int rowLast, double height, const Format &format, bool hidden) { Q_D(Worksheet); - QList > rowInfoList = getRowInfoList(rowFirst,rowLast); + + QList > rowInfoList = d->getRowInfoList(rowFirst,rowLast); foreach(QSharedPointer rowInfo, rowInfoList) { rowInfo->height = height; @@ -1772,8 +1778,10 @@ bool Worksheet::setRow(int rowFirst,int rowLast, double height, const Format &fo Returns true if success. */ bool Worksheet::setRowHeight(int rowFirst,int rowLast, double height) -{ - QList > rowInfoList = getRowInfoList(rowFirst,rowLast); +{ + Q_D(Worksheet); + + QList > rowInfoList = d->getRowInfoList(rowFirst,rowLast); foreach(QSharedPointer rowInfo, rowInfoList) { rowInfo->height = height; @@ -1793,7 +1801,7 @@ bool Worksheet::setRowFormat(int rowFirst,int rowLast, const Format &format) { Q_D(Worksheet); - QList > rowInfoList = getRowInfoList(rowFirst,rowLast); + QList > rowInfoList = d->getRowInfoList(rowFirst,rowLast); foreach(QSharedPointer rowInfo, rowInfoList) { rowInfo->format = format; @@ -1811,7 +1819,9 @@ bool Worksheet::setRowFormat(int rowFirst,int rowLast, const Format &format) */ bool Worksheet::setRowHidden(int rowFirst,int rowLast, bool hidden) { - QList > rowInfoList = getRowInfoList(rowFirst,rowLast); + Q_D(Worksheet); + + QList > rowInfoList = d->getRowInfoList(rowFirst,rowLast); foreach(QSharedPointer rowInfo, rowInfoList) { rowInfo->hidden = hidden; @@ -1862,54 +1872,6 @@ bool Worksheet::isRowHidden(int row) return d->rowsInfo[row]->hidden; } -QList > Worksheet::getColumnInfoList(int colFirst, int colLast) -{ - Q_D(Worksheet); - QList > columnsInfoList; - if(isColumnRangeValid(colFirst,colLast)) - { - QList nodes = getColumnIndexes(colFirst, colLast); - - for (int idx = 0; idx < nodes.size(); ++idx) { - int colStart = nodes[idx]; - if (d->colsInfo.contains(colStart)) { - QSharedPointer info = d->colsInfo[colStart]; - columnsInfoList.append(info); - } else { - int colEnd = (idx == nodes.size() - 1) ? colLast : nodes[idx+1] - 1; - QSharedPointer info(new XlsxColumnInfo(colStart, colEnd)); - d->colsInfo.insert(colFirst, info); - columnsInfoList.append(info); - for (int c = colStart; c <= colEnd; ++c) - d->colsInfoHelper[c] = info; - } - } - } - - return columnsInfoList; -} - -QList > Worksheet::getRowInfoList(int rowFirst, int rowLast) -{ - Q_D(Worksheet); - QList > rowInfoList; - - int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn(); - - for(int row = rowFirst; row <= rowLast; ++row) { - if (d->checkDimensions(row, min_col, false, true)) - continue; - - QSharedPointer rowInfo; - if ((d->rowsInfo[row]).isNull()){ - d->rowsInfo[row] = QSharedPointer(new XlsxRowInfo()); - } - rowInfoList.append(d->rowsInfo[row]); - } - - return rowInfoList; -} - /*! Groups rows from \a rowFirst to \a rowLast with the given \a collapsed. @@ -2362,17 +2324,17 @@ void WorksheetPrivate::loadXmlSheetFormatProps(QXmlStreamReader &reader) //Retain default values foreach (QXmlStreamAttribute attrib, attributes) { if(attrib.name() == QLatin1String("baseColWidth") ) { - formatProps.baseColWidth = attrib.value().toInt(); + formatProps.baseColWidth = attrib.value().toString().toInt(); } else if(attrib.name() == QLatin1String("customHeight")) { formatProps.customHeight = attrib.value() == QLatin1String("1"); } else if(attrib.name() == QLatin1String("defaultColWidth")) { - formatProps.defaultColWidth = attrib.value().toDouble(); + formatProps.defaultColWidth = attrib.value().toString().toDouble(); } else if(attrib.name() == QLatin1String("defaultRowHeight")) { - formatProps.defaultRowHeight = attrib.value().toDouble(); + formatProps.defaultRowHeight = attrib.value().toString().toDouble(); } else if(attrib.name() == QLatin1String("outlineLevelCol")) { - formatProps.outlineLevelCol = attrib.value().toInt(); + formatProps.outlineLevelCol = attrib.value().toString().toInt(); } else if(attrib.name() == QLatin1String("outlineLevelRow")) { - formatProps.outlineLevelRow = attrib.value().toInt(); + formatProps.outlineLevelRow = attrib.value().toString().toInt(); } else if(attrib.name() == QLatin1String("thickBottom")) { formatProps.thickBottom = attrib.value() == QLatin1String("1"); } else if(attrib.name() == QLatin1String("thickTop")) { @@ -2424,6 +2386,52 @@ void WorksheetPrivate::loadXmlHyperlinks(QXmlStreamReader &reader) } } +QList > WorksheetPrivate::getColumnInfoList(int colFirst, int colLast) +{ + QList > columnsInfoList; + if(isColumnRangeValid(colFirst,colLast)) + { + QList nodes = getColumnIndexes(colFirst, colLast); + + for (int idx = 0; idx < nodes.size(); ++idx) { + int colStart = nodes[idx]; + if (colsInfo.contains(colStart)) { + QSharedPointer info = colsInfo[colStart]; + columnsInfoList.append(info); + } else { + int colEnd = (idx == nodes.size() - 1) ? colLast : nodes[idx+1] - 1; + QSharedPointer info(new XlsxColumnInfo(colStart, colEnd)); + colsInfo.insert(colFirst, info); + columnsInfoList.append(info); + for (int c = colStart; c <= colEnd; ++c) + colsInfoHelper[c] = info; + } + } + } + + return columnsInfoList; +} + +QList > WorksheetPrivate::getRowInfoList(int rowFirst, int rowLast) +{ + QList > rowInfoList; + + int min_col = dimension.firstColumn() < 0 ? 0 : dimension.firstColumn(); + + for(int row = rowFirst; row <= rowLast; ++row) { + if (checkDimensions(row, min_col, false, true)) + continue; + + QSharedPointer rowInfo; + if ((rowsInfo[row]).isNull()){ + rowsInfo[row] = QSharedPointer(new XlsxRowInfo()); + } + rowInfoList.append(rowsInfo[row]); + } + + return rowInfoList; +} + bool Worksheet::loadFromXmlFile(QIODevice *device) { Q_D(Worksheet); diff --git a/src/xlsx/xlsxworksheet.h b/src/xlsx/xlsxworksheet.h index 7c91e05..17214cb 100755 --- a/src/xlsx/xlsxworksheet.h +++ b/src/xlsx/xlsxworksheet.h @@ -101,9 +101,10 @@ public: bool unmergeCells(const CellRange &range); QList mergedCells() const; - - bool setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden); - bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format, bool hidden); + Q_DECL_DEPRECATED + bool setColumn(int colFirst, int colLast, double width, const Format &format=Format(), bool hidden=false); + Q_DECL_DEPRECATED + bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format=Format(), bool hidden=false); bool setColumnWidth(const QString &colFirst, const QString &colLast, double width); bool setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format); bool setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden); @@ -114,7 +115,8 @@ public: Format columnFormat(int column); bool isColumnHidden(int column); - bool setRow(int rowFirst, int rowLast, double height, const Format &format, bool hidden); + Q_DECL_DEPRECATED + bool setRow(int rowFirst, int rowLast, double height, const Format &format=Format(), bool hidden=false); bool setRowHeight(int rowFirst,int rowLast, double height); bool setRowFormat(int rowFirst,int rowLast, const Format &format); bool setRowHidden(int rowFirst,int rowLast, bool hidden); @@ -161,11 +163,6 @@ private: void saveToXmlFile(QIODevice *device) const; bool loadFromXmlFile(QIODevice *device); - - QList > getRowInfoList(int rowFirst, int rowLast); - QList > getColumnInfoList(int colFirst, int colLast); - QList getColumnIndexes(int colFirst, int colLast); - bool isColumnRangeValid(int colFirst, int colLast); }; QT_END_NAMESPACE_XLSX diff --git a/src/xlsx/xlsxworksheet_p.h b/src/xlsx/xlsxworksheet_p.h index 870003d..0599167 100644 --- a/src/xlsx/xlsxworksheet_p.h +++ b/src/xlsx/xlsxworksheet_p.h @@ -177,6 +177,11 @@ public: void loadXmlSheetViews(QXmlStreamReader &reader); void loadXmlHyperlinks(QXmlStreamReader &reader); + QList > getRowInfoList(int rowFirst, int rowLast); + QList > getColumnInfoList(int colFirst, int colLast); + QList getColumnIndexes(int colFirst, int colLast); + bool isColumnRangeValid(int colFirst, int colLast); + SharedStrings *sharedStrings() const; QMap > > cellTable; From cafef771a2d39c6d45e7102ebd48c5ac5baf6121 Mon Sep 17 00:00:00 2001 From: Edwin van der Horst Date: Mon, 17 Mar 2014 11:14:06 +0100 Subject: [PATCH 5/5] Fixed review comments: Removed deprecated members. Replaced string arguments by CellRange. --- src/xlsx/xlsxdocument.cpp | 107 ++++++++++++++++--------------------- src/xlsx/xlsxdocument.h | 15 ++---- src/xlsx/xlsxworksheet.cpp | 71 +++++------------------- src/xlsx/xlsxworksheet.h | 10 ++-- 4 files changed, 64 insertions(+), 139 deletions(-) diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index eb07fce..b2a5cdb 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -515,88 +515,68 @@ bool Document::unmergeCells(const CellRange &range) } /*! - Sets the column properties for all columns from \a colFirst to \a colLast with - the given \a width, \a format and \a hidden. Column - width measured as the number of characters of the maximum digit width of the - numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. - \a colFirst and \a colLast are all 1-indexed. - - Return false if failed. + Sets width in characters of a range of columns. + Returns true on success. */ -bool Document::setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden) +bool Document::setColumnWidth(const CellRange &range, double width) { if (Worksheet *sheet = currentWorksheet()) - return sheet->setColumn(colFirst, colLast, width, format, hidden); + return sheet->setColumnWidth(range, width); return false; } /*! - \overload - - Sets column width and format for all columns from \a colFirst to \a colLast with - the given \a width and \a format. Column - \a width measured as the number of characters of the maximum digit width of the - numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. - \a colFirst and \a colLast should be "A", "B", "C", ... + Sets format property of a range of columns. + Returns true on success. */ -bool Document::setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format, bool hidden) -{ - if (Worksheet *sheet = currentWorksheet()) - return sheet->setColumn(colFirst, colLast, width, format, hidden); - return false; -} - -bool Document::setColumnWidth(const QString &column, double width) -{ - return setColumnWidth(column,column,width); -} - -bool Document::setColumnFormat(const QString &column, const Format &format) -{ - return setColumnFormat(column,column,format); -} - -bool Document::setColumnHidden(const QString &column, bool hidden) -{ - return setColumnHidden(column,column,hidden); -} - -bool Document::setColumnWidth(const QString &colFirst, const QString &colLast, double width) -{ - if (Worksheet *sheet = currentWorksheet()) - return sheet->setColumnWidth(colFirst, colLast, width); - return false; -} - -bool Document::setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format) +bool Document::setColumnFormat(const CellRange &range, const Format &format) { if (Worksheet *sheet = currentWorksheet()) - return sheet->setColumnFormat(colFirst, colLast, format); + return sheet->setColumnFormat(range, format); return false; } -bool Document::setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden) +/*! + Sets hidden property of a range of columns. Columns are 1-indexed. + Hidden columns are not visible. + Returns true on success. + */ +bool Document::setColumnHidden(const CellRange &range, bool hidden) { if (Worksheet *sheet = currentWorksheet()) - return sheet->setColumnWidth(colFirst, colLast, hidden); + return sheet->setColumnWidth(range, hidden); return false; } +/*! + Sets width in characters of a range of columns. Columns are 1-indexed. + Returns true on success. + */ bool Document::setColumnWidth(int column, double width) { return setColumnWidth(column,column,width); } +/*! + Sets format property of a range of columns. Columns are 1-indexed. + */ bool Document::setColumnFormat(int column, const Format &format) { return setColumnFormat(column,column,format); } +/*! + Sets hidden property of a column. Columns are 1-indexed. + */ bool Document::setColumnHidden(int column, bool hidden) { return setColumnHidden(column,column,hidden); } +/*! + Sets width in characters of a range of columns. Columns are 1-indexed. + Returns true on success. + */ bool Document::setColumnWidth(int colFirst, int colLast, double width) { if (Worksheet *sheet = currentWorksheet()) @@ -604,6 +584,9 @@ bool Document::setColumnWidth(int colFirst, int colLast, double width) return false; } +/*! + Sets format property of a range of columns. Columns are 1-indexed. + */ bool Document::setColumnFormat(int colFirst, int colLast, const Format &format) { if (Worksheet *sheet = currentWorksheet()) @@ -611,6 +594,10 @@ bool Document::setColumnFormat(int colFirst, int colLast, const Format &format) return false; } + +/*! + Sets hidden property of a range of columns. Columns are 1-indexed. + */ bool Document::setColumnHidden(int colFirst, int colLast, bool hidden) { if (Worksheet *sheet = currentWorksheet()) @@ -618,6 +605,9 @@ bool Document::setColumnHidden(int colFirst, int colLast, bool hidden) return false; } +/*! + Returns width of the column in characters of the normal font. Columns are 1-indexed. + */ double Document::columnWidth(int column) { if (Worksheet *sheet = currentWorksheet()) @@ -625,6 +615,9 @@ double Document::columnWidth(int column) return 0.0; } +/*! + Returns formatting of the column. Columns are 1-indexed. + */ Format Document::columnFormat(int column) { if (Worksheet *sheet = currentWorksheet()) @@ -632,23 +625,13 @@ Format Document::columnFormat(int column) return Format(); } -bool Document::isColumnHidden(int column) -{ - if (Worksheet *sheet = currentWorksheet()) - return sheet->isColumnHidden(column); - return false; -} - /*! - Sets the properties of \a row with the given \a height, \a format and \a hidden. - \a row is 1-indexed. - - Returns false if failed. + Returns true if column is hidden. Columns are 1-indexed. */ -bool Document::setRow(int row, double height, const Format &format, bool hidden) +bool Document::isColumnHidden(int column) { if (Worksheet *sheet = currentWorksheet()) - return sheet->setRow(row, row, height, format, hidden); + return sheet->isColumnHidden(column); return false; } diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index 1e2bad3..7770870 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -66,16 +66,9 @@ public: bool unmergeCells(const CellRange &range); bool unmergeCells(const QString &range); - Q_DECL_DEPRECATED - bool setColumn(int colFirst, int colLast, double width, const Format &format=Format(), bool hidden=false); - Q_DECL_DEPRECATED - bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format=Format(), bool hidden=false); - bool setColumnWidth(const QString &column, double width); - bool setColumnFormat(const QString &column, const Format &format); - bool setColumnHidden(const QString &column, bool hidden); - bool setColumnWidth(const QString &colFirst, const QString &colLast, double width); - bool setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format); - bool setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden); + bool setColumnWidth(const CellRange &range, double width); + bool setColumnFormat(const CellRange &range, const Format &format); + bool setColumnHidden(const CellRange &range, bool hidden); bool setColumnWidth(int column, double width); bool setColumnFormat(int column, const Format &format); bool setColumnHidden(int column, bool hidden); @@ -86,8 +79,6 @@ public: Format columnFormat(int column); bool isColumnHidden(int column); - Q_DECL_DEPRECATED - bool setRow(int row, double height, const Format &format=Format(), bool hidden=false); bool setRowHeight(int row, double height); bool setRowFormat(int row, const Format &format); bool setRowHidden(int row, bool hidden); diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index 6001199..cc4e8e7 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -1564,59 +1564,14 @@ QList WorksheetPrivate ::getColumnIndexes(int colFirst, int colLast) } /*! - Sets column \a width and \a format for all columns from \a colFirst to \a colLast. Column - width measured as the number of characters of the maximum digit width of the - numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format - equals 0 then format is ignored. Both \a colFirst and \a colLast are all 1-indexed. - Hides the column if \a hidden is true. - - Return true if success. - */ -bool Worksheet::setColumn(int colFirst, int colLast, double width, const Format &format, bool hidden) -{ - Q_D(Worksheet); - - QList > columnInfoList = d->getColumnInfoList(colFirst, colLast); - foreach(QSharedPointer columnInfo, columnInfoList) { - columnInfo->width = width; - columnInfo->format = format; - columnInfo->hidden = hidden; - } - - if(columnInfoList.count() > 0) { - d->workbook->styles()->addXfFormat(format); - return true; - } - - return false; -} - -/*! - Sets column width and format for all columns from colFirst to colLast. Column - width measured as the number of characters of the maximum digit width of the - numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. If format - equals 0 then format is ignored. \a colFirst and \a colLast should be "A", "B", "C", ... - */ -bool Worksheet::setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format, bool hidden) -{ - int col1 = xl_col_name_to_value(colFirst); - int col2 = xl_col_name_to_value(colLast); - - if (col1 == -1 || col2 == -1) - return false; - - return setColumn(col1, col2, width, format, hidden); -} - -/*! - Sets width in characters of a range of columns. Columns can be specified as "A", "B" ... "Z". + Sets width in characters of a range of columns. Returns true on success. */ -bool Worksheet::setColumnWidth(const QString &colFirst, const QString &colLast, double width) +bool Worksheet::setColumnWidth(const CellRange &range, double width) { - int col1 = xl_col_name_to_value(colFirst); - int col2 = xl_col_name_to_value(colLast); - if (col1 == -1 || col2 == -1) + int col1 = range.firstColumn(); + int col2 = range.lastColumn(); + if (col1 < 0|| col2 < 0) return false; return setColumnWidth(col1, col2, width); @@ -1626,11 +1581,11 @@ bool Worksheet::setColumnWidth(const QString &colFirst, const QString &colLast, Sets format property of a range of columns. Columns are 1-indexed. Returns true on success. */ -bool Worksheet::setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format) +bool Worksheet::setColumnFormat(const CellRange& range, const Format &format) { - int col1 = xl_col_name_to_value(colFirst); - int col2 = xl_col_name_to_value(colLast); - if (col1 == -1 || col2 == -1) + int col1 = range.firstColumn(); + int col2 = range.lastColumn(); + if (col1 < 0|| col2 < 0) return false; return setColumnFormat(col1, col2, format); @@ -1641,11 +1596,11 @@ bool Worksheet::setColumnFormat(const QString &colFirst, const QString &colLast, Hidden columns are not visible. Returns true on success. */ -bool Worksheet::setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden) +bool Worksheet::setColumnHidden(const CellRange &range, bool hidden) { - int col1 = xl_col_name_to_value(colFirst); - int col2 = xl_col_name_to_value(colLast); - if (col1 == -1 || col2 == -1) + int col1 = range.firstColumn(); + int col2 = range.lastColumn(); + if (col1 < 0|| col2 < 0) return false; return setColumnHidden(col1, col2, hidden); diff --git a/src/xlsx/xlsxworksheet.h b/src/xlsx/xlsxworksheet.h index 17214cb..193f81b 100755 --- a/src/xlsx/xlsxworksheet.h +++ b/src/xlsx/xlsxworksheet.h @@ -101,13 +101,9 @@ public: bool unmergeCells(const CellRange &range); QList mergedCells() const; - Q_DECL_DEPRECATED - bool setColumn(int colFirst, int colLast, double width, const Format &format=Format(), bool hidden=false); - Q_DECL_DEPRECATED - bool setColumn(const QString &colFirst, const QString &colLast, double width, const Format &format=Format(), bool hidden=false); - bool setColumnWidth(const QString &colFirst, const QString &colLast, double width); - bool setColumnFormat(const QString &colFirst, const QString &colLast, const Format &format); - bool setColumnHidden(const QString &colFirst, const QString &colLast, bool hidden); + bool setColumnWidth(const CellRange& range, double width); + bool setColumnFormat(const CellRange& range, const Format &format); + bool setColumnHidden(const CellRange& range, bool hidden); bool setColumnWidth(int colFirst, int colLast, double width); bool setColumnFormat(int colFirst, int colLast, const Format &format); bool setColumnHidden(int colFirst, int colLast, bool hidden);