Spreadsheet: add new API PropertySheet::getNonEmptyCells()

To exclude cells without any text content. Used when printing (among
other cases) to skip empty cells.
This commit is contained in:
Zheng, Lei
2022-05-06 11:07:28 +08:00
committed by Chris Hennes
parent 3bea43a228
commit 6d6af2dd3b
4 changed files with 27 additions and 15 deletions

View File

@@ -152,13 +152,27 @@ bool PropertySheet::isValidAlias(const std::string &candidate)
return false;
}
std::set<CellAddress> PropertySheet::getUsedCells() const
std::vector<CellAddress> PropertySheet::getUsedCells() const
{
std::set<CellAddress> usedSet;
std::vector<CellAddress> usedSet;
for (std::map<CellAddress, Cell*>::const_iterator i = data.begin(); i != data.end(); ++i) {
if (i->second->isUsed())
usedSet.insert(i->first);
usedSet.push_back(i->first);
}
return usedSet;
}
std::vector<CellAddress> PropertySheet::getNonEmptyCells() const
{
std::vector<CellAddress> usedSet;
std::string str;
for (std::map<CellAddress, Cell*>::const_iterator i = data.begin(); i != data.end(); ++i) {
str.clear();
if (i->second->isUsed() && i->second->getStringContent(str) && !str.empty())
usedSet.push_back(i->first);
}
return usedSet;
@@ -178,7 +192,7 @@ void PropertySheet::setDirty(CellAddress address)
void PropertySheet::setDirty()
{
AtomicPropertyChange signaller(*this);
for(auto &address : getUsedCells()) {
for(auto &address : getNonEmptyCells()) {
auto cell = cellAt(address);
std::string content;
if(cell && cell->getStringContent(content,false)) {

View File

@@ -113,7 +113,9 @@ public:
bool isValidAlias(const std::string &candidate);
std::set<App::CellAddress> getUsedCells() const;
std::vector<App::CellAddress> getUsedCells() const;
std::vector<App::CellAddress> getNonEmptyCells() const;
Sheet * sheet() const { return owner; }

View File

@@ -274,8 +274,8 @@ bool Sheet::exportToFile(const std::string &filename, char delimiter, char quote
if (!file.is_open())
return false;
std::set<CellAddress> usedCells = cells.getUsedCells();
std::set<CellAddress>::const_iterator i = usedCells.begin();
auto usedCells = cells.getNonEmptyCells();
auto i = usedCells.begin();
while (i != usedCells.end()) {
Property * prop = getProperty(*i);
@@ -1184,12 +1184,8 @@ int Sheet::getRowHeight(int row) const
std::vector<std::string> Sheet::getUsedCells() const
{
std::vector<std::string> usedCells;
// Insert int usedSet
std::set<CellAddress> usedSet = cells.getUsedCells();
for (std::set<CellAddress>::const_iterator i = usedSet.begin(); i != usedSet.end(); ++i)
usedCells.push_back(i->toString());
for (const auto &addr : cells.getUsedCells())
usedCells.push_back(addr.toString());
return usedCells;
}

View File

@@ -769,7 +769,7 @@ void SheetTableView::finishEditWithMove(int keyPressed, Qt::KeyboardModifiers mo
{
// End should take you to the last occupied cell in the current column
// Ctrl-End takes you to the last cell in the sheet
auto usedCells = sheet->getCells()->getUsedCells();
auto usedCells = sheet->getCells()->getNonEmptyCells();
for (const auto& cell : usedCells) {
if (modifiers == Qt::NoModifier) {
if (cell.col() == targetColumn)
@@ -984,7 +984,7 @@ void SheetTableView::contextMenuEvent(QContextMenuEvent *)
QString SheetTableView::toHtml() const
{
std::set<App::CellAddress> cells = sheet->getCells()->getUsedCells();
auto cells = sheet->getCells()->getNonEmptyCells();
int rowCount = 1;
int colCount = 1;
for (const auto& it : cells) {