Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions QXlsx/header/xlsxworksheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ class QXLSX_EXPORT Worksheet : public AbstractSheet
bool isWhiteSpaceVisible() const;
void setWhiteSpaceVisible(bool visible);
bool setStartPage(int spagen); // add by liufeijin20181028
CellReference selectionActiveCell() const;
void setSelectionActiveCell(CellReference activeCell);
quint32 selectionActiveCellId() const;
bool selectionActiveCellId(quint32 activeCellId);
QList<CellRange> selectionSqref() const;
void setSelectionSqref(const QList<CellRange> &selectionSqref);
void setSelectionSqref(QList<CellRange> &&selectionSqref);

QVector<CellLocation> getFullCells(int *maxRow, int *maxCol) const;

Expand Down
10 changes: 10 additions & 0 deletions QXlsx/header/xlsxworksheet_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@
bool collapsed;
};

// ECMA-376 Part1 18.3.1.78
struct XlsxSheetViewSelectionProps {
CellReference activeCell;
quint32 activeCellId;
// TODO: Add pane while adding multiple-`sheetView` support

Check warning on line 139 in QXlsx/header/xlsxworksheet_p.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=QtExcel_QXlsx&issues=AZ0zadlzLQrmbX5-NCSe&open=AZ0zadlzLQrmbX5-NCSe&pullRequest=384
QList<CellRange> sqref;
};

class CellTable
{
public:
Expand Down Expand Up @@ -286,6 +294,8 @@

QRegularExpression urlPattern;

XlsxSheetViewSelectionProps selectionProps;

private:
static double calculateColWidth(int characters);
};
Expand Down
94 changes: 94 additions & 0 deletions QXlsx/source/xlsxworksheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,27 @@
if (!d->showWhiteSpace)
writer.writeAttribute(QStringLiteral("showWhiteSpace"), QStringLiteral("0"));
writer.writeAttribute(QStringLiteral("workbookViewId"), QStringLiteral("0"));
if (d->selectionProps.activeCell.isValid() || !d->selectionProps.sqref.isEmpty()) {
writer.writeStartElement(QStringLiteral("selection"));
if (d->selectionProps.activeCell.isValid()) {
writer.writeAttribute(QStringLiteral("activeCell"),
d->selectionProps.activeCell.toString());
}
if (!d->selectionProps.sqref.isEmpty()) {
QString sqrefString;
for (const auto range : d->selectionProps.sqref) {
sqrefString.append(range.toString());
sqrefString.push_back(QLatin1Char(' '));
}
sqrefString.truncate(sqrefString.size() - 1);
writer.writeAttribute(QStringLiteral("sqref"), sqrefString);
if (d->selectionProps.sqref.size() > 1) {
writer.writeAttribute(QStringLiteral("activeCellId"),
QString::number(d->selectionProps.activeCellId));
}
}
writer.writeEndElement(); // selection
}
writer.writeEndElement(); // sheetView
writer.writeEndElement(); // sheetViews

Expand Down Expand Up @@ -1481,6 +1502,54 @@
}
//}}

CellReference Worksheet::selectionActiveCell() const
{
Q_D(const Worksheet);
return d->selectionProps.activeCell;
}

void Worksheet::setSelectionActiveCell(CellReference activeCell)
{
Q_D(Worksheet);
d->selectionProps.activeCell = activeCell;
}

quint32 Worksheet::selectionActiveCellId() const
{
Q_D(const Worksheet);
return d->selectionProps.activeCellId;
}

bool Worksheet::selectionActiveCellId(quint32 activeCellId)
{
Q_D(Worksheet);
if (activeCellId < d->selectionProps.sqref.size()) {

Check warning on line 1526 in QXlsx/source/xlsxworksheet.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the init-statement to declare "d" inside the if statement.

See more on https://sonarcloud.io/project/issues?id=QtExcel_QXlsx&issues=AZ0zadjiLQrmbX5-NCSa&open=AZ0zadjiLQrmbX5-NCSa&pullRequest=384
d->selectionProps.activeCellId = activeCellId;
return true;
}
return false;
}

QList<CellRange> Worksheet::selectionSqref() const
{
Q_D(const Worksheet);
return d->selectionProps.sqref;
}

void Worksheet::setSelectionSqref(const QList<CellRange> &selectionSqref)
{
Q_D(Worksheet);
d->selectionProps.sqref = selectionSqref;
d->selectionProps.activeCellId = 0;
}

void Worksheet::setSelectionSqref(QList<CellRange> &&selectionSqref)
{
Q_D(Worksheet);
d->selectionProps.sqref = std::move(selectionSqref);
d->selectionProps.activeCellId = 0;
}

void WorksheetPrivate::saveXmlSheetData(QXmlStreamWriter &writer) const
{
calculateSpans();
Expand Down Expand Up @@ -2552,7 +2621,7 @@
qDebug("read data validation error");
}

void WorksheetPrivate::loadXmlSheetViews(QXmlStreamReader &reader)

Check failure on line 2624 in QXlsx/source/xlsxworksheet.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 30 to the 25 allowed.

See more on https://sonarcloud.io/project/issues?id=QtExcel_QXlsx&issues=AZ0zadjiLQrmbX5-NCSb&open=AZ0zadjiLQrmbX5-NCSb&pullRequest=384
{
Q_ASSERT(reader.name() == QLatin1String("sheetViews"));

Expand All @@ -2576,6 +2645,31 @@
showOutlineSymbols =
attrs.value(QLatin1String("showOutlineSymbols")) != QLatin1String("0");
showWhiteSpace = attrs.value(QLatin1String("showWhiteSpace")) != QLatin1String("0");
reader.readNextStartElement();
if (reader.tokenType() == QXmlStreamReader::StartElement &&
reader.name() == QLatin1String("selection")) {
QXmlStreamAttributes selectionAttr = reader.attributes();
selectionProps.activeCell =
CellReference(selectionAttr.value(QLatin1String("activeCell")).toString());
selectionProps.activeCellId =
selectionAttr.value(QLatin1String("activeCellId")).toUInt();
QStringView sqrefStr = selectionAttr.value(QLatin1String("sqref"));
QList<QStringView> ranges = sqrefStr.split(QLatin1Char(' '));
for (const auto &range : ranges) {

Check failure on line 2658 in QXlsx/source/xlsxworksheet.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this code to not nest more than 3 if|for|do|while|switch statements.

See more on https://sonarcloud.io/project/issues?id=QtExcel_QXlsx&issues=AZ0zadjiLQrmbX5-NCSc&open=AZ0zadjiLQrmbX5-NCSc&pullRequest=384
selectionProps.sqref.append(CellRange(range.toString()));
}
if (selectionProps.activeCellId >= selectionProps.sqref.count()) {

Check failure on line 2661 in QXlsx/source/xlsxworksheet.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this code to not nest more than 3 if|for|do|while|switch statements.

See more on https://sonarcloud.io/project/issues?id=QtExcel_QXlsx&issues=AZ0zadjiLQrmbX5-NCSd&open=AZ0zadjiLQrmbX5-NCSd&pullRequest=384
for (auto it = selectionProps.sqref.begin(); it != selectionProps.sqref.end();
++it) {
const auto &range = *it;
if (!(range.topLeft() > selectionProps.activeCell) &&
!(selectionProps.activeCell > range.bottomRight())) {
selectionProps.activeCellId = it - selectionProps.sqref.begin();
break;
}
}
}
}
}
}
}
Expand Down