Browse Source

Add basic worksheet copy support

master
Debao Zhang 11 years ago
parent
commit
c6898ff732
  1. 8
      examples/xlsx/worksheetoperations/doc/src/worksheetoperations.qdoc
  2. 33
      examples/xlsx/worksheetoperations/main.cpp
  3. 9
      examples/xlsx/worksheetoperations/worksheetoperations.pro
  4. 1
      examples/xlsx/xlsx.pro
  5. 17
      src/xlsx/xlsxcell.cpp
  6. 1
      src/xlsx/xlsxcell.h
  7. 1
      src/xlsx/xlsxcell_p.h
  8. 2
      src/xlsx/xlsxdocument.h
  9. 28
      src/xlsx/xlsxworkbook.cpp
  10. 2
      src/xlsx/xlsxworkbook.h
  11. 38
      src/xlsx/xlsxworksheet.cpp
  12. 1
      src/xlsx/xlsxworksheet.h
  13. 53
      tests/auto/document/tst_documenttest.cpp

8
examples/xlsx/worksheetoperations/doc/src/worksheetoperations.qdoc

@ -0,0 +1,8 @@
/*!
\example worksheetoperations
\title Worksheet Operations Example
\brief Copy, delete, move worksheet
\ingroup qtxlsx-examples
This example demonstrates how to copy, delete, move worksheet.
*/

33
examples/xlsx/worksheetoperations/main.cpp

@ -0,0 +1,33 @@
#include <QtCore>
#include "xlsxdocument.h"
int main()
{
QXlsx::Document xlsx;
xlsx.renameWorksheet("Sheet1", "TheFirstSheet");
for (int i=1; i<20; ++i) {
for (int j=1; j<15; ++j)
xlsx.write(i, j, QString("R %1 C %2").arg(i).arg(j));
}
xlsx.addWorksheet("TheSecondSheet");
xlsx.write(2, 2, "Hello Qt Xlsx");
xlsx.copyWorksheet("TheFirstSheet", "CopyOfTheFirst");
xlsx.addWorksheet("TheForthSheet");
xlsx.write(3, 3, "This will be deleted...");
xlsx.selectWorksheet("CopyOfTheFirst");
xlsx.write(25, 2, "On the Copy Sheet");
xlsx.deleteWorksheet("TheForthSheet");
xlsx.moveWorksheet("TheSecondSheet", 0);
xlsx.save();
return 0;
}

9
examples/xlsx/worksheetoperations/worksheetoperations.pro

@ -0,0 +1,9 @@
TARGET = worksheetoperations
#include(../../../src/xlsx/qtxlsx.pri)
QT+=xlsx
CONFIG += console
CONFIG -= app_bundle
SOURCES += main.cpp

1
examples/xlsx/xlsx.pro

@ -12,5 +12,6 @@ SUBDIRS = hello \
formulas \
richtext \
conditionalformatting \
worksheetoperations \
demo

17
src/xlsx/xlsxcell.cpp

@ -39,6 +39,14 @@ CellPrivate::CellPrivate(Cell *p) :
}
CellPrivate::CellPrivate(const CellPrivate * const cp)
: value(cp->value), formula(cp->formula), dataType(cp->dataType)
, format(cp->format), range(cp->range), richString(cp->richString)
, parent(cp->parent)
{
}
/*!
\class Cell
\inmodule QtXlsx
@ -72,6 +80,15 @@ Cell::Cell(const QVariant &data, DataType type, const Format &format, Worksheet
d_ptr->parent = parent;
}
/*!
* \internal
*/
Cell::Cell(const Cell * const cell):
d_ptr(new CellPrivate(cell->d_ptr))
{
d_ptr->q_ptr = this;
}
/*!
* Destroys the Cell and cleans up.
*/

1
src/xlsx/xlsxcell.h

@ -67,6 +67,7 @@ private:
friend class WorksheetPrivate;
Cell(const QVariant &data=QVariant(), DataType type=Blank, const Format &format=Format(), Worksheet *parent=0);
Cell(const Cell * const cell);
CellPrivate * const d_ptr;
};

1
src/xlsx/xlsxcell_p.h

@ -50,6 +50,7 @@ class CellPrivate
Q_DECLARE_PUBLIC(Cell)
public:
CellPrivate(Cell *p);
CellPrivate(const CellPrivate * const cp);
QVariant value;
QString formula;

2
src/xlsx/xlsxdocument.h

@ -88,7 +88,7 @@ public:
bool insertWorkSheet(int index, const QString &name = QString());
bool selectWorksheet(const QString &name);
bool renameWorksheet(const QString &oldName, const QString &newName);
bool copyWorksheet(const QString &srcName, const QString &distName);
bool copyWorksheet(const QString &srcName, const QString &distName = QString());
bool moveWorksheet(const QString &srcName, int distIndex);
bool deleteWorksheet(const QString &name);

28
src/xlsx/xlsxworkbook.cpp

@ -282,7 +282,33 @@ bool Workbook::copyWorksheet(int index, const QString &newName)
Q_D(Workbook);
if (index < 0 || index >= d->worksheets.size())
return false;
//! Todo
QString worksheetName = newName;
if (!newName.isEmpty()) {
//If user given an already in-used name, we should not continue any more!
for (int i=0; i<d->worksheets.size(); ++i) {
if (d->worksheets[i]->sheetName() == newName) {
return 0;
}
}
} else {
int copy_index = 1;
bool exists;
do {
++copy_index;
exists = false;
worksheetName = QStringLiteral("%1(%2)").arg(d->worksheets[index]->sheetName()).arg(copy_index);
for (int i=0; i<d->worksheets.size(); ++i) {
if (d->worksheets[i]->sheetName() == worksheetName)
exists = true;
}
} while (exists);
}
++d->last_sheet_id;
QSharedPointer<Worksheet> sheet = d->worksheets[index]->copy(worksheetName, d->last_sheet_id);
d->worksheets.append(sheet);
return false;
}

2
src/xlsx/xlsxworkbook.h

@ -58,7 +58,7 @@ public:
Worksheet *insertWorkSheet(int index, const QString &name = QString());
bool renameWorksheet(int index, const QString &name);
bool deleteWorksheet(int index);
bool copyWorksheet(int index, const QString &newName);
bool copyWorksheet(int index, const QString &newName=QString());
bool moveWorksheet(int srcIndex, int distIndex);
Worksheet *activeWorksheet() const;

38
src/xlsx/xlsxworksheet.cpp

@ -182,6 +182,44 @@ Worksheet::Worksheet(const QString &name, int id, Workbook *workbook) :
d_ptr->workbook = workbook;
}
QSharedPointer<Worksheet> Worksheet::copy(const QString &distName, int distId) const
{
Q_D(const Worksheet);
QSharedPointer<Worksheet> sheet(new Worksheet(distName, distId, d->workbook));
WorksheetPrivate *sheet_d = sheet->d_ptr;
sheet_d->dimension = d->dimension;
QMapIterator<int, QMap<int, QSharedPointer<Cell> > > it(d->cellTable);
while (it.hasNext()) {
it.next();
int row = it.key();
QMapIterator<int, QSharedPointer<Cell> > it2(it.value());
while (it2.hasNext()) {
it2.next();
int col = it2.key();
QSharedPointer<Cell> cell(new Cell(it2.value().data()));
cell->d_ptr->parent = sheet.data();
if (cell->dataType() == Cell::String)
d->workbook->sharedStrings()->addSharedString(cell->d_ptr->richString);
sheet_d->cellTable[row][col] = cell;
}
}
sheet_d->merges = d->merges;
// sheet_d->rowsInfo = d->rowsInfo;
// sheet_d->colsInfo = d->colsInfo;
// sheet_d->colsInfoHelper = d->colsInfoHelper;
// sheet_d->dataValidationsList = d->dataValidationsList;
// sheet_d->conditionalFormattingList = d->conditionalFormattingList;
return sheet;
}
Worksheet::~Worksheet()
{
delete d_ptr;

1
src/xlsx/xlsxworksheet.h

@ -135,6 +135,7 @@ private:
friend class Workbook;
friend class ::WorksheetTest;
Worksheet(const QString &sheetName, int sheetId, Workbook *book);
QSharedPointer<Worksheet> copy(const QString &distName, int distId) const;
void saveToXmlFile(QIODevice *device);
QByteArray saveToXmlData();

53
tests/auto/document/tst_documenttest.cpp

@ -23,6 +23,10 @@ private Q_SLOTS:
void testReadWriteDateTime();
void testReadWriteDate();
void testReadWriteTime();
void testMoveWorksheet();
void testDeleteWorksheet();
void testCopyWorksheet();
};
DocumentTest::DocumentTest()
@ -294,6 +298,55 @@ void DocumentTest::testReadWriteTime()
QCOMPARE(xlsx2.read("A2").toTime(), QTime(1, 22));
}
void DocumentTest::testMoveWorksheet()
{
Document xlsx1;
xlsx1.addWorksheet();
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2");
xlsx1.moveWorksheet("Sheet2", 0);
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet2"<<"Sheet1");
xlsx1.moveWorksheet("Sheet2", 1);
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2");
}
void DocumentTest::testCopyWorksheet()
{
Document xlsx1;
xlsx1.addWorksheet();
xlsx1.write("A1", "String");
xlsx1.write("A2", 999);
xlsx1.write("A3", true);
xlsx1.addWorksheet();
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3");
xlsx1.copyWorksheet("Sheet2");
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"<<"Sheet2(2)");
xlsx1.deleteWorksheet("Sheet2");
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet3"<<"Sheet2(2)");
xlsx1.selectWorksheet("Sheet2(2)");
QCOMPARE(xlsx1.read("A1").toString(), QString("String"));
QCOMPARE(xlsx1.read("A2").toInt(), 999);
QCOMPARE(xlsx1.read("A3").toBool(), true);
}
void DocumentTest::testDeleteWorksheet()
{
Document xlsx1;
xlsx1.addWorksheet();
xlsx1.addWorksheet();
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3");
xlsx1.deleteWorksheet("Sheet2");
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet3");
xlsx1.deleteWorksheet("Sheet1");
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet3");
// xlsx1.deleteWorksheet("Sheet3");
// QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet4");
}
QTEST_APPLESS_MAIN(DocumentTest)
#include "tst_documenttest.moc"

Loading…
Cancel
Save