Add basic worksheet copy support
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ class CellPrivate
|
||||
Q_DECLARE_PUBLIC(Cell)
|
||||
public:
|
||||
CellPrivate(Cell *p);
|
||||
CellPrivate(const CellPrivate * const cp);
|
||||
|
||||
QVariant value;
|
||||
QString formula;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user