Browse Source

Worksheet index property should be stored in Workbook

master
Debao Zhang 11 years ago
parent
commit
7816c724dc
  1. 2
      examples/xlsx/hello/main.cpp
  2. 65
      src/xlsx/xlsxworkbook.cpp
  3. 4
      src/xlsx/xlsxworkbook.h
  4. 22
      src/xlsx/xlsxworksheet.cpp
  5. 5
      src/xlsx/xlsxworksheet.h
  6. 2
      src/xlsx/xlsxworksheet_p.h

2
examples/xlsx/hello/main.cpp

@ -31,6 +31,8 @@ int main()
sheet2->write(3, 1, 400);
sheet2->write(4, 1, "=SUM(B1:B4)");
workbook.setActivedWorksheet(1);
workbook.save(DATA_PATH"Test.xlsx");
workbook.save(DATA_PATH"Test.zip");
return 0;

65
src/xlsx/xlsxworkbook.cpp

@ -71,16 +71,11 @@ void Workbook::save(const QString &name)
addWorksheet();
//Ensure that at least one worksheet has been selected.
if (d->activesheet == 0) {
d->worksheets[0]->setHidden(false);
d->worksheets[0]->setSelected(true);
}
//Set the active sheet
foreach (Worksheet *sheet, d->worksheets) {
if (sheet->index() == d->activesheet)
sheet->setActived(true);
}
int actIndex = d->activesheet;
if (actIndex < 0 || actIndex >= d->worksheets.size())
actIndex = 0;
d->worksheets[actIndex]->setHidden(false);
d->worksheets[actIndex]->setSelected(true);
//Create the package based on current workbook
Package package(this);
@ -133,17 +128,50 @@ void Workbook::defineName(const QString &name, const QString &formula)
Worksheet *Workbook::addWorksheet(const QString &name)
{
Q_D(Workbook);
return insertWorkSheet(d->worksheets.size(), name);
}
Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
{
Q_D(Workbook);
static int lastIndex = -1;
QString worksheetName = name;
int index = d->worksheets.size()+1;
if (name.isEmpty())
worksheetName = QStringLiteral("Sheet%1").arg(index);
if (!name.isEmpty()) {
//If user given an already in-use name, we should not continue any more!
foreach (Worksheet *sheet, d->worksheets) {
if (sheet->name() == name)
return 0;
}
} else {
bool exists;
do {
++lastIndex;
exists = false;
worksheetName = QStringLiteral("Sheet%1").arg(lastIndex+1);
foreach (Worksheet *sheet, d->worksheets) {
if (sheet->name() == worksheetName)
exists = true;
}
} while (exists);
}
Worksheet *sheet = new Worksheet(worksheetName, index, this);
d->worksheets.append(sheet);
Worksheet *sheet = new Worksheet(worksheetName, this);
d->worksheets.insert(index, sheet);
return sheet;
}
int Workbook::activedWorksheet() const
{
Q_D(const Workbook);
return d->activesheet;
}
void Workbook::setActivedWorksheet(int index)
{
Q_D(Workbook);
d->activesheet = index;
}
Format *Workbook::addFormat()
{
Q_D(Workbook);
@ -205,13 +233,14 @@ void Workbook::saveToXmlFile(QIODevice *device)
writer.writeEndElement();//bookviews
writer.writeStartElement(QStringLiteral("sheets"));
foreach (Worksheet *sheet, d->worksheets) {
for (int i=0; i<d->worksheets.size(); ++i) {
Worksheet *sheet = d->worksheets[i];
writer.writeEmptyElement(QStringLiteral("sheet"));
writer.writeAttribute(QStringLiteral("name"), sheet->name());
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->index()));
writer.writeAttribute(QStringLiteral("sheetId"), QString::number(i+1));
if (sheet->isHidden())
writer.writeAttribute(QStringLiteral("state"), QStringLiteral("hidden"));
writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(sheet->index()));
writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(i+1));
}
writer.writeEndElement();//sheets

4
src/xlsx/xlsxworkbook.h

@ -49,6 +49,10 @@ public:
QList<Worksheet *> worksheets() const;
Worksheet *addWorksheet(const QString &name = QString());
Worksheet *insertWorkSheet(int index, const QString &name = QString());
int activedWorksheet() const;
void setActivedWorksheet(int index);
Format *addFormat();
// void addChart();
void defineName(const QString &name, const QString &formula);

22
src/xlsx/xlsxworksheet.cpp

@ -61,7 +61,6 @@ WorksheetPrivate::WorksheetPrivate(Worksheet *p) :
hidden = false;
selected = false;
actived = false;
right_to_left = false;
show_zeros = true;
}
@ -198,11 +197,10 @@ int WorksheetPrivate::checkDimensions(int row, int col, bool ignore_row, bool ig
* \param index Index of the worksheet in the workbook
* \param parent
*/
Worksheet::Worksheet(const QString &name, int index, Workbook *parent) :
Worksheet::Worksheet(const QString &name, Workbook *parent) :
QObject(parent), d_ptr(new WorksheetPrivate(this))
{
d_ptr->name = name;
d_ptr->index = index;
d_ptr->workbook = parent;
}
@ -222,12 +220,6 @@ QString Worksheet::name() const
return d->name;
}
int Worksheet::index() const
{
Q_D(const Worksheet);
return d->index;
}
bool Worksheet::isHidden() const
{
Q_D(const Worksheet);
@ -240,12 +232,6 @@ bool Worksheet::isSelected() const
return d->selected;
}
bool Worksheet::isActived() const
{
Q_D(const Worksheet);
return d->actived;
}
void Worksheet::setHidden(bool hidden)
{
Q_D(Worksheet);
@ -258,12 +244,6 @@ void Worksheet::setSelected(bool select)
d->selected = select;
}
void Worksheet::setActived(bool act)
{
Q_D(Worksheet);
d->actived = act;
}
void Worksheet::setRightToLeft(bool enable)
{
Q_D(Worksheet);

5
src/xlsx/xlsxworksheet.h

@ -63,18 +63,15 @@ public:
private:
friend class Package;
friend class Workbook;
Worksheet(const QString &sheetName, int sheetIndex, Workbook *parent=0);
Worksheet(const QString &sheetName, Workbook *parent=0);
~Worksheet();
virtual bool isChartsheet() const;
QString name() const;
int index() const;
bool isHidden() const;
bool isSelected() const;
bool isActived() const;
void setHidden(bool hidden);
void setSelected(bool select);
void setActived(bool act);
void saveToXmlFile(QIODevice *device);
WorksheetPrivate * const d_ptr;

2
src/xlsx/xlsxworksheet_p.h

@ -115,10 +115,8 @@ public:
bool default_row_zeroed;
QString name;
int index;
bool hidden;
bool selected;
bool actived;
bool right_to_left;
bool show_zeros;

Loading…
Cancel
Save