Change API to support AbstractSheet
This commit is contained in:
@@ -94,7 +94,7 @@ int main()
|
|||||||
|
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
//The default sheet is "Sheet1"
|
//The default sheet is "Sheet1"
|
||||||
xlsx.renameWorksheet("Sheet1", "Aligns & Borders");
|
xlsx.renameSheet("Sheet1", "Aligns & Borders");
|
||||||
xlsx.setColumn("B", "B", 20);
|
xlsx.setColumn("B", "B", 20);
|
||||||
xlsx.setColumn("H", "H", 12);
|
xlsx.setColumn("H", "H", 12);
|
||||||
xlsx.currentWorksheet()->setGridLinesVisible(false);
|
xlsx.currentWorksheet()->setGridLinesVisible(false);
|
||||||
@@ -138,7 +138,7 @@ int main()
|
|||||||
|
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
//Create the second sheet.
|
//Create the second sheet.
|
||||||
xlsx.addWorksheet("Fonts");
|
xlsx.addSheet("Fonts");
|
||||||
|
|
||||||
xlsx.write("B3", "Normal");
|
xlsx.write("B3", "Normal");
|
||||||
Format font_bold;
|
Format font_bold;
|
||||||
@@ -178,7 +178,7 @@ int main()
|
|||||||
|
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
//Create the third sheet.
|
//Create the third sheet.
|
||||||
xlsx.addWorksheet("Formulas");
|
xlsx.addSheet("Formulas");
|
||||||
xlsx.setColumn("A", "B", 40);
|
xlsx.setColumn("A", "B", 40);
|
||||||
Format rAlign;
|
Format rAlign;
|
||||||
rAlign.setHorizontalAlignment(Format::AlignRight);
|
rAlign.setHorizontalAlignment(Format::AlignRight);
|
||||||
@@ -241,7 +241,7 @@ int main()
|
|||||||
|
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
//Create the fourth sheet.
|
//Create the fourth sheet.
|
||||||
xlsx.addWorksheet("NumFormats");
|
xlsx.addSheet("NumFormats");
|
||||||
xlsx.setColumn("B", "B", 40);
|
xlsx.setColumn("B", "B", 40);
|
||||||
writeInternalNumFormatsCell(xlsx, 4, 2.5681, 2);
|
writeInternalNumFormatsCell(xlsx, 4, 2.5681, 2);
|
||||||
writeInternalNumFormatsCell(xlsx, 5, 2500000, 3);
|
writeInternalNumFormatsCell(xlsx, 5, 2500000, 3);
|
||||||
@@ -259,7 +259,7 @@ int main()
|
|||||||
|
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
//Create the fifth sheet.
|
//Create the fifth sheet.
|
||||||
xlsx.addWorksheet("Merging");
|
xlsx.addSheet("Merging");
|
||||||
Format centerAlign;
|
Format centerAlign;
|
||||||
centerAlign.setHorizontalAlignment(Format::AlignHCenter);
|
centerAlign.setHorizontalAlignment(Format::AlignHCenter);
|
||||||
centerAlign.setVerticalAlignment(Format::AlignVCenter);
|
centerAlign.setVerticalAlignment(Format::AlignVCenter);
|
||||||
@@ -272,7 +272,7 @@ int main()
|
|||||||
|
|
||||||
//---------------------------------------------------------------
|
//---------------------------------------------------------------
|
||||||
//Create the fifth sheet.
|
//Create the fifth sheet.
|
||||||
xlsx.addWorksheet("Grouping");
|
xlsx.addSheet("Grouping");
|
||||||
qsrand(QDateTime::currentMSecsSinceEpoch());
|
qsrand(QDateTime::currentMSecsSinceEpoch());
|
||||||
for (int row=2; row<31; ++row) {
|
for (int row=2; row<31; ++row) {
|
||||||
for (int col=1; col<=10; ++col)
|
for (int col=1; col<=10; ++col)
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ int main()
|
|||||||
//![1]
|
//![1]
|
||||||
|
|
||||||
//![2]
|
//![2]
|
||||||
xlsx.addWorksheet("ArrayFormula");
|
xlsx.addSheet("ArrayFormula");
|
||||||
Worksheet *sheet = xlsx.currentWorksheet();
|
Worksheet *sheet = xlsx.currentWorksheet();
|
||||||
|
|
||||||
for (int row=2; row<20; ++row) {
|
for (int row=2; row<20; ++row) {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Builtin number formats
|
//Builtin number formats
|
||||||
xlsx.addWorksheet();
|
xlsx.addSheet();
|
||||||
xlsx.setColumn(1, 4, 20.0);
|
xlsx.setColumn(1, 4, 20.0);
|
||||||
xlsx.write(1, 1, "Raw data", header);
|
xlsx.write(1, 1, "Raw data", header);
|
||||||
xlsx.write(1, 2, "Builtin Format", header);
|
xlsx.write(1, 2, "Builtin Format", header);
|
||||||
|
|||||||
@@ -5,27 +5,27 @@ int main()
|
|||||||
{
|
{
|
||||||
QXlsx::Document xlsx;
|
QXlsx::Document xlsx;
|
||||||
|
|
||||||
xlsx.renameWorksheet("Sheet1", "TheFirstSheet");
|
xlsx.renameSheet("Sheet1", "TheFirstSheet");
|
||||||
|
|
||||||
for (int i=1; i<20; ++i) {
|
for (int i=1; i<20; ++i) {
|
||||||
for (int j=1; j<15; ++j)
|
for (int j=1; j<15; ++j)
|
||||||
xlsx.write(i, j, QString("R %1 C %2").arg(i).arg(j));
|
xlsx.write(i, j, QString("R %1 C %2").arg(i).arg(j));
|
||||||
}
|
}
|
||||||
|
|
||||||
xlsx.addWorksheet("TheSecondSheet");
|
xlsx.addSheet("TheSecondSheet");
|
||||||
xlsx.write(2, 2, "Hello Qt Xlsx");
|
xlsx.write(2, 2, "Hello Qt Xlsx");
|
||||||
|
|
||||||
xlsx.copyWorksheet("TheFirstSheet", "CopyOfTheFirst");
|
xlsx.copySheet("TheFirstSheet", "CopyOfTheFirst");
|
||||||
|
|
||||||
xlsx.addWorksheet("TheForthSheet");
|
xlsx.addSheet("TheForthSheet");
|
||||||
xlsx.write(3, 3, "This will be deleted...");
|
xlsx.write(3, 3, "This will be deleted...");
|
||||||
|
|
||||||
xlsx.selectWorksheet("CopyOfTheFirst");
|
xlsx.selectSheet("CopyOfTheFirst");
|
||||||
xlsx.write(25, 2, "On the Copy Sheet");
|
xlsx.write(25, 2, "On the Copy Sheet");
|
||||||
|
|
||||||
xlsx.deleteWorksheet("TheForthSheet");
|
xlsx.deleteSheet("TheForthSheet");
|
||||||
|
|
||||||
xlsx.moveWorksheet("TheSecondSheet", 0);
|
xlsx.moveSheet("TheSecondSheet", 0);
|
||||||
|
|
||||||
xlsx.save();
|
xlsx.save();
|
||||||
|
|
||||||
|
|||||||
@@ -24,13 +24,15 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
//![2]
|
//![2]
|
||||||
Document xlsx(filePath);
|
Document xlsx(filePath);
|
||||||
foreach (QString sheetName, xlsx.worksheetNames()) {
|
foreach (QString sheetName, xlsx.sheetNames()) {
|
||||||
Worksheet *sheet = xlsx.worksheet(sheetName);
|
Worksheet *sheet = dynamic_cast<Worksheet *>(xlsx.sheet(sheetName));
|
||||||
QTableView *view = new QTableView(&tabWidget);
|
if (sheet) {
|
||||||
view->setModel(new SheetModel(sheet, view));
|
QTableView *view = new QTableView(&tabWidget);
|
||||||
foreach (CellRange range, sheet->mergedCells())
|
view->setModel(new SheetModel(sheet, view));
|
||||||
view->setSpan(range.firstRow()-1, range.firstColumn()-1, range.rowCount(), range.columnCount());
|
foreach (CellRange range, sheet->mergedCells())
|
||||||
tabWidget.addTab(view, sheetName);
|
view->setSpan(range.firstRow()-1, range.firstColumn()-1, range.rowCount(), range.columnCount());
|
||||||
|
tabWidget.addTab(view, sheetName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//![2]
|
//![2]
|
||||||
|
|
||||||
|
|||||||
@@ -146,4 +146,14 @@ Workbook *AbstractSheet::workbook() const
|
|||||||
Q_D(const AbstractSheet);
|
Q_D(const AbstractSheet);
|
||||||
return d->workbook;
|
return d->workbook;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \internal
|
||||||
|
*/
|
||||||
|
Relationships &AbstractSheet::relationships()
|
||||||
|
{
|
||||||
|
Q_D(AbstractSheet);
|
||||||
|
return d->relationships;
|
||||||
|
}
|
||||||
|
|
||||||
QT_END_NAMESPACE_XLSX
|
QT_END_NAMESPACE_XLSX
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
QT_BEGIN_NAMESPACE_XLSX
|
QT_BEGIN_NAMESPACE_XLSX
|
||||||
class Workbook;
|
class Workbook;
|
||||||
class Drawing;
|
class Drawing;
|
||||||
|
class Relationships;
|
||||||
class AbstractSheetPrivate;
|
class AbstractSheetPrivate;
|
||||||
class Q_XLSX_EXPORT AbstractSheet : public OOXmlFile
|
class Q_XLSX_EXPORT AbstractSheet : public OOXmlFile
|
||||||
{
|
{
|
||||||
@@ -49,11 +50,12 @@ public:
|
|||||||
QString sheetName() const;
|
QString sheetName() const;
|
||||||
bool isHidden() const;
|
bool isHidden() const;
|
||||||
Workbook *workbook() const;
|
Workbook *workbook() const;
|
||||||
//Relationships &relationships();
|
Relationships &relationships();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
friend class Workbook;
|
||||||
AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d);
|
AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d);
|
||||||
//QSharedPointer<AbstractSheet> copy(const QString &distName, int distId) const;
|
virtual AbstractSheet *copy(const QString &distName, int distId) const = 0;
|
||||||
void setSheetName(const QString &sheetName);
|
void setSheetName(const QString &sheetName);
|
||||||
void setHidden(bool hidden);
|
void setHidden(bool hidden);
|
||||||
void setSheetType(SheetType type);
|
void setSheetType(SheetType type);
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ public:
|
|||||||
|
|
||||||
Workbook *workbook;
|
Workbook *workbook;
|
||||||
QSharedPointer<Drawing> drawing;
|
QSharedPointer<Drawing> drawing;
|
||||||
|
mutable Relationships relationships;
|
||||||
|
|
||||||
QString name;
|
QString name;
|
||||||
int id;
|
int id;
|
||||||
|
|||||||
+58
-76
@@ -92,8 +92,8 @@ DocumentPrivate::DocumentPrivate(Document *p) :
|
|||||||
|
|
||||||
void DocumentPrivate::init()
|
void DocumentPrivate::init()
|
||||||
{
|
{
|
||||||
if (workbook->worksheetCount() == 0)
|
if (workbook->sheetCount() == 0)
|
||||||
workbook->addWorksheet();
|
workbook->addSheet();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DocumentPrivate::loadPackage(QIODevice *device)
|
bool DocumentPrivate::loadPackage(QIODevice *device)
|
||||||
@@ -179,9 +179,9 @@ bool DocumentPrivate::loadPackage(QIODevice *device)
|
|||||||
workbook->theme()->loadFromXmlData(zipReader.fileData(path));
|
workbook->theme()->loadFromXmlData(zipReader.fileData(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
//load worksheets
|
//load sheets
|
||||||
for (int i=0; i<workbook->worksheetCount(); ++i) {
|
for (int i=0; i<workbook->sheetCount(); ++i) {
|
||||||
Worksheet *sheet = workbook->worksheet(i);
|
AbstractSheet *sheet = workbook->sheet(i);
|
||||||
QString rel_path = getRelFilePath(sheet->filePath());
|
QString rel_path = getRelFilePath(sheet->filePath());
|
||||||
//If the .rel file exists, load it.
|
//If the .rel file exists, load it.
|
||||||
if (zipReader.filePaths().contains(rel_path))
|
if (zipReader.filePaths().contains(rel_path))
|
||||||
@@ -229,16 +229,18 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
|
|||||||
DocPropsApp docPropsApp;
|
DocPropsApp docPropsApp;
|
||||||
DocPropsCore docPropsCore;
|
DocPropsCore docPropsCore;
|
||||||
|
|
||||||
// save worksheet xml files
|
// save sheet xml files
|
||||||
for (int i=0; i<workbook->worksheetCount(); ++i) {
|
for (int i=0; i<workbook->sheetCount(); ++i) {
|
||||||
Worksheet *sheet = workbook->worksheet(i);
|
AbstractSheet *sheet = workbook->sheet(i);
|
||||||
contentTypes.addWorksheetName(QStringLiteral("sheet%1").arg(i+1));
|
if (sheet->sheetType() == AbstractSheet::ST_WorkSheet) {
|
||||||
docPropsApp.addPartTitle(sheet->sheetName());
|
contentTypes.addWorksheetName(QStringLiteral("sheet%1").arg(i+1));
|
||||||
|
docPropsApp.addPartTitle(sheet->sheetName());
|
||||||
|
|
||||||
zipWriter.addFile(QStringLiteral("xl/worksheets/sheet%1.xml").arg(i+1), sheet->saveToXmlData());
|
zipWriter.addFile(QStringLiteral("xl/worksheets/sheet%1.xml").arg(i+1), sheet->saveToXmlData());
|
||||||
Relationships &rel = sheet->relationships();
|
Relationships &rel = sheet->relationships();
|
||||||
if (!rel.isEmpty())
|
if (!rel.isEmpty())
|
||||||
zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(i+1), rel.saveToXmlData());
|
zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(i+1), rel.saveToXmlData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// save workbook xml file
|
// save workbook xml file
|
||||||
@@ -261,8 +263,8 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
|
|||||||
docPropsApp.setProperty(name, q->documentProperty(name));
|
docPropsApp.setProperty(name, q->documentProperty(name));
|
||||||
docPropsCore.setProperty(name, q->documentProperty(name));
|
docPropsCore.setProperty(name, q->documentProperty(name));
|
||||||
}
|
}
|
||||||
if (workbook->worksheetCount())
|
if (workbook->sheetCount())
|
||||||
docPropsApp.addHeadingPair(QStringLiteral("Worksheets"), workbook->worksheetCount());
|
docPropsApp.addHeadingPair(QStringLiteral("Worksheets"), workbook->sheetCount());
|
||||||
contentTypes.addDocPropApp();
|
contentTypes.addDocPropApp();
|
||||||
contentTypes.addDocPropCore();
|
contentTypes.addDocPropCore();
|
||||||
zipWriter.addFile(QStringLiteral("docProps/app.xml"), docPropsApp.saveToXmlData());
|
zipWriter.addFile(QStringLiteral("docProps/app.xml"), docPropsApp.saveToXmlData());
|
||||||
@@ -414,17 +416,6 @@ Chart *Document::insertChart(int row, int col, const QSize &size)
|
|||||||
return currentWorksheet()->insertChart(row, col, size);
|
return currentWorksheet()->insertChart(row, col, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
* \overload
|
|
||||||
* \deprecated
|
|
||||||
* Insert an \a image to current active worksheet to the position \a row, \a column with the given
|
|
||||||
* \a xOffset, \a yOffset, \a xScale and \a yScale.
|
|
||||||
*/
|
|
||||||
int Document::insertImage(int row, int column, const QImage &image, double /*xOffset*/, double /*yOffset*/, double /*xScale*/, double /*yScale*/)
|
|
||||||
{
|
|
||||||
return currentWorksheet()->insertImage(row, column, image);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Merge a \a range of cells. The first cell should contain the data and the others should
|
Merge a \a range of cells. The first cell should contain the data and the others should
|
||||||
be blank. All cells will be applied the same style if a valid \a format is given.
|
be blank. All cells will be applied the same style if a valid \a format is given.
|
||||||
@@ -635,138 +626,129 @@ Workbook *Document::workbook() const
|
|||||||
return d->workbook.data();
|
return d->workbook.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns the sheet object named \a sheetName.
|
||||||
|
*/
|
||||||
|
AbstractSheet *Document::sheet(const QString &sheetName) const
|
||||||
|
{
|
||||||
|
Q_D(const Document);
|
||||||
|
return d->workbook->sheet(sheetNames().indexOf(sheetName));
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Returns the worksheet object named \a sheetName.
|
* Returns the worksheet object named \a sheetName.
|
||||||
|
* If the type of sheet is not AbstractSheet::ST_WorkSheet, then 0 will be returned.
|
||||||
*/
|
*/
|
||||||
Worksheet *Document::worksheet(const QString &sheetName) const
|
Worksheet *Document::worksheet(const QString &sheetName) const
|
||||||
{
|
{
|
||||||
Q_D(const Document);
|
AbstractSheet *st = sheet(sheetName);
|
||||||
return d->workbook->worksheet(worksheetNames().indexOf(sheetName));
|
if (st && st->sheetType() == AbstractSheet::ST_WorkSheet)
|
||||||
|
return static_cast<Worksheet *>(st);
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Creates and append an document with name \a name.
|
* Creates and append an document with name \a name.
|
||||||
* Return true if success.
|
* Return true if success.
|
||||||
*/
|
*/
|
||||||
bool Document::addWorksheet(const QString &name)
|
bool Document::addSheet(const QString &name, AbstractSheet::SheetType type)
|
||||||
{
|
{
|
||||||
Q_D(Document);
|
Q_D(Document);
|
||||||
return d->workbook->addWorksheet(name);
|
return d->workbook->addSheet(name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Creates and inserts an document with name \a name at the \a index.
|
* Creates and inserts an document with name \a name at the \a index.
|
||||||
* Returns false if the \a name already used.
|
* Returns false if the \a name already used.
|
||||||
*/
|
*/
|
||||||
bool Document::insertWorkSheet(int index, const QString &name)
|
bool Document::insertSheet(int index, const QString &name, AbstractSheet::SheetType type)
|
||||||
{
|
{
|
||||||
Q_D(Document);
|
Q_D(Document);
|
||||||
return d->workbook->insertWorkSheet(index, name);
|
return d->workbook->insertSheet(index, name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Rename the worksheet from \a oldName to \a newName.
|
Rename the worksheet from \a oldName to \a newName.
|
||||||
Returns true if the success.
|
Returns true if the success.
|
||||||
*/
|
*/
|
||||||
bool Document::renameWorksheet(const QString &oldName, const QString &newName)
|
bool Document::renameSheet(const QString &oldName, const QString &newName)
|
||||||
{
|
{
|
||||||
Q_D(Document);
|
Q_D(Document);
|
||||||
if (oldName == newName)
|
if (oldName == newName)
|
||||||
return false;
|
return false;
|
||||||
return d->workbook->renameWorksheet(worksheetNames().indexOf(oldName), newName);
|
return d->workbook->renameSheet(sheetNames().indexOf(oldName), newName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Make a copy of the worksheet \a srcName with the new name \a distName.
|
Make a copy of the worksheet \a srcName with the new name \a distName.
|
||||||
Returns true if the success.
|
Returns true if the success.
|
||||||
*/
|
*/
|
||||||
bool Document::copyWorksheet(const QString &srcName, const QString &distName)
|
bool Document::copySheet(const QString &srcName, const QString &distName)
|
||||||
{
|
{
|
||||||
Q_D(Document);
|
Q_D(Document);
|
||||||
if (srcName == distName)
|
if (srcName == distName)
|
||||||
return false;
|
return false;
|
||||||
return d->workbook->copyWorksheet(worksheetNames().indexOf(srcName), distName);
|
return d->workbook->copySheet(sheetNames().indexOf(srcName), distName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Move the worksheet \a srcName to the new pos \a distIndex.
|
Move the worksheet \a srcName to the new pos \a distIndex.
|
||||||
Returns true if the success.
|
Returns true if the success.
|
||||||
*/
|
*/
|
||||||
bool Document::moveWorksheet(const QString &srcName, int distIndex)
|
bool Document::moveSheet(const QString &srcName, int distIndex)
|
||||||
{
|
{
|
||||||
Q_D(Document);
|
Q_D(Document);
|
||||||
return d->workbook->moveWorksheet(worksheetNames().indexOf(srcName), distIndex);
|
return d->workbook->moveSheet(sheetNames().indexOf(srcName), distIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Delete the worksheet \a name.
|
Delete the worksheet \a name.
|
||||||
Returns true if current sheet was deleted successfully.
|
Returns true if current sheet was deleted successfully.
|
||||||
*/
|
*/
|
||||||
bool Document::deleteWorksheet(const QString &name)
|
bool Document::deleteSheet(const QString &name)
|
||||||
{
|
{
|
||||||
Q_D(Document);
|
Q_D(Document);
|
||||||
return d->workbook->deleteWorksheet(worksheetNames().indexOf(name));
|
return d->workbook->deleteSheet(sheetNames().indexOf(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\deprecated
|
* \brief Return pointer of current sheet.
|
||||||
Rename current worksheet to new \a name.
|
|
||||||
Returns true if the name defined successful.
|
|
||||||
|
|
||||||
\sa renameWorksheet()
|
|
||||||
*/
|
*/
|
||||||
bool Document::setSheetName(const QString &name)
|
AbstractSheet *Document::currentSheet() const
|
||||||
{
|
{
|
||||||
return renameWorksheet(currentWorksheet()->sheetName(), name);
|
Q_D(const Document);
|
||||||
|
|
||||||
|
return d->workbook->activeSheet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Return pointer of current worksheet.
|
* \brief Return pointer of current worksheet.
|
||||||
|
* If the type of sheet is not AbstractSheet::ST_WorkSheet, then 0 will be returned.
|
||||||
*/
|
*/
|
||||||
Worksheet *Document::currentWorksheet() const
|
Worksheet *Document::currentWorksheet() const
|
||||||
{
|
{
|
||||||
Q_D(const Document);
|
AbstractSheet *st = currentSheet();
|
||||||
if (d->workbook->worksheetCount() == 0)
|
if (st && st->sheetType() == AbstractSheet::ST_WorkSheet)
|
||||||
|
return static_cast<Worksheet *>(st);
|
||||||
|
else
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return d->workbook->activeWorksheet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \deprecated
|
|
||||||
* Set current worksheet to be the sheet at \a index.
|
|
||||||
* \sa selectWorksheet()
|
|
||||||
*/
|
|
||||||
void Document::setCurrentWorksheet(int index)
|
|
||||||
{
|
|
||||||
Q_D(Document);
|
|
||||||
d->workbook->setActiveWorksheet(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \deprecated
|
|
||||||
* Set current selected worksheet to be the sheet named \a name.
|
|
||||||
* \sa selectWorksheet()
|
|
||||||
*/
|
|
||||||
void Document::setCurrentWorksheet(const QString &name)
|
|
||||||
{
|
|
||||||
selectWorksheet(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Set worksheet named \a name to be active sheet.
|
* \brief Set worksheet named \a name to be active sheet.
|
||||||
* Returns true if success.
|
* Returns true if success.
|
||||||
*/
|
*/
|
||||||
bool Document::selectWorksheet(const QString &name)
|
bool Document::selectSheet(const QString &name)
|
||||||
{
|
{
|
||||||
Q_D(Document);
|
Q_D(Document);
|
||||||
return d->workbook->setActiveWorksheet(worksheetNames().indexOf(name));
|
return d->workbook->setActiveSheet(sheetNames().indexOf(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Returns the names of worksheets contained in current document.
|
* Returns the names of worksheets contained in current document.
|
||||||
*/
|
*/
|
||||||
QStringList Document::worksheetNames() const
|
QStringList Document::sheetNames() const
|
||||||
{
|
{
|
||||||
Q_D(const Document);
|
Q_D(const Document);
|
||||||
return d->workbook->worksheetNames();
|
return d->workbook->worksheetNames();
|
||||||
|
|||||||
+11
-13
@@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#include "xlsxglobal.h"
|
#include "xlsxglobal.h"
|
||||||
#include "xlsxformat.h"
|
#include "xlsxformat.h"
|
||||||
|
#include "xlsxworksheet.h"
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
class QIODevice;
|
class QIODevice;
|
||||||
@@ -36,7 +37,6 @@ class QImage;
|
|||||||
QT_BEGIN_NAMESPACE_XLSX
|
QT_BEGIN_NAMESPACE_XLSX
|
||||||
|
|
||||||
class Workbook;
|
class Workbook;
|
||||||
class Worksheet;
|
|
||||||
class Cell;
|
class Cell;
|
||||||
class CellRange;
|
class CellRange;
|
||||||
class DataValidation;
|
class DataValidation;
|
||||||
@@ -60,7 +60,6 @@ public:
|
|||||||
QVariant read(const QString &cell) const;
|
QVariant read(const QString &cell) const;
|
||||||
QVariant read(int row, int col) const;
|
QVariant read(int row, int col) const;
|
||||||
bool insertImage(int row, int col, const QImage &image);
|
bool insertImage(int row, int col, const QImage &image);
|
||||||
Q_DECL_DEPRECATED int insertImage(int row, int column, const QImage &image, double xOffset, double yOffset, double xScale=1, double yScale=1);
|
|
||||||
Chart *insertChart(int row, int col, const QSize &size);
|
Chart *insertChart(int row, int col, const QSize &size);
|
||||||
int mergeCells(const CellRange &range, const Format &format=Format());
|
int mergeCells(const CellRange &range, const Format &format=Format());
|
||||||
int mergeCells(const QString &range, const Format &format=Format());
|
int mergeCells(const QString &range, const Format &format=Format());
|
||||||
@@ -85,21 +84,20 @@ public:
|
|||||||
void setDocumentProperty(const QString &name, const QString &property);
|
void setDocumentProperty(const QString &name, const QString &property);
|
||||||
QStringList documentPropertyNames() const;
|
QStringList documentPropertyNames() const;
|
||||||
|
|
||||||
QStringList worksheetNames() const;
|
QStringList sheetNames() const;
|
||||||
bool addWorksheet(const QString &name = QString());
|
bool addSheet(const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet);
|
||||||
bool insertWorkSheet(int index, const QString &name = QString());
|
bool insertSheet(int index, const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet);
|
||||||
bool selectWorksheet(const QString &name);
|
bool selectSheet(const QString &name);
|
||||||
bool renameWorksheet(const QString &oldName, const QString &newName);
|
bool renameSheet(const QString &oldName, const QString &newName);
|
||||||
bool copyWorksheet(const QString &srcName, const QString &distName = QString());
|
bool copySheet(const QString &srcName, const QString &distName = QString());
|
||||||
bool moveWorksheet(const QString &srcName, int distIndex);
|
bool moveSheet(const QString &srcName, int distIndex);
|
||||||
bool deleteWorksheet(const QString &name);
|
bool deleteSheet(const QString &name);
|
||||||
|
|
||||||
Workbook *workbook() const;
|
Workbook *workbook() const;
|
||||||
|
AbstractSheet *sheet(const QString &sheetName) const;
|
||||||
|
AbstractSheet *currentSheet() const;
|
||||||
Worksheet *worksheet(const QString &sheetName) const;
|
Worksheet *worksheet(const QString &sheetName) const;
|
||||||
Worksheet *currentWorksheet() const;
|
Worksheet *currentWorksheet() const;
|
||||||
Q_DECL_DEPRECATED bool setSheetName(const QString &name);
|
|
||||||
Q_DECL_DEPRECATED void setCurrentWorksheet(int index);
|
|
||||||
Q_DECL_DEPRECATED void setCurrentWorksheet(const QString &name);
|
|
||||||
|
|
||||||
bool save() const;
|
bool save() const;
|
||||||
bool saveAs(const QString &xlsXname) const;
|
bool saveAs(const QString &xlsXname) const;
|
||||||
|
|||||||
+73
-69
@@ -159,9 +159,9 @@ bool Workbook::defineName(const QString &name, const QString &formula, const QSt
|
|||||||
|
|
||||||
int id=-1;
|
int id=-1;
|
||||||
if (!scope.isEmpty()) {
|
if (!scope.isEmpty()) {
|
||||||
for (int i=0; i<d->worksheets.size(); ++i) {
|
for (int i=0; i<d->sheets.size(); ++i) {
|
||||||
if (d->worksheets[i]->sheetName() == scope) {
|
if (d->sheets[i]->sheetName() == scope) {
|
||||||
id = d->worksheets[i]->sheetId();
|
id = d->sheets[i]->sheetId();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -171,10 +171,10 @@ bool Workbook::defineName(const QString &name, const QString &formula, const QSt
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Worksheet *Workbook::addWorksheet(const QString &name)
|
AbstractSheet *Workbook::addSheet(const QString &name, AbstractSheet::SheetType type)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
return insertWorkSheet(d->worksheets.size(), name);
|
return insertSheet(d->sheets.size(), name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -183,44 +183,44 @@ Worksheet *Workbook::addWorksheet(const QString &name)
|
|||||||
QStringList Workbook::worksheetNames() const
|
QStringList Workbook::worksheetNames() const
|
||||||
{
|
{
|
||||||
Q_D(const Workbook);
|
Q_D(const Workbook);
|
||||||
return d->worksheetNames;
|
return d->sheetNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \internal
|
* \internal
|
||||||
* Used only when load the xlsx file!!
|
* Used only when load the xlsx file!!
|
||||||
*/
|
*/
|
||||||
Worksheet *Workbook::addWorksheet(const QString &name, int sheetId)
|
AbstractSheet *Workbook::addSheet(const QString &name, int sheetId, AbstractSheet::SheetType type)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
if (sheetId > d->last_sheet_id)
|
if (sheetId > d->last_sheet_id)
|
||||||
d->last_sheet_id = sheetId;
|
d->last_sheet_id = sheetId;
|
||||||
|
|
||||||
Worksheet *sheet = new Worksheet(name, sheetId, this);
|
Worksheet *sheet = new Worksheet(name, sheetId, this);
|
||||||
d->worksheets.append(QSharedPointer<Worksheet>(sheet));
|
d->sheets.append(QSharedPointer<Worksheet>(sheet));
|
||||||
d->worksheetNames.append(name);
|
d->sheetNames.append(name);
|
||||||
return sheet;
|
return sheet;
|
||||||
}
|
}
|
||||||
|
|
||||||
Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
|
AbstractSheet *Workbook::insertSheet(int index, const QString &name, AbstractSheet::SheetType type)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
QString worksheetName = createSafeSheetName(name);
|
QString worksheetName = createSafeSheetName(name);
|
||||||
if (!worksheetName.isEmpty()) {
|
if (!worksheetName.isEmpty()) {
|
||||||
//If user given an already in-used name, we should not continue any more!
|
//If user given an already in-used name, we should not continue any more!
|
||||||
if (d->worksheetNames.contains(worksheetName))
|
if (d->sheetNames.contains(worksheetName))
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
++d->last_sheet_index;
|
++d->last_sheet_index;
|
||||||
worksheetName = QStringLiteral("Sheet%1").arg(d->last_sheet_index);
|
worksheetName = QStringLiteral("Sheet%1").arg(d->last_sheet_index);
|
||||||
} while (d->worksheetNames.contains(worksheetName));
|
} while (d->sheetNames.contains(worksheetName));
|
||||||
}
|
}
|
||||||
|
|
||||||
++d->last_sheet_id;
|
++d->last_sheet_id;
|
||||||
Worksheet *sheet = new Worksheet(worksheetName, d->last_sheet_id, this);
|
Worksheet *sheet = new Worksheet(worksheetName, d->last_sheet_id, this);
|
||||||
d->worksheets.insert(index, QSharedPointer<Worksheet>(sheet));
|
d->sheets.insert(index, QSharedPointer<Worksheet>(sheet));
|
||||||
d->worksheetNames.insert(index, worksheetName);
|
d->sheetNames.insert(index, worksheetName);
|
||||||
d->activesheetIndex = index;
|
d->activesheetIndex = index;
|
||||||
return sheet;
|
return sheet;
|
||||||
}
|
}
|
||||||
@@ -228,16 +228,16 @@ Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
|
|||||||
/*!
|
/*!
|
||||||
* Returns current active worksheet.
|
* Returns current active worksheet.
|
||||||
*/
|
*/
|
||||||
Worksheet *Workbook::activeWorksheet() const
|
AbstractSheet *Workbook::activeSheet() const
|
||||||
{
|
{
|
||||||
Q_D(const Workbook);
|
Q_D(const Workbook);
|
||||||
return d->worksheets[d->activesheetIndex].data();
|
return d->sheets[d->activesheetIndex].data();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Workbook::setActiveWorksheet(int index)
|
bool Workbook::setActiveSheet(int index)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
if (index < 0 || index >= d->worksheets.size()) {
|
if (index < 0 || index >= d->sheets.size()) {
|
||||||
//warning
|
//warning
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -248,113 +248,104 @@ bool Workbook::setActiveWorksheet(int index)
|
|||||||
/*!
|
/*!
|
||||||
* Rename the worksheet at the \a index to \a name.
|
* Rename the worksheet at the \a index to \a name.
|
||||||
*/
|
*/
|
||||||
bool Workbook::renameWorksheet(int index, const QString &name)
|
bool Workbook::renameSheet(int index, const QString &name)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
//If user given an already in-used name, return false
|
//If user given an already in-used name, return false
|
||||||
for (int i=0; i<d->worksheets.size(); ++i) {
|
for (int i=0; i<d->sheets.size(); ++i) {
|
||||||
if (d->worksheets[i]->sheetName() == name)
|
if (d->sheets[i]->sheetName() == name)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
d->worksheets[index]->setSheetName(name);
|
d->sheets[index]->setSheetName(name);
|
||||||
d->worksheetNames[index] = name;
|
d->sheetNames[index] = name;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Remove the worksheet at pos \a index.
|
* Remove the worksheet at pos \a index.
|
||||||
*/
|
*/
|
||||||
bool Workbook::deleteWorksheet(int index)
|
bool Workbook::deleteSheet(int index)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
if (d->worksheets.size() <= 1)
|
if (d->sheets.size() <= 1)
|
||||||
return false;
|
return false;
|
||||||
if (index < 0 || index >= d->worksheets.size())
|
if (index < 0 || index >= d->sheets.size())
|
||||||
return false;
|
return false;
|
||||||
d->worksheets.removeAt(index);
|
d->sheets.removeAt(index);
|
||||||
d->worksheetNames.removeAt(index);
|
d->sheetNames.removeAt(index);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Moves the worksheet form \a srcIndex to \a distIndex.
|
* Moves the worksheet form \a srcIndex to \a distIndex.
|
||||||
*/
|
*/
|
||||||
bool Workbook::moveWorksheet(int srcIndex, int distIndex)
|
bool Workbook::moveSheet(int srcIndex, int distIndex)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
if (srcIndex == distIndex)
|
if (srcIndex == distIndex)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (srcIndex < 0 || srcIndex >= d->worksheets.size())
|
if (srcIndex < 0 || srcIndex >= d->sheets.size())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QSharedPointer<Worksheet> sheet = d->worksheets.takeAt(srcIndex);
|
QSharedPointer<AbstractSheet> sheet = d->sheets.takeAt(srcIndex);
|
||||||
d->worksheetNames.takeAt(srcIndex);
|
d->sheetNames.takeAt(srcIndex);
|
||||||
if (distIndex >= 0 || distIndex <= d->worksheets.size()) {
|
if (distIndex >= 0 || distIndex <= d->sheets.size()) {
|
||||||
d->worksheets.insert(distIndex, sheet);
|
d->sheets.insert(distIndex, sheet);
|
||||||
d->worksheetNames.insert(distIndex, sheet->sheetName());
|
d->sheetNames.insert(distIndex, sheet->sheetName());
|
||||||
} else {
|
} else {
|
||||||
d->worksheets.append(sheet);
|
d->sheets.append(sheet);
|
||||||
d->worksheetNames.append(sheet->sheetName());
|
d->sheetNames.append(sheet->sheetName());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Workbook::copyWorksheet(int index, const QString &newName)
|
bool Workbook::copySheet(int index, const QString &newName)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
if (index < 0 || index >= d->worksheets.size())
|
if (index < 0 || index >= d->sheets.size())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QString worksheetName = newName;
|
QString worksheetName = newName;
|
||||||
if (!newName.isEmpty()) {
|
if (!newName.isEmpty()) {
|
||||||
//If user given an already in-used name, we should not continue any more!
|
//If user given an already in-used name, we should not continue any more!
|
||||||
if (d->worksheetNames.contains(newName))
|
if (d->sheetNames.contains(newName))
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
int copy_index = 1;
|
int copy_index = 1;
|
||||||
do {
|
do {
|
||||||
++copy_index;
|
++copy_index;
|
||||||
worksheetName = QStringLiteral("%1(%2)").arg(d->worksheets[index]->sheetName()).arg(copy_index);
|
worksheetName = QStringLiteral("%1(%2)").arg(d->sheets[index]->sheetName()).arg(copy_index);
|
||||||
} while (d->worksheetNames.contains(worksheetName));
|
} while (d->sheetNames.contains(worksheetName));
|
||||||
}
|
}
|
||||||
|
|
||||||
++d->last_sheet_id;
|
++d->last_sheet_id;
|
||||||
QSharedPointer<Worksheet> sheet = d->worksheets[index]->copy(worksheetName, d->last_sheet_id);
|
AbstractSheet *sheet = d->sheets[index]->copy(worksheetName, d->last_sheet_id);
|
||||||
d->worksheets.append(sheet);
|
d->sheets.append(QSharedPointer<AbstractSheet> (sheet));
|
||||||
d->worksheetNames.append(sheet->sheetName());
|
d->sheetNames.append(sheet->sheetName());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\deprecated
|
|
||||||
*/
|
|
||||||
QList<QSharedPointer<Worksheet> > Workbook::worksheets() const
|
|
||||||
{
|
|
||||||
Q_D(const Workbook);
|
|
||||||
return d->worksheets;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Returns count of worksheets.
|
* Returns count of worksheets.
|
||||||
*/
|
*/
|
||||||
int Workbook::worksheetCount() const
|
int Workbook::sheetCount() const
|
||||||
{
|
{
|
||||||
Q_D(const Workbook);
|
Q_D(const Workbook);
|
||||||
return d->worksheets.count();
|
return d->sheets.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Returns the sheet object at index \a sheetIndex.
|
* Returns the sheet object at index \a sheetIndex.
|
||||||
*/
|
*/
|
||||||
Worksheet *Workbook::worksheet(int index) const
|
AbstractSheet *Workbook::sheet(int index) const
|
||||||
{
|
{
|
||||||
Q_D(const Workbook);
|
Q_D(const Workbook);
|
||||||
if (index < 0 || index >= d->worksheets.size())
|
if (index < 0 || index >= d->sheets.size())
|
||||||
return 0;
|
return 0;
|
||||||
return d->worksheets.at(index).data();
|
return d->sheets.at(index).data();
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedStrings *Workbook::sharedStrings() const
|
SharedStrings *Workbook::sharedStrings() const
|
||||||
@@ -384,8 +375,8 @@ QList<Drawing *> Workbook::drawings()
|
|||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
QList<Drawing *> ds;
|
QList<Drawing *> ds;
|
||||||
for (int i=0; i<d->worksheets.size(); ++i) {
|
for (int i=0; i<d->sheets.size(); ++i) {
|
||||||
QSharedPointer<Worksheet> sheet = d->worksheets[i];
|
QSharedPointer<AbstractSheet> sheet = d->sheets[i];
|
||||||
if (sheet->drawing())
|
if (sheet->drawing())
|
||||||
ds.append(sheet->drawing());
|
ds.append(sheet->drawing());
|
||||||
}
|
}
|
||||||
@@ -398,7 +389,7 @@ void Workbook::saveToXmlFile(QIODevice *device) const
|
|||||||
Q_D(const Workbook);
|
Q_D(const Workbook);
|
||||||
d->relationships.clear();
|
d->relationships.clear();
|
||||||
|
|
||||||
for (int i=0; i<worksheetCount(); ++i)
|
for (int i=0; i<sheetCount(); ++i)
|
||||||
d->relationships.addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(i+1));
|
d->relationships.addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(i+1));
|
||||||
d->relationships.addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml"));
|
d->relationships.addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml"));
|
||||||
d->relationships.addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml"));
|
d->relationships.addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml"));
|
||||||
@@ -440,8 +431,8 @@ void Workbook::saveToXmlFile(QIODevice *device) const
|
|||||||
writer.writeEndElement();//bookViews
|
writer.writeEndElement();//bookViews
|
||||||
|
|
||||||
writer.writeStartElement(QStringLiteral("sheets"));
|
writer.writeStartElement(QStringLiteral("sheets"));
|
||||||
for (int i=0; i<d->worksheets.size(); ++i) {
|
for (int i=0; i<d->sheets.size(); ++i) {
|
||||||
QSharedPointer<Worksheet> sheet = d->worksheets[i];
|
QSharedPointer<AbstractSheet> sheet = d->sheets[i];
|
||||||
writer.writeEmptyElement(QStringLiteral("sheet"));
|
writer.writeEmptyElement(QStringLiteral("sheet"));
|
||||||
writer.writeAttribute(QStringLiteral("name"), sheet->sheetName());
|
writer.writeAttribute(QStringLiteral("name"), sheet->sheetName());
|
||||||
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId()));
|
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId()));
|
||||||
@@ -460,8 +451,8 @@ void Workbook::saveToXmlFile(QIODevice *device) const
|
|||||||
writer.writeAttribute(QStringLiteral("comment"), data.comment);
|
writer.writeAttribute(QStringLiteral("comment"), data.comment);
|
||||||
if (data.sheetId != -1) {
|
if (data.sheetId != -1) {
|
||||||
//find the local index of the sheet.
|
//find the local index of the sheet.
|
||||||
for (int i=0; i<d->worksheets.size(); ++i) {
|
for (int i=0; i<d->sheets.size(); ++i) {
|
||||||
if (d->worksheets[i]->sheetId() == data.sheetId) {
|
if (d->sheets[i]->sheetId() == data.sheetId) {
|
||||||
writer.writeAttribute(QStringLiteral("localSheetId"), QString::number(i));
|
writer.writeAttribute(QStringLiteral("localSheetId"), QString::number(i));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -497,9 +488,22 @@ bool Workbook::loadFromXmlFile(QIODevice *device)
|
|||||||
// if (attributes.hasAttribute(QLatin1String("state")))
|
// if (attributes.hasAttribute(QLatin1String("state")))
|
||||||
// QString state = attributes.value(QLatin1String("state")).toString();
|
// QString state = attributes.value(QLatin1String("state")).toString();
|
||||||
|
|
||||||
Worksheet *sheet = addWorksheet(name, sheetId);
|
XlsxRelationship relationship = d->relationships.getRelationshipById(rId);
|
||||||
const QString path = d->relationships.getRelationshipById(rId).target;
|
|
||||||
const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ path);
|
AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet;
|
||||||
|
if (relationship.type.endsWith(QLatin1String("/worksheet")))
|
||||||
|
type = AbstractSheet::ST_WorkSheet;
|
||||||
|
else if (relationship.type.endsWith(QLatin1String("/chartsheet")))
|
||||||
|
type = AbstractSheet::ST_ChartSheet;
|
||||||
|
else if (relationship.type.endsWith(QLatin1String("/dialogsheet")))
|
||||||
|
type = AbstractSheet::ST_DialogSheet;
|
||||||
|
else if (relationship.type.endsWith(QLatin1String("/xlMacrosheet")))
|
||||||
|
type = AbstractSheet::ST_MacroSheet;
|
||||||
|
else
|
||||||
|
qWarning("unknown sheet type");
|
||||||
|
|
||||||
|
AbstractSheet *sheet = addSheet(name, sheetId, type);
|
||||||
|
const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ relationship.target);
|
||||||
sheet->setFilePath(fullPath);
|
sheet->setFilePath(fullPath);
|
||||||
} else if (reader.name() == QLatin1String("workbookPr")) {
|
} else if (reader.name() == QLatin1String("workbookPr")) {
|
||||||
QXmlStreamAttributes attrs = reader.attributes();
|
QXmlStreamAttributes attrs = reader.attributes();
|
||||||
|
|||||||
+12
-13
@@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
#include "xlsxglobal.h"
|
#include "xlsxglobal.h"
|
||||||
#include "xlsxooxmlfile.h"
|
#include "xlsxooxmlfile.h"
|
||||||
|
#include "xlsxabstractsheet.h"
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
@@ -35,7 +36,6 @@ class QIODevice;
|
|||||||
|
|
||||||
QT_BEGIN_NAMESPACE_XLSX
|
QT_BEGIN_NAMESPACE_XLSX
|
||||||
|
|
||||||
class Worksheet;
|
|
||||||
class SharedStrings;
|
class SharedStrings;
|
||||||
class Styles;
|
class Styles;
|
||||||
class Drawing;
|
class Drawing;
|
||||||
@@ -53,19 +53,18 @@ class Q_XLSX_EXPORT Workbook : public OOXmlFile
|
|||||||
public:
|
public:
|
||||||
~Workbook();
|
~Workbook();
|
||||||
|
|
||||||
Q_DECL_DEPRECATED QList<QSharedPointer<Worksheet> > worksheets() const;
|
int sheetCount() const;
|
||||||
int worksheetCount() const;
|
AbstractSheet *sheet(int index) const;
|
||||||
Worksheet *worksheet(int sheetIndex) const;
|
|
||||||
|
|
||||||
Worksheet *addWorksheet(const QString &name = QString());
|
AbstractSheet *addSheet(const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet);
|
||||||
Worksheet *insertWorkSheet(int index, const QString &name = QString());
|
AbstractSheet *insertSheet(int index, const QString &name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet);
|
||||||
bool renameWorksheet(int index, const QString &name);
|
bool renameSheet(int index, const QString &name);
|
||||||
bool deleteWorksheet(int index);
|
bool deleteSheet(int index);
|
||||||
bool copyWorksheet(int index, const QString &newName=QString());
|
bool copySheet(int index, const QString &newName=QString());
|
||||||
bool moveWorksheet(int srcIndex, int distIndex);
|
bool moveSheet(int srcIndex, int distIndex);
|
||||||
|
|
||||||
Worksheet *activeWorksheet() const;
|
AbstractSheet *activeSheet() const;
|
||||||
bool setActiveWorksheet(int index);
|
bool setActiveSheet(int index);
|
||||||
|
|
||||||
// void addChart();
|
// void addChart();
|
||||||
bool defineName(const QString &name, const QString &formula, const QString &comment=QString(), const QString &scope=QString());
|
bool defineName(const QString &name, const QString &formula, const QString &comment=QString(), const QString &scope=QString());
|
||||||
@@ -102,7 +101,7 @@ private:
|
|||||||
QList<QImage> images();
|
QList<QImage> images();
|
||||||
QList<Drawing *> drawings();
|
QList<Drawing *> drawings();
|
||||||
QStringList worksheetNames() const;
|
QStringList worksheetNames() const;
|
||||||
Worksheet *addWorksheet(const QString &name, int sheetId);
|
AbstractSheet *addSheet(const QString &name, int sheetId, AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet);
|
||||||
};
|
};
|
||||||
|
|
||||||
QT_END_NAMESPACE_XLSX
|
QT_END_NAMESPACE_XLSX
|
||||||
|
|||||||
@@ -83,8 +83,8 @@ public:
|
|||||||
mutable Relationships relationships;
|
mutable Relationships relationships;
|
||||||
|
|
||||||
QSharedPointer<SharedStrings> sharedStrings;
|
QSharedPointer<SharedStrings> sharedStrings;
|
||||||
QList<QSharedPointer<Worksheet> > worksheets;
|
QList<QSharedPointer<AbstractSheet> > sheets;
|
||||||
QStringList worksheetNames;
|
QStringList sheetNames;
|
||||||
QSharedPointer<Styles> styles;
|
QSharedPointer<Styles> styles;
|
||||||
QSharedPointer<Theme> theme;
|
QSharedPointer<Theme> theme;
|
||||||
QList<QSharedPointer<MediaFile> > mediaFiles;
|
QList<QSharedPointer<MediaFile> > mediaFiles;
|
||||||
|
|||||||
@@ -184,11 +184,10 @@ Worksheet::Worksheet(const QString &name, int id, Workbook *workbook)
|
|||||||
* Make a copy of this sheet.
|
* Make a copy of this sheet.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
QSharedPointer<Worksheet> Worksheet::copy(const QString &distName, int distId) const
|
Worksheet *Worksheet::copy(const QString &distName, int distId) const
|
||||||
{
|
{
|
||||||
Q_D(const Worksheet);
|
Q_D(const Worksheet);
|
||||||
QSharedPointer<Worksheet> sheet(new Worksheet(distName, distId, d->workbook));
|
Worksheet *sheet = new Worksheet(distName, distId, d->workbook);
|
||||||
|
|
||||||
WorksheetPrivate *sheet_d = sheet->d_func();
|
WorksheetPrivate *sheet_d = sheet->d_func();
|
||||||
|
|
||||||
sheet_d->dimension = d->dimension;
|
sheet_d->dimension = d->dimension;
|
||||||
@@ -203,7 +202,7 @@ QSharedPointer<Worksheet> Worksheet::copy(const QString &distName, int distId) c
|
|||||||
int col = it2.key();
|
int col = it2.key();
|
||||||
|
|
||||||
QSharedPointer<Cell> cell(new Cell(it2.value().data()));
|
QSharedPointer<Cell> cell(new Cell(it2.value().data()));
|
||||||
cell->d_ptr->parent = sheet.data();
|
cell->d_ptr->parent = sheet;
|
||||||
|
|
||||||
if (cell->dataType() == Cell::String)
|
if (cell->dataType() == Cell::String)
|
||||||
d->workbook->sharedStrings()->addSharedString(cell->d_ptr->richString);
|
d->workbook->sharedStrings()->addSharedString(cell->d_ptr->richString);
|
||||||
@@ -229,15 +228,6 @@ Worksheet::~Worksheet()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
* \internal
|
|
||||||
*/
|
|
||||||
Relationships &Worksheet::relationships()
|
|
||||||
{
|
|
||||||
Q_D(Worksheet);
|
|
||||||
return d->relationships;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Returns whether sheet is protected.
|
* Returns whether sheet is protected.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -129,14 +129,13 @@ public:
|
|||||||
bool isWhiteSpaceVisible() const;
|
bool isWhiteSpaceVisible() const;
|
||||||
void setWhiteSpaceVisible(bool visible);
|
void setWhiteSpaceVisible(bool visible);
|
||||||
|
|
||||||
Relationships &relationships();
|
|
||||||
~Worksheet();
|
~Worksheet();
|
||||||
private:
|
private:
|
||||||
friend class DocumentPrivate;
|
friend class DocumentPrivate;
|
||||||
friend class Workbook;
|
friend class Workbook;
|
||||||
friend class ::WorksheetTest;
|
friend class ::WorksheetTest;
|
||||||
Worksheet(const QString &sheetName, int sheetId, Workbook *book);
|
Worksheet(const QString &sheetName, int sheetId, Workbook *book);
|
||||||
QSharedPointer<Worksheet> copy(const QString &distName, int distId) const;
|
Worksheet *copy(const QString &distName, int distId) const;
|
||||||
|
|
||||||
void saveToXmlFile(QIODevice *device) const;
|
void saveToXmlFile(QIODevice *device) const;
|
||||||
bool loadFromXmlFile(QIODevice *device);
|
bool loadFromXmlFile(QIODevice *device);
|
||||||
|
|||||||
@@ -41,7 +41,6 @@
|
|||||||
#include "xlsxcell.h"
|
#include "xlsxcell.h"
|
||||||
#include "xlsxdatavalidation.h"
|
#include "xlsxdatavalidation.h"
|
||||||
#include "xlsxconditionalformatting.h"
|
#include "xlsxconditionalformatting.h"
|
||||||
#include "xlsxrelationships_p.h"
|
|
||||||
|
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
@@ -143,7 +142,6 @@ public:
|
|||||||
|
|
||||||
SharedStrings *sharedStrings() const;
|
SharedStrings *sharedStrings() const;
|
||||||
|
|
||||||
mutable Relationships relationships;
|
|
||||||
QMap<int, QMap<int, QSharedPointer<Cell> > > cellTable;
|
QMap<int, QMap<int, QSharedPointer<Cell> > > cellTable;
|
||||||
QMap<int, QMap<int, QString> > comments;
|
QMap<int, QMap<int, QString> > comments;
|
||||||
QMap<int, QMap<int, QSharedPointer<XlsxHyperlinkData> > > urlTable;
|
QMap<int, QMap<int, QSharedPointer<XlsxHyperlinkData> > > urlTable;
|
||||||
|
|||||||
@@ -308,32 +308,32 @@ void DocumentTest::testReadWriteTime()
|
|||||||
void DocumentTest::testMoveWorksheet()
|
void DocumentTest::testMoveWorksheet()
|
||||||
{
|
{
|
||||||
Document xlsx1;
|
Document xlsx1;
|
||||||
xlsx1.addWorksheet();
|
xlsx1.addSheet();
|
||||||
|
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2");
|
||||||
xlsx1.moveWorksheet("Sheet2", 0);
|
xlsx1.moveSheet("Sheet2", 0);
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet2"<<"Sheet1");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet2"<<"Sheet1");
|
||||||
xlsx1.moveWorksheet("Sheet2", 1);
|
xlsx1.moveSheet("Sheet2", 1);
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2");
|
||||||
}
|
}
|
||||||
|
|
||||||
void DocumentTest::testCopyWorksheet()
|
void DocumentTest::testCopyWorksheet()
|
||||||
{
|
{
|
||||||
Document xlsx1;
|
Document xlsx1;
|
||||||
xlsx1.addWorksheet();
|
xlsx1.addSheet();
|
||||||
xlsx1.write("A1", "String");
|
xlsx1.write("A1", "String");
|
||||||
xlsx1.write("A2", 999);
|
xlsx1.write("A2", 999);
|
||||||
xlsx1.write("A3", true);
|
xlsx1.write("A3", true);
|
||||||
xlsx1.addWorksheet();
|
xlsx1.addSheet();
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3");
|
||||||
|
|
||||||
xlsx1.copyWorksheet("Sheet2");
|
xlsx1.copySheet("Sheet2");
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"<<"Sheet2(2)");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3"<<"Sheet2(2)");
|
||||||
|
|
||||||
xlsx1.deleteWorksheet("Sheet2");
|
xlsx1.deleteSheet("Sheet2");
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet3"<<"Sheet2(2)");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet3"<<"Sheet2(2)");
|
||||||
|
|
||||||
xlsx1.selectWorksheet("Sheet2(2)");
|
xlsx1.selectSheet("Sheet2(2)");
|
||||||
QCOMPARE(xlsx1.read("A1").toString(), QString("String"));
|
QCOMPARE(xlsx1.read("A1").toString(), QString("String"));
|
||||||
QCOMPARE(xlsx1.read("A2").toInt(), 999);
|
QCOMPARE(xlsx1.read("A2").toInt(), 999);
|
||||||
QCOMPARE(xlsx1.read("A3").toBool(), true);
|
QCOMPARE(xlsx1.read("A3").toBool(), true);
|
||||||
@@ -342,18 +342,18 @@ void DocumentTest::testCopyWorksheet()
|
|||||||
void DocumentTest::testDeleteWorksheet()
|
void DocumentTest::testDeleteWorksheet()
|
||||||
{
|
{
|
||||||
Document xlsx1;
|
Document xlsx1;
|
||||||
xlsx1.addWorksheet();
|
xlsx1.addSheet();
|
||||||
xlsx1.addWorksheet();
|
xlsx1.addSheet();
|
||||||
|
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet2"<<"Sheet3");
|
||||||
xlsx1.deleteWorksheet("Sheet2");
|
xlsx1.deleteSheet("Sheet2");
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet1"<<"Sheet3");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet1"<<"Sheet3");
|
||||||
xlsx1.deleteWorksheet("Sheet1");
|
xlsx1.deleteSheet("Sheet1");
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet3");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet3");
|
||||||
|
|
||||||
//Cann't delete the last worksheet
|
//Cann't delete the last worksheet
|
||||||
xlsx1.deleteWorksheet("Sheet3");
|
xlsx1.deleteSheet("Sheet3");
|
||||||
QCOMPARE(xlsx1.worksheetNames(), QStringList()<<"Sheet3");
|
QCOMPARE(xlsx1.sheetNames(), QStringList()<<"Sheet3");
|
||||||
}
|
}
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(DocumentTest)
|
QTEST_APPLESS_MAIN(DocumentTest)
|
||||||
|
|||||||
Reference in New Issue
Block a user