Skip to content

Commit

Permalink
feat(worksheet): 加载数据时解析有效区域 和选中一个单元格 按Ctrl-A的效果大体相同
Browse files Browse the repository at this point in the history
(Resolve valid regions when loading data
Similar to selecting a cell
The effect of pressing Ctrl-A is roughly the same)
  • Loading branch information
juvenile committed May 13, 2024
1 parent 79da3e7 commit 8795189
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions QXlsx/header/xlsxworksheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ class QXLSX_EXPORT Worksheet : public AbstractSheet
private:
void saveToXmlFile(QIODevice *device) const override;
bool loadFromXmlFile(QIODevice *device) override;

public:
const QList<CellRange> &editRegion() const;
};

QT_END_NAMESPACE_XLSX
Expand Down
3 changes: 3 additions & 0 deletions QXlsx/header/xlsxworksheet_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ class WorksheetPrivate : public AbstractSheetPrivate

private:
static double calculateColWidth(int characters);

private:
QList<CellRange> editRegions;
};

QT_END_NAMESPACE_XLSX
Expand Down
26 changes: 24 additions & 2 deletions QXlsx/source/xlsxworksheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2321,8 +2321,7 @@ void WorksheetPrivate::loadXmlSheetData(QXmlStreamReader &reader)
QXmlStreamAttributes attributes = reader.attributes();
QString r = attributes.value(QLatin1String("r")).toString();
CellReference pos(r);
if (r.isEmpty())
{
if (r.isEmpty()) {
pos.setRow(row_num);
pos.setColumn(++col_num);
}
Expand Down Expand Up @@ -2444,6 +2443,23 @@ void WorksheetPrivate::loadXmlSheetData(QXmlStreamReader &reader)
}

cellTable[pos.row()][pos.column()] = cell;

if (cell->value().isNull()) {
continue;
}

if (editRegions.empty()) {
editRegions << CellRange{pos, pos};
} else {
auto &cellRange = editRegions.last();
if (cellRange.lastRow() == pos.row() && cellRange.lastColumn() < pos.column()) {
cellRange.setLastColumn(pos.column());
}

if (pos.row() - cellRange.lastRow() == 1) {
cellRange.setLastRow(pos.row());
}
}
}
}
}
Expand Down Expand Up @@ -2910,4 +2926,10 @@ QVector<CellLocation> Worksheet::getFullCells(int *maxRow, int *maxCol)
return ret;
}

const QList<CellRange> &Worksheet::editRegion() const
{
Q_D(const Worksheet);
return d->editRegions;
}

QT_END_NAMESPACE_XLSX

0 comments on commit 8795189

Please sign in to comment.