From 27c0029df1ac055f6c1b6275939dad79c9bfb43b Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Thu, 31 Oct 2013 12:19:48 +0800 Subject: [PATCH] Add overload function for setRow and setColumn --- src/xlsx/xlsxdocument.cpp | 31 ++++++++++++++++++----- src/xlsx/xlsxdocument.h | 2 ++ src/xlsx/xlsxutility.cpp | 18 +++++++++++++ src/xlsx/xlsxutility_p.h | 1 + src/xlsx/xlsxworksheet.cpp | 52 ++++++++++++++++++++++++++++++-------- src/xlsx/xlsxworksheet.h | 2 ++ src/xlsx/xlsxworksheet_p.h | 8 +++--- 7 files changed, 94 insertions(+), 20 deletions(-) diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index 3013ce3..dd0e956 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -170,18 +170,37 @@ bool Document::setRow(int row, double height, Format *format, bool hidden) } /*! - * \brief Set properties for columns of cells. - * \param First column (zero-indexed). - * \param Last column (zero-indexed). - * \param width The width of the column(s). - * \param format Optional Format object. - * \param hidden + \overload + Sets row height and format. Row height measured in point size. If format + equals 0 then format is ignored. \a row should be "1", "2", "3", ... + */ +bool Document::setRow(const QString &row, double height, Format *format, bool hidden) +{ + return currentWorksheet()->setRow(row, height, format, hidden); +} + +/*! + 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 are all zero-indexed. */ bool Document::setColumn(int colFirst, int colLast, double width, Format *format, bool hidden) { return currentWorksheet()->setColumn(colFirst, colLast, width, format, hidden); } +/*! + 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 Document::setColumn(const QString &colFirst, const QString &colLast, double width, Format *format, bool hidden) +{ + return currentWorksheet()->setColumn(colFirst, colLast, width, format, hidden); +} + /*! * \brief Add a data validation rule for current worksheet * \param validation diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index 137df0b..bfaf55e 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -61,7 +61,9 @@ public: int mergeCells(const QString &range); int unmergeCells(const QString &range); bool setRow(int row, double height, Format* format=0, bool hidden=false); + 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 addDataValidation(const DataValidation &validation); Cell *cellAt(const QString &cell) const; diff --git a/src/xlsx/xlsxutility.cpp b/src/xlsx/xlsxutility.cpp index 16760c9..b9f459b 100755 --- a/src/xlsx/xlsxutility.cpp +++ b/src/xlsx/xlsxutility.cpp @@ -129,6 +129,24 @@ QString xl_col_to_name(int col_num) return col_str; } +int xl_col_name_to_value(const QString &col_str) +{ + QRegularExpression re(QStringLiteral("^([A-Z]{1,3})$")); + QRegularExpressionMatch match = re.match(col_str); + if (match.hasMatch()) { + int col = 0; + int expn = 0; + for (int i=col_str.size()-1; i>-1; --i) { + col += (col_str[i].unicode() - 'A' + 1) * intPow(26, expn); + expn++; + } + + col--; + return col; + } + return -1; +} + QString xl_rowcol_to_cell(int row, int col, bool row_abs, bool col_abs) { row += 1; //Change to 1-index diff --git a/src/xlsx/xlsxutility_p.h b/src/xlsx/xlsxutility_p.h index 958797f..35a4017 100755 --- a/src/xlsx/xlsxutility_p.h +++ b/src/xlsx/xlsxutility_p.h @@ -42,6 +42,7 @@ XLSX_AUTOTEST_EXPORT QDateTime datetimeFromNumber(double num, bool is1904=false) 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 int xl_col_name_to_value(const QString &col_str); XLSX_AUTOTEST_EXPORT QString xl_rowcol_to_cell(int row, int col, bool row_abs=false, bool col_abs=false); XLSX_AUTOTEST_EXPORT QString xl_rowcol_to_cell_fast(int row, int col); diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index 0331206..d60a402 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -804,8 +804,8 @@ void Worksheet::saveToXmlFile(QIODevice *device) for (int i=0; icolsInfo.size(); ++i) { QSharedPointer col_info = d->colsInfo[i]; writer.writeStartElement(QStringLiteral("col")); - writer.writeAttribute(QStringLiteral("min"), QString::number(col_info->column_min + 1)); - writer.writeAttribute(QStringLiteral("max"), QString::number(col_info->column_max)); + writer.writeAttribute(QStringLiteral("min"), QString::number(col_info->firstColumn + 1)); + writer.writeAttribute(QStringLiteral("max"), QString::number(col_info->lastColumn + 1)); writer.writeAttribute(QStringLiteral("width"), QString::number(col_info->width, 'g', 15)); if (col_info->format) writer.writeAttribute(QStringLiteral("style"), QString::number(col_info->format->xfIndex())); @@ -1072,9 +1072,9 @@ void WorksheetPrivate::writeDrawings(XmlStreamWriter &writer) writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(index)); } -/* +/*! Sets row height and format. Row height measured in point size. If format - equals 0 then format is ignored. + equals 0 then format is ignored. \a row is zero-indexed. */ bool Worksheet::setRow(int row, double height, Format *format, bool hidden) { @@ -1089,7 +1089,22 @@ bool Worksheet::setRow(int row, double height, Format *format, bool hidden) return true; } -/* +/*! + \overload + Sets row height and format. Row height measured in point size. If format + equals 0 then format is ignored. \a row should be "1", "2", "3", ... + */ +bool Worksheet::setRow(const QString &row, double height, Format *format, bool hidden) +{ + bool ok=true; + int r = row.toInt(&ok); + if (ok) + return setRow(r, height, format, hidden); + + 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 @@ -1101,7 +1116,7 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, Format *forma bool ignore_row = true; bool ignore_col = (format || (width && hidden)) ? false : true; - if (colFirst >= colLast) + if (colFirst > colLast) return false; if (d->checkDimensions(0, colLast, ignore_row, ignore_col)) @@ -1112,7 +1127,7 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, Format *forma QSharedPointer info(new XlsxColumnInfo(colFirst, colLast, width, format, hidden)); d->colsInfo.append(info); - for (int col=colFirst; colcolsInfoHelper[col] = info; d->workbook->styles()->addFormat(format); @@ -1120,6 +1135,23 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, Format *forma return true; } +/*! + 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, 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); +} + /*! Return the range that contains cell data. */ @@ -1489,8 +1521,8 @@ void WorksheetPrivate::readColumnsInfo(XmlStreamReader &reader) QXmlStreamAttributes colAttrs = reader.attributes(); int min = colAttrs.value(QLatin1String("min")).toInt(); int max = colAttrs.value(QLatin1String("max")).toInt(); - info->column_min = min - 1; - info->column_max = max; + info->firstColumn = min - 1; + info->lastColumn = max - 1; if (colAttrs.hasAttribute(QLatin1String("customWidth"))) { double width = colAttrs.value(QLatin1String("width")).toDouble(); @@ -1506,7 +1538,7 @@ void WorksheetPrivate::readColumnsInfo(XmlStreamReader &reader) } colsInfo.append(info); - for (int col=min; col