Export the dimension api of worksheet

This commit is contained in:
Debao Zhang
2013-10-29 22:07:50 +08:00
parent 0155cc5409
commit 18d00f9665
8 changed files with 53 additions and 189 deletions
+5
View File
@@ -116,4 +116,9 @@ QString CellRange::toString() const
return cell_1 + QLatin1String(":") + cell_2;
}
bool CellRange::isValid() const
{
return left <= right && top <= bottom;
}
QT_END_NAMESPACE_XLSX
+5
View File
@@ -38,6 +38,11 @@ public:
~CellRange();
QString toString() const;
bool isValid() const;
inline void setFirstRow(int row) { top = row; }
inline void setLastRow(int row) { bottom = row; }
inline void setFirstColumn(int col) { left = col; }
inline void setLastColumn(int col) { right = col; }
inline int firstRow() const { return top; }
inline int lastRow() const { return bottom; }
inline int firstColumn() const { return left; }
+8
View File
@@ -208,6 +208,14 @@ Cell *Document::cellAt(int row, int col) const
return currentWorksheet()->cellAt(row, col);
}
/*!
Return the range that contains cell data.
*/
CellRange Document::dimension() const
{
return currentWorksheet()->dimension();
}
/*!
* Returns the value of the document's \a key property.
*/
+3
View File
@@ -39,6 +39,7 @@ class Worksheet;
class Package;
class Format;
class Cell;
class CellRange;
class DataValidation;
class DocumentPrivate;
@@ -66,6 +67,8 @@ public:
Cell *cellAt(const QString &cell) const;
Cell *cellAt(int row, int col) const;
CellRange dimension() const;
QString documentProperty(const QString &name) const;
void setDocumentProperty(const QString &name, const QString &property);
QStringList documentPropertyNames() const;
+20 -115
View File
@@ -57,10 +57,6 @@ WorksheetPrivate::WorksheetPrivate(Worksheet *p) :
xls_rowmax = 1048576;
xls_colmax = 16384;
xls_strmax = 32767;
dim_rowmin = INT32_MAX;
dim_rowmax = INT32_MIN;
dim_colmin = INT32_MAX;
dim_colmax = INT32_MIN;
previous_row = 0;
@@ -94,9 +90,9 @@ void WorksheetPrivate::calculateSpans()
int span_min = INT32_MAX;
int span_max = INT32_MIN;
for (int row_num = dim_rowmin; row_num <= dim_rowmax; row_num++) {
for (int row_num = dimension.firstRow(); row_num <= dimension.lastRow(); row_num++) {
if (cellTable.contains(row_num)) {
for (int col_num = dim_colmin; col_num <= dim_colmax; col_num++) {
for (int col_num = dimension.firstColumn(); col_num <= dimension.lastColumn(); col_num++) {
if (cellTable[row_num].contains(col_num)) {
if (span_max == INT32_MIN) {
span_min = col_num;
@@ -111,7 +107,7 @@ void WorksheetPrivate::calculateSpans()
}
}
if (comments.contains(row_num)) {
for (int col_num = dim_colmin; col_num <= dim_colmax; col_num++) {
for (int col_num = dimension.firstColumn(); col_num <= dimension.lastColumn(); col_num++) {
if (comments[row_num].contains(col_num)) {
if (span_max == INT32_MIN) {
span_min = col_num;
@@ -126,7 +122,7 @@ void WorksheetPrivate::calculateSpans()
}
}
if ((row_num + 1)%16 == 0 || row_num == dim_rowmax) {
if ((row_num + 1)%16 == 0 || row_num == dimension.lastRow()) {
int span_index = row_num / 16;
if (span_max != INT32_MIN) {
span_min += 1;
@@ -141,32 +137,10 @@ void WorksheetPrivate::calculateSpans()
QString WorksheetPrivate::generateDimensionString()
{
if (dim_rowmax == INT32_MIN && dim_colmax == INT32_MIN) {
//If the max dimensions are equal to INT32_MIN, then no dimension have been set
//and we use the default "A1"
if (!dimension.isValid())
return QStringLiteral("A1");
}
if (dim_rowmax == INT32_MIN) {
//row dimensions aren't set but the column dimensions are set
if (dim_colmin == dim_colmax) {
//The dimensions are a single cell and not a range
return xl_rowcol_to_cell(0, dim_colmin);
} else {
const QString cell_1 = xl_rowcol_to_cell(0, dim_colmin);
const QString cell_2 = xl_rowcol_to_cell(0, dim_colmax);
return cell_1 + QLatin1String(":") + cell_2;
}
}
if (dim_rowmin == dim_rowmax && dim_colmin == dim_colmax) {
//Single cell
return xl_rowcol_to_cell(dim_rowmin, dim_rowmin);
}
QString cell_1 = xl_rowcol_to_cell(dim_rowmin, dim_colmin);
QString cell_2 = xl_rowcol_to_cell(dim_rowmax, dim_colmax);
return cell_1 + QLatin1String(":") + cell_2;
else
return dimension.toString();
}
/*
@@ -182,12 +156,12 @@ int WorksheetPrivate::checkDimensions(int row, int col, bool ignore_row, bool ig
return -1;
if (!ignore_row) {
if (row < dim_rowmin) dim_rowmin = row;
if (row > dim_rowmax) dim_rowmax = row;
if (row < dimension.firstRow() || dimension.firstRow() == -1) dimension.setFirstRow(row);
if (row > dimension.lastRow()) dimension.setLastRow(row);
}
if (!ignore_col) {
if (col < dim_colmin) dim_colmin = col;
if (col > dim_colmax) dim_colmax = col;
if (col < dimension.firstColumn() || dimension.firstColumn() == -1) dimension.setFirstColumn(col);
if (col > dimension.lastColumn()) dimension.setLastColumn(col);
}
return 0;
@@ -671,11 +645,8 @@ void Worksheet::saveToXmlFile(QIODevice *device)
}
writer.writeStartElement(QStringLiteral("sheetData"));
if (d->dim_rowmax == INT32_MIN) {
//If the max dimensions are equal to INT32_MIN, then there is no data to write
} else {
if (d->dimension.isValid())
d->writeSheetData(writer);
}
writer.writeEndElement();//sheetData
d->writeMergeCells(writer);
@@ -690,7 +661,7 @@ void Worksheet::saveToXmlFile(QIODevice *device)
void WorksheetPrivate::writeSheetData(XmlStreamWriter &writer)
{
calculateSpans();
for (int row_num = dim_rowmin; row_num <= dim_rowmax; row_num++) {
for (int row_num = dimension.firstRow(); row_num <= dimension.lastRow(); row_num++) {
if (!(cellTable.contains(row_num) || comments.contains(row_num) || rowsInfo.contains(row_num))) {
//Only process rows with cell data / comments / formatting
continue;
@@ -722,7 +693,7 @@ void WorksheetPrivate::writeSheetData(XmlStreamWriter &writer)
writer.writeAttribute(QStringLiteral("hidden"), QStringLiteral("1"));
}
for (int col_num = dim_colmin; col_num <= dim_colmax; col_num++) {
for (int col_num = dimension.firstColumn(); col_num <= dimension.lastColumn(); col_num++) {
if (cellTable[row_num].contains(col_num)) {
writeCellData(writer, row_num, col_num, cellTable[row_num][col_num]);
}
@@ -934,7 +905,7 @@ void WorksheetPrivate::writeDrawings(XmlStreamWriter &writer)
bool Worksheet::setRow(int row, double height, Format *format, bool hidden)
{
Q_D(Worksheet);
int min_col = d->dim_colmax == INT32_MIN ? 0 : d->dim_colmin;
int min_col = d->dimension.firstColumn() < 0 ? 0 : d->dimension.firstColumn();
if (d->checkDimensions(row, min_col))
return false;
@@ -976,65 +947,12 @@ bool Worksheet::setColumn(int colFirst, int colLast, double width, Format *forma
}
/*!
* Returns the first row in the sheet that contains a used cell.
Return the range that contains cell data.
*/
int Worksheet::firstRow() const
CellRange Worksheet::dimension() const
{
Q_D(const Worksheet);
if (d->dim_rowmax == INT32_MIN) {
//Row dimenstion isn't set.
return 0;
} else {
return d->dim_rowmin;
}
}
/*!
* Returns the zero-based index of the row after the last row in
* the sheet that contains a used cell.
*/
int Worksheet::lastRow() const
{
Q_D(const Worksheet);
if (d->dim_rowmax == INT32_MIN) {
//Row dimenstion isn't set.
return 0;
} else {
return d->dim_rowmax + 1;
}
}
/*!
* Returns the first column in the sheet that contains a used cell.
*/
int Worksheet::firstColumn() const
{
Q_D(const Worksheet);
if (d->dim_colmax == INT32_MIN) {
//Col dimenstion isn't set.
return 0;
} else {
return d->dim_colmin;
}
}
/*!
* Returns the zero-based index of the column after the last column
* in the sheet that contains a used cell.
*/
int Worksheet::lastColumn() const
{
Q_D(const Worksheet);
if (d->dim_colmax == INT32_MIN) {
//Col dimenstion isn't set.
return 0;
} else {
return d->dim_colmax + 1;
}
return d->dimension;
}
Drawing *Worksheet::drawing() const
@@ -1571,21 +1489,8 @@ bool Worksheet::loadFromXmlFile(QIODevice *device)
if (reader.tokenType() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("dimension")) {
QXmlStreamAttributes attributes = reader.attributes();
QStringList range = attributes.value(QLatin1String("ref")).toString().split(QLatin1Char(':'));
if (range.size() == 2) {
QPoint start = xl_cell_to_rowcol(range[0]);
QPoint end = xl_cell_to_rowcol(range[1]);
d->dim_rowmin = start.x();
d->dim_colmin = start.y();
d->dim_rowmax = end.x();
d->dim_colmax = end.y();
} else {
QPoint p = xl_cell_to_rowcol(range[0]);
d->dim_rowmin = p.x();
d->dim_colmin = p.y();
d->dim_rowmax = p.x();
d->dim_colmax = p.y();
}
QString range = attributes.value(QLatin1String("ref")).toString();
d->dimension = CellRange(range);
} else if (reader.name() == QLatin1String("sheetViews")) {
} else if (reader.name() == QLatin1String("sheetFormatPr")) {
+2 -5
View File
@@ -45,6 +45,7 @@ class Workbook;
class Format;
class Drawing;
class DataValidation;
class CellRange;
struct XlsxImageData;
class WorksheetPrivate;
@@ -79,11 +80,7 @@ public:
bool setRow(int row, double height, Format* format=0, bool hidden=false);
bool setColumn(int colFirst, int colLast, double width, Format* format=0, bool hidden=false);
int firstRow() const;
int lastRow() const;
int firstColumn() const;
int lastColumn() const;
CellRange dimension() const;
void setRightToLeft(bool enable);
void setZeroValuesHidden(bool enable);
+1 -4
View File
@@ -195,10 +195,7 @@ public:
int xls_rowmax;
int xls_colmax;
int xls_strmax;
int dim_rowmin;
int dim_rowmax;
int dim_colmin;
int dim_colmax;
CellRange dimension;
int previous_row;
QMap<int, QString> row_spans;