diff --git a/examples/xlsx/definename/main.cpp b/examples/xlsx/definename/main.cpp index 75ff2e8..9491eb2 100644 --- a/examples/xlsx/definename/main.cpp +++ b/examples/xlsx/definename/main.cpp @@ -7,10 +7,10 @@ int main() { //![0] Document xlsx; - for (int i=0; i<10; ++i) { - xlsx.write(i, 0, i); - xlsx.write(i, 1, i*i); - xlsx.write(i, 2, i*i*i); + for (int i=1; i<=10; ++i) { + xlsx.write(i, 1, i); + xlsx.write(i, 2, i*i); + xlsx.write(i, 3, i*i*i); } //![0] //![1] @@ -20,12 +20,12 @@ int main() xlsx.defineName("Factor", "=0.5"); //![1] //![2] - xlsx.write(10, 0, "=SUM(MyCol_1)"); - xlsx.write(10, 1, "=SUM(MyCol_2)"); - xlsx.write(10, 2, "=SUM(MyCol_3)"); - xlsx.write(11, 0, "=SUM(MyCol_1)*Factor"); - xlsx.write(11, 1, "=SUM(MyCol_2)*Factor"); - xlsx.write(11, 2, "=SUM(MyCol_3)*Factor"); + xlsx.write(11, 1, "=SUM(MyCol_1)"); + xlsx.write(11, 2, "=SUM(MyCol_2)"); + xlsx.write(11, 3, "=SUM(MyCol_3)"); + xlsx.write(12, 1, "=SUM(MyCol_1)*Factor"); + xlsx.write(12, 2, "=SUM(MyCol_2)*Factor"); + xlsx.write(12, 3, "=SUM(MyCol_3)*Factor"); //![2] xlsx.save(); diff --git a/examples/xlsx/demo/main.cpp b/examples/xlsx/demo/main.cpp index 91c7da2..2dad0fd 100644 --- a/examples/xlsx/demo/main.cpp +++ b/examples/xlsx/demo/main.cpp @@ -74,18 +74,18 @@ void writeInternalNumFormatsCell(Document &xlsx, int row, double value, int numF { Format *format = xlsx.createFormat(); format->setNumberFormatIndex(numFmt); - xlsx.write(row, 0, value); - xlsx.write(row, 1, QString("Builtin NumFmt %1").arg(numFmt)); - xlsx.write(row, 2, value, format); + xlsx.write(row, 1, value); + xlsx.write(row, 2, QString("Builtin NumFmt %1").arg(numFmt)); + xlsx.write(row, 3, value, format); } void writeCustomNumFormatsCell(Document &xlsx, int row, double value, const QString &numFmt) { Format *format = xlsx.createFormat(); format->setNumberFormat(numFmt); - xlsx.write(row, 0, value); - xlsx.write(row, 1, numFmt); - xlsx.write(row, 2, value, format); + xlsx.write(row, 1, value); + xlsx.write(row, 2, numFmt); + xlsx.write(row, 3, value, format); } int main() @@ -243,19 +243,19 @@ int main() //Create the fourth sheet. xlsx.addWorksheet("NumFormats"); xlsx.setColumn("B", "B", 40); - writeInternalNumFormatsCell(xlsx, 3, 2.5681, 2); - writeInternalNumFormatsCell(xlsx, 4, 2500000, 3); - writeInternalNumFormatsCell(xlsx, 5, -500, 5); - writeInternalNumFormatsCell(xlsx, 6, -0.25, 9); - writeInternalNumFormatsCell(xlsx, 7, 890, 11); - writeInternalNumFormatsCell(xlsx, 8, 0.75, 12); - writeInternalNumFormatsCell(xlsx, 9, 41499, 14); - writeInternalNumFormatsCell(xlsx, 10, 41499, 17); - - writeCustomNumFormatsCell(xlsx, 12, 20.5627, "#.###"); - writeCustomNumFormatsCell(xlsx, 13, 4.8, "#.00"); - writeCustomNumFormatsCell(xlsx, 14, 1.23, "0.00 \"RMB\""); - writeCustomNumFormatsCell(xlsx, 15, 60, "[Red][<=100];[Green][>100]"); + writeInternalNumFormatsCell(xlsx, 4, 2.5681, 2); + writeInternalNumFormatsCell(xlsx, 5, 2500000, 3); + writeInternalNumFormatsCell(xlsx, 6, -500, 5); + writeInternalNumFormatsCell(xlsx, 7, -0.25, 9); + writeInternalNumFormatsCell(xlsx, 8, 890, 11); + writeInternalNumFormatsCell(xlsx, 9, 0.75, 12); + writeInternalNumFormatsCell(xlsx, 10, 41499, 14); + writeInternalNumFormatsCell(xlsx, 11, 41499, 17); + + writeCustomNumFormatsCell(xlsx, 13, 20.5627, "#.###"); + writeCustomNumFormatsCell(xlsx, 14, 4.8, "#.00"); + writeCustomNumFormatsCell(xlsx, 15, 1.23, "0.00 \"RMB\""); + writeCustomNumFormatsCell(xlsx, 16, 60, "[Red][<=100];[Green][>100]"); //--------------------------------------------------------------- //Create the fifth sheet. @@ -274,16 +274,17 @@ int main() //Create the fifth sheet. xlsx.addWorksheet("Grouping"); qsrand(QDateTime::currentMSecsSinceEpoch()); - for (int row=1; row<31; ++row) { - for (int col=0; col<10; ++col) + for (int row=2; row<31; ++row) { + for (int col=1; col<=10; ++col) xlsx.write(row, col, qrand() % 100); } - xlsx.groupRows(3, 6); - xlsx.groupRows(10, 25, false); - xlsx.groupRows(14, 16, false); - xlsx.groupRows(19, 21, false); - xlsx.groupColumns(0, 1); - xlsx.groupColumns(4, 7, false); + xlsx.groupRows(4, 7); + xlsx.groupRows(11, 26, false); + xlsx.groupRows(15, 17, false); + xlsx.groupRows(20, 22, false); + xlsx.setColumn(1, 10, 10.0); + xlsx.groupColumns(1, 2); + xlsx.groupColumns(5, 8, false); xlsx.saveAs("Book1.xlsx"); diff --git a/examples/xlsx/numberformat/main.cpp b/examples/xlsx/numberformat/main.cpp index 967ceb3..e099811 100644 --- a/examples/xlsx/numberformat/main.cpp +++ b/examples/xlsx/numberformat/main.cpp @@ -7,7 +7,7 @@ int main(int argc, char** argv) QGuiApplication(argc, argv); QXlsx::Document xlsx; - xlsx.setColumn(0, 4, 20.0); + xlsx.setColumn(1, 4, 20.0); QXlsx::Format *header = xlsx.createFormat(); header->setFontBold(true); @@ -19,32 +19,32 @@ int main(int argc, char** argv) <<"yyyy-mmm-dd" <<"$ #,##0.00" <<"[red]0.00"; - xlsx.write(0, 0, "Raw data", header); - xlsx.write(0, 1, "Format", header); - xlsx.write(0, 2, "Shown value", header); + xlsx.write(1, 1, "Raw data", header); + xlsx.write(1, 2, "Format", header); + xlsx.write(1, 3, "Shown value", header); for (int i=0; isetNumberFormat(numFormats[i]); - xlsx.write(row, 2, 100.0, format); + xlsx.write(row, 3, 100.0, format); } //Builtin number formats xlsx.addWorksheet(); - xlsx.setColumn(0, 4, 20.0); - xlsx.write(0, 0, "Raw data", header); - xlsx.write(0, 1, "Builtin Format", header); - xlsx.write(0, 2, "Shown value", header); + xlsx.setColumn(1, 4, 20.0); + xlsx.write(1, 1, "Raw data", header); + xlsx.write(1, 2, "Builtin Format", header); + xlsx.write(1, 3, "Shown value", header); for (int i=0; i<50; ++i) { - int row = i+1; + int row = i+2; int numFmt = i; - xlsx.write(row, 0, 100.0); - xlsx.write(row, 1, numFmt); + xlsx.write(row, 1, 100.0); + xlsx.write(row, 2, numFmt); QXlsx::Format *format = xlsx.createFormat(); format->setNumberFormatIndex(numFmt); - xlsx.write(row, 2, 100.0, format); + xlsx.write(row, 3, 100.0, format); } xlsx.save(); diff --git a/examples/xlsx/rowcolumn/main.cpp b/examples/xlsx/rowcolumn/main.cpp index 639a579..7e20062 100644 --- a/examples/xlsx/rowcolumn/main.cpp +++ b/examples/xlsx/rowcolumn/main.cpp @@ -5,31 +5,31 @@ int main() { QXlsx::Document xlsx; - xlsx.write(0, 2, "Row:0, Col:2 ==> (C1)"); + xlsx.write(1, 2, "Row:0, Col:2 ==> (C1)"); //Set the height of the first row to 50.0(points) - xlsx.setRow(0, 50.0); + xlsx.setRow(1, 50.0); //Set the width of the third column to 40.0(chars) - xlsx.setColumn(2, 3, 40.0); + xlsx.setColumn(3, 3, 40.0); //Set style for the row 11th. QXlsx::Format *format1 = xlsx.createFormat(); format1->setFontBold(true); format1->setFontColor(QColor(Qt::blue)); format1->setFontSize(20); - xlsx.write(10, 0, "Hello Row Style"); - xlsx.write(10, 5, "Blue Color"); - xlsx.setRow(10, 40, format1); + xlsx.write(11, 1, "Hello Row Style"); + xlsx.write(11, 6, "Blue Color"); + xlsx.setRow(11, 41, format1); //Set style for the col [9th, 16th) QXlsx::Format *format2 = xlsx.createFormat(); format2->setFontBold(true); format2->setFontColor(QColor(Qt::magenta)); - for (int row=11; row<30; row++) - for (int col=8; col<15; col++) + for (int row=12; row<=30; row++) + for (int col=9; col<=15; col++) xlsx.write(row, col, row+col); - xlsx.setColumn(8, 15, 5.0, format2); + xlsx.setColumn(9, 16, 5.0, format2); xlsx.save(); return 0; diff --git a/examples/xlsx/style/main.cpp b/examples/xlsx/style/main.cpp index 6b97577..5e9eb0f 100644 --- a/examples/xlsx/style/main.cpp +++ b/examples/xlsx/style/main.cpp @@ -24,17 +24,17 @@ int main() format3->setFontBold(true); format3->setFontColor(QColor(Qt::blue)); format3->setFontSize(20); - xlsx.write(10, 0, "Hello Row Style"); - xlsx.write(10, 5, "Blue Color"); - xlsx.setRow(10, 40, format3); + xlsx.write(11, 1, "Hello Row Style"); + xlsx.write(11, 6, "Blue Color"); + xlsx.setRow(11, 41, format3); QXlsx::Format *format4 = xlsx.createFormat(); format4->setFontBold(true); format4->setFontColor(QColor(Qt::magenta)); - for (int row=20; row<40; row++) - for (int col=8; col<15; col++) + for (int row=21; row<=40; row++) + for (int col=9; col<16; col++) xlsx.write(row, col, row+col); - xlsx.setColumn(8, 15, 5.0, format4); + xlsx.setColumn(9, 16, 5.0, format4); xlsx.write("A5", QDate(2013, 8, 29)); diff --git a/src/xlsx/xlsxdocument.cpp b/src/xlsx/xlsxdocument.cpp index cae48bf..47dfcb6 100644 --- a/src/xlsx/xlsxdocument.cpp +++ b/src/xlsx/xlsxdocument.cpp @@ -181,32 +181,19 @@ int Document::unmergeCells(const CellRange &range) } /*! - * \brief Set properties for a row of cells. - * \param row The worksheet row (zero indexed). - * \param height The row height. - * \param format Optional Format object. - * \param hidden + Sets row \a height and \a format. Row height measured in point size. If format + equals 0 then format is ignored. \a row is 1-indexed. */ bool Document::setRow(int row, double height, Format *format, bool hidden) { return currentWorksheet()->setRow(row, height, format, 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. + equals 0 then format is ignored. \a colFirst and \a colLast are all 1-indexed. */ bool Document::setColumn(int colFirst, int colLast, double width, Format *format, bool hidden) { diff --git a/src/xlsx/xlsxdocument.h b/src/xlsx/xlsxdocument.h index 097dc7f..3a8053e 100644 --- a/src/xlsx/xlsxdocument.h +++ b/src/xlsx/xlsxdocument.h @@ -63,7 +63,6 @@ public: int unmergeCells(const CellRange &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 groupRows(int rowFirst, int rowLast, bool collapsed = true); diff --git a/src/xlsx/xlsxutility.cpp b/src/xlsx/xlsxutility.cpp index b9f459b..0e6eeb2 100755 --- a/src/xlsx/xlsxutility.cpp +++ b/src/xlsx/xlsxutility.cpp @@ -91,7 +91,7 @@ QDateTime datetimeFromNumber(double num, bool is1904) QPoint xl_cell_to_rowcol(const QString &cell_str) { if (cell_str.isEmpty()) - return QPoint(0, 0); + return QPoint(-1, -1); QRegularExpression re(QStringLiteral("^([A-Z]{1,3})(\\d+)$")); QRegularExpressionMatch match = re.match(cell_str); if (match.hasMatch()) { @@ -104,8 +104,7 @@ QPoint xl_cell_to_rowcol(const QString &cell_str) expn++; } - col--; - int row = row_str.toInt() - 1; + int row = row_str.toInt(); return QPoint(row, col); } else { return QPoint(-1, -1); //... @@ -114,7 +113,6 @@ QPoint xl_cell_to_rowcol(const QString &cell_str) QString xl_col_to_name(int col_num) { - col_num += 1; //Change to 1-index QString col_str; int remainder; @@ -141,7 +139,6 @@ int xl_col_name_to_value(const QString &col_str) expn++; } - col--; return col; } return -1; @@ -149,7 +146,6 @@ int xl_col_name_to_value(const QString &col_str) QString xl_rowcol_to_cell(int row, int col, bool row_abs, bool col_abs) { - row += 1; //Change to 1-index QString cell_str; if (col_abs) cell_str.append(QLatin1Char('$')); @@ -170,7 +166,7 @@ QString xl_rowcol_to_cell_fast(int row, int col) col_str = xl_col_to_name(col); col_cache[col] = col_str; } - return col_str + QString::number(row+1); + return col_str + QString::number(row); } } //namespace QXlsx diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index f8747bf..ba2c0b7 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -122,7 +122,7 @@ void WorksheetPrivate::calculateSpans() } } - if ((row_num + 1)%16 == 0 || row_num == dimension.lastRow()) { + if ((row_num)%16 == 0 || row_num == dimension.lastRow()) { int span_index = row_num / 16; if (span_max != INT32_MIN) { span_min += 1; @@ -426,6 +426,10 @@ QList > Worksheet::drawingLinks() const return d->drawingLinks; } +/*! + * Write \a value to cell (\a row, \a column) with the \a format. + * Both \a row and \a column are all 1-indexed value. + */ int Worksheet::write(int row, int column, const QVariant &value, Format *format) { Q_D(Worksheet); @@ -850,9 +854,10 @@ void Worksheet::saveToXmlFile(QIODevice *device) it.next(); QSharedPointer col_info = it.value(); writer.writeStartElement(QStringLiteral("col")); - 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)); + writer.writeAttribute(QStringLiteral("min"), QString::number(col_info->firstColumn)); + writer.writeAttribute(QStringLiteral("max"), QString::number(col_info->lastColumn)); + if (col_info->width) + 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())); if (col_info->hidden) @@ -891,14 +896,14 @@ void WorksheetPrivate::writeSheetData(XmlStreamWriter &writer) continue; } - int span_index = row_num / 16; + int span_index = (row_num-1) / 16; QString span; 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 + 1)); + writer.writeAttribute(QStringLiteral("r"), QString::number(row_num)); if (!span.isEmpty()) writer.writeAttribute(QStringLiteral("spans"), span); @@ -938,10 +943,10 @@ void WorksheetPrivate::writeSheetData(XmlStreamWriter &writer) void WorksheetPrivate::writeCellData(XmlStreamWriter &writer, int row, int col, QSharedPointer cell) { //This is the innermost loop so efficiency is important. - QString cell_range = xl_rowcol_to_cell_fast(row, col); + QString cell_pos = xl_rowcol_to_cell_fast(row, col); writer.writeStartElement(QStringLiteral("c")); - writer.writeAttribute(QStringLiteral("r"), cell_range); + writer.writeAttribute(QStringLiteral("r"), cell_pos); //Style used by the cell, row or col if (cell->format()) @@ -1127,8 +1132,8 @@ void WorksheetPrivate::writeDrawings(XmlStreamWriter &writer) } /*! - Sets row height and format. Row height measured in point size. If format - equals 0 then format is ignored. \a row is zero-indexed. + Sets row \a height and \a format. Row height measured in point size. If format + equals 0 then format is ignored. \a row is 1-indexed. */ bool Worksheet::setRow(int row, double height, Format *format, bool hidden) { @@ -1143,21 +1148,6 @@ 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-1, height, format, hidden); - - return false; -} - void WorksheetPrivate::splitColsInfo(int colFirst, int colLast) { // Split current columnInfo, for example, if "A:H" has been set, @@ -1201,10 +1191,10 @@ void WorksheetPrivate::splitColsInfo(int colFirst, int colLast) } /*! - Sets column width and format for all columns from colFirst to colLast. Column + 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. + equals 0 then format is ignored. Both \a colFirst and \a colLast are all 1-indexed. */ bool Worksheet::setColumn(int colFirst, int colLast, double width, Format *format, bool hidden) { @@ -1625,7 +1615,7 @@ void WorksheetPrivate::readSheetData(XmlStreamReader &reader) //"r" is optional too. if (attributes.hasAttribute(QLatin1String("r"))) { - int row = attributes.value(QLatin1String("r")).toInt()-1; + int row = attributes.value(QLatin1String("r")).toInt(); rowsInfo[row] = info; } } @@ -1731,8 +1721,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->firstColumn = min - 1; - info->lastColumn = max - 1; + info->firstColumn = min; + info->lastColumn = max; //!Todo, customWidth support. //Note, node may have "width" without "customWidth" diff --git a/tests/auto/utility/tst_utilitytest.cpp b/tests/auto/utility/tst_utilitytest.cpp index e5ea725..71151bc 100644 --- a/tests/auto/utility/tst_utilitytest.cpp +++ b/tests/auto/utility/tst_utilitytest.cpp @@ -68,18 +68,18 @@ void UtilityTest::test_cell_to_rowcol_data() QTest::addColumn("row"); QTest::addColumn("col"); - QTest::newRow("A1") << "A1" << 0 << 0; - QTest::newRow("B1") << "B1" << 0 << 1; - QTest::newRow("C1") << "C1" << 0 << 2; - QTest::newRow("J1") << "J1" << 0 << 9; - QTest::newRow("A2") << "A2" << 1 << 0; - QTest::newRow("A3") << "A3" << 2 << 0; - QTest::newRow("A10") << "A10" << 9 << 0; - QTest::newRow("Z8") << "Z8" << 7 << 25; - QTest::newRow("AA10") << "AA10" << 9 << 26; - QTest::newRow("IU2") << "IU2" << 1 << 254; - QTest::newRow("XFD1") << "XFD1" << 0 << 16383; - QTest::newRow("XFE1048577") << "XFE1048577" << 1048576 << 16384; + QTest::newRow("A1") << "A1" << 1 << 1; + QTest::newRow("B1") << "B1" << 1 << 2; + QTest::newRow("C1") << "C1" << 1 << 3; + QTest::newRow("J1") << "J1" << 1 << 10; + QTest::newRow("A2") << "A2" << 2 << 1; + QTest::newRow("A3") << "A3" << 3 << 1; + QTest::newRow("A10") << "A10" << 10 << 1; + QTest::newRow("Z8") << "Z8" << 8 << 26; + QTest::newRow("AA10") << "AA10" << 10 << 27; + QTest::newRow("IU2") << "IU2" << 2 << 255; + QTest::newRow("XFD1") << "XFD1" << 1 << 16384; + QTest::newRow("XFE1048577") << "XFE1048577" << 1048577 << 16385; } void UtilityTest::test_rowcol_to_cell() @@ -101,11 +101,11 @@ void UtilityTest::test_rowcol_to_cell_data() QTest::addColumn("col_abs"); QTest::addColumn("cell"); - QTest::newRow("simple") << 0 << 0 << false << false << "A1"; - QTest::newRow("rowabs") << 0 << 0 << true << false << "A$1"; - QTest::newRow("colabs") << 0 << 0 << false << true << "$A1"; - QTest::newRow("bothabs") << 0 << 0 << true << true << "$A$1"; - QTest::newRow("...") << 1048576 << 16384 << false << false << "XFE1048577"; + QTest::newRow("simple") << 1 << 1 << false << false << "A1"; + QTest::newRow("rowabs") << 1 << 1 << true << false << "A$1"; + QTest::newRow("colabs") << 1 << 1 << false << true << "$A1"; + QTest::newRow("bothabs") << 1 << 1 << true << true << "$A$1"; + QTest::newRow("...") << 1048577 << 16385 << false << false << "XFE1048577"; } void UtilityTest::test_datetimeToNumber_data() diff --git a/tests/auto/worksheet/tst_worksheet.cpp b/tests/auto/worksheet/tst_worksheet.cpp index d437d15..aa37071 100644 --- a/tests/auto/worksheet/tst_worksheet.cpp +++ b/tests/auto/worksheet/tst_worksheet.cpp @@ -55,16 +55,16 @@ void WorksheetTest::testDimension() sheet.write("C3", "Test"); qDebug()<readSheetData(reader); QCOMPARE(sheet.d_ptr->rowsInfo.size(), 1); - QCOMPARE(sheet.d_ptr->rowsInfo[2]->height, 40.0); + QCOMPARE(sheet.d_ptr->rowsInfo[3]->height, 40.0); } void WorksheetTest::testReadMergeCells()